Spaces:
Sleeping
Sleeping
Update app.py
Browse files4 unmodified lines56789101112131415161718192021222324252627282930313233343536373839409 unmodified lines5051525354555616 unmodified lines7374757677787980818283845 unmodified lines909192939495969798991001011021031041051061071081091103 unmodified lines1141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541556 unmodified lines1621631641651661671681691701 unmodified line1721731741751761771781798 unmodified lines1881891901911921931941951965 unmodified lines2022032042052062072082092106 unmodified lines2172182192202212222232242254 unmodified lines2302312322332342352369 unmodified lines2462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932941 unmodified line2962972982993003013023033043053063073083093103113123133143153163173182 unmodified lines3213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533544 unmodified lines3593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444 unmodified linesimport queueimport gradio as grimport httpxfrom typing import Generator, Any, Dict, List, Optionalfrom functools import lru_cache# -------------------- Configuration --------------------logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")# -------------------- External Model Call (with Caching and Retry) --------------------async def call_model(prompt: str, model: str = "gpt-4o", api_key: str = None, max_retries: int = 3) -> str:"""Sends a prompt to the OpenAI API endpoint with retries and exponential backoff."""if api_key is None:api_key = os.getenv("OPENAI_API_KEY")if api_key is None:raise ValueError("OpenAI API key not found.")url = "https://api.openai.com/v1/chat/completions"headers = {"Authorization": f"Bearer {api_key}","Content-Type": "application/json",}payload = {"model": model, "messages": [{"role": "user", "content": prompt}]}for attempt in range(max_retries):try:async with httpx.AsyncClient(timeout=httpx.Timeout(300.0)) as client:response = await client.post(url, headers=headers, json=payload)response.raise_for_status()response_json = response.json() # Synchronous parsing is acceptable herereturn response_json["choices"][0]["message"]["content"]except httpx.HTTPStatusError as e:logging.error(f"HTTP error (attempt {attempt + 1}/{max_retries}): {e}")9 unmodified linesexcept Exception as e:logging.error(f"Unexpected error (attempt {attempt+1}/{max_retries}): {e}")raiseraise Exception(f"Failed to get response from OpenAI API after {max_retries} attempts.")# -------------------- Shared Context --------------------class Context:16 unmodified linesself.conversation_history.append({"agent": agent_name, "message": message})# -------------------- Agent Classes --------------------class PromptOptimizerAgent:async def optimize_prompt(self, context: Context, api_key: str) -> Context:"""Optimizes the user’s original prompt."""system_prompt = ("Improve the prompt. Be clear, specific, and complete. ""Keep original intent. Return ONLY the revised prompt."5 unmodified linesreturn contextclass OrchestratorAgent:def __init__(self, log_queue: queue.Queue, human_event: threading.Event, human_input_queue: queue.Queue) -> None:self.log_queue = log_queueself.human_event = human_eventself.human_input_queue = human_input_queueasync def generate_plan(self, context: Context, api_key: str) -> Context:"""Generates (or revises) a plan using human feedback if necessary.Uses an iterative approach instead of recursion."""while True:if context.plan:prompt = (f"You are a planner. Revise/complete the plan for '{context.original_task}' using feedback:\n"f"{context.plan}\n\n""If unsure, output 'REQUEST_HUMAN_FEEDBACK\\n[Question]'")else:3 unmodified lines"Include review/revision steps, error handling, and documentation instructions.\n\n""If unsure, output 'REQUEST_HUMAN_FEEDBACK\\n[Question]'")plan = await call_model(prompt, model="gpt-4o", api_key=api_key)context.add_conversation_entry("Orchestrator", f"Plan:\n{plan}")# Check if human feedback is requested.if "REQUEST_HUMAN_FEEDBACK" in plan:question = plan.split("REQUEST_HUMAN_FEEDBACK\n", 1)[1].strip()self.log_queue.put("[Orchestrator]: Requesting human feedback...")self.log_queue.put(f"[Orchestrator]: Question for human: {question}")# Prepare feedback context and trigger the human feedback event.feedback_request_context = (f"The orchestrator agent is requesting feedback on the following task:\n"f"**{context.optimized_task}**\n\n"f"Current plan:\n**{context.plan or 'None'}**\n\n"f"Question:\n**{question}**")self.human_event.set()# Pass the context to the human input handler.self.human_input_queue.put(feedback_request_context)human_response = self.human_input_queue.get() # Blocking call for human response.self.human_event.clear()self.log_queue.put(f"[Orchestrator]: Received human feedback: {human_response}")# Incorporate human feedback into the plan and loop again.context.plan = context.plan + "\n" + human_response if context.plan else human_responseelse:context.plan = planbreak # Exit loop when no feedback is requested.return contextclass CoderAgent:async def generate_code(self, context: Context, api_key: str, model: str = "gpt-4o") -> Context:"""Generates code based on the provided plan."""prompt = ("You are a coding agent. Output ONLY the code. ""Adhere to best practices and include error handling.\n\n"6 unmodified linesclass CodeReviewerAgent:async def review_code(self, context: Context, api_key: str) -> Context:"""Reviews the generated code and returns either actionable feedback or 'APPROVE'."""prompt = ("You are a code reviewer. Provide CONCISE feedback focusing on correctness, efficiency, readability, error handling, and security. ""If the code is acceptable, respond with ONLY 'APPROVE'. Do NOT generate code.\n\n"1 unmodified line)review = await call_model(prompt, model="gpt-4o", api_key=api_key)context.add_conversation_entry("Code Reviewer", f"Review:\n{review}")# Check for approval; if not approved, parse feedback.if "APPROVE" not in review.upper():structured_review = {"comments": []}for line in review.splitlines():8 unmodified linesclass QualityAssuranceTesterAgent:async def generate_test_cases(self, context: Context, api_key: str) -> Context:"""Generates test cases considering edge and error cases."""prompt = ("You are a testing agent. Generate comprehensive test cases considering edge cases and error scenarios. ""Output in a clear format.\n\n"5 unmodified linesreturn contextasync def run_tests(self, context: Context, api_key: str) -> Context:"""Runs the generated test cases and compares expected vs. actual outcomes."""prompt = ("Run the test cases. Compare actual vs expected outputs and state any discrepancies. ""If all tests pass, output 'TESTS PASSED'.\n\n"6 unmodified linesclass DocumentationAgent:async def generate_documentation(self, context: Context, api_key: str) -> Context:"""Generates concise documentation including a --help message."""prompt = ("Generate clear documentation including a brief description, explanation, and a --help message.\n\n"f"Code:\n{context.code}"4 unmodified linesreturn context# -------------------- Agent Dispatcher --------------------class AgentDispatcher:def __init__(self, log_queue: queue.Queue, human_event: threading.Event, human_input_queue: queue.Queue):self.log_queue = log_queue9 unmodified lines}async def dispatch(self, agent_name: str, context: Context, api_key: str, **kwargs) -> Context:"""Dispatches the task to the specified agent."""agent = self.agents.get(agent_name)if not agent:raise ValueError(f"Unknown agent: {agent_name}")self.log_queue.put(f"[{agent_name.replace('_', ' ').title()}]: Starting task...")if agent_name == "prompt_optimizer":context = await agent.optimize_prompt(context, api_key)elif agent_name == "orchestrator":context = await agent.generate_plan(context, api_key)elif agent_name == "coder":context = await agent.generate_code(context, api_key, **kwargs)elif agent_name == "code_reviewer":context = await agent.review_code(context, api_key)elif agent_name == "qa_tester":if kwargs.get("generate_tests", False):context = await agent.generate_test_cases(context, api_key)elif kwargs.get("run_tests", False):context = await agent.run_tests(context, api_key)elif agent_name == "documentation_agent":context = await agent.generate_documentation(context, api_key)else:raise ValueError(f"Unknown Agent Name: {agent_name}")return contextasync def determine_next_agent(self, context: Context, api_key: str) -> str:"""Determines the next agent to run based on the current context."""if not context.optimized_task:return "prompt_optimizer"if not context.plan:return "orchestrator"if not context.code:return "coder"# Check if any review comment lacks an APPROVE.if not any("APPROVE" in comment.get("issue", "").upper()for review in context.review_commentsfor comment in review.get("comments", [])):return "code_reviewer"if not context.test_cases:return "qa_tester"1 unmodified linereturn "qa_tester"if not context.documentation:return "documentation_agent"return "done" # All tasks are complete# -------------------- Multi-Agent Conversation --------------------async def multi_agent_conversation(task_message: str, log_queue: queue.Queue, api_key: str,human_event: threading.Event, human_input_queue: queue.Queue) -> None:"""Orchestrates the multi-agent conversation."""context = Context(original_task=task_message)dispatcher = AgentDispatcher(log_queue, human_event, human_input_queue)next_agent = await dispatcher.determine_next_agent(context, api_key)# Prevent endless revisions by tracking coder iterations.coder_iterations = 0while next_agent != "done":if next_agent == "qa_tester":if not context.test_cases:2 unmodified linescontext = await dispatcher.dispatch(next_agent, context, api_key, run_tests=True)elif next_agent == "coder" and (context.review_comments or context.test_results):coder_iterations += 1# Switch to a different model after the first iteration.context = await dispatcher.dispatch(next_agent, context, api_key, model="gpt-3.5-turbo-16k")else:context = await dispatcher.dispatch(next_agent, context, api_key)# Check for approval in code review if applicable.if next_agent == "code_reviewer":approved = any("APPROVE" in comment.get("issue", "").upper()for review in context.review_commentsfor comment in review.get("comments", []))if not approved:# If not approved, we continue with coder to improve the code.next_agent = "coder"else:next_agent = await dispatcher.determine_next_agent(context, api_key)else:next_agent = await dispatcher.determine_next_agent(context, api_key)if next_agent == "coder" and coder_iterations > 5:log_queue.put("Maximum revision iterations reached. Exiting.")breaklog_queue.put("Conversation complete.")log_queue.put(("result", context.conversation_history))# -------------------- Process Generator and Human Input --------------------def process_conversation_generator(task_message: str, api_key: str,human_event: threading.Event, human_input_queue: queue.Queue,log_queue: queue.Queue) -> Generator[str, None, None]:4 unmodified linesdef run_conversation():asyncio.run(multi_agent_conversation(task_message, log_queue, api_key, human_event, human_input_queue))# Start the asynchronous conversation in a separate thread.conversation_thread = threading.Thread(target=run_conversation)conversation_thread.start()# Continuously yield log messages until the conversation is complete.while conversation_thread.is_alive() or not log_queue.empty():try:msg = log_queue.get(timeout=0.1)if isinstance(msg, tuple) and msg[0] == "result":# Update the chat box with the final conversation history.yield gr.Chatbot.update(value=msg[1], visible=True)else:yield msgexcept queue.Empty:passif human_event.is_set():yield "Waiting for human feedback..."# Small sleep to prevent busy-waiting.time.sleep(0.1)yield "Conversation complete."def get_human_feedback(placeholder_text: str, human_input_queue: queue.Queue) -> gr.Blocks:"""Constructs the Gradio interface to collect human feedback."""with gr.Blocks() as human_feedback_interface:with gr.Row():human_input = gr.Textbox(lines=4, label="Human Feedback", placeholder=placeholder_text)with gr.Row():submit_button = gr.Button("Submit Feedback")def submit_feedback(input_text: str):human_input_queue.put(input_text)return ""submit_button.click(fn=submit_feedback, inputs=human_input, outputs=human_input)return human_feedback_interface# -------------------- Chat Function for Gradio --------------------def multi_agent_chat(message: str, history: List[Any], openai_api_key: str = None) -> Generator[Any, None, None]:"""Gradio chat function that runs the multi-agent conversation."""if not openai_api_key:openai_api_key = os.getenv("OPENAI_API_KEY")if not openai_api_key:yield "Error: API key not provided."returnhuman_event = threading.Event()human_input_queue = queue.Queue()log_queue = queue.Queue()yield from process_conversation_generator(message, openai_api_key, human_event, human_input_queue, log_queue)# -------------------- Launch the Chatbot --------------------iface = gr.ChatInterface(fn=multi_agent_chat,chatbot=gr.Chatbot(type="messages"),additional_inputs=[gr.Textbox(label="OpenAI API Key (optional)", type="password", placeholder="Leave blank to use env variable")],title="Multi-Agent Task Solver with Human-in-the-Loop",description=("- Collaborative workflow with Human-in-the-Loop.\n""- Orchestrator can ask for human feedback.\n""- Enter a task; agents will work on it. You may be prompted for input.\n""- Max 5 revisions.\n""- Provide API Key."))# Dummy interface to prevent Gradio errors.dummy_iface = gr.Interface(lambda x: x, "textbox", "textbox")if __name__ == "__main__":demo = gr.TabbedInterface([iface, dummy_iface], ["Chatbot", "Dummy"])demo.launch(share=True)4 unmodified lines56789101112131415161718192021222324252627282930313233343536379 unmodified lines4748495051525316 unmodified lines70717273747576775 unmodified lines838485868788899091929394959697983 unmodified lines1021031041051061071081091101111121131141151161171181191201211221231241251261271281296 unmodified lines1361371381391401411 unmodified line1431441451461471488 unmodified lines1571581591601611625 unmodified lines1681691701711721736 unmodified lines1801811821831841854 unmodified lines1901911921931941959 unmodified lines2052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372381 unmodified line2402412422432442452462472482492502512522532542 unmodified lines2572582592602612622632642652662672682692702712722732742752762772782792804 unmodified lines2852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363374 unmodified linesimport queueimport gradio as grimport httpximport timefrom typing import Generator, Any, Dict, List, Optional# -------------------- Configuration --------------------logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")# -------------------- External Model Call (with Caching and Retry) --------------------async def call_model(prompt: str, model: str = "gpt-4o", api_key: str = None, max_retries: int = 3) -> str:if api_key is None:api_key = os.getenv("OPENAI_API_KEY")if api_key is None:raise ValueError("OpenAI API key not provided.")url = "https://api.openai.com/v1/chat/completions"headers = {"Authorization": f"Bearer {api_key}","Content-Type": "application/json",}payload = {"model": model,"messages": [{"role": "user", "content": prompt}],}for attempt in range(max_retries):try:async with httpx.AsyncClient(timeout=httpx.Timeout(300.0)) as client:response = await client.post(url, headers=headers, json=payload)response.raise_for_status()response_json = response.json()return response_json["choices"][0]["message"]["content"]except httpx.HTTPStatusError as e:logging.error(f"HTTP error (attempt {attempt + 1}/{max_retries}): {e}")9 unmodified linesexcept Exception as e:logging.error(f"Unexpected error (attempt {attempt+1}/{max_retries}): {e}")raiseraise Exception(f"Failed to get response after {max_retries} attempts.")# -------------------- Shared Context --------------------class Context:16 unmodified linesself.conversation_history.append({"agent": agent_name, "message": message})# -------------------- Agent Classes --------------------class PromptOptimizerAgent:async def optimize_prompt(self, context: Context, api_key: str) -> Context:system_prompt = ("Improve the prompt. Be clear, specific, and complete. ""Keep original intent. Return ONLY the revised prompt."5 unmodified linesreturn contextclass OrchestratorAgent:def __init__(self, log_queue: queue.Queue, human_event: threading.Event, human_input_queue: queue.Queue):self.log_queue = log_queueself.human_event = human_eventself.human_input_queue = human_input_queueasync def generate_plan(self, context: Context, api_key: str) -> Context:while True:if context.plan:prompt = (f"You are a planner. Revise/complete the plan for '{context.original_task}'. ""If unsure, output 'REQUEST_HUMAN_FEEDBACK\\n[Question]'")else:3 unmodified lines"Include review/revision steps, error handling, and documentation instructions.\n\n""If unsure, output 'REQUEST_HUMAN_FEEDBACK\\n[Question]'")plan = await call_model(prompt, model="gpt-4o", api_key=api_key)context.add_conversation_entry("Orchestrator", f"Plan:\n{plan}")if "REQUEST_HUMAN_FEEDBACK" in plan:question = plan.split("REQUEST_HUMAN_FEEDBACK\n", 1)[1].strip()self.log_queue.put("[Orchestrator]: Requesting human feedback...")self.log_queue.put(f"[Orchestrator]: Question: {question}")feedback_context = (f"Task: {context.optimized_task}\nCurrent Plan: {context.plan or 'None'}\nQuestion: {question}")self.human_event.set()self.human_input_queue.put(feedback_context)human_response = self.human_input_queue.get() # blocking call waiting for human responseself.human_event.clear()self.log_queue.put(f"[Orchestrator]: Received human feedback: {human_response}")context.plan = (context.plan + "\n" + human_response) if context.plan else human_responseelse:context.plan = planbreakreturn contextclass CoderAgent:async def generate_code(self, context: Context, api_key: str, model: str = "gpt-4o") -> Context:prompt = ("You are a coding agent. Output ONLY the code. ""Adhere to best practices and include error handling.\n\n"6 unmodified linesclass CodeReviewerAgent:async def review_code(self, context: Context, api_key: str) -> Context:prompt = ("You are a code reviewer. Provide CONCISE feedback focusing on correctness, efficiency, readability, error handling, and security. ""If the code is acceptable, respond with ONLY 'APPROVE'. Do NOT generate code.\n\n"1 unmodified line)review = await call_model(prompt, model="gpt-4o", api_key=api_key)context.add_conversation_entry("Code Reviewer", f"Review:\n{review}")if "APPROVE" not in review.upper():structured_review = {"comments": []}for line in review.splitlines():8 unmodified linesclass QualityAssuranceTesterAgent:async def generate_test_cases(self, context: Context, api_key: str) -> Context:prompt = ("You are a testing agent. Generate comprehensive test cases considering edge cases and error scenarios. ""Output in a clear format.\n\n"5 unmodified linesreturn contextasync def run_tests(self, context: Context, api_key: str) -> Context:prompt = ("Run the test cases. Compare actual vs expected outputs and state any discrepancies. ""If all tests pass, output 'TESTS PASSED'.\n\n"6 unmodified linesclass DocumentationAgent:async def generate_documentation(self, context: Context, api_key: str) -> Context:prompt = ("Generate clear documentation including a brief description, explanation, and a --help message.\n\n"f"Code:\n{context.code}"4 unmodified linesreturn context# -------------------- Agent Dispatcher --------------------class AgentDispatcher:def __init__(self, log_queue: queue.Queue, human_event: threading.Event, human_input_queue: queue.Queue):self.log_queue = log_queue9 unmodified lines}async def dispatch(self, agent_name: str, context: Context, api_key: str, **kwargs) -> Context:self.log_queue.put(f"[{agent_name.replace('_', ' ').title()}]: Starting task...")if agent_name == "prompt_optimizer":context = await self.agents[agent_name].optimize_prompt(context, api_key)elif agent_name == "orchestrator":context = await self.agents[agent_name].generate_plan(context, api_key)elif agent_name == "coder":context = await self.agents[agent_name].generate_code(context, api_key, **kwargs)elif agent_name == "code_reviewer":context = await self.agents[agent_name].review_code(context, api_key)elif agent_name == "qa_tester":if kwargs.get("generate_tests", False):context = await self.agents[agent_name].generate_test_cases(context, api_key)elif kwargs.get("run_tests", False):context = await self.agents[agent_name].run_tests(context, api_key)elif agent_name == "documentation_agent":context = await self.agents[agent_name].generate_documentation(context, api_key)else:raise ValueError(f"Unknown agent: {agent_name}")return contextasync def determine_next_agent(self, context: Context, api_key: str) -> str:if not context.optimized_task:return "prompt_optimizer"if not context.plan:return "orchestrator"if not context.code:return "coder"if not any("APPROVE" in comment.get("issue", "").upper() for review in context.review_comments for comment in review.get("comments", [])):return "code_reviewer"if not context.test_cases:return "qa_tester"1 unmodified linereturn "qa_tester"if not context.documentation:return "documentation_agent"return "done"# -------------------- Multi-Agent Conversation --------------------async def multi_agent_conversation(task_message: str, log_queue: queue.Queue, api_key: str,human_event: threading.Event, human_input_queue: queue.Queue) -> None:context = Context(original_task=task_message)dispatcher = AgentDispatcher(log_queue, human_event, human_input_queue)next_agent = await dispatcher.determine_next_agent(context, api_key)coder_iterations = 0while next_agent != "done":if next_agent == "qa_tester":if not context.test_cases:2 unmodified linescontext = await dispatcher.dispatch(next_agent, context, api_key, run_tests=True)elif next_agent == "coder" and (context.review_comments or context.test_results):coder_iterations += 1context = await dispatcher.dispatch(next_agent, context, api_key, model="gpt-3.5-turbo-16k")else:context = await dispatcher.dispatch(next_agent, context, api_key)if next_agent == "code_reviewer":approved = any("APPROVE" in comment.get("issue", "").upper() for review in context.review_comments for comment in review.get("comments", []))if not approved:next_agent = "coder"else:next_agent = await dispatcher.determine_next_agent(context, api_key)else:next_agent = await dispatcher.determine_next_agent(context, api_key)if next_agent == "coder" and coder_iterations > 5:log_queue.put("Maximum revision iterations reached. Exiting.")breaklog_queue.put("Conversation complete.")log_queue.put(("result", context.conversation_history))# -------------------- Process Conversation Generator --------------------def process_conversation_generator(task_message: str, api_key: str,human_event: threading.Event, human_input_queue: queue.Queue,log_queue: queue.Queue) -> Generator[str, None, None]:4 unmodified linesdef run_conversation():asyncio.run(multi_agent_conversation(task_message, log_queue, api_key, human_event, human_input_queue))conversation_thread = threading.Thread(target=run_conversation)conversation_thread.start()while conversation_thread.is_alive() or not log_queue.empty():try:msg = log_queue.get(timeout=0.1)if isinstance(msg, tuple) and msg[0] == "result":yield gr.Chatbot.update(value=msg[1], visible=True)else:yield msgexcept queue.Empty:passif human_event.is_set():yield "Waiting for human feedback..."time.sleep(0.1)yield "Conversation complete."# -------------------- Multi-Agent Chat Function --------------------def multi_agent_chat(message: str, openai_api_key: str = None) -> Generator[Any, None, None]:if not openai_api_key:openai_api_key = os.getenv("OPENAI_API_KEY")if not openai_api_key:yield "Error: API key not provided."returnhuman_event = threading.Event()human_input_queue = queue.Queue()log_queue = queue.Queue()yield from process_conversation_generator(message, openai_api_key, human_event, human_input_queue, log_queue)# -------------------- Custom Gradio Blocks Interface --------------------with gr.Blocks() as demo:gr.Markdown("## Multi-Agent Task Solver with Human-in-the-Loop")with gr.Row():chat_output = gr.Chatbot(label="Conversation")with gr.Row():with gr.Column(scale=8):message_input = gr.Textbox(label="Enter your task", placeholder="Type your task here...", lines=3)with gr.Column(scale=2):api_key_input = gr.Textbox(label="API Key (optional)", type="password", placeholder="Leave blank to use env variable")send_button = gr.Button("Send")# When Send is clicked, the multi_agent_chat generator is called and its output is streamed to the chat.send_button.click(fn=multi_agent_chat, inputs=[message_input, api_key_input], outputs=chat_output, stream=True)if __name__ == "__main__":demo.launch(share=True)
app.py+49-156