Spaces:
Runtime error
Runtime error
Update backend_agent/api_generator.py
Browse files- backend_agent/api_generator.py +12 -10
backend_agent/api_generator.py
CHANGED
|
@@ -9,25 +9,27 @@ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
|
| 9 |
|
| 10 |
def generate_backend_code_llm(task_name):
|
| 11 |
"""
|
| 12 |
-
Generates
|
| 13 |
-
using an LLM.
|
| 14 |
|
| 15 |
-
|
|
|
|
| 16 |
"""
|
| 17 |
prompt = f"""
|
| 18 |
-
You are an expert backend developer.
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
Task: {task_name}
|
| 22 |
|
| 23 |
-
Python
|
|
|
|
| 24 |
"""
|
| 25 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 26 |
-
outputs = model.generate(**inputs, max_new_tokens=
|
| 27 |
code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
if "
|
| 31 |
-
code = code.split("
|
| 32 |
|
| 33 |
return code
|
|
|
|
| 9 |
|
| 10 |
def generate_backend_code_llm(task_name):
|
| 11 |
"""
|
| 12 |
+
Generates Python backend code (REST or GraphQL) for a given task using an LLM.
|
|
|
|
| 13 |
|
| 14 |
+
The LLM decides whether the task is best suited for a REST API or GraphQL API
|
| 15 |
+
based on the task description.
|
| 16 |
"""
|
| 17 |
prompt = f"""
|
| 18 |
+
You are an expert backend developer. Given this task, generate Python code
|
| 19 |
+
for either a REST API (Flask/FastAPI) or a GraphQL API (Graphene/FastAPI-GraphQL),
|
| 20 |
+
depending on which is more appropriate for the task.
|
| 21 |
|
| 22 |
Task: {task_name}
|
| 23 |
|
| 24 |
+
Provide the Python code with routes, input validation, and placeholders for business logic.
|
| 25 |
+
Also include comments explaining what each part does.
|
| 26 |
"""
|
| 27 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 28 |
+
outputs = model.generate(**inputs, max_new_tokens=350)
|
| 29 |
code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 30 |
|
| 31 |
+
# Remove prompt from output if it’s repeated
|
| 32 |
+
if "Task:" in code:
|
| 33 |
+
code = code.split("Task:")[-1].strip()
|
| 34 |
|
| 35 |
return code
|