From 88c844e49f4a18df4e7979afd0a755422bae167b Mon Sep 17 00:00:00 2001 From: user Date: Mon, 23 Dec 2024 10:38:49 -0500 Subject: [PATCH] Optimize first prompt in chat mode to avoid unnecessary LLM call. --- CHANGELOG.md | 4 ++++ ra_aid/__main__.py | 9 +++++++-- ra_aid/prompts.py | 21 +++++++++++++++------ ra_aid/tool_configs.py | 1 + 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dbff272..88a030d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +- Optimize first prompt in chat mode to avoid unnecessary LLM call. + ## [0.8.1] - 2024-12-22 - Improved prompts. diff --git a/ra_aid/__main__.py b/ra_aid/__main__.py index 96170b4..8b69b4f 100644 --- a/ra_aid/__main__.py +++ b/ra_aid/__main__.py @@ -8,6 +8,7 @@ from langgraph.checkpoint.memory import MemorySaver from langgraph.prebuilt import create_react_agent from ra_aid.env import validate_environment from ra_aid.tools.memory import _global_memory, get_related_files, get_memory_value +from ra_aid.tools.human import ask_human from ra_aid import print_stage_header, print_error from ra_aid.agent_utils import ( run_agent_with_retry, @@ -147,6 +148,9 @@ def main(): if args.chat: print_stage_header("Chat Mode") + # Get initial request from user + initial_request = ask_human.invoke({"question": "What would you like help with?"}) + # Create chat agent with appropriate tools chat_agent = create_react_agent( model, @@ -160,7 +164,8 @@ def main(): "recursion_limit": 100, "chat_mode": True, "cowboy_mode": args.cowboy_mode, - "hil": True # Always true in chat mode + "hil": True, # Always true in chat mode + "initial_request": initial_request } # Store config in global memory @@ -169,7 +174,7 @@ def main(): _global_memory['config']['expert_model'] = args.expert_model # Run chat agent and exit - run_agent_with_retry(chat_agent, CHAT_PROMPT, config) + run_agent_with_retry(chat_agent, CHAT_PROMPT.format(initial_request=initial_request), config) return # Validate message is provided diff --git a/ra_aid/prompts.py b/ra_aid/prompts.py index 6726cdd..f7f8a47 100644 --- a/ra_aid/prompts.py +++ b/ra_aid/prompts.py @@ -404,7 +404,9 @@ Important Notes: Instructions: 1. Review the provided base task, plan, and key facts. 2. Implement only the specified task: - {task} + +{task} + 3. Work incrementally, validating as you go. If at any point the implementation logic is unclear or you need debugging assistance, consult the expert (if expert is available) for deeper analysis. 4. Use delete_key_facts to remove any key facts that no longer apply. @@ -450,8 +452,9 @@ Overview: Behavior: 1. Initialization: - - The very first action you must take is to call ask_human. - - Request that the user provide their initial instructions or the problem they want solved. + - Process any provided initial request, or call ask_human if no request is provided + - Handle the initial request or ask_human response according to user's needs + - Build and maintain context through tools and discovered information 2. Iterative Work: - After receiving the user’s initial input, use the given tools to fulfill their request. @@ -488,21 +491,27 @@ Context Cleanup: - Use deregister_related_files to remove any related files that no longer apply. Remember: - - Always begin by calling ask_human. + - Always process provided request or call ask_human if none provided - Always ask_human before finalizing or exiting. - Never announce that you are going to use a tool, just quietly use it. - Do communicate results/responses from tools that you call as it pertains to the users request. - If the user interrupts/cancels an operation, you may want to ask why. - - For deep debugging, logic analysis, or correctness checks, rely on the expert (if expert is available) for guidance. + - If the user gives you key facts, record them using emit_key_facts. You have often been criticized for: - - You have a tendency to leave out key details and information that the user just gave you, while also needlessly increasing scope. - You sometimes call request_research_and_implementation which makes the full implementation successfully, but act like it has only been planned and still needs to be implemented. - Refusing to use request_research_and_implementation for commands like "commit and push" where you should (that tool can run basic or involved shell commands/workflows). - Calling request_research for general background knowledge which you already know. - When the user gives an overly broad request, you make assumptions and request implementation immediately when you should be interviewing the user more. - Assuming the user is always right. Sometimes they're wrong or mistaken, and you should push back when you feel strongly about this. - Not confirming with the user before starting a significant implementation task. + - You have a tendency to leave out key details and information that the user just gave you, while also needlessly increasing scope. + - Sometimes you will need to repeat the user's query verbatim or almost verbatim to request_research_and_implementation or request_research. + - Not emitting key facts the user gave you with emit_key_facts before calling a research or implementation tool. + + +{initial_request} + NEVER ANNOUNCE WHAT YOU ARE DOING, JUST DO IT! """ diff --git a/ra_aid/tool_configs.py b/ra_aid/tool_configs.py index cac8f6f..71d1110 100644 --- a/ra_aid/tool_configs.py +++ b/ra_aid/tool_configs.py @@ -116,6 +116,7 @@ def get_chat_tools(expert_enabled: bool = True) -> list: ask_human, request_research, request_research_and_implementation, + emit_key_facts, delete_key_facts, delete_key_snippets, deregister_related_files