2022-11-26 14:03:08 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "ba5f8741",
"metadata": {},
"source": [
2023-06-16 18:52:56 +00:00
"# Custom agent\n",
2022-11-26 14:03:08 +00:00
"\n",
"This notebook goes through how to create your own custom agent.\n",
"\n",
2023-05-17 15:52:22 +00:00
"An agent consists of two parts:\n",
2022-11-26 14:03:08 +00:00
" \n",
" - Tools: The tools the agent has available to use.\n",
2023-04-02 16:12:54 +00:00
" - The agent class itself: this decides which action to take.\n",
2022-11-26 14:03:08 +00:00
" \n",
" \n",
2023-04-02 16:12:54 +00:00
"In this notebook we walk through how to create a custom agent."
2022-11-26 14:03:08 +00:00
]
},
{
"cell_type": "code",
2023-04-02 16:12:54 +00:00
"execution_count": 1,
2022-11-26 14:03:08 +00:00
"id": "9af9734e",
"metadata": {},
"outputs": [],
"source": [
2023-04-02 16:12:54 +00:00
"from langchain.agents import Tool, AgentExecutor, BaseSingleActionAgent\n",
"from langchain import OpenAI, SerpAPIWrapper"
2022-11-26 14:03:08 +00:00
]
},
{
"cell_type": "code",
2023-04-02 16:12:54 +00:00
"execution_count": 2,
2022-11-26 14:03:08 +00:00
"id": "becda2a1",
"metadata": {},
"outputs": [],
"source": [
2022-11-30 04:07:44 +00:00
"search = SerpAPIWrapper()\n",
2022-11-26 14:03:08 +00:00
"tools = [\n",
" Tool(\n",
2023-06-16 18:52:56 +00:00
" name=\"Search\",\n",
2022-11-26 14:03:08 +00:00
" func=search.run,\n",
2023-04-02 16:12:54 +00:00
" description=\"useful for when you need to answer questions about current events\",\n",
2023-06-16 18:52:56 +00:00
" return_direct=True,\n",
2022-11-26 14:03:08 +00:00
" )\n",
"]"
]
},
{
"cell_type": "code",
2023-04-25 23:06:47 +00:00
"execution_count": 3,
2023-04-02 16:12:54 +00:00
"id": "a33e2f7e",
2022-11-26 14:03:08 +00:00
"metadata": {},
"outputs": [],
"source": [
2023-04-02 16:12:54 +00:00
"from typing import List, Tuple, Any, Union\n",
"from langchain.schema import AgentAction, AgentFinish\n",
2022-11-26 14:03:08 +00:00
"\n",
2023-06-16 18:52:56 +00:00
"\n",
2023-04-02 16:12:54 +00:00
"class FakeAgent(BaseSingleActionAgent):\n",
" \"\"\"Fake Custom Agent.\"\"\"\n",
2023-06-16 18:52:56 +00:00
"\n",
2023-04-02 16:12:54 +00:00
" @property\n",
" def input_keys(self):\n",
" return [\"input\"]\n",
2023-06-16 18:52:56 +00:00
"\n",
2023-04-02 16:12:54 +00:00
" def plan(\n",
" self, intermediate_steps: List[Tuple[AgentAction, str]], **kwargs: Any\n",
" ) -> Union[AgentAction, AgentFinish]:\n",
" \"\"\"Given input, decided what to do.\n",
2022-11-26 14:03:08 +00:00
"\n",
2023-04-02 16:12:54 +00:00
" Args:\n",
" intermediate_steps: Steps the LLM has taken to date,\n",
" along with observations\n",
" **kwargs: User inputs.\n",
2023-01-18 06:47:15 +00:00
"\n",
2023-04-02 16:12:54 +00:00
" Returns:\n",
" Action specifying what tool to use.\n",
" \"\"\"\n",
2023-04-12 16:13:46 +00:00
" return AgentAction(tool=\"Search\", tool_input=kwargs[\"input\"], log=\"\")\n",
2023-04-02 16:12:54 +00:00
"\n",
" async def aplan(\n",
" self, intermediate_steps: List[Tuple[AgentAction, str]], **kwargs: Any\n",
" ) -> Union[AgentAction, AgentFinish]:\n",
" \"\"\"Given input, decided what to do.\n",
"\n",
" Args:\n",
" intermediate_steps: Steps the LLM has taken to date,\n",
" along with observations\n",
" **kwargs: User inputs.\n",
"\n",
" Returns:\n",
" Action specifying what tool to use.\n",
" \"\"\"\n",
2023-04-12 16:13:46 +00:00
" return AgentAction(tool=\"Search\", tool_input=kwargs[\"input\"], log=\"\")"
2022-11-26 14:03:08 +00:00
]
},
{
"cell_type": "code",
2023-04-25 23:06:47 +00:00
"execution_count": 4,
2023-04-02 16:12:54 +00:00
"id": "655d72f6",
2022-11-26 14:03:08 +00:00
"metadata": {},
"outputs": [],
"source": [
2023-04-02 16:12:54 +00:00
"agent = FakeAgent()"
2022-11-26 14:03:08 +00:00
]
},
{
"cell_type": "code",
2023-04-25 23:06:47 +00:00
"execution_count": 5,
2022-12-19 02:51:23 +00:00
"id": "490604e9",
"metadata": {},
"outputs": [],
"source": [
2023-06-16 18:52:56 +00:00
"agent_executor = AgentExecutor.from_agent_and_tools(\n",
" agent=agent, tools=tools, verbose=True\n",
")"
2022-12-19 02:51:23 +00:00
]
},
{
"cell_type": "code",
2023-04-25 23:06:47 +00:00
"execution_count": 6,
2022-11-26 14:03:08 +00:00
"id": "653b1617",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
2022-12-19 02:51:23 +00:00
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
2023-04-25 23:06:47 +00:00
"\u001b[32;1m\u001b[1;3m\u001b[0m\u001b[36;1m\u001b[1;3mThe current population of Canada is 38,669,152 as of Monday, April 24, 2023, based on Worldometer elaboration of the latest United Nations data.\u001b[0m\u001b[32;1m\u001b[1;3m\u001b[0m\n",
2023-01-28 15:23:04 +00:00
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
2022-12-19 02:51:23 +00:00
]
},
{
"data": {
"text/plain": [
2023-04-25 23:06:47 +00:00
"'The current population of Canada is 38,669,152 as of Monday, April 24, 2023, based on Worldometer elaboration of the latest United Nations data.'"
2022-12-19 02:51:23 +00:00
]
},
2023-04-25 23:06:47 +00:00
"execution_count": 6,
2022-12-19 02:51:23 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2023-02-18 21:40:43 +00:00
"agent_executor.run(\"How many people live in canada as of 2023?\")"
2022-12-19 02:51:23 +00:00
]
},
2022-11-26 14:03:08 +00:00
{
"cell_type": "code",
"execution_count": null,
"id": "adefb4c2",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
2023-01-28 15:23:04 +00:00
"display_name": "Python 3 (ipykernel)",
2022-11-26 14:03:08 +00:00
"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",
2023-06-16 18:52:56 +00:00
"version": "3.11.3"
Docs refactor (#480)
Big docs refactor! Motivation is to make it easier for people to find
resources they are looking for. To accomplish this, there are now three
main sections:
- Getting Started: steps for getting started, walking through most core
functionality
- Modules: these are different modules of functionality that langchain
provides. Each part here has a "getting started", "how to", "key
concepts" and "reference" section (except in a few select cases where it
didnt easily fit).
- Use Cases: this is to separate use cases (like summarization, question
answering, evaluation, etc) from the modules, and provide a different
entry point to the code base.
There is also a full reference section, as well as extra resources
(glossary, gallery, etc)
Co-authored-by: Shreya Rajpal <ShreyaR@users.noreply.github.com>
2023-01-02 16:24:09 +00:00
},
"vscode": {
"interpreter": {
2023-01-18 06:47:15 +00:00
"hash": "18784188d7ecd866c0586ac068b02361a6896dc3a29b64f5cc957f09c590acef"
Docs refactor (#480)
Big docs refactor! Motivation is to make it easier for people to find
resources they are looking for. To accomplish this, there are now three
main sections:
- Getting Started: steps for getting started, walking through most core
functionality
- Modules: these are different modules of functionality that langchain
provides. Each part here has a "getting started", "how to", "key
concepts" and "reference" section (except in a few select cases where it
didnt easily fit).
- Use Cases: this is to separate use cases (like summarization, question
answering, evaluation, etc) from the modules, and provide a different
entry point to the code base.
There is also a full reference section, as well as extra resources
(glossary, gallery, etc)
Co-authored-by: Shreya Rajpal <ShreyaR@users.noreply.github.com>
2023-01-02 16:24:09 +00:00
}
2022-11-26 14:03:08 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}