An interactive, intelligent assistant for Python code generation, safe execution, performance tracking, and dynamic visualization. Built using LangChain and Groq's Llama3 models, the application ensures human-in-the-loop safety, clear reasoning, and a seamless coding experience through a sleek Streamlit interface.
While many AI assistants generate code, they often lack explainability, execution control, and performance transparency. This project bridges those gaps by building a reliable and safe AI agent that:
- Generates accurate Python code for diverse tasks.
- Explains its reasoning and planning steps.
- Awaits explicit user approval before executing code (Human-in-the-Loop).
- Reports execution metrics (time, success/failure).
- Handles errors gracefully and transparently.
- Supports file creation and visual output directly from code.
- 🧠 Code Generation: Understands complex, multi-step requests and writes executable Python code.
- ⚡ Groq-Powered LLM: Uses
llama3-70b-8192andllama3-8b-8192for intelligent, fast, and high-quality responses. - 👀 Human-in-the-Loop Execution:
- Agent explains the thought process and execution plan.
- Explicit approval (
✅ Approve & Execute) required before running any code. - Validates and cleans code before execution.
- 📊 Performance Metrics:
- Tracks execution time.
- Reports status (
Success,Timeout,Error). - Logs and displays agent’s reasoning and tool usage.
- 📈 Visualization Support:
- Auto-generates charts (line, bar) from JSON outputs.
- Supports complex visualizations via Matplotlib/Seaborn.
- 📂 File Handling: Captures and exposes generated files for download.
- 🚨 Robust Error Handling: Detects and explains errors using logs and feedback.
- 🧩 Streamlit UI:
- Sidebar configuration (LLM model, temperature).
- Dynamic chat history and execution logs.
- Feedback buttons (
👍/👎) after each interaction.
- Language: Python 3.9+
- Frameworks:
- LLM Provider: Groq
- Models Used: Llama3 70B and 8B
- Libraries:
langchain-core,langchain-groqpandas,matplotlib,seabornsubprocess,sys,os,re,datetimepython-dotenv,pydantic,pathlib
.
├── agent/
│ ├── **init**.py
│ ├── core.py # Agent logic, tools, and execution flow
│ ├── llm\_config.py # Groq LLM setup
│ ├── prompt.py # ReAct-style agent instructions
├── tools/
│ ├── **init**.py
│ ├── python\_executor.py # Code execution + metrics + approval
│ ├── task\_completed.py # Signals task completion, feedback
├── utils/
│ ├── **init**.py
│ ├── callbacks.py # Custom LangChain callback for HIL and logging
├── .env # GROQ\_API\_KEY stored here
├── custom\_code\_agent.py # Main Streamlit app
├── requirements.txt # Dependencies
├── README.md
- Manaswi Kamble
- Python: Install Python 3.9 or newer.
- Groq API Key: Get your free API key.
# Clone the repo
git clone <your_repo_url>
cd <your_project_directory>
# Create virtual environment
python3 -m venv venv
source venv/bin/activate # For Windows: .\venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt- Create a
.envfile in the root:
GROQ_API_KEY="your_groq_api_key_here"OR
- Enter it manually when prompted in the sidebar UI.
streamlit run custom_code_agent.pyThe app will launch in your browser. Select your LLM model, set temperature, and start coding with your AI partner!
User:
Create a Python function to calculate the factorial of 5.
AI Assistant:
🧠 Agent's Thought: I’ll write a recursive factorial function, call it with 5, and print the result.
💡 Generated Code (after approval):
def calculate_factorial(n):
if n < 0:
return "Factorial is not defined for negative numbers."
elif n == 0:
return 1
else:
result = 1
for i in range(1, n + 1):
result *= i
return result
number = 5
factorial_result = calculate_factorial(number)
print(f"The factorial of {number} is: {factorial_result}")✅ Execution Output:
The factorial of 5 is: 120
- Prompt Engineering: Crafting structured ReAct prompts improves reasoning and tool use.
- Human-in-the-Loop: Managing Streamlit state to pause execution until user approval.
- Sandboxed Execution: Secure use of
subprocess.run()and careful input sanitization. - LangChain Callbacks: Used
BaseCallbackHandlerto capture and visualize agent steps. - UI State Management: Leveraged
st.session_statefor dynamic flow control.
- 🧑🔧 Code Debugger: Suggest fixes based on traceback analysis.
- 📊 Plotly/Altair Support: Enable interactive charting.
- 🗂 File Awareness: Let agents read from local user files.
- 💾 Conversation Persistence: Save chat history between sessions.
- 🔐 Code Risk Alerts: Warn users about risky patterns (e.g., network access).
We welcome your feedback! Use the 👍 or 👎 buttons after agent responses to help improve this assistant.
- AI Agent Engineering: Built a LangChain ReAct agent with advanced capabilities.
- LLM Deployment Expertise: Hands-on with Groq API, prompt optimization, and execution pipelines.
- Safe Code Generation: Human-in-the-Loop, execution metrics, and explainability.
- UI/UX: User-centric Streamlit interface with logging, feedback, and control.
⚡ Ready to code smarter, faster, and safer? Run the AI Code Assistant and supercharge your Python development workflow!