Spaces:
Runtime error
Runtime error
Merge branch 'main' of https://huggingface.co/spaces/zhengogo/LangChainGo
Browse files- agent_chat_memory.ipynb +111 -0
- agent_csv.ipynb +185 -0
- agent_custom.ipynb +150 -0
- agent_custom_lizhen.ipynb +285 -0
- agent_multi_action.ipynb +209 -0
- agent_self_ask.ipynb +81 -0
agent_chat_memory.ipynb
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": null,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"from langchain.agents import Tool\n",
|
| 10 |
+
"from langchain.memory import ConversationBufferMemory\n",
|
| 11 |
+
"from langchain.chat_models import ChatOpenAI\n",
|
| 12 |
+
"from langchain.utilities import SerpAPIWrapper\n",
|
| 13 |
+
"from langchain.agents import initialize_agent\n",
|
| 14 |
+
"from langchain.agents import AgentType"
|
| 15 |
+
]
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
"cell_type": "code",
|
| 19 |
+
"execution_count": null,
|
| 20 |
+
"metadata": {},
|
| 21 |
+
"outputs": [],
|
| 22 |
+
"source": [
|
| 23 |
+
"search = SerpAPIWrapper()\n",
|
| 24 |
+
"tools = [\n",
|
| 25 |
+
" Tool(\n",
|
| 26 |
+
" name = \"Current Search\",\n",
|
| 27 |
+
" func=search.run,\n",
|
| 28 |
+
" description=\"useful for when you need to answer questions about current events or the current state of the world. the input to this should be a single search term.\"\n",
|
| 29 |
+
" ),\n",
|
| 30 |
+
"]"
|
| 31 |
+
]
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
"cell_type": "code",
|
| 35 |
+
"execution_count": null,
|
| 36 |
+
"metadata": {},
|
| 37 |
+
"outputs": [],
|
| 38 |
+
"source": [
|
| 39 |
+
"memory = ConversationBufferMemory(memory_key=\"chat_history\", return_messages=True)"
|
| 40 |
+
]
|
| 41 |
+
},
|
| 42 |
+
{
|
| 43 |
+
"cell_type": "code",
|
| 44 |
+
"execution_count": null,
|
| 45 |
+
"metadata": {},
|
| 46 |
+
"outputs": [],
|
| 47 |
+
"source": [
|
| 48 |
+
"llm=ChatOpenAI(temperature=0)\n",
|
| 49 |
+
"agent_chain = initialize_agent(tools, llm, agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=memory)"
|
| 50 |
+
]
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
"cell_type": "code",
|
| 54 |
+
"execution_count": null,
|
| 55 |
+
"metadata": {},
|
| 56 |
+
"outputs": [],
|
| 57 |
+
"source": [
|
| 58 |
+
"agent_chain.run(input=\"你好,我叫李振\")"
|
| 59 |
+
]
|
| 60 |
+
},
|
| 61 |
+
{
|
| 62 |
+
"cell_type": "code",
|
| 63 |
+
"execution_count": null,
|
| 64 |
+
"metadata": {},
|
| 65 |
+
"outputs": [],
|
| 66 |
+
"source": [
|
| 67 |
+
"agent_chain.run(input=\"今天是几号\")"
|
| 68 |
+
]
|
| 69 |
+
},
|
| 70 |
+
{
|
| 71 |
+
"cell_type": "code",
|
| 72 |
+
"execution_count": null,
|
| 73 |
+
"metadata": {},
|
| 74 |
+
"outputs": [],
|
| 75 |
+
"source": [
|
| 76 |
+
"agent_chain.run(input=\"回答对吗\")"
|
| 77 |
+
]
|
| 78 |
+
},
|
| 79 |
+
{
|
| 80 |
+
"cell_type": "code",
|
| 81 |
+
"execution_count": null,
|
| 82 |
+
"metadata": {},
|
| 83 |
+
"outputs": [],
|
| 84 |
+
"source": [
|
| 85 |
+
"agent_chain.run(input=\"我叫什么\")"
|
| 86 |
+
]
|
| 87 |
+
}
|
| 88 |
+
],
|
| 89 |
+
"metadata": {
|
| 90 |
+
"kernelspec": {
|
| 91 |
+
"display_name": "base",
|
| 92 |
+
"language": "python",
|
| 93 |
+
"name": "python3"
|
| 94 |
+
},
|
| 95 |
+
"language_info": {
|
| 96 |
+
"codemirror_mode": {
|
| 97 |
+
"name": "ipython",
|
| 98 |
+
"version": 3
|
| 99 |
+
},
|
| 100 |
+
"file_extension": ".py",
|
| 101 |
+
"mimetype": "text/x-python",
|
| 102 |
+
"name": "python",
|
| 103 |
+
"nbconvert_exporter": "python",
|
| 104 |
+
"pygments_lexer": "ipython3",
|
| 105 |
+
"version": "3.10.10"
|
| 106 |
+
},
|
| 107 |
+
"orig_nbformat": 4
|
| 108 |
+
},
|
| 109 |
+
"nbformat": 4,
|
| 110 |
+
"nbformat_minor": 2
|
| 111 |
+
}
|
agent_csv.ipynb
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 4,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"from langchain.agents import create_csv_agent"
|
| 10 |
+
]
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
"cell_type": "code",
|
| 14 |
+
"execution_count": 5,
|
| 15 |
+
"metadata": {},
|
| 16 |
+
"outputs": [],
|
| 17 |
+
"source": [
|
| 18 |
+
"from langchain.llms import OpenAI"
|
| 19 |
+
]
|
| 20 |
+
},
|
| 21 |
+
{
|
| 22 |
+
"cell_type": "code",
|
| 23 |
+
"execution_count": 6,
|
| 24 |
+
"metadata": {},
|
| 25 |
+
"outputs": [],
|
| 26 |
+
"source": [
|
| 27 |
+
"agent = create_csv_agent(OpenAI(temperature=0), '.inner/xbxb.csv', verbose=True)"
|
| 28 |
+
]
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"cell_type": "code",
|
| 32 |
+
"execution_count": 7,
|
| 33 |
+
"metadata": {},
|
| 34 |
+
"outputs": [
|
| 35 |
+
{
|
| 36 |
+
"name": "stdout",
|
| 37 |
+
"output_type": "stream",
|
| 38 |
+
"text": [
|
| 39 |
+
"\n",
|
| 40 |
+
"\n",
|
| 41 |
+
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
| 42 |
+
"\u001b[32;1m\u001b[1;3mThought: I need to count the rows\n",
|
| 43 |
+
"Action: python_repl_ast\n",
|
| 44 |
+
"Action Input: len(df)\u001b[0m\n",
|
| 45 |
+
"Observation: \u001b[36;1m\u001b[1;3m59867\u001b[0m\n",
|
| 46 |
+
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
|
| 47 |
+
"Final Answer: There are 59867 rows.\u001b[0m\n",
|
| 48 |
+
"\n",
|
| 49 |
+
"\u001b[1m> Finished chain.\u001b[0m\n"
|
| 50 |
+
]
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
"data": {
|
| 54 |
+
"text/plain": [
|
| 55 |
+
"'There are 59867 rows.'"
|
| 56 |
+
]
|
| 57 |
+
},
|
| 58 |
+
"execution_count": 7,
|
| 59 |
+
"metadata": {},
|
| 60 |
+
"output_type": "execute_result"
|
| 61 |
+
}
|
| 62 |
+
],
|
| 63 |
+
"source": [
|
| 64 |
+
"agent.run(\"how many rows are there?\")"
|
| 65 |
+
]
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"cell_type": "code",
|
| 69 |
+
"execution_count": 8,
|
| 70 |
+
"metadata": {},
|
| 71 |
+
"outputs": [
|
| 72 |
+
{
|
| 73 |
+
"name": "stdout",
|
| 74 |
+
"output_type": "stream",
|
| 75 |
+
"text": [
|
| 76 |
+
"\n",
|
| 77 |
+
"\n",
|
| 78 |
+
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
| 79 |
+
"\u001b[32;1m\u001b[1;3mThought: I need to find out what values are in the domain释义 column\n",
|
| 80 |
+
"Action: python_repl_ast\n",
|
| 81 |
+
"Action Input: df['domain释义'].unique()\u001b[0m\n",
|
| 82 |
+
"Observation: \u001b[36;1m\u001b[1;3m[nan '歌曲点播' '天气查询' '笑话' '诗词背诵' '故事点播' '音量控制' '电量查询' '屏幕亮度' '绕口令' '十万个为什么'\n",
|
| 83 |
+
" '儿童聊天' '维基百科' '音量调节' '日期查询' '四则运算' '拍照' '知识库' '意图不明确' '闹钟' '休眠']\u001b[0m\n",
|
| 84 |
+
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
|
| 85 |
+
"Final Answer: domain释义这一列有nan '歌曲点播' '天气查询' '笑话' '诗词背诵' '故事点播' '音量控制' '电量查询' '屏幕亮度' '绕口令' '十万个为什么' '儿童聊天' '维基百科' '音量调节' '日期查询' '四则运算' '拍照' '知识库' '意图不明确' '闹钟' '休眠'\u001b[0m\n",
|
| 86 |
+
"\n",
|
| 87 |
+
"\u001b[1m> Finished chain.\u001b[0m\n"
|
| 88 |
+
]
|
| 89 |
+
},
|
| 90 |
+
{
|
| 91 |
+
"data": {
|
| 92 |
+
"text/plain": [
|
| 93 |
+
"\"domain释义这一列有nan '歌曲点播' '天气查询' '笑话' '诗词背诵' '故事点播' '音量控制' '电量查询' '屏幕亮度' '绕口令' '十万个为什么' '儿童聊天' '维基百科' '音量调节' '日期查询' '四则运算' '拍照' '知识库' '意图不明确' '闹钟' '休眠'\""
|
| 94 |
+
]
|
| 95 |
+
},
|
| 96 |
+
"execution_count": 8,
|
| 97 |
+
"metadata": {},
|
| 98 |
+
"output_type": "execute_result"
|
| 99 |
+
}
|
| 100 |
+
],
|
| 101 |
+
"source": [
|
| 102 |
+
"agent.run(\"domain释义这一列,有哪些内容\")"
|
| 103 |
+
]
|
| 104 |
+
},
|
| 105 |
+
{
|
| 106 |
+
"cell_type": "code",
|
| 107 |
+
"execution_count": 9,
|
| 108 |
+
"metadata": {},
|
| 109 |
+
"outputs": [
|
| 110 |
+
{
|
| 111 |
+
"name": "stdout",
|
| 112 |
+
"output_type": "stream",
|
| 113 |
+
"text": [
|
| 114 |
+
"\n",
|
| 115 |
+
"\n",
|
| 116 |
+
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
| 117 |
+
"\u001b[32;1m\u001b[1;3mThought: I need to find out what type of domain释义 is most common\n",
|
| 118 |
+
"Action: python_repl_ast\n",
|
| 119 |
+
"Action Input: df['domain释义'].value_counts()\u001b[0m\n",
|
| 120 |
+
"Observation: \u001b[36;1m\u001b[1;3m儿童聊天 9037\n",
|
| 121 |
+
"意图不明确 4335\n",
|
| 122 |
+
"十万个为什么 3585\n",
|
| 123 |
+
"日期查询 2492\n",
|
| 124 |
+
"诗词背诵 2174\n",
|
| 125 |
+
"知识库 1127\n",
|
| 126 |
+
"天气查询 1125\n",
|
| 127 |
+
"四则运算 1072\n",
|
| 128 |
+
"维基百科 735\n",
|
| 129 |
+
"笑话 531\n",
|
| 130 |
+
"绕口令 250\n",
|
| 131 |
+
"故事点播 245\n",
|
| 132 |
+
"歌曲点播 226\n",
|
| 133 |
+
"闹钟 64\n",
|
| 134 |
+
"音量调节 58\n",
|
| 135 |
+
"屏幕亮度 41\n",
|
| 136 |
+
"电量查询 41\n",
|
| 137 |
+
"音量控制 41\n",
|
| 138 |
+
"拍照 16\n",
|
| 139 |
+
"休眠 5\n",
|
| 140 |
+
"Name: domain释义, dtype: int64\u001b[0m\n",
|
| 141 |
+
"Thought:\u001b[32;1m\u001b[1;3m 儿童聊天类型最多\n",
|
| 142 |
+
"Final Answer: 儿童聊天类型最多\u001b[0m\n",
|
| 143 |
+
"\n",
|
| 144 |
+
"\u001b[1m> Finished chain.\u001b[0m\n"
|
| 145 |
+
]
|
| 146 |
+
},
|
| 147 |
+
{
|
| 148 |
+
"data": {
|
| 149 |
+
"text/plain": [
|
| 150 |
+
"'儿童聊天类型最多'"
|
| 151 |
+
]
|
| 152 |
+
},
|
| 153 |
+
"execution_count": 9,
|
| 154 |
+
"metadata": {},
|
| 155 |
+
"output_type": "execute_result"
|
| 156 |
+
}
|
| 157 |
+
],
|
| 158 |
+
"source": [
|
| 159 |
+
"agent.run(\"domain释义这一列,哪种类型最多\")"
|
| 160 |
+
]
|
| 161 |
+
}
|
| 162 |
+
],
|
| 163 |
+
"metadata": {
|
| 164 |
+
"kernelspec": {
|
| 165 |
+
"display_name": "base",
|
| 166 |
+
"language": "python",
|
| 167 |
+
"name": "python3"
|
| 168 |
+
},
|
| 169 |
+
"language_info": {
|
| 170 |
+
"codemirror_mode": {
|
| 171 |
+
"name": "ipython",
|
| 172 |
+
"version": 3
|
| 173 |
+
},
|
| 174 |
+
"file_extension": ".py",
|
| 175 |
+
"mimetype": "text/x-python",
|
| 176 |
+
"name": "python",
|
| 177 |
+
"nbconvert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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"from langchain.agents import Tool, AgentExecutor, BaseSingleActionAgent\n",
|
| 10 |
+
"from langchain import OpenAI, SerpAPIWrapper"
|
| 11 |
+
]
|
| 12 |
+
},
|
| 13 |
+
{
|
| 14 |
+
"cell_type": "code",
|
| 15 |
+
"execution_count": 2,
|
| 16 |
+
"metadata": {},
|
| 17 |
+
"outputs": [],
|
| 18 |
+
"source": [
|
| 19 |
+
"search = SerpAPIWrapper()\n",
|
| 20 |
+
"tools = [\n",
|
| 21 |
+
" Tool(\n",
|
| 22 |
+
" name = \"Search\",\n",
|
| 23 |
+
" func=search.run,\n",
|
| 24 |
+
" description=\"useful for when you need to answer questions about current events\",\n",
|
| 25 |
+
" return_direct=True\n",
|
| 26 |
+
" )\n",
|
| 27 |
+
"]"
|
| 28 |
+
]
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"cell_type": "code",
|
| 32 |
+
"execution_count": 3,
|
| 33 |
+
"metadata": {},
|
| 34 |
+
"outputs": [],
|
| 35 |
+
"source": [
|
| 36 |
+
"from typing import List, Tuple, Any, Union\n",
|
| 37 |
+
"from langchain.schema import AgentAction, AgentFinish\n",
|
| 38 |
+
"\n",
|
| 39 |
+
"class FakeAgent(BaseSingleActionAgent):\n",
|
| 40 |
+
" \"\"\"Fake Custom Agent.\"\"\"\n",
|
| 41 |
+
" \n",
|
| 42 |
+
" @property\n",
|
| 43 |
+
" def input_keys(self):\n",
|
| 44 |
+
" return [\"input\"]\n",
|
| 45 |
+
" \n",
|
| 46 |
+
" def plan(\n",
|
| 47 |
+
" self, intermediate_steps: List[Tuple[AgentAction, str]], **kwargs: Any\n",
|
| 48 |
+
" ) -> Union[AgentAction, AgentFinish]:\n",
|
| 49 |
+
" \"\"\"Given input, decided what to do.\n",
|
| 50 |
+
"\n",
|
| 51 |
+
" Args:\n",
|
| 52 |
+
" intermediate_steps: Steps the LLM has taken to date,\n",
|
| 53 |
+
" along with observations\n",
|
| 54 |
+
" **kwargs: User inputs.\n",
|
| 55 |
+
"\n",
|
| 56 |
+
" Returns:\n",
|
| 57 |
+
" Action specifying what tool to use.\n",
|
| 58 |
+
" \"\"\"\n",
|
| 59 |
+
" return AgentAction(tool=\"Search\", tool_input=kwargs[\"input\"], log=\"\")\n",
|
| 60 |
+
"\n",
|
| 61 |
+
" async def aplan(\n",
|
| 62 |
+
" self, intermediate_steps: List[Tuple[AgentAction, str]], **kwargs: Any\n",
|
| 63 |
+
" ) -> Union[AgentAction, AgentFinish]:\n",
|
| 64 |
+
" \"\"\"Given input, decided what to do.\n",
|
| 65 |
+
"\n",
|
| 66 |
+
" Args:\n",
|
| 67 |
+
" intermediate_steps: Steps the LLM has taken to date,\n",
|
| 68 |
+
" along with observations\n",
|
| 69 |
+
" **kwargs: User inputs.\n",
|
| 70 |
+
"\n",
|
| 71 |
+
" Returns:\n",
|
| 72 |
+
" Action specifying what tool to use.\n",
|
| 73 |
+
" \"\"\"\n",
|
| 74 |
+
" return AgentAction(tool=\"Search\", tool_input=kwargs[\"input\"], log=\"\")"
|
| 75 |
+
]
|
| 76 |
+
},
|
| 77 |
+
{
|
| 78 |
+
"cell_type": "code",
|
| 79 |
+
"execution_count": 4,
|
| 80 |
+
"metadata": {},
|
| 81 |
+
"outputs": [],
|
| 82 |
+
"source": [
|
| 83 |
+
"agent = FakeAgent()"
|
| 84 |
+
]
|
| 85 |
+
},
|
| 86 |
+
{
|
| 87 |
+
"cell_type": "code",
|
| 88 |
+
"execution_count": 5,
|
| 89 |
+
"metadata": {},
|
| 90 |
+
"outputs": [],
|
| 91 |
+
"source": [
|
| 92 |
+
"agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)"
|
| 93 |
+
]
|
| 94 |
+
},
|
| 95 |
+
{
|
| 96 |
+
"cell_type": "code",
|
| 97 |
+
"execution_count": 6,
|
| 98 |
+
"metadata": {},
|
| 99 |
+
"outputs": [
|
| 100 |
+
{
|
| 101 |
+
"name": "stdout",
|
| 102 |
+
"output_type": "stream",
|
| 103 |
+
"text": [
|
| 104 |
+
"\n",
|
| 105 |
+
"\n",
|
| 106 |
+
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
| 107 |
+
"\u001b[32;1m\u001b[1;3m\u001b[0m\u001b[36;1m\u001b[1;3mThe current population of Canada is 38,680,894 as of Sunday, May 7, 2023, based on Worldometer elaboration of the latest United Nations data.\u001b[0m\n",
|
| 108 |
+
"\n",
|
| 109 |
+
"\u001b[1m> Finished chain.\u001b[0m\n"
|
| 110 |
+
]
|
| 111 |
+
},
|
| 112 |
+
{
|
| 113 |
+
"data": {
|
| 114 |
+
"text/plain": [
|
| 115 |
+
"'The current population of Canada is 38,680,894 as of Sunday, May 7, 2023, based on Worldometer elaboration of the latest United Nations data.'"
|
| 116 |
+
]
|
| 117 |
+
},
|
| 118 |
+
"execution_count": 6,
|
| 119 |
+
"metadata": {},
|
| 120 |
+
"output_type": "execute_result"
|
| 121 |
+
}
|
| 122 |
+
],
|
| 123 |
+
"source": [
|
| 124 |
+
"agent_executor.run(\"How many people live in canada as of 2023?\")"
|
| 125 |
+
]
|
| 126 |
+
}
|
| 127 |
+
],
|
| 128 |
+
"metadata": {
|
| 129 |
+
"kernelspec": {
|
| 130 |
+
"display_name": "base",
|
| 131 |
+
"language": "python",
|
| 132 |
+
"name": "python3"
|
| 133 |
+
},
|
| 134 |
+
"language_info": {
|
| 135 |
+
"codemirror_mode": {
|
| 136 |
+
"name": "ipython",
|
| 137 |
+
"version": 3
|
| 138 |
+
},
|
| 139 |
+
"file_extension": ".py",
|
| 140 |
+
"mimetype": "text/x-python",
|
| 141 |
+
"name": "python",
|
| 142 |
+
"nbconvert_exporter": "python",
|
| 143 |
+
"pygments_lexer": "ipython3",
|
| 144 |
+
"version": "3.10.10"
|
| 145 |
+
},
|
| 146 |
+
"orig_nbformat": 4
|
| 147 |
+
},
|
| 148 |
+
"nbformat": 4,
|
| 149 |
+
"nbformat_minor": 2
|
| 150 |
+
}
|
agent_custom_lizhen.ipynb
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
| 7 |
+
"outputs": [
|
| 8 |
+
{
|
| 9 |
+
"data": {
|
| 10 |
+
"text/plain": [
|
| 11 |
+
"'22222, lizhen'"
|
| 12 |
+
]
|
| 13 |
+
},
|
| 14 |
+
"execution_count": 97,
|
| 15 |
+
"metadata": {},
|
| 16 |
+
"output_type": "execute_result"
|
| 17 |
+
}
|
| 18 |
+
],
|
| 19 |
+
"source": [
|
| 20 |
+
"# 测试,自定义tool,一个输入\n",
|
| 21 |
+
"from langchain.agents import load_tools, AgentExecutor\n",
|
| 22 |
+
"from langchain.agents import AgentType\n",
|
| 23 |
+
"from pydantic import BaseModel, Field\n",
|
| 24 |
+
"from langchain.tools import Tool\n",
|
| 25 |
+
"\n",
|
| 26 |
+
"class SingleInput(BaseModel):\n",
|
| 27 |
+
" msg: str = Field()\n",
|
| 28 |
+
"\n",
|
| 29 |
+
"def singleInputAddLizhen(msg):\n",
|
| 30 |
+
" return msg+\", lizhen\"\n",
|
| 31 |
+
"\n",
|
| 32 |
+
"singleTool = Tool.from_function(\n",
|
| 33 |
+
" name = \"SingleInput\",\n",
|
| 34 |
+
" func=singleInputAddLizhen,\n",
|
| 35 |
+
" description=\"数字相加\",\n",
|
| 36 |
+
" args_schema=SingleInput,\n",
|
| 37 |
+
" return_direct=False\n",
|
| 38 |
+
")\n",
|
| 39 |
+
"\n",
|
| 40 |
+
"singleTool.run(tool_input=\"22222\")"
|
| 41 |
+
]
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"cell_type": "code",
|
| 45 |
+
"execution_count": 98,
|
| 46 |
+
"metadata": {},
|
| 47 |
+
"outputs": [
|
| 48 |
+
{
|
| 49 |
+
"data": {
|
| 50 |
+
"text/plain": [
|
| 51 |
+
"'result=3'"
|
| 52 |
+
]
|
| 53 |
+
},
|
| 54 |
+
"execution_count": 98,
|
| 55 |
+
"metadata": {},
|
| 56 |
+
"output_type": "execute_result"
|
| 57 |
+
}
|
| 58 |
+
],
|
| 59 |
+
"source": [
|
| 60 |
+
"# 测试,自定义tool,多个输入\n",
|
| 61 |
+
"from langchain.tools import StructuredTool\n",
|
| 62 |
+
"\n",
|
| 63 |
+
"class MultiInput(BaseModel):\n",
|
| 64 |
+
" a: int = Field()\n",
|
| 65 |
+
" b: int = Field()\n",
|
| 66 |
+
" \n",
|
| 67 |
+
"def multiInput(a, b) -> str:\n",
|
| 68 |
+
" return f\"result={a+b}\"\n",
|
| 69 |
+
"\n",
|
| 70 |
+
"multiTool = StructuredTool.from_function(\n",
|
| 71 |
+
" name = \"MultiInput\",\n",
|
| 72 |
+
" func=multiInput,\n",
|
| 73 |
+
" description=\"多个输入,数字相加\",\n",
|
| 74 |
+
" args_schema=MultiInput,\n",
|
| 75 |
+
" return_direct=True\n",
|
| 76 |
+
")\n",
|
| 77 |
+
"\n",
|
| 78 |
+
"multiTool.run(tool_input={\"a\":1, \"b\":2})"
|
| 79 |
+
]
|
| 80 |
+
},
|
| 81 |
+
{
|
| 82 |
+
"cell_type": "code",
|
| 83 |
+
"execution_count": 99,
|
| 84 |
+
"metadata": {},
|
| 85 |
+
"outputs": [
|
| 86 |
+
{
|
| 87 |
+
"name": "stdout",
|
| 88 |
+
"output_type": "stream",
|
| 89 |
+
"text": [
|
| 90 |
+
"\n",
|
| 91 |
+
"\n",
|
| 92 |
+
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
| 93 |
+
"\u001b[32;1m\u001b[1;3m\u001b[0m\u001b[36;1m\u001b[1;3mresult=3\u001b[0m\n",
|
| 94 |
+
"\n",
|
| 95 |
+
"\u001b[1m> Finished chain.\u001b[0m\n"
|
| 96 |
+
]
|
| 97 |
+
},
|
| 98 |
+
{
|
| 99 |
+
"data": {
|
| 100 |
+
"text/plain": [
|
| 101 |
+
"'result=3'"
|
| 102 |
+
]
|
| 103 |
+
},
|
| 104 |
+
"execution_count": 99,
|
| 105 |
+
"metadata": {},
|
| 106 |
+
"output_type": "execute_result"
|
| 107 |
+
}
|
| 108 |
+
],
|
| 109 |
+
"source": [
|
| 110 |
+
"#自定义代理\n",
|
| 111 |
+
"from langchain.agents import Tool, AgentExecutor, BaseSingleActionAgent\n",
|
| 112 |
+
"from typing import List, Tuple, Any, Union\n",
|
| 113 |
+
"from langchain.schema import AgentAction, AgentFinish\n",
|
| 114 |
+
"\n",
|
| 115 |
+
"class LizhenAgent(BaseSingleActionAgent):\n",
|
| 116 |
+
" \"\"\"自定义代理\"\"\"\n",
|
| 117 |
+
" \n",
|
| 118 |
+
" @property\n",
|
| 119 |
+
" def input_keys(self):\n",
|
| 120 |
+
" return [\"input\"]\n",
|
| 121 |
+
" \n",
|
| 122 |
+
" def plan(\n",
|
| 123 |
+
" self,\n",
|
| 124 |
+
" intermediate_steps: List[Tuple[AgentAction, str]],\n",
|
| 125 |
+
" **kwargs: Any,\n",
|
| 126 |
+
" ) -> Union[AgentAction, AgentFinish]:\n",
|
| 127 |
+
" return AgentAction(tool=\"MultiInput\", tool_input=kwargs[\"input\"], log=\"\")\n",
|
| 128 |
+
" \n",
|
| 129 |
+
" def aplan(\n",
|
| 130 |
+
" self,\n",
|
| 131 |
+
" intermediate_steps: List[Tuple[AgentAction, str]],\n",
|
| 132 |
+
" **kwargs: Any,\n",
|
| 133 |
+
" ) -> Union[AgentAction, AgentFinish]:\n",
|
| 134 |
+
" return AgentAction(tool=\"MultiInput\", tool_input=kwargs[\"input\"], log=\"\")\n",
|
| 135 |
+
" \n",
|
| 136 |
+
"agent = LizhenAgent()\n",
|
| 137 |
+
"tools = [multiTool, singleTool]\n",
|
| 138 |
+
"agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)\n",
|
| 139 |
+
"agent_executor.run(input={\"a\":1, \"b\":2})"
|
| 140 |
+
]
|
| 141 |
+
},
|
| 142 |
+
{
|
| 143 |
+
"cell_type": "code",
|
| 144 |
+
"execution_count": 104,
|
| 145 |
+
"metadata": {},
|
| 146 |
+
"outputs": [
|
| 147 |
+
{
|
| 148 |
+
"data": {
|
| 149 |
+
"text/plain": [
|
| 150 |
+
"'BaseMultiActionAgent , run tool1'"
|
| 151 |
+
]
|
| 152 |
+
},
|
| 153 |
+
"execution_count": 104,
|
| 154 |
+
"metadata": {},
|
| 155 |
+
"output_type": "execute_result"
|
| 156 |
+
}
|
| 157 |
+
],
|
| 158 |
+
"source": [
|
| 159 |
+
"from langchain.tools import tool\n",
|
| 160 |
+
"\n",
|
| 161 |
+
"@tool\n",
|
| 162 |
+
"def tool1(event) -> str:\n",
|
| 163 |
+
" \"\"\"这里必须添加描述,不然AssertionError: Function must have a docstring if description not provided.\"\"\"\n",
|
| 164 |
+
" return f\"BaseMultiActionAgent , {event}\"\n",
|
| 165 |
+
"\n",
|
| 166 |
+
"tool1.run(\"run tool1\")"
|
| 167 |
+
]
|
| 168 |
+
},
|
| 169 |
+
{
|
| 170 |
+
"cell_type": "code",
|
| 171 |
+
"execution_count": 105,
|
| 172 |
+
"metadata": {},
|
| 173 |
+
"outputs": [
|
| 174 |
+
{
|
| 175 |
+
"data": {
|
| 176 |
+
"text/plain": [
|
| 177 |
+
"'BaseMultiActionAgent , run tool2'"
|
| 178 |
+
]
|
| 179 |
+
},
|
| 180 |
+
"execution_count": 105,
|
| 181 |
+
"metadata": {},
|
| 182 |
+
"output_type": "execute_result"
|
| 183 |
+
}
|
| 184 |
+
],
|
| 185 |
+
"source": [
|
| 186 |
+
"from langchain.tools import tool\n",
|
| 187 |
+
"\n",
|
| 188 |
+
"@tool\n",
|
| 189 |
+
"def tool2(event) -> str:\n",
|
| 190 |
+
" \"\"\"这里必须添加描述,不然AssertionError: Function must have a docstring if description not provided.\"\"\"\n",
|
| 191 |
+
" return f\"BaseMultiActionAgent , {event}\"\n",
|
| 192 |
+
"\n",
|
| 193 |
+
"tool2.run(\"run tool2\")"
|
| 194 |
+
]
|
| 195 |
+
},
|
| 196 |
+
{
|
| 197 |
+
"cell_type": "code",
|
| 198 |
+
"execution_count": 106,
|
| 199 |
+
"metadata": {},
|
| 200 |
+
"outputs": [
|
| 201 |
+
{
|
| 202 |
+
"name": "stdout",
|
| 203 |
+
"output_type": "stream",
|
| 204 |
+
"text": [
|
| 205 |
+
"\n",
|
| 206 |
+
"\n",
|
| 207 |
+
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
| 208 |
+
"\u001b[32;1m\u001b[1;3m\u001b[0mMultiInput is not a valid tool, try another one.\u001b[32;1m\u001b[1;3m\u001b[0mSingleInput is not a valid tool, try another one.\u001b[32;1m\u001b[1;3m\u001b[0m\n",
|
| 209 |
+
"\n",
|
| 210 |
+
"\u001b[1m> Finished chain.\u001b[0m\n"
|
| 211 |
+
]
|
| 212 |
+
},
|
| 213 |
+
{
|
| 214 |
+
"data": {
|
| 215 |
+
"text/plain": [
|
| 216 |
+
"'bar'"
|
| 217 |
+
]
|
| 218 |
+
},
|
| 219 |
+
"execution_count": 106,
|
| 220 |
+
"metadata": {},
|
| 221 |
+
"output_type": "execute_result"
|
| 222 |
+
}
|
| 223 |
+
],
|
| 224 |
+
"source": [
|
| 225 |
+
"#自定义代理\n",
|
| 226 |
+
"from langchain.agents import Tool, AgentExecutor, BaseMultiActionAgent\n",
|
| 227 |
+
"from typing import List, Tuple, Any, Union\n",
|
| 228 |
+
"from langchain.schema import AgentAction, AgentFinish\n",
|
| 229 |
+
"\n",
|
| 230 |
+
"class LizhenMultiAgent(BaseMultiActionAgent):\n",
|
| 231 |
+
" \"\"\"自定义代理\"\"\"\n",
|
| 232 |
+
" \n",
|
| 233 |
+
" @property\n",
|
| 234 |
+
" def input_keys(self):\n",
|
| 235 |
+
" return [\"input\"]\n",
|
| 236 |
+
" \n",
|
| 237 |
+
" def plan(\n",
|
| 238 |
+
" self,\n",
|
| 239 |
+
" intermediate_steps: List[Tuple[AgentAction, str]],\n",
|
| 240 |
+
" **kwargs: Any,\n",
|
| 241 |
+
" ) -> Union[AgentAction, AgentFinish]:\n",
|
| 242 |
+
" if len(intermediate_steps) == 0:\n",
|
| 243 |
+
" return [AgentAction(tool=\"MultiInput\", tool_input=kwargs[\"input\"], log=\"\"), AgentAction(tool=\"SingleInput\", tool_input=kwargs[\"input\"], log=\"\")]\n",
|
| 244 |
+
" else:\n",
|
| 245 |
+
" return AgentFinish(return_values={\"output\": \"bar\"}, log=\"\")\n",
|
| 246 |
+
" def aplan(\n",
|
| 247 |
+
" self,\n",
|
| 248 |
+
" intermediate_steps: List[Tuple[AgentAction, str]],\n",
|
| 249 |
+
" **kwargs: Any,\n",
|
| 250 |
+
" ) -> Union[AgentAction, AgentFinish]:\n",
|
| 251 |
+
" if len(intermediate_steps) == 0:\n",
|
| 252 |
+
" return [AgentAction(tool=\"MultiInput\", tool_input=kwargs[\"input\"], log=\"\"), AgentAction(tool=\"SingleInput\", tool_input=kwargs[\"input\"], log=\"\")]\n",
|
| 253 |
+
" else:\n",
|
| 254 |
+
" return AgentFinish(return_values={\"output\": \"bar\"}, log=\"\")\n",
|
| 255 |
+
" \n",
|
| 256 |
+
"multiAgent = LizhenMultiAgent()\n",
|
| 257 |
+
"multiTools = [tool1, tool2]\n",
|
| 258 |
+
"multi_agent_executor = AgentExecutor.from_agent_and_tools(agent=multiAgent, tools=multiTools, verbose=True)\n",
|
| 259 |
+
"multi_agent_executor.run(input={\"a\":1, \"b\":2})"
|
| 260 |
+
]
|
| 261 |
+
}
|
| 262 |
+
],
|
| 263 |
+
"metadata": {
|
| 264 |
+
"kernelspec": {
|
| 265 |
+
"display_name": "base",
|
| 266 |
+
"language": "python",
|
| 267 |
+
"name": "python3"
|
| 268 |
+
},
|
| 269 |
+
"language_info": {
|
| 270 |
+
"codemirror_mode": {
|
| 271 |
+
"name": "ipython",
|
| 272 |
+
"version": 3
|
| 273 |
+
},
|
| 274 |
+
"file_extension": ".py",
|
| 275 |
+
"mimetype": "text/x-python",
|
| 276 |
+
"name": "python",
|
| 277 |
+
"nbconvert_exporter": "python",
|
| 278 |
+
"pygments_lexer": "ipython3",
|
| 279 |
+
"version": "3.10.10"
|
| 280 |
+
},
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"from langchain.agents import Tool, AgentExecutor, BaseMultiActionAgent\n",
|
| 10 |
+
"from langchain import OpenAI, SerpAPIWrapper"
|
| 11 |
+
]
|
| 12 |
+
},
|
| 13 |
+
{
|
| 14 |
+
"cell_type": "code",
|
| 15 |
+
"execution_count": 2,
|
| 16 |
+
"metadata": {},
|
| 17 |
+
"outputs": [],
|
| 18 |
+
"source": [
|
| 19 |
+
"def random_word(query: str) -> str:\n",
|
| 20 |
+
" print(\"\\nNow I'm doing this!\")\n",
|
| 21 |
+
" return f\"{query}, foo\""
|
| 22 |
+
]
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"cell_type": "code",
|
| 26 |
+
"execution_count": 3,
|
| 27 |
+
"metadata": {},
|
| 28 |
+
"outputs": [],
|
| 29 |
+
"source": [
|
| 30 |
+
"search = SerpAPIWrapper()\n",
|
| 31 |
+
"tools = [\n",
|
| 32 |
+
" Tool(\n",
|
| 33 |
+
" name = \"Search\",\n",
|
| 34 |
+
" func=search.run,\n",
|
| 35 |
+
" description=\"useful for when you need to answer questions about current events\"\n",
|
| 36 |
+
" ),\n",
|
| 37 |
+
" Tool(\n",
|
| 38 |
+
" name = \"RandomWord\",\n",
|
| 39 |
+
" func=random_word,\n",
|
| 40 |
+
" description=\"call this to get a random word.\",\n",
|
| 41 |
+
" )\n",
|
| 42 |
+
"]"
|
| 43 |
+
]
|
| 44 |
+
},
|
| 45 |
+
{
|
| 46 |
+
"cell_type": "code",
|
| 47 |
+
"execution_count": 4,
|
| 48 |
+
"metadata": {},
|
| 49 |
+
"outputs": [],
|
| 50 |
+
"source": [
|
| 51 |
+
"from langchain.tools import tool\n",
|
| 52 |
+
"\n",
|
| 53 |
+
"@tool\n",
|
| 54 |
+
"def tool1(event) -> str:\n",
|
| 55 |
+
" \"\"\"这里必须添加描述,不然AssertionError: Function must have a docstring if description not provided.\"\"\"\n",
|
| 56 |
+
" return \"111\"\n",
|
| 57 |
+
"\n",
|
| 58 |
+
"from langchain.tools import tool\n",
|
| 59 |
+
"\n",
|
| 60 |
+
"@tool\n",
|
| 61 |
+
"def tool2(event) -> str:\n",
|
| 62 |
+
" \"\"\"这里必须添加描述,不然AssertionError: Function must have a docstring if description not provided.\"\"\"\n",
|
| 63 |
+
" return \"222\"\n",
|
| 64 |
+
"\n",
|
| 65 |
+
"tools = [tool1, tool2]"
|
| 66 |
+
]
|
| 67 |
+
},
|
| 68 |
+
{
|
| 69 |
+
"cell_type": "code",
|
| 70 |
+
"execution_count": 5,
|
| 71 |
+
"metadata": {},
|
| 72 |
+
"outputs": [],
|
| 73 |
+
"source": [
|
| 74 |
+
"from typing import List, Tuple, Any, Union\n",
|
| 75 |
+
"from langchain.schema import AgentAction, AgentFinish\n",
|
| 76 |
+
"\n",
|
| 77 |
+
"class FakeAgent(BaseMultiActionAgent):\n",
|
| 78 |
+
" \"\"\"Fake Custom Agent.\"\"\"\n",
|
| 79 |
+
" \n",
|
| 80 |
+
" @property\n",
|
| 81 |
+
" def input_keys(self):\n",
|
| 82 |
+
" return [\"input\"]\n",
|
| 83 |
+
" \n",
|
| 84 |
+
" def plan(\n",
|
| 85 |
+
" self, intermediate_steps: List[Tuple[AgentAction, str]], **kwargs: Any\n",
|
| 86 |
+
" ) -> Union[List[AgentAction], AgentFinish]:\n",
|
| 87 |
+
" \"\"\"Given input, decided what to do.\n",
|
| 88 |
+
"\n",
|
| 89 |
+
" Args:\n",
|
| 90 |
+
" intermediate_steps: Steps the LLM has taken to date,\n",
|
| 91 |
+
" along with observations\n",
|
| 92 |
+
" **kwargs: User inputs.\n",
|
| 93 |
+
"\n",
|
| 94 |
+
" Returns:\n",
|
| 95 |
+
" Action specifying what tool to use.\n",
|
| 96 |
+
" \"\"\"\n",
|
| 97 |
+
" print(\"lizhen0\",intermediate_steps)\n",
|
| 98 |
+
" if len(intermediate_steps) == 0:\n",
|
| 99 |
+
" print(\"lizhen1\",intermediate_steps)\n",
|
| 100 |
+
" return [\n",
|
| 101 |
+
" AgentAction(tool=\"tool1\", tool_input=kwargs[\"input\"], log=\"\"),\n",
|
| 102 |
+
" AgentAction(tool=\"tool2\", tool_input=kwargs[\"input\"], log=\"\"),\n",
|
| 103 |
+
" ]\n",
|
| 104 |
+
" else:\n",
|
| 105 |
+
" for tuple in intermediate_steps:\n",
|
| 106 |
+
" print(tuple[0].tool, tuple[1])\n",
|
| 107 |
+
" return AgentFinish(return_values={\"output\": \"bar\"}, log=\"\")\n",
|
| 108 |
+
"\n",
|
| 109 |
+
" async def aplan(\n",
|
| 110 |
+
" self, intermediate_steps: List[Tuple[AgentAction, str]], **kwargs: Any\n",
|
| 111 |
+
" ) -> Union[List[AgentAction], AgentFinish]:\n",
|
| 112 |
+
" \"\"\"Given input, decided what to do.\n",
|
| 113 |
+
"\n",
|
| 114 |
+
" Args:\n",
|
| 115 |
+
" intermediate_steps: Steps the LLM has taken to date,\n",
|
| 116 |
+
" along with observations\n",
|
| 117 |
+
" **kwargs: User inputs.\n",
|
| 118 |
+
"\n",
|
| 119 |
+
" Returns:\n",
|
| 120 |
+
" Action specifying what tool to use.\n",
|
| 121 |
+
" \"\"\"\n",
|
| 122 |
+
" if len(intermediate_steps) == 0:\n",
|
| 123 |
+
" return [\n",
|
| 124 |
+
" AgentAction(tool=\"tool1\", tool_input=kwargs[\"input\"], log=\"\"),\n",
|
| 125 |
+
" AgentAction(tool=\"tool2\", tool_input=kwargs[\"input\"], log=\"\"),\n",
|
| 126 |
+
" ]\n",
|
| 127 |
+
" else:\n",
|
| 128 |
+
" return AgentFinish(return_values={\"output\": \"bar\"}, log=\"\")"
|
| 129 |
+
]
|
| 130 |
+
},
|
| 131 |
+
{
|
| 132 |
+
"cell_type": "code",
|
| 133 |
+
"execution_count": 6,
|
| 134 |
+
"metadata": {},
|
| 135 |
+
"outputs": [],
|
| 136 |
+
"source": [
|
| 137 |
+
"agent = FakeAgent()"
|
| 138 |
+
]
|
| 139 |
+
},
|
| 140 |
+
{
|
| 141 |
+
"cell_type": "code",
|
| 142 |
+
"execution_count": 7,
|
| 143 |
+
"metadata": {},
|
| 144 |
+
"outputs": [],
|
| 145 |
+
"source": [
|
| 146 |
+
"agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)"
|
| 147 |
+
]
|
| 148 |
+
},
|
| 149 |
+
{
|
| 150 |
+
"cell_type": "code",
|
| 151 |
+
"execution_count": 8,
|
| 152 |
+
"metadata": {},
|
| 153 |
+
"outputs": [
|
| 154 |
+
{
|
| 155 |
+
"name": "stdout",
|
| 156 |
+
"output_type": "stream",
|
| 157 |
+
"text": [
|
| 158 |
+
"\n",
|
| 159 |
+
"\n",
|
| 160 |
+
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
| 161 |
+
"lizhen0 []\n",
|
| 162 |
+
"lizhen1 []\n",
|
| 163 |
+
"\u001b[32;1m\u001b[1;3m\u001b[0m\u001b[36;1m\u001b[1;3m111\u001b[0m\u001b[32;1m\u001b[1;3m\u001b[0m\u001b[33;1m\u001b[1;3m222\u001b[0mlizhen0 [(AgentAction(tool='tool1', tool_input='How many people live in canada as of 2023?', log=''), '111'), (AgentAction(tool='tool2', tool_input='How many people live in canada as of 2023?', log=''), '222')]\n",
|
| 164 |
+
"tool1 111\n",
|
| 165 |
+
"tool2 222\n",
|
| 166 |
+
"\u001b[32;1m\u001b[1;3m\u001b[0m\n",
|
| 167 |
+
"\n",
|
| 168 |
+
"\u001b[1m> Finished chain.\u001b[0m\n"
|
| 169 |
+
]
|
| 170 |
+
},
|
| 171 |
+
{
|
| 172 |
+
"data": {
|
| 173 |
+
"text/plain": [
|
| 174 |
+
"'bar'"
|
| 175 |
+
]
|
| 176 |
+
},
|
| 177 |
+
"execution_count": 8,
|
| 178 |
+
"metadata": {},
|
| 179 |
+
"output_type": "execute_result"
|
| 180 |
+
}
|
| 181 |
+
],
|
| 182 |
+
"source": [
|
| 183 |
+
"agent_executor.run(\"How many people live in canada as of 2023?\")"
|
| 184 |
+
]
|
| 185 |
+
}
|
| 186 |
+
],
|
| 187 |
+
"metadata": {
|
| 188 |
+
"kernelspec": {
|
| 189 |
+
"display_name": "base",
|
| 190 |
+
"language": "python",
|
| 191 |
+
"name": "python3"
|
| 192 |
+
},
|
| 193 |
+
"language_info": {
|
| 194 |
+
"codemirror_mode": {
|
| 195 |
+
"name": "ipython",
|
| 196 |
+
"version": 3
|
| 197 |
+
},
|
| 198 |
+
"file_extension": ".py",
|
| 199 |
+
"mimetype": "text/x-python",
|
| 200 |
+
"name": "python",
|
| 201 |
+
"nbconvert_exporter": "python",
|
| 202 |
+
"pygments_lexer": "ipython3",
|
| 203 |
+
"version": "3.10.10"
|
| 204 |
+
},
|
| 205 |
+
"orig_nbformat": 4
|
| 206 |
+
},
|
| 207 |
+
"nbformat": 4,
|
| 208 |
+
"nbformat_minor": 2
|
| 209 |
+
}
|
agent_self_ask.ipynb
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [
|
| 8 |
+
{
|
| 9 |
+
"name": "stdout",
|
| 10 |
+
"output_type": "stream",
|
| 11 |
+
"text": [
|
| 12 |
+
"\n",
|
| 13 |
+
"\n",
|
| 14 |
+
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
| 15 |
+
"\u001b[32;1m\u001b[1;3m Yes.\n",
|
| 16 |
+
"Follow up: 谁是最著名的导演?\u001b[0m\n",
|
| 17 |
+
"Intermediate answer: \u001b[36;1m\u001b[1;3mTOP1: 史蒂文·斯皮尔伯格于1946年12月18日生于美国俄亥俄州的辛辛那提市,犹太人血统,电影导演、bai编剧和电影制作人。 代表作品:《辛德勒的名单》、《拯救大兵瑞恩》、《侏罗纪公园》、《林肯》等。 代表作:《蝴蝶梦》、《惊魂记》、《西北偏北》等。\u001b[0m\n",
|
| 18 |
+
"\u001b[32;1m\u001b[1;3m\n",
|
| 19 |
+
"Follow up: 是男的还是女的?\u001b[0m\n",
|
| 20 |
+
"Intermediate answer: \u001b[36;1m\u001b[1;3mEnglish translation of 是男的还是女的- Translations, examples and discussions from LingQ.\u001b[0m\n",
|
| 21 |
+
"\u001b[32;1m\u001b[1;3mIs it a man or a woman?\n",
|
| 22 |
+
"\n",
|
| 23 |
+
"So the final answer is: 男\u001b[0m\n",
|
| 24 |
+
"\n",
|
| 25 |
+
"\u001b[1m> Finished chain.\u001b[0m\n"
|
| 26 |
+
]
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"data": {
|
| 30 |
+
"text/plain": [
|
| 31 |
+
"'男'"
|
| 32 |
+
]
|
| 33 |
+
},
|
| 34 |
+
"execution_count": 1,
|
| 35 |
+
"metadata": {},
|
| 36 |
+
"output_type": "execute_result"
|
| 37 |
+
}
|
| 38 |
+
],
|
| 39 |
+
"source": [
|
| 40 |
+
"from langchain import OpenAI, SerpAPIWrapper\n",
|
| 41 |
+
"from langchain.agents import initialize_agent, Tool\n",
|
| 42 |
+
"from langchain.agents import AgentType\n",
|
| 43 |
+
"\n",
|
| 44 |
+
"llm = OpenAI(temperature=0)\n",
|
| 45 |
+
"search = SerpAPIWrapper()\n",
|
| 46 |
+
"tools = [\n",
|
| 47 |
+
" Tool(\n",
|
| 48 |
+
" name=\"Intermediate Answer\",\n",
|
| 49 |
+
" func=search.run,\n",
|
| 50 |
+
" description=\"useful for when you need to ask with search\"\n",
|
| 51 |
+
" )\n",
|
| 52 |
+
"]\n",
|
| 53 |
+
"\n",
|
| 54 |
+
"self_ask_with_search = initialize_agent(tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True)\n",
|
| 55 |
+
"self_ask_with_search.run(\"最著名导演是男的还是女的?\")"
|
| 56 |
+
]
|
| 57 |
+
}
|
| 58 |
+
],
|
| 59 |
+
"metadata": {
|
| 60 |
+
"kernelspec": {
|
| 61 |
+
"display_name": "base",
|
| 62 |
+
"language": "python",
|
| 63 |
+
"name": "python3"
|
| 64 |
+
},
|
| 65 |
+
"language_info": {
|
| 66 |
+
"codemirror_mode": {
|
| 67 |
+
"name": "ipython",
|
| 68 |
+
"version": 3
|
| 69 |
+
},
|
| 70 |
+
"file_extension": ".py",
|
| 71 |
+
"mimetype": "text/x-python",
|
| 72 |
+
"name": "python",
|
| 73 |
+
"nbconvert_exporter": "python",
|
| 74 |
+
"pygments_lexer": "ipython3",
|
| 75 |
+
"version": "3.10.10"
|
| 76 |
+
},
|
| 77 |
+
"orig_nbformat": 4
|
| 78 |
+
},
|
| 79 |
+
"nbformat": 4,
|
| 80 |
+
"nbformat_minor": 2
|
| 81 |
+
}
|