瀏覽代碼

fix problem in chapter1 code

jjyaoao 5 月之前
父節點
當前提交
16d1087802

+ 102 - 96
code/chapter1/FirstAgentTest.ipynb

@@ -2,6 +2,7 @@
  "cells": [
   {
    "cell_type": "code",
+   "execution_count": null,
    "id": "938b2e36-f95c-4b0f-8770-335c6bb5bc0e",
    "metadata": {
     "ExecuteTime": {
@@ -9,6 +10,7 @@
      "start_time": "2026-01-14T15:56:53.608893241Z"
     }
    },
+   "outputs": [],
    "source": [
     "import requests\n",
     "import os\n",
@@ -36,23 +38,28 @@
     "- `get_weather(city: str)`: 查询指定城市的实时天气。\n",
     "- `get_attraction(city: str, weather: str)`: 根据城市和天气搜索推荐的旅游景点。\n",
     "\n",
-    "# 行动格式:\n",
-    "你的回答必须严格遵循以下格式。首先是你的思考过程,然后是你要执行的具体行动,每次回复只输出一对Thought-Action:\n",
-    "Thought: [这里是你的思考过程和下一步计划]\n",
-    "Action: 你决定采取的行动,必须是以下格式之一:\n",
-    "- `function_name(arg_name=\"arg_value\")`:调用一个可用工具。\n",
-    "- `Finish[最终答案]`:当你认为已经获得最终答案时。\n",
-    "- 当你收集到足够的信息,能够回答用户的最终问题时,你必须在Action:字段后使用 Finish[最终答案] 来输出最终答案。\n",
+    "# 输出格式要求:\n",
+    "你的每次回复必须严格遵循以下格式,包含一对Thought和Action:\n",
+    "\n",
+    "Thought: [你的思考过程和下一步计划]\n",
+    "Action: [你要执行的具体行动]\n",
     "\n",
+    "Action的格式必须是以下之一:\n",
+    "1. 调用工具:function_name(arg_name=\"arg_value\")\n",
+    "2. 结束任务:Finish[最终答案]\n",
+    "\n",
+    "# 重要提示:\n",
+    "- 每次只输出一对Thought-Action\n",
+    "- Action必须在同一行,不要换行\n",
+    "- 当收集到足够信息可以回答用户问题时,必须使用 Action: Finish[最终答案] 格式结束\n",
     "\n",
     "请开始吧!\n",
-    "\"\"\""
-   ],
-   "outputs": [],
-   "execution_count": 8
+    "\"\"\"\n"
+   ]
   },
   {
    "cell_type": "code",
+   "execution_count": 9,
    "id": "66d9d404-3c95-42f2-8975-436769b3cb87",
    "metadata": {
     "ExecuteTime": {
@@ -60,6 +67,15 @@
      "start_time": "2026-01-14T15:56:54.532054736Z"
     }
    },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "✅ 工具函数定义完成!\n"
+     ]
+    }
+   ],
    "source": [
     "def get_weather(city: str) -> str:\n",
     "    \"\"\"\n",
@@ -133,20 +149,11 @@
     "    \"get_attraction\": get_attraction,\n",
     "}\n",
     "print(\"✅ 工具函数定义完成!\")"
-   ],
-   "outputs": [
-    {
-     "name": "stdout",
-     "output_type": "stream",
-     "text": [
-      "✅ 工具函数定义完成!\n"
-     ]
-    }
-   ],
-   "execution_count": 9
+   ]
   },
   {
    "cell_type": "code",
+   "execution_count": 10,
    "id": "e953fee4-9e3c-4e34-bf48-4ea002c3bb92",
    "metadata": {
     "ExecuteTime": {
@@ -154,6 +161,15 @@
      "start_time": "2026-01-14T15:56:55.671146801Z"
     }
    },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "✅ 智能助手类定义完成!\n"
+     ]
+    }
+   ],
    "source": [
     "class OpenAICompatibleClient:\n",
     "    \"\"\"\n",
@@ -211,20 +227,11 @@
     "        \"\"\"添加观察结果到历史\"\"\"\n",
     "        self.prompt_history.append(f\"Observation: {observation}\")\n",
     "print(\"✅ 智能助手类定义完成!\")"
-   ],
-   "outputs": [
-    {
-     "name": "stdout",
-     "output_type": "stream",
-     "text": [
-      "✅ 智能助手类定义完成!\n"
-     ]
-    }
-   ],
-   "execution_count": 10
+   ]
   },
   {
    "cell_type": "code",
+   "execution_count": 11,
    "id": "ab5d3142-c119-46ad-a7a1-ec1aa2e79435",
    "metadata": {
     "ExecuteTime": {
@@ -232,6 +239,15 @@
      "start_time": "2026-01-14T15:56:56.608751066Z"
     }
    },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "✅ 显示函数定义完成!\n"
+     ]
+    }
+   ],
    "source": [
     "\n",
     "def display_conversation(history):\n",
@@ -276,20 +292,11 @@
     "    \n",
     "    return tool_name, kwargs\n",
     "print(\"✅ 显示函数定义完成!\")"
-   ],
-   "outputs": [
-    {
-     "name": "stdout",
-     "output_type": "stream",
-     "text": [
-      "✅ 显示函数定义完成!\n"
-     ]
-    }
-   ],
-   "execution_count": 11
+   ]
   },
   {
    "cell_type": "code",
+   "execution_count": 12,
    "id": "cc543309-fe16-44a9-9735-bce828b9c7ad",
    "metadata": {
     "ExecuteTime": {
@@ -297,6 +304,7 @@
      "start_time": "2026-01-14T15:56:57.463244826Z"
     }
    },
+   "outputs": [],
    "source": [
     "def run_assistant(user_input, max_iterations=5, display=True):\n",
     "    \"\"\"\n",
@@ -378,12 +386,11 @@
     "        print(f\"⏰ 达到最大循环次数: {timeout_answer}\")\n",
     "    \n",
     "    return timeout_answer, assistant.prompt_history"
-   ],
-   "outputs": [],
-   "execution_count": 12
+   ]
   },
   {
    "cell_type": "code",
+   "execution_count": 13,
    "id": "3f6e44eb-ff3d-4060-b4c2-ea3e139bf307",
    "metadata": {
     "ExecuteTime": {
@@ -391,28 +398,6 @@
      "start_time": "2026-01-14T15:56:58.506429375Z"
     }
    },
-   "source": [
-    "# 测试示例\n",
-    "def test_basic_example():\n",
-    "    \"\"\"测试北京天气+景点推荐的示例\"\"\"\n",
-    "    print(\"🚀 开始测试北京天气+景点推荐示例\")\n",
-    "    user_input = \"你好,请帮我查询一下今天北京的天气,然后根据天气推荐一个合适的旅游景点。\"\n",
-    "    \n",
-    "    final_answer, history = run_assistant(user_input, display=True)\n",
-    "    \n",
-    "    print(\"\\n\" + \"=\"*60)\n",
-    "    print(\"📊 测试完成!\")\n",
-    "    print(\"=\"*60)\n",
-    "    print(f\"最终答案: {final_answer}\")\n",
-    "    \n",
-    "    # 显示完整对话历史\n",
-    "    display_conversation(history)\n",
-    "    \n",
-    "    return final_answer, history\n",
-    "\n",
-    "# 运行测试示例\n",
-    "final_answer, history = test_basic_example()"
-   ],
    "outputs": [
     {
      "name": "stdout",
@@ -481,10 +466,32 @@
      ]
     }
    ],
-   "execution_count": 13
+   "source": [
+    "# 测试示例\n",
+    "def test_basic_example():\n",
+    "    \"\"\"测试北京天气+景点推荐的示例\"\"\"\n",
+    "    print(\"🚀 开始测试北京天气+景点推荐示例\")\n",
+    "    user_input = \"你好,请帮我查询一下今天北京的天气,然后根据天气推荐一个合适的旅游景点。\"\n",
+    "    \n",
+    "    final_answer, history = run_assistant(user_input, display=True)\n",
+    "    \n",
+    "    print(\"\\n\" + \"=\"*60)\n",
+    "    print(\"📊 测试完成!\")\n",
+    "    print(\"=\"*60)\n",
+    "    print(f\"最终答案: {final_answer}\")\n",
+    "    \n",
+    "    # 显示完整对话历史\n",
+    "    display_conversation(history)\n",
+    "    \n",
+    "    return final_answer, history\n",
+    "\n",
+    "# 运行测试示例\n",
+    "final_answer, history = test_basic_example()"
+   ]
   },
   {
    "cell_type": "code",
+   "execution_count": 14,
    "id": "68c735c1-eb3e-40e7-8b70-2be941798187",
    "metadata": {
     "ExecuteTime": {
@@ -492,6 +499,7 @@
      "start_time": "2026-01-14T15:58:30.317379757Z"
     }
    },
+   "outputs": [],
    "source": [
     "def interactive_travel_assistant():\n",
     "    \"\"\"\n",
@@ -537,12 +545,11 @@
     "    print(f\"🚀 快速测试: {user_input}\")\n",
     "    final_answer, _ = run_assistant(user_input, display=True)\n",
     "    return final_answer"
-   ],
-   "outputs": [],
-   "execution_count": 14
+   ]
   },
   {
    "cell_type": "code",
+   "execution_count": 17,
    "id": "bc58c911-8502-4cf5-aa3f-a9f57a94b679",
    "metadata": {
     "ExecuteTime": {
@@ -550,28 +557,6 @@
      "start_time": "2026-01-14T16:00:39.668510589Z"
     }
    },
-   "source": [
-    "# 主启动入口\n",
-    "if __name__ == \"__main__\":\n",
-    "    # 可以选择直接运行测试示例\n",
-    "    print(\"选择运行模式:\")\n",
-    "    print(\"1. 运行测试示例 (北京)\")\n",
-    "    print(\"2. 交互模式\")\n",
-    "    print(\"3. 快速测试其他城市\")\n",
-    "    \n",
-    "    choice = input(\"请输入选择 (1/2/3): \").strip()\n",
-    "    \n",
-    "    if choice == \"1\":\n",
-    "        test_basic_example()\n",
-    "    elif choice == \"2\":\n",
-    "        interactive_travel_assistant()\n",
-    "    elif choice == \"3\":\n",
-    "        city = input(\"请输入要测试的城市: \").strip() or \"上海\"\n",
-    "        quick_test(city)\n",
-    "    else:\n",
-    "        print(\"无效选择,运行测试示例...\")\n",
-    "        test_basic_example()"
-   ],
    "outputs": [
     {
      "name": "stdout",
@@ -626,7 +611,28 @@
      ]
     }
    ],
-   "execution_count": 17
+   "source": [
+    "# 主启动入口\n",
+    "if __name__ == \"__main__\":\n",
+    "    # 可以选择直接运行测试示例\n",
+    "    print(\"选择运行模式:\")\n",
+    "    print(\"1. 运行测试示例 (北京)\")\n",
+    "    print(\"2. 交互模式\")\n",
+    "    print(\"3. 快速测试其他城市\")\n",
+    "    \n",
+    "    choice = input(\"请输入选择 (1/2/3): \").strip()\n",
+    "    \n",
+    "    if choice == \"1\":\n",
+    "        test_basic_example()\n",
+    "    elif choice == \"2\":\n",
+    "        interactive_travel_assistant()\n",
+    "    elif choice == \"3\":\n",
+    "        city = input(\"请输入要测试的城市: \").strip() or \"上海\"\n",
+    "        quick_test(city)\n",
+    "    else:\n",
+    "        print(\"无效选择,运行测试示例...\")\n",
+    "        test_basic_example()"
+   ]
   },
   {
    "cell_type": "code",

+ 14 - 7
code/chapter1/FirstAgentTest.py

@@ -5,13 +5,20 @@ AGENT_SYSTEM_PROMPT = """
 - `get_weather(city: str)`: 查询指定城市的实时天气。
 - `get_attraction(city: str, weather: str)`: 根据城市和天气搜索推荐的旅游景点。
 
-# 行动格式:
-你的回答必须严格遵循以下格式。首先是你的思考过程,然后是你要执行的具体行动,每次回复只输出一对Thought-Action:
-Thought: [这里是你的思考过程和下一步计划]
-Action: 你决定采取的行动,必须是以下格式之一:
-- `function_name(arg_name="arg_value")`:调用一个可用工具。
-- `Finish[最终答案]`:当你认为已经获得最终答案时。
-- 当你收集到足够的信息,能够回答用户的最终问题时,你必须在Action:字段后使用 Finish[最终答案] 来输出最终答案。
+# 输出格式要求:
+你的每次回复必须严格遵循以下格式,包含一对Thought和Action:
+
+Thought: [你的思考过程和下一步计划]
+Action: [你要执行的具体行动]
+
+Action的格式必须是以下之一:
+1. 调用工具:function_name(arg_name="arg_value")
+2. 结束任务:Finish[最终答案]
+
+# 重要提示:
+- 每次只输出一对Thought-Action
+- Action必须在同一行,不要换行
+- 当收集到足够信息可以回答用户问题时,必须使用 Action: Finish[最终答案] 格式结束
 
 请开始吧!
 """

+ 14 - 7
docs/chapter1/Chapter1-Introduction-to-Agents.md

@@ -237,13 +237,20 @@ You are an intelligent travel assistant. Your task is to analyze user requests a
 - `get_weather(city: str)`: Query real-time weather for a specified city.
 - `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. Each response should output only one Thought-Action pair:
-Thought: [Here is your thinking process and next step plan]
-Action: The action you decide to take, which must be in one of the following formats:
-- function_name(arg_name="arg_value"): Call an available tool.
-- Finish[final answer]: When you believe you have obtained the final answer.
-- When you have collected enough information to answer the user's final question, you must use `Finish[final answer]` after the Action: field to output the final answer.
+# Output Format Requirements:
+Each response must strictly follow this format, containing one Thought-Action pair:
+
+Thought: [Your thinking process and next step plan]
+Action: [The specific action you want to execute]
+
+Action format must be one of the following:
+1. Call a tool: function_name(arg_name="arg_value")
+2. Finish task: Finish[final answer]
+
+# Important Notes:
+- Output only one Thought-Action pair each time
+- Action must be on the same line, do not break lines
+- When you have collected enough information to answer the user's question, you must use Action: Finish[final answer] format to end
 
 Let's begin!
 """

+ 14 - 7
docs/chapter1/第一章 初识智能体.md

@@ -241,13 +241,20 @@ AGENT_SYSTEM_PROMPT = """
 - `get_weather(city: str)`: 查询指定城市的实时天气。
 - `get_attraction(city: str, weather: str)`: 根据城市和天气搜索推荐的旅游景点。
 
-# 行动格式:
-你的回答必须严格遵循以下格式。首先是你的思考过程,然后是你要执行的具体行动,每次回复只输出一对Thought-Action:
-Thought: [这里是你的思考过程和下一步计划]
-Action: 你决定采取的行动,必须是以下格式之一:
-- `function_name(arg_name="arg_value")`:调用一个可用工具。
-- `Finish[最终答案]`:当你认为已经获得最终答案时。
-- 当你收集到足够的信息,能够回答用户的最终问题时,你必须在Action:字段后使用 Finish[最终答案] 来输出最终答案。
+# 输出格式要求:
+你的每次回复必须严格遵循以下格式,包含一对Thought和Action:
+
+Thought: [你的思考过程和下一步计划]
+Action: [你要执行的具体行动]
+
+Action的格式必须是以下之一:
+1. 调用工具:function_name(arg_name="arg_value")
+2. 结束任务:Finish[最终答案]
+
+# 重要提示:
+- 每次只输出一对Thought-Action
+- Action必须在同一行,不要换行
+- 当收集到足够信息可以回答用户问题时,必须使用 Action: Finish[最终答案] 格式结束
 
 请开始吧!
 """