danielrosehill's picture
commit
3ee7577
raw
history blame
5.36 kB
import gradio as gr
import openai
import os
from typing import Optional
# System prompt for reformatting rough drafts into polished system prompts
SYSTEM_PROMPT = """Your task is to assist the user by acting as a friendly assistant with the purpose of converting notes for AI assistants and reformatting them into polished, clear specifications for AI code generation agents and subagents. The text which the user will provide for your reformatting will be a rough draft describing the desired functionalities of the AI code generation agent which will likely fulfill the role of a sub-agent within a multi-agent framework focused on code generation or technical projects Upon receiving this rough draft your task is to reformat it into an improved and polished version defining a clear system prompt directing the agent in the second person you and ensuring that all of the user's desired functionalities are properly reflected in a better organised and clearer version of the system prompt intended to achieve the same deterministic behaviour as the user. as the user intended. Return this improved system prompt to the user in markdown within a code fence and without any other text. Assume each system prompt draft provided by the user to be intended as a new turn in the conversation - the previous agent specifications should not influence your generation of the next spec."""
def reformat_prompt(api_key: str, rough_draft: str) -> str:
"""
Reformat a rough draft into a polished system prompt using OpenAI API.
Args:
api_key: OpenAI API key
rough_draft: The rough draft text to be reformatted
Returns:
Reformatted system prompt or error message
"""
if not api_key.strip():
return "Error: Please provide your OpenAI API key."
if not rough_draft.strip():
return "Error: Please provide a rough draft to reformat."
try:
# Initialize OpenAI client with the provided API key
client = openai.OpenAI(api_key=api_key)
# Make API call to reformat the prompt
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": rough_draft}
],
temperature=0.1,
max_tokens=2000
)
reformatted_text = response.choices[0].message.content
return reformatted_text
except openai.AuthenticationError:
return "Error: Invalid API key. Please check your OpenAI API key."
except openai.RateLimitError:
return "Error: Rate limit exceeded. Please try again later."
except openai.APIError as e:
return f"Error: OpenAI API error - {str(e)}"
except Exception as e:
return f"Error: {str(e)}"
def create_interface():
"""Create and configure the Gradio interface."""
with gr.Blocks(
title="Code Gen Prompt Reformatter",
theme=gr.themes.Soft(),
css="""
.container {
max-width: 1200px;
margin: auto;
}
.header {
text-align: center;
margin-bottom: 2rem;
}
.api-key-box {
background-color: #f8f9fa;
padding: 1rem;
border-radius: 8px;
margin-bottom: 1rem;
}
"""
) as interface:
gr.HTML("""
<div class="header">
<h1>Code Gen Prompt Reformatter</h1>
<p>Text trans</p>
</div>
""")
with gr.Row():
with gr.Column():
api_key_input = gr.Textbox(
label="OpenAI API Key",
placeholder="sk-...",
type="password",
info="Your API key is not stored and only used for this session"
)
rough_draft_input = gr.Textbox(
label="Rough Draft",
placeholder="Enter your rough draft describing the desired functionalities of the AI code generation agent...",
lines=10,
max_lines=20
)
with gr.Row():
clear_btn = gr.Button("Clear", variant="secondary")
submit_btn = gr.Button("Reformat Prompt", variant="primary", scale=2)
with gr.Column():
output = gr.Textbox(
label="Reformatted System Prompt",
lines=15,
max_lines=25,
show_copy_button=True,
interactive=False
)
# Event handlers
submit_btn.click(
fn=reformat_prompt,
inputs=[api_key_input, rough_draft_input],
outputs=output,
show_progress=True
)
clear_btn.click(
fn=lambda: ("", "", ""),
outputs=[api_key_input, rough_draft_input, output]
)
return interface
# Create and launch the interface
if __name__ == "__main__":
app = create_interface()
app.launch(
server_name="0.0.0.0",
server_port=7860,
show_error=True
)