Spaces:
Build error
Build error
Create coder_agent.py
Browse files- coder_agent.py +25 -0
coder_agent.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import os
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
|
| 5 |
+
load_dotenv()
|
| 6 |
+
|
| 7 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
+
|
| 9 |
+
def code_agent(prompt, context=None):
|
| 10 |
+
"""
|
| 11 |
+
Generate code based on the prompt and optional context from RAG.
|
| 12 |
+
"""
|
| 13 |
+
if context:
|
| 14 |
+
prompt = f"Use the following context to help with the task:\n{context}\n\nTask: {prompt}"
|
| 15 |
+
|
| 16 |
+
response = openai.ChatCompletion.create(
|
| 17 |
+
model="gpt-4", # or "gpt-3.5-turbo" if you prefer
|
| 18 |
+
messages=[
|
| 19 |
+
{"role": "system", "content": "You are a helpful coding assistant. Write code in Python."},
|
| 20 |
+
{"role": "user", "content": prompt}
|
| 21 |
+
],
|
| 22 |
+
temperature=0.5,
|
| 23 |
+
max_tokens=500
|
| 24 |
+
)
|
| 25 |
+
return response.choices[0].message.content
|