mirror of
https://github.com/hwchase17/langchain
synced 2024-10-31 15:20:26 +00:00
341 lines
10 KiB
Plaintext
341 lines
10 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ba5f8741",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Custom Agent\n",
|
|
"\n",
|
|
"This notebook goes through how to create your own custom agent.\n",
|
|
"\n",
|
|
"An agent consists of three parts:\n",
|
|
" \n",
|
|
" - Tools: The tools the agent has available to use.\n",
|
|
" - LLMChain: The LLMChain that produces the text that is parsed in a certain way to determine which action to take.\n",
|
|
" - The agent class itself: this parses the output of the LLMChain to determin which action to take.\n",
|
|
" \n",
|
|
" \n",
|
|
"In this notebook we walk through two types of custom agents. The first type shows how to create a custom LLMChain, but still use an existing agent class to parse the output. The second shows how to create a custom agent class."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "6064f080",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Custom LLMChain\n",
|
|
"\n",
|
|
"The first way to create a custom agent is to use an existing Agent class, but use a custom LLMChain. This is the simplest way to create a custom Agent. It is highly reccomended that you work with the `ZeroShotAgent`, as at the moment that is by far the most generalizable one. \n",
|
|
"\n",
|
|
"Most of the work in creating the custom LLMChain comes down to the prompt. Because we are using an existing agent class to parse the output, it is very important that the prompt say to produce text in that format. Additionally, we currently require an `agent_scratchpad` input variable to put notes on previous actions and observations. This should almost always be the final part of the prompt. However, besides those instructions, you can customize the prompt as you wish.\n",
|
|
"\n",
|
|
"To ensure that the prompt contains the appropriate instructions, we will utilize a helper method on that class. The helper method for the `ZeroShotAgent` takes the following arguments:\n",
|
|
"\n",
|
|
"- tools: List of tools the agent will have access to, used to format the prompt.\n",
|
|
"- prefix: String to put before the list of tools.\n",
|
|
"- suffix: String to put after the list of tools.\n",
|
|
"- input_variables: List of input variables the final prompt will expect.\n",
|
|
"\n",
|
|
"For this exercise, we will give our agent access to Google Search, and we will customize it in that we will have it answer as a pirate."
|
|
]
|
|
},
|
|
{
|
|
"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"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "becda2a1",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"search = SerpAPIWrapper()\n",
|
|
"tools = [\n",
|
|
" Tool(\n",
|
|
" name = \"Search\",\n",
|
|
" func=search.run,\n",
|
|
" description=\"useful for when you need to answer questions about current events\"\n",
|
|
" )\n",
|
|
"]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "339b1bb8",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"prefix = \"\"\"Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools:\"\"\"\n",
|
|
"suffix = \"\"\"Begin! Remember to speak as a pirate when giving your final answer. Use lots of \"Args\"\n",
|
|
"\n",
|
|
"Question: {input}\n",
|
|
"{agent_scratchpad}\"\"\"\n",
|
|
"\n",
|
|
"prompt = ZeroShotAgent.create_prompt(\n",
|
|
" tools, \n",
|
|
" prefix=prefix, \n",
|
|
" suffix=suffix, \n",
|
|
" input_variables=[\"input\", \"agent_scratchpad\"]\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "59db7b58",
|
|
"metadata": {},
|
|
"source": [
|
|
"In case we are curious, we can now take a look at the final prompt template to see what it looks like when its all put together."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "e21d2098",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools:\n",
|
|
"\n",
|
|
"Search: useful for when you need to answer questions about current events\n",
|
|
"\n",
|
|
"Use the following format:\n",
|
|
"\n",
|
|
"Question: the input question you must answer\n",
|
|
"Thought: you should always think about what to do\n",
|
|
"Action: the action to take, should be one of [Search]\n",
|
|
"Action Input: the input to the action\n",
|
|
"Observation: the result of the action\n",
|
|
"... (this Thought/Action/Action Input/Observation can repeat N times)\n",
|
|
"Thought: I now know the final answer\n",
|
|
"Final Answer: the final answer to the original input question\n",
|
|
"\n",
|
|
"Begin! Remember to speak as a pirate when giving your final answer. Use lots of \"Args\"\n",
|
|
"\n",
|
|
"Question: {input}\n",
|
|
"{agent_scratchpad}\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(prompt.template)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "9b1cc2a2",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"id": "e4f5092f",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"id": "490604e9",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"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;3mThought: I need to find out how many people live in Canada\n",
|
|
"Action: Search\n",
|
|
"Action Input: Population of Canada\u001b[0m\n",
|
|
"Observation: \u001b[36;1m\u001b[1;3mThe current population of Canada is 38,553,548 as of Saturday, December 17, 2022, based on Worldometer elaboration of the latest United Nations data. Canada ...\u001b[0m\n",
|
|
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
|
|
"Final Answer: Arrr, there be 38,553,548 scallywags livin' in Canada!\u001b[0m\n",
|
|
"\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"\"Arrr, there be 38,553,548 scallywags livin' in Canada!\""
|
|
]
|
|
},
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"agent_executor.run(\"How many people live in canada?\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "040eb343",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Multiple inputs\n",
|
|
"Agents can also work with prompts that require multiple inputs."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"id": "43dbfa2f",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"prefix = \"\"\"Answer the following questions as best you can. You have access to the following tools:\"\"\"\n",
|
|
"suffix = \"\"\"When answering, you MUST speak in the following language: {language}.\n",
|
|
"\n",
|
|
"Question: {input}\n",
|
|
"{agent_scratchpad}\"\"\"\n",
|
|
"\n",
|
|
"prompt = ZeroShotAgent.create_prompt(\n",
|
|
" tools, \n",
|
|
" prefix=prefix, \n",
|
|
" suffix=suffix, \n",
|
|
" input_variables=[\"input\", \"language\", \"agent_scratchpad\"]\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"id": "0f087313",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"id": "92c75a10",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"id": "ac5b83bf",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 14,
|
|
"id": "c960e4ff",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"\n",
|
|
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
|
"\u001b[32;1m\u001b[1;3mThought: I should look up the population of Canada.\n",
|
|
"Action: Search\n",
|
|
"Action Input: Population of Canada\u001b[0m\n",
|
|
"Observation: \u001b[36;1m\u001b[1;3mThe current population of Canada is 38,553,548 as of Saturday, December 17, 2022, based on Worldometer elaboration of the latest United Nations data. Canada ...\u001b[0m\n",
|
|
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n",
|
|
"Final Answer: La popolazione attuale del Canada è 38.553.548 al sabato 17 dicembre 2022, secondo l'elaborazione di Worldometer dei dati più recenti delle Nazioni Unite.\u001b[0m\n",
|
|
"\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"\"La popolazione attuale del Canada è 38.553.548 al sabato 17 dicembre 2022, secondo l'elaborazione di Worldometer dei dati più recenti delle Nazioni Unite.\""
|
|
]
|
|
},
|
|
"execution_count": 14,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"agent_executor.run(input=\"How many people live in canada?\", language=\"italian\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "90171b2b",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Custom Agent Class\n",
|
|
"\n",
|
|
"Coming soon."
|
|
]
|
|
},
|
|
{
|
|
"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.10.8"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|