mirror of
https://github.com/hwchase17/langchain.git
synced 2026-03-18 11:07:36 +00:00
196 lines
5.7 KiB
Plaintext
196 lines
5.7 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ba5f8741",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Chat Agent\n",
|
|
"\n",
|
|
"This notebook goes through how to create a ChatGPT based agent\n",
|
|
"\n",
|
|
"First, we set up the agent with tools as normal."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "9af9734e",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain.agents import ZeroShotAgent, Tool, AgentExecutor\n",
|
|
"from langchain import OpenAI, SerpAPIWrapper, LLMChain, LLMMathChain, SQLDatabase, SQLDatabaseChain"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "becda2a1",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"llm = OpenAI(temperature=0)\n",
|
|
"search = SerpAPIWrapper()\n",
|
|
"llm_math_chain = LLMMathChain(llm=llm, verbose=True)\n",
|
|
"db = SQLDatabase.from_uri(\"sqlite:///../../../../notebooks/Chinook.db\")\n",
|
|
"db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)\n",
|
|
"tools = [\n",
|
|
" Tool(\n",
|
|
" name = \"Search\",\n",
|
|
" func=search.run,\n",
|
|
" description=\"useful for when you need to answer questions about current events. You should ask targeted questions\"\n",
|
|
" ),\n",
|
|
" Tool(\n",
|
|
" name=\"Calculator\",\n",
|
|
" func=llm_math_chain.run,\n",
|
|
" description=\"useful for when you need to answer questions about math\"\n",
|
|
" ),\n",
|
|
" Tool(\n",
|
|
" name=\"FooBar DB\",\n",
|
|
" func=db_chain.run,\n",
|
|
" description=\"useful for when you need to answer questions about FooBar. Input should be in the form of a question containing full context\"\n",
|
|
" )\n",
|
|
"]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1717e36a",
|
|
"metadata": {},
|
|
"source": [
|
|
"Now we create a ChatGPT based model and agent."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"id": "b1f12d80",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain.chat_models.openai import ChatOpenAI\n",
|
|
"from langchain.agents.chat.base import ChatAgent\n",
|
|
"\n",
|
|
"agent = ChatAgent.from_chat_model_and_tools(ChatOpenAI(temperature=0), tools)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "5f57b076",
|
|
"metadata": {},
|
|
"source": [
|
|
"We can now use this as normal"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"id": "490604e9",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"id": "653b1617",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"\n",
|
|
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
|
"\u001b[32;1m\u001b[1;3mQuestion: Who is Dua Lipa's boyfriend? What is his current age raised to the 0.43 power?\n",
|
|
"Thought: We need to find out the name of Dua Lipa's boyfriend and his current age.\n",
|
|
"Action: Search\n",
|
|
"Action Input: \"Dua Lipa boyfriend\"\n",
|
|
"\u001b[0m\n",
|
|
"Observation: \u001b[36;1m\u001b[1;3mDua Lipa was seen heading home from the Saint Laurent Paris Fashion Week show with her new boyfriend Romain Gavras on Tuesday. Advertisement.\u001b[0m\n",
|
|
"Thought:\u001b[32;1m\u001b[1;3mWe found out the name of Dua Lipa's boyfriend, now we need to find out his age.\n",
|
|
"Action: Search\n",
|
|
"Action Input: \"Romain Gavras age\"\n",
|
|
"\u001b[0m\n",
|
|
"Observation: \u001b[36;1m\u001b[1;3m41 years\u001b[0m\n",
|
|
"Thought:\u001b[32;1m\u001b[1;3mWe have the age of Romain Gavras, now we need to calculate his age raised to the 0.43 power.\n",
|
|
"Action: Calculator\n",
|
|
"Action Input: 41^(0.43)\n",
|
|
"\n",
|
|
"\u001b[0m\n",
|
|
"\n",
|
|
"\u001b[1m> Entering new LLMMathChain chain...\u001b[0m\n",
|
|
"41^(0.43)\n",
|
|
"\n",
|
|
"\u001b[32;1m\u001b[1;3m```python\n",
|
|
"print(41**(0.43))\n",
|
|
"```\n",
|
|
"\u001b[0m\n",
|
|
"Answer: \u001b[33;1m\u001b[1;3m4.9373857399466665\n",
|
|
"\u001b[0m\n",
|
|
"\u001b[1m> Finished chain.\u001b[0m\n",
|
|
"\n",
|
|
"Observation: \u001b[33;1m\u001b[1;3mAnswer: 4.9373857399466665\n",
|
|
"\u001b[0m\n",
|
|
"Thought:\u001b[32;1m\u001b[1;3mWe have the answer to the second question, which is 4.9373857399466665.\n",
|
|
"Final Answer: Romain Gavras is Dua Lipa's boyfriend and his current age raised to the 0.43 power is 4.9373857399466665.\u001b[0m\n",
|
|
"\n",
|
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"\"Romain Gavras is Dua Lipa's boyfriend and his current age raised to the 0.43 power is 4.9373857399466665.\""
|
|
]
|
|
},
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"agent_executor.run(\"Who is Dua Lipa's boyfriend? What is his current age raised to the 0.43 power?\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "adefb4c2",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.9.1"
|
|
},
|
|
"vscode": {
|
|
"interpreter": {
|
|
"hash": "18784188d7ecd866c0586ac068b02361a6896dc3a29b64f5cc957f09c590acef"
|
|
}
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|