Просмотр исходного кода

fix: Multiple Thought-Action-Observation in One Loop in Chapter 1.3.4

lyxx2535 7 месяцев назад
Родитель
Сommit
e45a41dd71

+ 9 - 2
code/chapter1/FirstAgentTest.ipynb

@@ -35,7 +35,7 @@
     "- `get_attraction(city: str, weather: str)`: 根据城市和天气搜索推荐的旅游景点。\n",
     "\n",
     "# 行动格式:\n",
-    "你的回答必须严格遵循以下格式。首先是你的思考过程,然后是你要执行的具体行动\n",
+    "你的回答必须严格遵循以下格式。首先是你的思考过程,然后是你要执行的具体行动,每次回复只输出一对Thought-Action:\n",
     "Thought: [这里是你的思考过程和下一步计划]\n",
     "Action: [这里是你要调用的工具,格式为 function_name(arg_name=\"arg_value\")]\n",
     "\n",
@@ -269,7 +269,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 5,
+   "execution_count": null,
    "id": "cc543309-fe16-44a9-9735-bce828b9c7ad",
    "metadata": {},
    "outputs": [],
@@ -300,6 +300,13 @@
     "        # 构建完整prompt并调用LLM\n",
     "        full_prompt = \"\\n\".join(assistant.prompt_history)\n",
     "        llm_output = assistant.llm.generate(full_prompt, AGENT_SYSTEM_PROMPT)\n",
+    "        # 模型可能会输出多余的Thought-Action,需要截断\n",
+    "        match = re.search(r'(Thought:.*?Action:.*?)(?=\\n\\s*(?:Thought:|Action:|Observation:)|\\Z)', llm_output, re.DOTALL)\n",
+    "        if match:\n",
+    "            truncated = match.group(1).strip()\n",
+    "            if truncated != llm_output.strip():\n",
+    "                llm_output = truncated\n",
+    "                print(\"⚠️ 已截断多余的 Thought-Action 对\")\n",
     "        \n",
     "        assistant.add_assistant_message(llm_output)\n",
     "        \n",

+ 8 - 1
code/chapter1/FirstAgentTest.py

@@ -6,7 +6,7 @@ AGENT_SYSTEM_PROMPT = """
 - `get_attraction(city: str, weather: str)`: 根据城市和天气搜索推荐的旅游景点。
 
 # 行动格式:
-你的回答必须严格遵循以下格式。首先是你的思考过程,然后是你要执行的具体行动
+你的回答必须严格遵循以下格式。首先是你的思考过程,然后是你要执行的具体行动,每次回复只输出一对Thought-Action:
 Thought: [这里是你的思考过程和下一步计划]
 Action: [这里是你要调用的工具,格式为 function_name(arg_name="arg_value")]
 
@@ -162,6 +162,13 @@ for i in range(5): # 设置最大循环次数
     
     # 3.2. 调用LLM进行思考
     llm_output = llm.generate(full_prompt, system_prompt=AGENT_SYSTEM_PROMPT)
+    # 模型可能会输出多余的Thought-Action,需要截断
+    match = re.search(r'(Thought:.*?Action:.*?)(?=\n\s*(?:Thought:|Action:|Observation:)|\Z)', llm_output, re.DOTALL)
+    if match:
+        truncated = match.group(1).strip()
+        if truncated != llm_output.strip():
+            llm_output = truncated
+            print("已截断多余的 Thought-Action 对")
     print(f"模型输出:\n{llm_output}\n")
     prompt_history.append(llm_output)
     

+ 9 - 1
docs/chapter1/Chapter1-Introduction-to-Agents.md

@@ -238,7 +238,7 @@ You are an intelligent travel assistant. Your task is to analyze user requests a
 - `get_attraction(city: str, weather: str)`: Search for recommended tourist attractions based on city and weather.
 
 # Action Format:
-Your response must strictly follow the following format. First is your thinking process, then the specific action you want to execute.
+Your response must strictly follow the following format. First is your thinking process, then the specific action you want to execute. Each response should output only one Thought-Action pair:
 Thought: [Here is your thinking process and next step plan]
 Action: [Here is the tool you want to call, in the format function_name(arg_name="arg_value")]
 
@@ -418,6 +418,14 @@ for i in range(5): # Set maximum number of loops
 
     # 3.2. Call LLM for thinking
     llm_output = llm.generate(full_prompt, system_prompt=AGENT_SYSTEM_PROMPT)
+    # Truncate extra Thought-Action pairs that the model may generate
+    match = re.search(r'(Thought:.*?Action:.*?)(?=\n\s*(?:Thought:|Action:|Observation:)|\Z)', 
+                    llm_output, re.DOTALL)
+    if match:
+        truncated = match.group(1).strip()
+        if truncated != llm_output.strip():
+            llm_output = truncated
+            print("Truncated extra Thought-Action pairs")
     print(f"Model output:\n{llm_output}\n")
     prompt_history.append(llm_output)
 

+ 8 - 1
docs/chapter1/第一章 初识智能体.md

@@ -242,7 +242,7 @@ AGENT_SYSTEM_PROMPT = """
 - `get_attraction(city: str, weather: str)`: 根据城市和天气搜索推荐的旅游景点。
 
 # 行动格式:
-你的回答必须严格遵循以下格式。首先是你的思考过程,然后是你要执行的具体行动
+你的回答必须严格遵循以下格式。首先是你的思考过程,然后是你要执行的具体行动,每次回复只输出一对Thought-Action:
 Thought: [这里是你的思考过程和下一步计划]
 Action: [这里是你要调用的工具,格式为 function_name(arg_name="arg_value")]
 
@@ -424,6 +424,13 @@ for i in range(5): # 设置最大循环次数
     
     # 3.2. 调用LLM进行思考
     llm_output = llm.generate(full_prompt, system_prompt=AGENT_SYSTEM_PROMPT)
+    # 模型可能会输出多余的Thought-Action,需要截断
+    match = re.search(r'(Thought:.*?Action:.*?)(?=\n\s*(?:Thought:|Action:|Observation:)|\Z)', llm_output, re.DOTALL)
+    if match:
+        truncated = match.group(1).strip()
+        if truncated != llm_output.strip():
+            llm_output = truncated
+            print("已截断多余的 Thought-Action 对")
     print(f"模型输出:\n{llm_output}\n")
     prompt_history.append(llm_output)