curiouscurrent commited on
Commit
423ab13
·
verified ·
1 Parent(s): b7b2532

Update backend_agent/api_generator.py

Browse files
Files changed (1) hide show
  1. 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 a Python backend skeleton (Flask API) for a given backend task
13
- using an LLM.
14
 
15
- Returns a string containing the code.
 
16
  """
17
  prompt = f"""
18
- You are an expert backend developer. Generate a Python Flask API skeleton for this backend task.
19
- Include routes, basic input validation, and placeholders for business logic.
 
20
 
21
  Task: {task_name}
22
 
23
- Python Flask code:
 
24
  """
25
  inputs = tokenizer(prompt, return_tensors="pt")
26
- outputs = model.generate(**inputs, max_new_tokens=250)
27
  code = tokenizer.decode(outputs[0], skip_special_tokens=True)
28
 
29
- # Clean up the prompt portion from the output
30
- if "Python Flask code:" in code:
31
- code = code.split("Python Flask code:")[-1].strip()
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