Spaces:
Sleeping
Sleeping
fix
Browse files17 unmodified lines1819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626317 unmodified lines# Initialize the search toolsearch_tool = DuckDuckGoSearchTool()# Create a custom system prompt that ensures code is formatted correctlysystem_prompt = """You are an AI assistant that responds with Python code.When answering questions, always follow this format:Thoughts: First explain your thinking process hereCode:```py# Your Python code here to solve the problem# Make sure to use proper Python syntax# Include comments to explain your code```Always include the ```py syntax at the beginning of your code block and end with ```.Never skip the code section, even if you're unsure - make your best attempt."""# Initialize Agent with the custom system promptself.agent = CodeAgent(model=model,tools=[search_tool],system_prompt=system_prompt)def __call__(self, question: str) -> str:print(f"Agent received question (first 50 chars): {question[:50]}...")# Enhance the question to reinforce code format requirementsenhanced_question = f"""Please solve this question using Python code:{question}Remember to format your answer with:Thoughts: Your analysis hereCode:```py# Your Python code solution here```"""fixed_answer = self.agent.run(enhanced_question)print(f"Agent returning fixed answer: {fixed_answer}")return fixed_answerdef run_and_submit_all(profile: gr.OAuthProfile | None):17 unmodified lines181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787917 unmodified lines# Initialize the search toolsearch_tool = DuckDuckGoSearchTool()# Initialize Agent without system promptself.agent = CodeAgent(model=model,tools=[search_tool])def __call__(self, question: str) -> str:print(f"Agent received question (first 50 chars): {question[:50]}...")# Enhance the question to reinforce code format requirementsenhanced_question = f"""You MUST solve this problem using Python code. Always start with 'Thoughts:' section followed by a 'Code:' section with a ```py codeblock:QUESTION: {question}REMEMBER:1. Your response MUST follow this format exactly:Thoughts: [your reasoning here]Code:```py# Your Python code solution here```2. Always include ```py at the beginning of your code block and close with ```3. Even if the answer seems simple, you MUST include code to derive it4. If you need to search, use web_search("query")5. If you need to provide a final answer, use final_answer(value)Now solve the problem:"""try:fixed_answer = self.agent.run(enhanced_question)print(f"Agent returning answer (first 100 chars): {fixed_answer[:100]}...")return fixed_answerexcept Exception as e:print(f"Error running agent: {e}")# If agent fails, provide a formatted response that matches expected patternfallback_answer = f"""Thoughts: I encountered an error while solving this problem.Code:```py# Fallback solutionfrom datetime import datetimedef solve_problem():# The original question was: {question[:100]}...print("Attempting to solve the problem")# Fallback approachreturn "Unable to automatically solve this problem"# Execute solutionresult = solve_problem()print(f"Result: {result}")```Due to technical difficulties, I couldn't fully process this question. Here's my best assessment: The question appears to be about {question[:50]}... Without being able to run a complete analysis, I would recommend approaching this by breaking down the problem and using appropriate Python libraries for the specific task."""return fallback_answerdef run_and_submit_all(profile: gr.OAuthProfile | None):
app.py+42-26