From ff653c7208fb18a440f681e0cce6212c43119893 Mon Sep 17 00:00:00 2001 From: AI Christianson Date: Sat, 8 Mar 2025 15:12:13 -0500 Subject: [PATCH] reasoning assistance for implementation tasks --- ra_aid/agent_utils.py | 107 +++++++++++++++++++++- ra_aid/prompts/implementation_prompts.py | 2 + ra_aid/prompts/reasoning_assist_prompt.py | 56 ++++++++++- 3 files changed, 161 insertions(+), 4 deletions(-) diff --git a/ra_aid/agent_utils.py b/ra_aid/agent_utils.py index 8f6f98d..ed8ccbe 100644 --- a/ra_aid/agent_utils.py +++ b/ra_aid/agent_utils.py @@ -71,7 +71,7 @@ from ra_aid.prompts.human_prompts import ( from ra_aid.prompts.implementation_prompts import IMPLEMENTATION_PROMPT from ra_aid.prompts.common_prompts import NEW_PROJECT_HINTS from ra_aid.prompts.planning_prompts import PLANNING_PROMPT -from ra_aid.prompts.reasoning_assist_prompt import REASONING_ASSIST_PROMPT_PLANNING +from ra_aid.prompts.reasoning_assist_prompt import REASONING_ASSIST_PROMPT_PLANNING, REASONING_ASSIST_PROMPT_IMPLEMENTATION from ra_aid.prompts.research_prompts import ( RESEARCH_ONLY_PROMPT, RESEARCH_PROMPT, @@ -977,6 +977,108 @@ def run_task_implementation_agent( formatted_project_info = "Project info unavailable" # Get environment inventory information + env_inv = get_env_inv() + + # Get model configuration to check for reasoning_assist_default + provider = config.get("provider") if config else get_config_repository().get("provider", "") + model_name = config.get("model") if config else get_config_repository().get("model", "") + logger.debug("Checking for reasoning_assist_default on %s/%s", provider, model_name) + + model_config = {} + provider_models = models_params.get(provider, {}) + if provider_models and model_name in provider_models: + model_config = provider_models[model_name] + + # Check if reasoning assist is enabled + reasoning_assist_enabled = model_config.get("reasoning_assist_default", False) + logger.debug("Reasoning assist enabled: %s", reasoning_assist_enabled) + + # Initialize implementation guidance section + implementation_guidance_section = "" + + # If reasoning assist is enabled, make a one-off call to the expert model + if reasoning_assist_enabled: + try: + logger.info("Reasoning assist enabled for model %s, getting implementation guidance", model_name) + + # Collect tool descriptions + tool_metadata = [] + from ra_aid.tools.reflection import get_function_info as get_tool_info + + for tool in tools: + try: + tool_info = get_tool_info(tool.func) + name = tool.func.__name__ + description = inspect.getdoc(tool.func) + tool_metadata.append(f"Tool: {name}\\nDescription: {description}\\n") + except Exception as e: + logger.warning(f"Error getting tool info for {tool}: {e}") + + # Format tool metadata + formatted_tool_metadata = "\\n".join(tool_metadata) + + # Initialize expert model + expert_model = initialize_expert_llm(provider, model_name) + + # Format the reasoning assist prompt for implementation + reasoning_assist_prompt = REASONING_ASSIST_PROMPT_IMPLEMENTATION.format( + current_date=current_date, + working_directory=working_directory, + task=task, + key_facts=key_facts, + key_snippets=format_key_snippets_dict(get_key_snippet_repository().get_snippets_dict()), + research_notes=formatted_research_notes, + related_files="\\n".join(related_files), + env_inv=env_inv, + tool_metadata=formatted_tool_metadata, + ) + + # Show the reasoning assist query in a panel + console.print( + Panel(Markdown("Consulting with the reasoning model on the best implementation approach."), title="📝 Thinking about implementation...", border_style="yellow") + ) + + logger.debug("Invoking expert model for implementation reasoning assist") + # Make the call to the expert model + response = expert_model.invoke(reasoning_assist_prompt) + + # Check if the model supports think tags + supports_think_tag = model_config.get("supports_think_tag", False) + supports_thinking = model_config.get("supports_thinking", False) + + # Process response content + content = None + + if hasattr(response, 'content'): + content = response.content + else: + # Fallback if content attribute is missing + content = str(response) + + # Process the response content using the centralized function + content, extracted_thinking = process_thinking_content( + content=content, + supports_think_tag=supports_think_tag, + supports_thinking=supports_thinking, + panel_title="💭 Implementation Thinking", + panel_style="yellow", + logger=logger + ) + + # Display the implementation guidance in a panel + console.print( + Panel(Markdown(content), title="Implementation Guidance", border_style="blue") + ) + + # Format the implementation guidance section for the prompt + implementation_guidance_section = f""" +{content} +""" + + logger.info("Received implementation guidance") + except Exception as e: + logger.error("Error getting implementation guidance: %s", e) + implementation_guidance_section = "" prompt = IMPLEMENTATION_PROMPT.format( current_date=current_date, @@ -1001,8 +1103,9 @@ def run_task_implementation_agent( if config.get("web_research_enabled") else "" ), - env_inv=get_env_inv(), + env_inv=env_inv, project_info=formatted_project_info, + implementation_guidance_section=implementation_guidance_section, ) config = get_config_repository().get_all() if not config else config diff --git a/ra_aid/prompts/implementation_prompts.py b/ra_aid/prompts/implementation_prompts.py index af739ed..333afb2 100644 --- a/ra_aid/prompts/implementation_prompts.py +++ b/ra_aid/prompts/implementation_prompts.py @@ -45,6 +45,8 @@ YOU MUST **EXPLICITLY** INCLUDE ANY PATHS FROM THE ABOVE INFO IF NEEDED. IT IS N READ AND STUDY ACTUAL LIBRARY HEADERS/CODE FROM THE ENVIRONMENT, IF AVAILABLE AND RELEVANT. +{implementation_guidance_section} + Important Notes: - Focus solely on the given task and implement it as described. - Scale the complexity of your solution to the complexity of the request. For simple requests, keep it straightforward and minimal. For complex requests, maintain the previously planned depth. diff --git a/ra_aid/prompts/reasoning_assist_prompt.py b/ra_aid/prompts/reasoning_assist_prompt.py index 1611962..20d7102 100644 --- a/ra_aid/prompts/reasoning_assist_prompt.py +++ b/ra_aid/prompts/reasoning_assist_prompt.py @@ -1,4 +1,4 @@ -"""Reasoning assist prompts for planning stage.""" +"""Reasoning assist prompts for planning and implementation stages.""" REASONING_ASSIST_PROMPT_PLANNING = """Current Date: {current_date} Working Directory: {working_directory} @@ -51,6 +51,58 @@ It should be the most beautiful, elegant, simple logic ever. YOUR OUTPUT MUST BE MARKDOWN. - WE ARE IN THE **PLANNING** STAGE RIGHT NOW. NO CODE SHOULD BE WRITTEN. WE SHOULD BE THINKING LOGICALLY ABOUT HOW TO APPROACH THE PROBLEM, PLANNING OUT WHICH TASKS TO REQUEST IMPLEMENTATION OF, ETC. + +DO NOT OVERTHINK OR OVERCOMPLICATE THE ANSWER. YOU ARE AN EXPERT AND CAN RESPOND ASSERTIVELY AND CONFIDENTLY. +""" + +REASONING_ASSIST_PROMPT_IMPLEMENTATION = """Current Date: {current_date} +Working Directory: {working_directory} + +I am an agent about to implement the following task. I need your assistance in thinking through the implementation details in a structured, logical way before I start writing code. The task is: + + +{task} + + + +{key_facts} + + + +{key_snippets} + + + +{research_notes} + + + +{related_files} + + + +{env_inv} + + + +{tool_metadata} + + +Please provide detailed implementation guidance including: +1. Code structure and patterns to follow +2. Potential edge cases to handle +3. Testing strategies to validate the implementation +4. Key files to modify and how +5. Dependencies and their interactions +6. Error handling considerations +7. Performance considerations + +Please be concise, practical, and specific to this task. Avoid generic advice. + +Your output should include pseudo-code where appropriate and clear step-by-step implementation instructions. Remember I am an agent and will use this logic to guide my implementation actions. + +You are guiding an agent. Suggest how and when to use the tools. Write a couple paragraphs about it in markdown and you're done. + +DO NOT OVERTHINK OR OVERCOMPLICATE THE ANSWER. YOU ARE AN EXPERT AND CAN RESPOND ASSERTIVELY AND CONFIDENTLY. """