Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update0806 agent code #111

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 37 additions & 34 deletions langchain/jupyter/agents/openai_function.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: langchain in c:\\users\\lenovo\\appdata\\roaming\\python\\python310\\site-packages (0.2.7)\n",
"Defaulting to user installation because normal site-packages is not writeable\n",
"Requirement already satisfied: langchain in c:\\users\\lenovo\\appdata\\roaming\\python\\python310\\site-packages (0.2.12)\n",
"Requirement already satisfied: PyYAML>=5.3 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain) (6.0.1)\n",
"Requirement already satisfied: SQLAlchemy<3,>=1.4 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain) (2.0.31)\n",
"Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain) (3.9.5)\n",
"Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain) (4.0.3)\n",
"Requirement already satisfied: langchain-core<0.3.0,>=0.2.12 in c:\\users\\lenovo\\appdata\\roaming\\python\\python310\\site-packages (from langchain) (0.2.13)\n",
"Requirement already satisfied: langchain-core<0.3.0,>=0.2.27 in c:\\users\\lenovo\\appdata\\roaming\\python\\python310\\site-packages (from langchain) (0.2.28)\n",
"Requirement already satisfied: langchain-text-splitters<0.3.0,>=0.2.0 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain) (0.2.0)\n",
"Requirement already satisfied: langsmith<0.2.0,>=0.1.17 in c:\\users\\lenovo\\appdata\\roaming\\python\\python310\\site-packages (from langchain) (0.1.85)\n",
"Requirement already satisfied: numpy<2,>=1 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain) (1.26.4)\n",
Expand All @@ -35,18 +36,18 @@
"Requirement already satisfied: frozenlist>=1.1.1 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.4.1)\n",
"Requirement already satisfied: multidict<7.0,>=4.5 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.5)\n",
"Requirement already satisfied: yarl<2.0,>=1.0 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.4)\n",
"Requirement already satisfied: jsonpatch<2.0,>=1.33 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.12->langchain) (1.33)\n",
"Requirement already satisfied: packaging<25,>=23.2 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.12->langchain) (23.2)\n",
"Requirement already satisfied: jsonpatch<2.0,>=1.33 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.27->langchain) (1.33)\n",
"Requirement already satisfied: packaging<25,>=23.2 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.27->langchain) (23.2)\n",
"Requirement already satisfied: typing-extensions>=4.7 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.27->langchain) (4.12.2)\n",
"Requirement already satisfied: orjson<4.0.0,>=3.9.14 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langsmith<0.2.0,>=0.1.17->langchain) (3.10.5)\n",
"Requirement already satisfied: annotated-types>=0.4.0 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from pydantic<3,>=1->langchain) (0.6.0)\n",
"Requirement already satisfied: pydantic-core==2.14.6 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from pydantic<3,>=1->langchain) (2.14.6)\n",
"Requirement already satisfied: typing-extensions>=4.6.1 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from pydantic<3,>=1->langchain) (4.12.2)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from requests<3,>=2->langchain) (3.3.2)\n",
"Requirement already satisfied: idna<4,>=2.5 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from requests<3,>=2->langchain) (3.6)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from requests<3,>=2->langchain) (2.2.2)\n",
"Requirement already satisfied: certifi>=2017.4.17 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from requests<3,>=2->langchain) (2024.2.2)\n",
"Requirement already satisfied: greenlet!=0.4.17 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from SQLAlchemy<3,>=1.4->langchain) (3.0.3)\n",
"Requirement already satisfied: jsonpointer>=1.9 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.3.0,>=0.2.12->langchain) (3.0.0)\n"
"Requirement already satisfied: jsonpointer>=1.9 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.3.0,>=0.2.27->langchain) (3.0.0)\n"
]
}
],
Expand Down Expand Up @@ -92,7 +93,7 @@
"outputs": [],
"source": [
"from langchain.schema import SystemMessage\n",
"from langchain.agents import OpenAIFunctionsAgent\n",
"from langchain.agents import OpenAIFunctionsAgent, create_openai_functions_agent\n",
"\n",
"system_message = SystemMessage(content=\"你是非常强大的AI助手,但在计算单词长度方面不擅长。\")\n",
"prompt = OpenAIFunctionsAgent.create_prompt(system_message=system_message)"
Expand All @@ -103,23 +104,24 @@
"execution_count": 5,
"id": "ef0874c6-8076-4536-9533-bb9f69f450fe",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\lenovo\\AppData\\Roaming\\Python\\Python310\\site-packages\\langchain_core\\_api\\deprecation.py:139: LangChainDeprecationWarning: The class `OpenAIFunctionsAgent` was deprecated in LangChain 0.1.0 and will be removed in 0.3.0. Use create_openai_functions_agent instead.\n",
" warn_deprecated(\n"
]
}
],
"outputs": [],
"source": [
"agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)"
"# agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)#过时"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "0532b789-455c-4188-ba32-dba615131b2e",
"metadata": {},
"outputs": [],
"source": [
"agent = create_openai_functions_agent(llm=llm, tools=tools, prompt=prompt)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "e7061c25-d090-4b52-a137-ba7545759241",
"metadata": {},
"outputs": [],
Expand All @@ -132,7 +134,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"id": "4f0ae154-5cb9-44cf-91d6-cc80308dcffc",
"metadata": {},
"outputs": [
Expand All @@ -158,18 +160,18 @@
"{'input': '单词“educa”中有多少个字母?', 'output': '单词“educa”中有5个字母。'}"
]
},
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent_executor.invoke(\"单词“educa”中有多少个字母?\")"
"agent_executor.invoke({\"input\":\"单词“educa”中有多少个字母?\"})"
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"id": "2070d22f-c9d9-4346-b41d-e9beecb40625",
"metadata": {},
"outputs": [],
Expand All @@ -185,7 +187,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"id": "849ebeac-aa8c-4ae8-b36c-c81a120147fb",
"metadata": {},
"outputs": [],
Expand All @@ -197,18 +199,19 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"id": "9af06fad-4f85-4411-b4f8-4fca7a45e09b",
"metadata": {},
"outputs": [],
"source": [
"agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)\n",
"# agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt) # 过时\n",
"agent = create_openai_functions_agent(llm=llm, tools=tools, prompt=prompt)\n",
"agent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory, verbose=True)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 13,
"id": "fc912215-61d3-4ce1-9a48-b795cf445bde",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -237,18 +240,18 @@
" 'output': '单词“educa”中有5个字母。'}"
]
},
"execution_count": 11,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent_executor.invoke(\"单词“educa”中有多少个字母?\")"
"agent_executor.invoke({\"input\":\"单词“educa”中有多少个字母?\"})"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 15,
"id": "d3f96ac9-b548-4908-9c5e-d58118ed6a9a",
"metadata": {},
"outputs": [
Expand All @@ -259,7 +262,7 @@
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m抱歉,我无法确定“educa”是否是一个真实的单词。您可以查阅词典或在线搜索以确认该单词的存在。\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m抱歉,我无法确定“educa”是否是一个真实的单词。您可以查阅词典或搜索引擎来确认这个单词的存在。\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
Expand All @@ -271,17 +274,17 @@
" 'chat_history': [HumanMessage(content='单词“educa”中有多少个字母?'),\n",
" AIMessage(content='单词“educa”中有5个字母。'),\n",
" HumanMessage(content='那是一个真实的单词吗?'),\n",
" AIMessage(content='抱歉,我无法确定“educa”是否是一个真实的单词。您可以查阅词典或在线搜索以确认该单词的存在。')],\n",
" 'output': '抱歉,我无法确定“educa”是否是一个真实的单词。您可以查阅词典或在线搜索以确认该单词的存在。'}"
" AIMessage(content='抱歉,我无法确定“educa”是否是一个真实的单词。您可以查阅词典或搜索引擎来确认这个单词的存在。')],\n",
" 'output': '抱歉,我无法确定“educa”是否是一个真实的单词。您可以查阅词典或搜索引擎来确认这个单词的存在。'}"
]
},
"execution_count": 12,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent_executor.invoke(\"那是一个真实的单词吗?\")"
"agent_executor.invoke({\"input\":\"那是一个真实的单词吗?\"})"
]
},
{
Expand Down
Loading