Skip to content

krishagarwal314/coding_agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🤖 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

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages