diff --git a/examples/langchain_tooluse.ipynb b/examples/langchain_tooluse.ipynb index de6f8e2..0efc608 100644 --- a/examples/langchain_tooluse.ipynb +++ b/examples/langchain_tooluse.ipynb @@ -9,7 +9,7 @@ "source": [ "# 如何让 Qwen-7b 使用 Langchain 中的 工具\n", "\n", - "本文档主要介绍如何让千问调用 [LangChain](https://python.langchain.com/docs/get_started/introduction.html) 框架中实现好的谷歌搜索、WolframAlpha 等工具。将主要基于 [ReAct Prompting](https://github.com/QwenLM/Qwen-7B/blob/main/examples/react_prompt.md) 技术,一种特殊的链式思考(Chain-of-Thought,简称 CoT)提示技巧,来实现这一目的。" + "本文档主要介绍如何让千问调用 [LangChain](https://python.langchain.com/docs/get_started/introduction.html) 框架中实现好的谷歌搜索、 WolframAlpha 等工具。将主要基于 [ReAct Prompting](https://github.com/QwenLM/Qwen-7B/blob/main/examples/react_prompt.md) 技术,一种特殊的链式思考(Chain-of-Thought,简称 CoT)提示技巧,来实现这一目的。" ] }, { @@ -56,6 +56,7 @@ " - [谷歌搜索API](https://serper.dev/?gclid=EAIaIQobChMIj9eqof7OgAMV44VbCh1F3QZoEAAYASABEgIh3fD_BwE#google-search-api)\n", " - [WolframAlpha](https://products.wolframalpha.com/api/)\n", " - arxiv论文搜索\n", + " - python shell (需升级python至3.9以上使用)\n", "\n", "注1:此处推荐模仿此案例,细致地构造给千问看的工具描述。\n", "\n", @@ -64,7 +65,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "07e49b98-9d6c-41f2-9b18-f043f2d13e1a", "metadata": {}, "outputs": [], @@ -72,6 +73,7 @@ "from langchain import SerpAPIWrapper\n", "from langchain.utilities.wolfram_alpha import WolframAlphaAPIWrapper\n", "from langchain.utilities import ArxivAPIWrapper\n", + "from langchain.tools.python.tool import PythonAstREPLTool\n", "\n", "from typing import Dict, Tuple\n", "import os\n", @@ -87,6 +89,7 @@ "search = SerpAPIWrapper()\n", "WolframAlpha = WolframAlphaAPIWrapper()\n", "arxiv = ArxivAPIWrapper()\n", + "python=PythonAstREPLTool()\n", "\n", "def tool_wrapper_for_qwen(tool):\n", " def tool_(query):\n", @@ -140,7 +143,24 @@ " 'required': True\n", " }], \n", " 'tool_api': tool_wrapper_for_qwen(arxiv)\n", - " } \n", + " },\n", + " {\n", + " 'name_for_human':\n", + " 'python',\n", + " 'name_for_model':\n", + " 'python',\n", + " 'description_for_model':\n", + " \"A Python shell. Use this to execute python commands. When using this tool, sometimes output is abbreviated - Make sure it does not look abbreviated before using it in your answer. \"\n", + " \"Don't add comments to your python code.\",\n", + " 'parameters': [{\n", + " \"name\": \"query\",\n", + " \"type\": \"string\",\n", + " \"description\": \"a valid python command.\",\n", + " 'required': True\n", + " }],\n", + " 'tool_api': tool_wrapper_for_qwen(python)\n", + " }\n", + "\n", "]\n" ] }, @@ -162,7 +182,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "id": "4a8feb0e-22f7-4184-9ea0-b864812c9b09", "metadata": { "scrolled": true @@ -249,10 +269,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "f71b2577-118c-4ce2-a0ed-a45ec59ea35b", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "The model is automatically converting to bf16 for faster inference. If you want to disable the automatic precision, please manually add bf16/fp16/fp32=True to \"AutoModelForCausalLM.from_pretrained\".\n", + "Try importing flash-attention for faster inference...\n", + "Warning: import flash_attn rotary fail, please install FlashAttention rotary to get higher efficiency https://github.com/Dao-AILab/flash-attention/tree/main/csrc/rotary\n", + "Warning: import flash_attn rms_norm fail, please install FlashAttention layer_norm to get higher efficiency https://github.com/Dao-AILab/flash-attention/tree/main/csrc/layer_norm\n", + "Warning: import flash_attn fail, please install FlashAttention to get higher efficiency https://github.com/Dao-AILab/flash-attention\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "ed76951f2662402c9932f0b27548a47b", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Loading checkpoint shards: 0%| | 0/8 [00:00