Spaces:
Running
Running
| import requests | |
| import os | |
| from dotenv import load_dotenv | |
| from typing import Optional | |
| load_dotenv() | |
| OPENAI_API_KEY=os.getenv("OPENAI_API_KEY") | |
| MODELS = { | |
| "nano":"gpt-4.1-nano", | |
| "mini":"gpt-4.1-mini" | |
| } | |
| def prompt_generator(prompt: str, tools: Optional[str] = None, model=MODELS["nano"], api_key: Optional[str] = None) -> str: | |
| key = api_key if api_key else OPENAI_API_KEY | |
| if not key: | |
| return "No API key provided." | |
| client_instruction = """Run the following prompt after you receive it and act directly upon it: """ | |
| with open("prompts/coding.txt", "r") as file: | |
| instructions = client_instruction + file.read() | |
| if tools: | |
| tools_prompt = f""" | |
| The LLM/agent consuming the prompt you are creating has access to the following MCP clients/tools: {tools}. | |
| For this problem, include in the prompt indications that use {tools}, as the agent will be able to invoke them while running it. | |
| The instructions related to the usage of this tools can be added as a remark at the end in a way like this: | |
| Use [example tool] to scrape the web and find dummy data to build the tests for the software created. | |
| If possible, provide concrete examples of how to use '{tools}' as part of your solution or instructions. | |
| If you do not know the capabilities of the tools, use your internal knowledge base to determine it, invoke tooling like web searching if you have it available or infer it from thecontext as a last resource. | |
| """ | |
| instructions += tools_prompt | |
| headers = { | |
| "Authorization": f"Bearer {key}", | |
| "Content-Type": "application/json" | |
| } | |
| data = { | |
| "model": model, | |
| "input": prompt, | |
| "instructions": instructions, | |
| "tool_choice": "auto" | |
| } | |
| response = requests.post( | |
| url="https://api.openai.com/v1/responses", | |
| headers=headers, | |
| json=data | |
| ) | |
| if response.status_code == 200: | |
| response_json = response.json() | |
| output = response_json.get("output", []) | |
| if not output or not isinstance(output, list): | |
| return "No output found in response." | |
| first_output = output[0] | |
| content = first_output.get("content", []) | |
| if not content or not isinstance(content, list): | |
| return "No content found in output." | |
| first_content = content[0] | |
| output_context = "Run the following prompt as if it were the prompt given by the user:\n\n" | |
| output_text = output_context + first_content.get("text", "No 'text' field found in content.") | |
| return output_text | |
| else: | |
| return f"Request failed with status code {response.status_code}: {response.text}" | |
| def basic(prompt: str, tools: Optional[str] = None, api_key: Optional[str] = None): | |
| return prompt_generator(prompt, tools, model=MODELS["nano"], api_key=api_key) | |
| def no_tooling(prompt: str, api_key: Optional[str] = None) -> str: | |
| return prompt_generator(prompt, None, model=MODELS["nano"], api_key=api_key) | |
| def advanced(prompt: str, tools: Optional[str] = None, api_key: Optional[str] = None): | |
| return prompt_generator(prompt, tools, model=MODELS["mini"], api_key=api_key) | |
| TOOLS = { | |
| "B": basic, | |
| "A": advanced, | |
| "N": no_tooling | |
| } | |
| def prompt_tool(prompt: str, tool: str, api_key: Optional[str] = None, tools: Optional[str] = None): | |
| if tool == "N": | |
| return TOOLS[tool](prompt, api_key=api_key) | |
| else: | |
| return TOOLS[tool](prompt, tools=tools, api_key=api_key) | |
| def main(): | |
| prompt = """ | |
| I need to write a Django view for creating an apointment | |
| for a Restaurant w a lot of constraints like it being within | |
| the hours, there being tables avaiable, | |
| having enough staff to serve a customer, | |
| as well as connecting it with my existing views and url schemas. | |
| """ | |
| tools = "playwright" | |
| print(prompt_tool(prompt, "A", tools=tools)) | |
| if __name__ == "__main__": | |
| main() | |