Spaces:
Build error
Build error
Create agents/coder_agent.py
Browse files- agents/coder_agent.py +91 -0
agents/coder_agent.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import os
|
| 3 |
+
from typing import Dict, Any
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load environment variables
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
class CoderAgent:
|
| 10 |
+
"""
|
| 11 |
+
Agent responsible for generating code based on user prompts.
|
| 12 |
+
This agent understands programming requirements and writes initial code.
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
def __init__(self, model="gpt-3.5-turbo", temperature=0.7):
|
| 16 |
+
"""
|
| 17 |
+
Initialize the coder agent with specific model parameters.
|
| 18 |
+
|
| 19 |
+
Args:
|
| 20 |
+
model: Which OpenAI model to use
|
| 21 |
+
temperature: Controls creativity (0=deterministic, 1=creative)
|
| 22 |
+
"""
|
| 23 |
+
self.model = model
|
| 24 |
+
self.temperature = temperature
|
| 25 |
+
self.api_key = os.getenv("OPENAI_API_KEY")
|
| 26 |
+
|
| 27 |
+
if not self.api_key:
|
| 28 |
+
raise ValueError("OPENAI_API_KEY not found in environment variables")
|
| 29 |
+
|
| 30 |
+
openai.api_key = self.api_key
|
| 31 |
+
|
| 32 |
+
def generate_code(self, prompt: str, context: str = "") -> Dict[str, Any]:
|
| 33 |
+
"""
|
| 34 |
+
Generate code based on user prompt and optional context.
|
| 35 |
+
|
| 36 |
+
Args:
|
| 37 |
+
prompt: User's coding requirement (e.g., "Write a function to reverse a string")
|
| 38 |
+
context: Additional context from RAG or previous conversations
|
| 39 |
+
|
| 40 |
+
Returns:
|
| 41 |
+
Dictionary containing code and metadata
|
| 42 |
+
"""
|
| 43 |
+
try:
|
| 44 |
+
# Build the system message
|
| 45 |
+
system_message = """You are an expert Python programmer.
|
| 46 |
+
Write clean, efficient, and well-documented code.
|
| 47 |
+
Always include function signatures with type hints.
|
| 48 |
+
Provide only the code without explanations unless asked."""
|
| 49 |
+
|
| 50 |
+
# Build user message with context
|
| 51 |
+
user_message = prompt
|
| 52 |
+
if context:
|
| 53 |
+
user_message = f"Context: {context}\n\nTask: {prompt}"
|
| 54 |
+
|
| 55 |
+
# Call OpenAI API
|
| 56 |
+
response = openai.ChatCompletion.create(
|
| 57 |
+
model=self.model,
|
| 58 |
+
messages=[
|
| 59 |
+
{"role": "system", "content": system_message},
|
| 60 |
+
{"role": "user", "content": user_message}
|
| 61 |
+
],
|
| 62 |
+
temperature=self.temperature,
|
| 63 |
+
max_tokens=500
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Extract and clean the code
|
| 67 |
+
raw_code = response.choices[0].message.content
|
| 68 |
+
|
| 69 |
+
# Clean the response (remove markdown code blocks if present)
|
| 70 |
+
if "```python" in raw_code:
|
| 71 |
+
code = raw_code.split("```python")[1].split("```")[0].strip()
|
| 72 |
+
elif "```" in raw_code:
|
| 73 |
+
code = raw_code.split("```")[1].split("```")[0].strip()
|
| 74 |
+
else:
|
| 75 |
+
code = raw_code.strip()
|
| 76 |
+
|
| 77 |
+
return {
|
| 78 |
+
"status": "success",
|
| 79 |
+
"code": code,
|
| 80 |
+
"raw_response": raw_code,
|
| 81 |
+
"model_used": self.model,
|
| 82 |
+
"tokens_used": response.usage.total_tokens
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
except Exception as e:
|
| 86 |
+
return {
|
| 87 |
+
"status": "error",
|
| 88 |
+
"error": str(e),
|
| 89 |
+
"code": "",
|
| 90 |
+
"raw_response": ""
|
| 91 |
+
}
|