From b8f05f582762264361cea2cedc21a92bdf03ba62 Mon Sep 17 00:00:00 2001 From: Yizhou Chi Date: Tue, 13 Aug 2024 16:17:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86quick=20think=20prompt=E6=94=B9?= =?UTF-8?q?=E4=B8=BAsystem=20prompt=EF=BC=8C=E5=87=8F=E5=B0=91request?= =?UTF-8?q?=E7=A6=BB=E7=BB=93=E5=B0=BE=E7=9A=84=E8=B7=9D=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- metagpt/prompts/di/role_zero.py | 47 ++++++++++++++++++++------------ metagpt/roles/di/data_analyst.py | 2 +- metagpt/roles/di/role_zero.py | 8 +++++- metagpt/roles/di/team_leader.py | 10 ++++--- 4 files changed, 43 insertions(+), 24 deletions(-) diff --git a/metagpt/prompts/di/role_zero.py b/metagpt/prompts/di/role_zero.py index 895424e07..1a340cdef 100644 --- a/metagpt/prompts/di/role_zero.py +++ b/metagpt/prompts/di/role_zero.py @@ -125,32 +125,45 @@ If no issues are detected, the original json data should be returned unchanged. Output the JSON data in a format that can be loaded by the json.loads() function. """ -QUICK_THINK_PROMPT = """ +QUICK_THINK_SYSTEM_PROMPT = """ +Your role is to determine the appropriate response category for the given request. + # Response Categories ## QUICK: -For straightforward questions or requests that can be answered directly. This includes common-sense inquiries, legal or logical questions, basic math, short coding tasks, multiple-choice questions, greetings, casual chat, and inquiries about you or your team. +For straightforward questions or requests that can be answered directly. This includes common-sense inquiries, legal or logical questions, basic math, short coding tasks, multiple-choice questions, greetings, casual chat, schedule planning, and inquiries about you or your team. ## SEARCH For queries that require retrieving up-to-date or detailed information. This includes time-sensitive or location-specific questions like current events or weather. Use this only if the information isn't readily available. ## TASK -For complex requests that involve multiple steps or detailed instructions. Examples include software development, project planning, or any task that requires a sequence of actions. +For complex requests that involve tool utilizations, multiple steps or detailed instructions. Examples include software development, project planning, or any task that requires tool usage. ## AMBIGUOUS For requests that are unclear, lack sufficient detail, or are outside the system's capabilities. Common characteristics of AMBIGUOUS requests: -- Incomplete Information: Requests that imply complex tasks but lack critical details (e.g., "Redesign this logo" without providing the original logo or specifying design requirements). +- Incomplete Information: Requests that imply complex tasks but lack critical details (e.g., "Redesign this logo" without specifying design requirements). - Vagueness: Broad, unspecified, or unclear requests that make it difficult to provide a precise answer. -- Out of Expertise: Requests for specialized advice (e.g., medical or legal advice) or highly technical tasks beyond the model's scope. - Unrealistic Scope: Overly broad requests that are impossible to address meaningfully in a single response (e.g., "Tell me everything about..."). +- Missing files: Requests that refer to specific documents, images, or data without providing them for reference. (when providing a file, website, or data, either the content, link, or path **must** be included) -**Note:** Before categorizing a request as TASK, consider whether the user has provided sufficient information to proceed with the task. If the request is complex but lacks essential details or the mentioned files, it should fall under AMBIGUOUS. +**Note:** Before categorizing a request as TASK: +1. Consider whether the user has provided sufficient information to proceed with the task. If the request is complex but lacks essential details or the mentioned files' content or path, it should fall under AMBIGUOUS. +2. If the request is a "how-to" question that asks for a general approach or strategy, it should be categorized as QUICK. +3. Writing a travel/learning plan or providing a general outline should be categorized as **QUICK* because it doesn't involve detailed instructions or tool usage. {examples} +""" +QUICK_THINK_PROMPT = """ +# Instruction +Determine the previous message's intent. Respond with a concise thought, then provide the appropriate response category: QUICK, SEARCH, TASK, or AMBIGUOUS. -Important: You should **only** provide the thought, response category, and nothing else. -Your response: + +# Format +Thought: [Your thought here] +Response Category: [QUICK/SEARCH/TASK/AMBIGUOUS] + +# Response: """ @@ -166,7 +179,7 @@ Thought: This is a general knowledge question that can be answered concisely. Response Category: QUICK. 3. Request: "Can you help me plan a healthy diet for a week?" -Thought: The user is requesting a simple plan that can be provided immediately. +Thought: Writing a diet plan is a general task that can be answered directly. Response Category: QUICK. 4. Request: "Can you help me find the latest research papers on deep learning?" @@ -178,18 +191,16 @@ Thought: This is a detailed software development task that requires multiple ste Response Category: TASK. 6. Request: "Summarize this document for me." -Thought: The request mentions summarizing a document but doesn't provide the document itself, making it impossible to fulfill. +Thought: The request mentions summarizing a document but doesn't provide the path or content of the document, making it impossible to fulfill. Response Category: AMBIGUOUS. -7. Request: "Optimize this process." +7. Request: "Summarize this document for me '/data/path/docmument.pdf'." +Thought: The request mentions summarizing a document and has provided the path to the document. It can be done by reading the document using a tool then summarizing it. +Response Category: TASK. + +8. Request: "Optimize this process." Thought: The request is vague and lacks specifics, requiring clarification on the process to optimize. Response Category: AMBIGUOUS. -8. Request: "Create a poster for our upcoming event." -Thought: Critical details like event theme, date, and location are missing, making it impossible to complete the task. -Response Category: AMBIGUOUS. - -# Instruction """ - -QUICK_THINK_PROMPT = QUICK_THINK_PROMPT.format(examples=QUICK_THINK_EXAMPLES) \ No newline at end of file +# QUICK_THINK_EXAMPLES = "" \ No newline at end of file diff --git a/metagpt/roles/di/data_analyst.py b/metagpt/roles/di/data_analyst.py index f65042217..78f0b3040 100644 --- a/metagpt/roles/di/data_analyst.py +++ b/metagpt/roles/di/data_analyst.py @@ -30,7 +30,7 @@ class DataAnalyst(RoleZero): instruction: str = ROLE_INSTRUCTION + EXTRA_INSTRUCTION task_type_desc: str = TASK_TYPE_DESC - tools: list[str] = ["Plan", "DataAnalyst", "RoleZero", "Browser"] + tools: list[str] = ["Plan", "DataAnalyst", "RoleZero", "Browser", "Editor:write,read"] custom_tools: list[str] = ["web scraping", "Terminal"] custom_tool_recommender: ToolRecommender = None experience_retriever: Annotated[ExpRetriever, Field(exclude=True)] = KeywordExpRetriever() diff --git a/metagpt/roles/di/role_zero.py b/metagpt/roles/di/role_zero.py index 8d46b9c02..7cdd9bdea 100644 --- a/metagpt/roles/di/role_zero.py +++ b/metagpt/roles/di/role_zero.py @@ -21,6 +21,8 @@ from metagpt.prompts.di.role_zero import ( CMD_PROMPT, JSON_REPAIR_PROMPT, QUICK_THINK_PROMPT, + QUICK_THINK_EXAMPLES, + QUICK_THINK_SYSTEM_PROMPT, REGENERATE_PROMPT, ROLE_INSTRUCTION, SYSTEM_PROMPT, @@ -249,6 +251,10 @@ class RoleZero(Role): rsp = await self._act() actions_taken += 1 return rsp # return output from the last action + + def format_quick_system_prompt(self) -> str: + """Format the system prompt for quick thinking.""" + return QUICK_THINK_SYSTEM_PROMPT.format(examples=QUICK_THINK_EXAMPLES) async def _quick_think(self) -> Tuple[Message, str]: answer = "" @@ -260,7 +266,7 @@ class RoleZero(Role): # routing memory = self.get_memories(k=4) # FIXME: A magic number for two rounds of Q&A context = self.llm.format_msg(memory + [UserMessage(content=QUICK_THINK_PROMPT)]) - intent_result = await self.llm.aask(context) + intent_result = await self.llm.aask(context, system_msgs=self.format_quick_system_prompt()) if "QUICK" in intent_result or "AMBIGUOUS " in intent_result: # llm call with the original context async with ThoughtReporter(enable_llm_stream=True) as reporter: diff --git a/metagpt/roles/di/team_leader.py b/metagpt/roles/di/team_leader.py index 353e00620..f934c09f9 100644 --- a/metagpt/roles/di/team_leader.py +++ b/metagpt/roles/di/team_leader.py @@ -26,7 +26,7 @@ class TeamLeader(RoleZero): # TeamLeader only reacts once each time, but may encounter errors or need to ask human, thus allowing 2 more turns max_react_loop: int = 3 - tools: list[str] = ["Plan", "RoleZero", "TeamLeader"] + tools: list[str] = ["Plan", "RoleZero", "TeamLeader", "Editor:write,read"] experience_retriever: Annotated[ExpRetriever, Field(exclude=True)] = SimpleExpRetriever() @@ -48,12 +48,14 @@ class TeamLeader(RoleZero): team_info += f"{role.name}: {role.profile}, {role.goal}\n" return team_info - async def _quick_think(self) -> Message: - # insert team info for quick question - self.llm.system_prompt = QUICK_THINK_SYSTEM_PROMPT.format( + def format_quick_system_prompt(self) -> str: + qt_system_prompt = super().format_quick_system_prompt() + return qt_system_prompt + QUICK_THINK_SYSTEM_PROMPT.format( role_info=super()._get_prefix(), team_info=self._get_team_info(), ) + + async def _quick_think(self) -> Message: return await super()._quick_think() async def _think(self) -> bool: