Skip to content

Latest commit

Β 

History

History
93 lines (67 loc) Β· 2.28 KB

README.md

File metadata and controls

93 lines (67 loc) Β· 2.28 KB

πŸ€– AI Code Fixer Agent

A beginner-friendly project to learn how AI agents work using LangChain. The agent automatically reads a buggy Python file, fixes it, and saves the corrected code back to disk.

Built for learning purposes β€” covers core concepts like tools, agents, and LLMs.


🧠 How It Works

  1. Agent calls read_file to read the buggy file
  2. LLM figures out what's wrong and fixes it
  3. Agent calls write_file to save the corrected code

πŸ› οΈ Tools

  • read_file(filepath) β€” reads a file and returns its contents
  • write_file(filepath, content) β€” overwrites the file with fixed code

πŸ“ Code

import os
from dotenv import load_dotenv
load_dotenv()

from langchain_core.tools import tool
from langchain.chat_models import init_chat_model
from langchain.agents import create_agent

@tool
def read_file(filepath: str) -> str:
    """Read and return the contents of a file at the given filepath."""
    with open(filepath, "r") as f:
        return f.read()

@tool
def write_file(filepath: str, content: str) -> str:
    """Write content to a file at the given filepath, overwriting it completely."""
    with open(filepath, "w") as f:
        f.write(content)
    return f"Successfully wrote to {filepath}"

tools = [read_file, write_file]

model = init_chat_model("groq:qwen/qwen3-32b", temperature=0)

agent = create_agent(
    model,
    tools,
    system_prompt=(
        "You are a helpful coding assistant. "
        "When asked to fix code in a file: "
        "1. Use read_file to read the file's current content. "
        "2. Fix the code. "
        "3. Use write_file to save the corrected code back to the same file. "
        "Do not add explanations inside the file β€” only write valid code."
    )
)

if __name__ == "__main__":
    result = agent.invoke({
        "messages": [{"role": "user", "content": "Fix the code in test.py so it works correctly."}]
    })
    print("\n agent response:", result["messages"][-1].content)

πŸ“Έ Demo

❌ Incorrect Code

Incorrect Code

πŸ€– Agent Response

Agent Response

βœ… Fixed Code

Fixed Code


πŸš€ Setup

uv add -r requirements.txt
# add GROQ_API_KEY to .env
uv run python app.py