forked from Archives/langchain
Add Enum for agent types (#2321)
This pull request adds an enum class for the various types of agents used in the project, located in the `agent_types.py` file. Currently, the project is using hardcoded strings for the initialization of these agents, which can lead to errors and make the code harder to maintain. With the introduction of the new enums, the code will be more readable and less error-prone. The new enum members include: - ZERO_SHOT_REACT_DESCRIPTION - REACT_DOCSTORE - SELF_ASK_WITH_SEARCH - CONVERSATIONAL_REACT_DESCRIPTION - CHAT_ZERO_SHOT_REACT_DESCRIPTION - CHAT_CONVERSATIONAL_REACT_DESCRIPTION In this PR, I have also replaced the hardcoded strings with the appropriate enum members throughout the codebase, ensuring a smooth transition to the new approach.
This commit is contained in:
parent
c969a779c9
commit
10dab053b4
@ -205,7 +205,8 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.agents import initialize_agent, load_tools"
|
"from langchain.agents import initialize_agent, load_tools\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -252,7 +253,7 @@
|
|||||||
"agent = initialize_agent(\n",
|
"agent = initialize_agent(\n",
|
||||||
" tools,\n",
|
" tools,\n",
|
||||||
" llm,\n",
|
" llm,\n",
|
||||||
" agent=\"zero-shot-react-description\",\n",
|
" agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",
|
||||||
" callback_manager=manager,\n",
|
" callback_manager=manager,\n",
|
||||||
" verbose=True,\n",
|
" verbose=True,\n",
|
||||||
")\n",
|
")\n",
|
||||||
|
@ -520,13 +520,14 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.agents import initialize_agent, load_tools\n",
|
"from langchain.agents import initialize_agent, load_tools\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# SCENARIO 2 - Agent with Tools\n",
|
"# SCENARIO 2 - Agent with Tools\n",
|
||||||
"tools = load_tools([\"serpapi\", \"llm-math\"], llm=llm, callback_manager=manager)\n",
|
"tools = load_tools([\"serpapi\", \"llm-math\"], llm=llm, callback_manager=manager)\n",
|
||||||
"agent = initialize_agent(\n",
|
"agent = initialize_agent(\n",
|
||||||
" tools,\n",
|
" tools,\n",
|
||||||
" llm,\n",
|
" llm,\n",
|
||||||
" agent=\"zero-shot-react-description\",\n",
|
" agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",
|
||||||
" callback_manager=manager,\n",
|
" callback_manager=manager,\n",
|
||||||
" verbose=True,\n",
|
" verbose=True,\n",
|
||||||
")\n",
|
")\n",
|
||||||
|
@ -23,6 +23,7 @@ You can use it as part of a Self Ask chain:
|
|||||||
from langchain.utilities import GoogleSerperAPIWrapper
|
from langchain.utilities import GoogleSerperAPIWrapper
|
||||||
from langchain.llms.openai import OpenAI
|
from langchain.llms.openai import OpenAI
|
||||||
from langchain.agents import initialize_agent, Tool
|
from langchain.agents import initialize_agent, Tool
|
||||||
|
from langchain.agents.agent_types import AgentType
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@ -39,7 +40,7 @@ tools = [
|
|||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
||||||
self_ask_with_search = initialize_agent(tools, llm, agent="self-ask-with-search", verbose=True)
|
self_ask_with_search = initialize_agent(tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True)
|
||||||
self_ask_with_search.run("What is the hometown of the reigning men's U.S. Open champion?")
|
self_ask_with_search.run("What is the hometown of the reigning men's U.S. Open champion?")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -505,7 +505,8 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.agents import initialize_agent, load_tools"
|
"from langchain.agents import initialize_agent, load_tools\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -580,7 +581,7 @@
|
|||||||
"agent = initialize_agent(\n",
|
"agent = initialize_agent(\n",
|
||||||
" tools,\n",
|
" tools,\n",
|
||||||
" llm,\n",
|
" llm,\n",
|
||||||
" agent=\"zero-shot-react-description\",\n",
|
" agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",
|
||||||
" callback_manager=manager,\n",
|
" callback_manager=manager,\n",
|
||||||
" verbose=True,\n",
|
" verbose=True,\n",
|
||||||
")\n",
|
")\n",
|
||||||
|
@ -197,6 +197,7 @@ Now we can get started!
|
|||||||
```python
|
```python
|
||||||
from langchain.agents import load_tools
|
from langchain.agents import load_tools
|
||||||
from langchain.agents import initialize_agent
|
from langchain.agents import initialize_agent
|
||||||
|
from langchain.agents.agent_types import AgentType
|
||||||
from langchain.llms import OpenAI
|
from langchain.llms import OpenAI
|
||||||
|
|
||||||
# First, let's load the language model we're going to use to control the agent.
|
# First, let's load the language model we're going to use to control the agent.
|
||||||
@ -207,7 +208,7 @@ tools = load_tools(["serpapi", "llm-math"], llm=llm)
|
|||||||
|
|
||||||
|
|
||||||
# Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.
|
# Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.
|
||||||
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
|
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
|
||||||
|
|
||||||
# Now let's test it out!
|
# Now let's test it out!
|
||||||
agent.run("What was the high temperature in SF yesterday in Fahrenheit? What is that number raised to the .023 power?")
|
agent.run("What was the high temperature in SF yesterday in Fahrenheit? What is that number raised to the .023 power?")
|
||||||
@ -404,11 +405,12 @@ chain.run(input_language="English", output_language="French", text="I love progr
|
|||||||
`````
|
`````
|
||||||
|
|
||||||
`````{dropdown} Agents with Chat Models
|
`````{dropdown} Agents with Chat Models
|
||||||
Agents can also be used with chat models, you can initialize one using `"chat-zero-shot-react-description"` as the agent type.
|
Agents can also be used with chat models, you can initialize one using `AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION` as the agent type.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from langchain.agents import load_tools
|
from langchain.agents import load_tools
|
||||||
from langchain.agents import initialize_agent
|
from langchain.agents import initialize_agent
|
||||||
|
from langchain.agents.agent_types import AgentType
|
||||||
from langchain.chat_models import ChatOpenAI
|
from langchain.chat_models import ChatOpenAI
|
||||||
from langchain.llms import OpenAI
|
from langchain.llms import OpenAI
|
||||||
|
|
||||||
@ -421,7 +423,7 @@ tools = load_tools(["serpapi", "llm-math"], llm=llm)
|
|||||||
|
|
||||||
|
|
||||||
# Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.
|
# Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.
|
||||||
agent = initialize_agent(tools, chat, agent="chat-zero-shot-react-description", verbose=True)
|
agent = initialize_agent(tools, chat, agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
|
||||||
|
|
||||||
# Now let's test it out!
|
# Now let's test it out!
|
||||||
agent.run("Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?")
|
agent.run("Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?")
|
||||||
|
@ -154,6 +154,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# Import things that are needed generically\n",
|
"# Import things that are needed generically\n",
|
||||||
"from langchain.agents import initialize_agent, Tool\n",
|
"from langchain.agents import initialize_agent, Tool\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"from langchain.tools import BaseTool\n",
|
"from langchain.tools import BaseTool\n",
|
||||||
"from langchain.llms import OpenAI\n",
|
"from langchain.llms import OpenAI\n",
|
||||||
"from langchain import LLMMathChain, SerpAPIWrapper"
|
"from langchain import LLMMathChain, SerpAPIWrapper"
|
||||||
@ -189,7 +190,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# Construct the agent. We will use the default agent type here.\n",
|
"# Construct the agent. We will use the default agent type here.\n",
|
||||||
"# See documentation for a full list of options.\n",
|
"# See documentation for a full list of options.\n",
|
||||||
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
|
"agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -316,7 +317,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
|
"agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -433,7 +434,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# Construct the agent. We will use the default agent type here.\n",
|
"# Construct the agent. We will use the default agent type here.\n",
|
||||||
"# See documentation for a full list of options.\n",
|
"# See documentation for a full list of options.\n",
|
||||||
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
|
"agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -39,6 +39,7 @@
|
|||||||
"import time\n",
|
"import time\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from langchain.agents import initialize_agent, load_tools\n",
|
"from langchain.agents import initialize_agent, load_tools\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"from langchain.llms import OpenAI\n",
|
"from langchain.llms import OpenAI\n",
|
||||||
"from langchain.callbacks.stdout import StdOutCallbackHandler\n",
|
"from langchain.callbacks.stdout import StdOutCallbackHandler\n",
|
||||||
"from langchain.callbacks.base import CallbackManager\n",
|
"from langchain.callbacks.base import CallbackManager\n",
|
||||||
@ -175,7 +176,7 @@
|
|||||||
" llm = OpenAI(temperature=0)\n",
|
" llm = OpenAI(temperature=0)\n",
|
||||||
" tools = load_tools([\"llm-math\", \"serpapi\"], llm=llm)\n",
|
" tools = load_tools([\"llm-math\", \"serpapi\"], llm=llm)\n",
|
||||||
" agent = initialize_agent(\n",
|
" agent = initialize_agent(\n",
|
||||||
" tools, llm, agent=\"zero-shot-react-description\", verbose=True\n",
|
" tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION verbose=True\n",
|
||||||
" )\n",
|
" )\n",
|
||||||
" agent.run(q)\n",
|
" agent.run(q)\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -311,7 +312,7 @@
|
|||||||
" llm = OpenAI(temperature=0, callback_manager=manager)\n",
|
" llm = OpenAI(temperature=0, callback_manager=manager)\n",
|
||||||
" async_tools = load_tools([\"llm-math\", \"serpapi\"], llm=llm, aiosession=aiosession, callback_manager=manager)\n",
|
" async_tools = load_tools([\"llm-math\", \"serpapi\"], llm=llm, aiosession=aiosession, callback_manager=manager)\n",
|
||||||
" agents.append(\n",
|
" agents.append(\n",
|
||||||
" initialize_agent(async_tools, llm, agent=\"zero-shot-react-description\", verbose=True, callback_manager=manager)\n",
|
" initialize_agent(async_tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, callback_manager=manager)\n",
|
||||||
" )\n",
|
" )\n",
|
||||||
" tasks = [async_agent.arun(q) for async_agent, q in zip(agents, questions)]\n",
|
" tasks = [async_agent.arun(q) for async_agent, q in zip(agents, questions)]\n",
|
||||||
" await asyncio.gather(*tasks)\n",
|
" await asyncio.gather(*tasks)\n",
|
||||||
@ -381,7 +382,7 @@
|
|||||||
"llm = OpenAI(temperature=0, callback_manager=manager)\n",
|
"llm = OpenAI(temperature=0, callback_manager=manager)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"async_tools = load_tools([\"llm-math\", \"serpapi\"], llm=llm, aiosession=aiosession)\n",
|
"async_tools = load_tools([\"llm-math\", \"serpapi\"], llm=llm, aiosession=aiosession)\n",
|
||||||
"async_agent = initialize_agent(async_tools, llm, agent=\"zero-shot-react-description\", verbose=True, callback_manager=manager)\n",
|
"async_agent = initialize_agent(async_tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, callback_manager=manager)\n",
|
||||||
"await async_agent.arun(questions[0])\n",
|
"await async_agent.arun(questions[0])\n",
|
||||||
"await aiosession.close()"
|
"await aiosession.close()"
|
||||||
]
|
]
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from langchain.agents import load_tools\n",
|
"from langchain.agents import load_tools\n",
|
||||||
"from langchain.agents import initialize_agent\n",
|
"from langchain.agents import initialize_agent\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"from langchain.llms import OpenAI"
|
"from langchain.llms import OpenAI"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -56,7 +57,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True, return_intermediate_steps=True)"
|
"agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, return_intermediate_steps=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from langchain.agents import load_tools\n",
|
"from langchain.agents import load_tools\n",
|
||||||
"from langchain.agents import initialize_agent, Tool\n",
|
"from langchain.agents import initialize_agent, Tool\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"from langchain.llms import OpenAI"
|
"from langchain.llms import OpenAI"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -59,7 +60,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
|
"agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -139,7 +140,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True, max_iterations=2)"
|
"agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, max_iterations=2)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -198,7 +199,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True, max_iterations=2, early_stopping_method=\"generate\")"
|
"agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, max_iterations=2, early_stopping_method=\"generate\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -34,7 +34,8 @@
|
|||||||
"from langchain.memory import ConversationBufferMemory\n",
|
"from langchain.memory import ConversationBufferMemory\n",
|
||||||
"from langchain.chat_models import ChatOpenAI\n",
|
"from langchain.chat_models import ChatOpenAI\n",
|
||||||
"from langchain.utilities import SerpAPIWrapper\n",
|
"from langchain.utilities import SerpAPIWrapper\n",
|
||||||
"from langchain.agents import initialize_agent"
|
"from langchain.agents import initialize_agent\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -72,7 +73,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"llm=ChatOpenAI(temperature=0)\n",
|
"llm=ChatOpenAI(temperature=0)\n",
|
||||||
"agent_chain = initialize_agent(tools, llm, agent=\"chat-conversational-react-description\", verbose=True, memory=memory)"
|
"agent_chain = initialize_agent(tools, llm, agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=memory)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.agents import Tool\n",
|
"from langchain.agents import Tool\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"from langchain.memory import ConversationBufferMemory\n",
|
"from langchain.memory import ConversationBufferMemory\n",
|
||||||
"from langchain import OpenAI\n",
|
"from langchain import OpenAI\n",
|
||||||
"from langchain.utilities import GoogleSearchAPIWrapper\n",
|
"from langchain.utilities import GoogleSearchAPIWrapper\n",
|
||||||
@ -61,7 +62,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"llm=OpenAI(temperature=0)\n",
|
"llm=OpenAI(temperature=0)\n",
|
||||||
"agent_chain = initialize_agent(tools, llm, agent=\"conversational-react-description\", verbose=True, memory=memory)"
|
"agent_chain = initialize_agent(tools, llm, agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=memory)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -27,7 +27,8 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain import LLMMathChain, OpenAI, SerpAPIWrapper, SQLDatabase, SQLDatabaseChain\n",
|
"from langchain import LLMMathChain, OpenAI, SerpAPIWrapper, SQLDatabase, SQLDatabaseChain\n",
|
||||||
"from langchain.agents import initialize_agent, Tool"
|
"from langchain.agents import initialize_agent, Tool\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -68,7 +69,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"mrkl = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
|
"mrkl = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from langchain import OpenAI, LLMMathChain, SerpAPIWrapper, SQLDatabase, SQLDatabaseChain\n",
|
"from langchain import OpenAI, LLMMathChain, SerpAPIWrapper, SQLDatabase, SQLDatabaseChain\n",
|
||||||
"from langchain.agents import initialize_agent, Tool\n",
|
"from langchain.agents import initialize_agent, Tool\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"from langchain.chat_models import ChatOpenAI"
|
"from langchain.chat_models import ChatOpenAI"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -70,7 +71,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"mrkl = initialize_agent(tools, llm, agent=\"chat-zero-shot-react-description\", verbose=True)"
|
"mrkl = initialize_agent(tools, llm, agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from langchain import OpenAI, Wikipedia\n",
|
"from langchain import OpenAI, Wikipedia\n",
|
||||||
"from langchain.agents import initialize_agent, Tool\n",
|
"from langchain.agents import initialize_agent, Tool\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"from langchain.agents.react.base import DocstoreExplorer\n",
|
"from langchain.agents.react.base import DocstoreExplorer\n",
|
||||||
"docstore=DocstoreExplorer(Wikipedia())\n",
|
"docstore=DocstoreExplorer(Wikipedia())\n",
|
||||||
"tools = [\n",
|
"tools = [\n",
|
||||||
@ -35,7 +36,7 @@
|
|||||||
"]\n",
|
"]\n",
|
||||||
"\n",
|
"\n",
|
||||||
"llm = OpenAI(temperature=0, model_name=\"text-davinci-002\")\n",
|
"llm = OpenAI(temperature=0, model_name=\"text-davinci-002\")\n",
|
||||||
"react = initialize_agent(tools, llm, agent=\"react-docstore\", verbose=True)"
|
"react = initialize_agent(tools, llm, agent=AgentType.REACT_DOCSTORE, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -46,6 +46,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from langchain import OpenAI, SerpAPIWrapper\n",
|
"from langchain import OpenAI, SerpAPIWrapper\n",
|
||||||
"from langchain.agents import initialize_agent, Tool\n",
|
"from langchain.agents import initialize_agent, Tool\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"\n",
|
"\n",
|
||||||
"llm = OpenAI(temperature=0)\n",
|
"llm = OpenAI(temperature=0)\n",
|
||||||
"search = SerpAPIWrapper()\n",
|
"search = SerpAPIWrapper()\n",
|
||||||
@ -57,7 +58,7 @@
|
|||||||
" )\n",
|
" )\n",
|
||||||
"]\n",
|
"]\n",
|
||||||
"\n",
|
"\n",
|
||||||
"self_ask_with_search = initialize_agent(tools, llm, agent=\"self-ask-with-search\", verbose=True)\n",
|
"self_ask_with_search = initialize_agent(tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True)\n",
|
||||||
"self_ask_with_search.run(\"What is the hometown of the reigning men's U.S. Open champion?\")"
|
"self_ask_with_search.run(\"What is the hometown of the reigning men's U.S. Open champion?\")"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
|
"agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# Import things that are needed generically\n",
|
"# Import things that are needed generically\n",
|
||||||
"from langchain.agents import initialize_agent, Tool\n",
|
"from langchain.agents import initialize_agent, Tool\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"from langchain.tools import BaseTool\n",
|
"from langchain.tools import BaseTool\n",
|
||||||
"from langchain.llms import OpenAI\n",
|
"from langchain.llms import OpenAI\n",
|
||||||
"from langchain import LLMMathChain, SerpAPIWrapper"
|
"from langchain import LLMMathChain, SerpAPIWrapper"
|
||||||
@ -102,7 +103,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# Construct the agent. We will use the default agent type here.\n",
|
"# Construct the agent. We will use the default agent type here.\n",
|
||||||
"# See documentation for a full list of options.\n",
|
"# See documentation for a full list of options.\n",
|
||||||
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
|
"agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -217,7 +218,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
|
"agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -410,7 +411,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
|
"agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -484,6 +485,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# Import things that are needed generically\n",
|
"# Import things that are needed generically\n",
|
||||||
"from langchain.agents import initialize_agent, Tool\n",
|
"from langchain.agents import initialize_agent, Tool\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"from langchain.llms import OpenAI\n",
|
"from langchain.llms import OpenAI\n",
|
||||||
"from langchain import LLMMathChain, SerpAPIWrapper\n",
|
"from langchain import LLMMathChain, SerpAPIWrapper\n",
|
||||||
"search = SerpAPIWrapper()\n",
|
"search = SerpAPIWrapper()\n",
|
||||||
@ -500,7 +502,7 @@
|
|||||||
" )\n",
|
" )\n",
|
||||||
"]\n",
|
"]\n",
|
||||||
"\n",
|
"\n",
|
||||||
"agent = initialize_agent(tools, OpenAI(temperature=0), agent=\"zero-shot-react-description\", verbose=True)"
|
"agent = initialize_agent(tools, OpenAI(temperature=0), agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -576,7 +578,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"llm = OpenAI(temperature=0)\n",
|
"llm = OpenAI(temperature=0)\n",
|
||||||
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
|
"agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from langchain.chat_models import ChatOpenAI\n",
|
"from langchain.chat_models import ChatOpenAI\n",
|
||||||
"from langchain.agents import load_tools, initialize_agent\n",
|
"from langchain.agents import load_tools, initialize_agent\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"from langchain.tools import AIPluginTool"
|
"from langchain.tools import AIPluginTool"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -83,7 +84,7 @@
|
|||||||
"tools = load_tools([\"requests\"] )\n",
|
"tools = load_tools([\"requests\"] )\n",
|
||||||
"tools += [tool]\n",
|
"tools += [tool]\n",
|
||||||
"\n",
|
"\n",
|
||||||
"agent_chain = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)\n",
|
"agent_chain = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION verbose=True)\n",
|
||||||
"agent_chain.run(\"what t shirts are available in klarna?\")"
|
"agent_chain.run(\"what t shirts are available in klarna?\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -115,6 +115,7 @@
|
|||||||
"from langchain.utilities import GoogleSerperAPIWrapper\n",
|
"from langchain.utilities import GoogleSerperAPIWrapper\n",
|
||||||
"from langchain.llms.openai import OpenAI\n",
|
"from langchain.llms.openai import OpenAI\n",
|
||||||
"from langchain.agents import initialize_agent, Tool\n",
|
"from langchain.agents import initialize_agent, Tool\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"\n",
|
"\n",
|
||||||
"llm = OpenAI(temperature=0)\n",
|
"llm = OpenAI(temperature=0)\n",
|
||||||
"search = GoogleSerperAPIWrapper()\n",
|
"search = GoogleSerperAPIWrapper()\n",
|
||||||
@ -126,7 +127,7 @@
|
|||||||
" )\n",
|
" )\n",
|
||||||
"]\n",
|
"]\n",
|
||||||
"\n",
|
"\n",
|
||||||
"self_ask_with_search = initialize_agent(tools, llm, agent=\"self-ask-with-search\", verbose=True)\n",
|
"self_ask_with_search = initialize_agent(tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True)\n",
|
||||||
"self_ask_with_search.run(\"What is the hometown of the reigning men's U.S. Open champion?\")"
|
"self_ask_with_search.run(\"What is the hometown of the reigning men's U.S. Open champion?\")"
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
"from langchain.chat_models import ChatOpenAI\n",
|
"from langchain.chat_models import ChatOpenAI\n",
|
||||||
"from langchain.llms import OpenAI\n",
|
"from langchain.llms import OpenAI\n",
|
||||||
"from langchain.agents import load_tools, initialize_agent\n",
|
"from langchain.agents import load_tools, initialize_agent\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"\n",
|
"\n",
|
||||||
"llm = ChatOpenAI(temperature=0.0)\n",
|
"llm = ChatOpenAI(temperature=0.0)\n",
|
||||||
"math_llm = OpenAI(temperature=0.0)\n",
|
"math_llm = OpenAI(temperature=0.0)\n",
|
||||||
@ -31,7 +32,7 @@
|
|||||||
"agent_chain = initialize_agent(\n",
|
"agent_chain = initialize_agent(\n",
|
||||||
" tools,\n",
|
" tools,\n",
|
||||||
" llm,\n",
|
" llm,\n",
|
||||||
" agent=\"zero-shot-react-description\",\n",
|
" agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",
|
||||||
" verbose=True,\n",
|
" verbose=True,\n",
|
||||||
")"
|
")"
|
||||||
]
|
]
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from langchain.agents import load_tools\n",
|
"from langchain.agents import load_tools\n",
|
||||||
"from langchain.agents import initialize_agent\n",
|
"from langchain.agents import initialize_agent\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"from langchain.llms import OpenAI"
|
"from langchain.llms import OpenAI"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -63,7 +64,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
|
"agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -131,7 +132,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
|
"agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -199,7 +200,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
|
"agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -266,7 +267,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
|
"agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -77,6 +77,7 @@
|
|||||||
"from langchain.llms import OpenAI\n",
|
"from langchain.llms import OpenAI\n",
|
||||||
"from langchain.agents import initialize_agent\n",
|
"from langchain.agents import initialize_agent\n",
|
||||||
"from langchain.agents.agent_toolkits import ZapierToolkit\n",
|
"from langchain.agents.agent_toolkits import ZapierToolkit\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"from langchain.utilities.zapier import ZapierNLAWrapper"
|
"from langchain.utilities.zapier import ZapierNLAWrapper"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -105,7 +106,7 @@
|
|||||||
"llm = OpenAI(temperature=0)\n",
|
"llm = OpenAI(temperature=0)\n",
|
||||||
"zapier = ZapierNLAWrapper()\n",
|
"zapier = ZapierNLAWrapper()\n",
|
||||||
"toolkit = ZapierToolkit.from_zapier_nla_wrapper(zapier)\n",
|
"toolkit = ZapierToolkit.from_zapier_nla_wrapper(zapier)\n",
|
||||||
"agent = initialize_agent(toolkit.get_tools(), llm, agent=\"zero-shot-react-description\", verbose=True)"
|
"agent = initialize_agent(toolkit.get_tools(), llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -23,7 +23,8 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.llms import OpenAI\n",
|
"from langchain.llms import OpenAI\n",
|
||||||
"from langchain.agents import initialize_agent, Tool"
|
"from langchain.agents import initialize_agent, Tool\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -64,7 +65,7 @@
|
|||||||
" description=\"useful for when you need to multiply two numbers together. The input to this tool should be a comma separated list of numbers of length two, representing the two numbers you want to multiply together. For example, `1,2` would be the input if you wanted to multiply 1 by 2.\"\n",
|
" description=\"useful for when you need to multiply two numbers together. The input to this tool should be a comma separated list of numbers of length two, representing the two numbers you want to multiply together. For example, `1,2` would be the input if you wanted to multiply 1 by 2.\"\n",
|
||||||
" )\n",
|
" )\n",
|
||||||
"]\n",
|
"]\n",
|
||||||
"mrkl = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
|
"mrkl = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -31,7 +31,8 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.agents import load_tools\n",
|
"from langchain.agents import load_tools\n",
|
||||||
"from langchain.agents import initialize_agent"
|
"from langchain.agents import initialize_agent\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -65,7 +66,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
|
"agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -107,11 +107,12 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from langchain.agents import load_tools\n",
|
"from langchain.agents import load_tools\n",
|
||||||
"from langchain.agents import initialize_agent\n",
|
"from langchain.agents import initialize_agent\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"from langchain.llms import OpenAI\n",
|
"from langchain.llms import OpenAI\n",
|
||||||
"\n",
|
"\n",
|
||||||
"llm = OpenAI(temperature=0)\n",
|
"llm = OpenAI(temperature=0)\n",
|
||||||
"tools = load_tools([\"serpapi\", \"llm-math\"], llm=llm)\n",
|
"tools = load_tools([\"serpapi\", \"llm-math\"], llm=llm)\n",
|
||||||
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)"
|
"agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -35,6 +35,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"import langchain\n",
|
"import langchain\n",
|
||||||
"from langchain.agents import Tool, initialize_agent, load_tools\n",
|
"from langchain.agents import Tool, initialize_agent, load_tools\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"from langchain.chat_models import ChatOpenAI\n",
|
"from langchain.chat_models import ChatOpenAI\n",
|
||||||
"from langchain.llms import OpenAI"
|
"from langchain.llms import OpenAI"
|
||||||
]
|
]
|
||||||
@ -93,7 +94,7 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"agent = initialize_agent(\n",
|
"agent = initialize_agent(\n",
|
||||||
" tools, llm, agent=\"zero-shot-react-description\", verbose=True\n",
|
" tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True\n",
|
||||||
")\n",
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"agent.run(\"What is 2 raised to .123243 power?\")"
|
"agent.run(\"What is 2 raised to .123243 power?\")"
|
||||||
@ -177,7 +178,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# Agent run with tracing using a chat model\n",
|
"# Agent run with tracing using a chat model\n",
|
||||||
"agent = initialize_agent(\n",
|
"agent = initialize_agent(\n",
|
||||||
" tools, ChatOpenAI(temperature=0), agent=\"chat-zero-shot-react-description\", verbose=True\n",
|
" tools, ChatOpenAI(temperature=0), agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True\n",
|
||||||
")\n",
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"agent.run(\"What is 2 raised to .123243 power?\")"
|
"agent.run(\"What is 2 raised to .123243 power?\")"
|
||||||
|
@ -85,9 +85,10 @@
|
|||||||
"from langchain.llms import OpenAI\n",
|
"from langchain.llms import OpenAI\n",
|
||||||
"from langchain.chains import LLMMathChain\n",
|
"from langchain.chains import LLMMathChain\n",
|
||||||
"from langchain.agents import initialize_agent, Tool, load_tools\n",
|
"from langchain.agents import initialize_agent, Tool, load_tools\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"\n",
|
"\n",
|
||||||
"tools = load_tools(['serpapi', 'llm-math'], llm=OpenAI(temperature=0))\n",
|
"tools = load_tools(['serpapi', 'llm-math'], llm=OpenAI(temperature=0))\n",
|
||||||
"agent = initialize_agent(tools, OpenAI(temperature=0), agent=\"zero-shot-react-description\")\n"
|
"agent = initialize_agent(tools, OpenAI(temperature=0), agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION)\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -255,6 +255,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.agents import initialize_agent, Tool\n",
|
"from langchain.agents import initialize_agent, Tool\n",
|
||||||
|
"from langchain.agents.agent_types import AgentType\n",
|
||||||
"tools = [\n",
|
"tools = [\n",
|
||||||
" Tool(\n",
|
" Tool(\n",
|
||||||
" name = \"State of Union QA System\",\n",
|
" name = \"State of Union QA System\",\n",
|
||||||
@ -276,7 +277,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"agent = initialize_agent(tools, OpenAI(temperature=0), agent=\"zero-shot-react-description\", max_iterations=3)"
|
"agent = initialize_agent(tools, OpenAI(temperature=0), agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, max_iterations=3)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
10
langchain/agents/agent_types.py
Normal file
10
langchain/agents/agent_types.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
|
||||||
|
class AgentType(str, Enum):
|
||||||
|
ZERO_SHOT_REACT_DESCRIPTION = "zero-shot-react-description"
|
||||||
|
REACT_DOCSTORE = "react-docstore"
|
||||||
|
SELF_ASK_WITH_SEARCH = "self-ask-with-search"
|
||||||
|
CONVERSATIONAL_REACT_DESCRIPTION = "conversational-react-description"
|
||||||
|
CHAT_ZERO_SHOT_REACT_DESCRIPTION = "chat-zero-shot-react-description"
|
||||||
|
CHAT_CONVERSATIONAL_REACT_DESCRIPTION = "chat-conversational-react-description"
|
@ -5,6 +5,7 @@ import re
|
|||||||
from typing import Any, List, Optional, Sequence, Tuple
|
from typing import Any, List, Optional, Sequence, Tuple
|
||||||
|
|
||||||
from langchain.agents.agent import Agent
|
from langchain.agents.agent import Agent
|
||||||
|
from langchain.agents.agent_types import AgentType
|
||||||
from langchain.agents.conversational.prompt import FORMAT_INSTRUCTIONS, PREFIX, SUFFIX
|
from langchain.agents.conversational.prompt import FORMAT_INSTRUCTIONS, PREFIX, SUFFIX
|
||||||
from langchain.callbacks.base import BaseCallbackManager
|
from langchain.callbacks.base import BaseCallbackManager
|
||||||
from langchain.chains import LLMChain
|
from langchain.chains import LLMChain
|
||||||
@ -21,7 +22,7 @@ class ConversationalAgent(Agent):
|
|||||||
@property
|
@property
|
||||||
def _agent_type(self) -> str:
|
def _agent_type(self) -> str:
|
||||||
"""Return Identifier of agent type."""
|
"""Return Identifier of agent type."""
|
||||||
return "conversational-react-description"
|
return AgentType.CONVERSATIONAL_REACT_DESCRIPTION
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def observation_prefix(self) -> str:
|
def observation_prefix(self) -> str:
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
from typing import Any, Optional, Sequence
|
from typing import Any, Optional, Sequence
|
||||||
|
|
||||||
from langchain.agents.agent import AgentExecutor
|
from langchain.agents.agent import AgentExecutor
|
||||||
|
from langchain.agents.agent_types import AgentType
|
||||||
from langchain.agents.loading import AGENT_TO_CLASS, load_agent
|
from langchain.agents.loading import AGENT_TO_CLASS, load_agent
|
||||||
from langchain.callbacks.base import BaseCallbackManager
|
from langchain.callbacks.base import BaseCallbackManager
|
||||||
from langchain.schema import BaseLanguageModel
|
from langchain.schema import BaseLanguageModel
|
||||||
@ -11,7 +12,7 @@ from langchain.tools.base import BaseTool
|
|||||||
def initialize_agent(
|
def initialize_agent(
|
||||||
tools: Sequence[BaseTool],
|
tools: Sequence[BaseTool],
|
||||||
llm: BaseLanguageModel,
|
llm: BaseLanguageModel,
|
||||||
agent: Optional[str] = None,
|
agent: Optional[AgentType] = None,
|
||||||
callback_manager: Optional[BaseCallbackManager] = None,
|
callback_manager: Optional[BaseCallbackManager] = None,
|
||||||
agent_path: Optional[str] = None,
|
agent_path: Optional[str] = None,
|
||||||
agent_kwargs: Optional[dict] = None,
|
agent_kwargs: Optional[dict] = None,
|
||||||
@ -22,15 +23,8 @@ def initialize_agent(
|
|||||||
Args:
|
Args:
|
||||||
tools: List of tools this agent has access to.
|
tools: List of tools this agent has access to.
|
||||||
llm: Language model to use as the agent.
|
llm: Language model to use as the agent.
|
||||||
agent: A string that specified the agent type to use. Valid options are:
|
agent: Agent type to use. If None and agent_path is also None, will default to
|
||||||
`zero-shot-react-description`
|
AgentType.ZERO_SHOT_REACT_DESCRIPTION.
|
||||||
`react-docstore`
|
|
||||||
`self-ask-with-search`
|
|
||||||
`conversational-react-description`
|
|
||||||
`chat-zero-shot-react-description`,
|
|
||||||
`chat-conversational-react-description`,
|
|
||||||
If None and agent_path is also None, will default to
|
|
||||||
`zero-shot-react-description`.
|
|
||||||
callback_manager: CallbackManager to use. Global callback manager is used if
|
callback_manager: CallbackManager to use. Global callback manager is used if
|
||||||
not provided. Defaults to None.
|
not provided. Defaults to None.
|
||||||
agent_path: Path to serialized agent to use.
|
agent_path: Path to serialized agent to use.
|
||||||
@ -41,7 +35,7 @@ def initialize_agent(
|
|||||||
An agent executor
|
An agent executor
|
||||||
"""
|
"""
|
||||||
if agent is None and agent_path is None:
|
if agent is None and agent_path is None:
|
||||||
agent = "zero-shot-react-description"
|
agent = AgentType.ZERO_SHOT_REACT_DESCRIPTION
|
||||||
if agent is not None and agent_path is not None:
|
if agent is not None and agent_path is not None:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Both `agent` and `agent_path` are specified, "
|
"Both `agent` and `agent_path` are specified, "
|
||||||
|
@ -6,6 +6,7 @@ from typing import Any, List, Optional, Union
|
|||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from langchain.agents.agent import Agent
|
from langchain.agents.agent import Agent
|
||||||
|
from langchain.agents.agent_types import AgentType
|
||||||
from langchain.agents.chat.base import ChatAgent
|
from langchain.agents.chat.base import ChatAgent
|
||||||
from langchain.agents.conversational.base import ConversationalAgent
|
from langchain.agents.conversational.base import ConversationalAgent
|
||||||
from langchain.agents.conversational_chat.base import ConversationalChatAgent
|
from langchain.agents.conversational_chat.base import ConversationalChatAgent
|
||||||
@ -18,12 +19,12 @@ from langchain.llms.base import BaseLLM
|
|||||||
from langchain.utilities.loading import try_load_from_hub
|
from langchain.utilities.loading import try_load_from_hub
|
||||||
|
|
||||||
AGENT_TO_CLASS = {
|
AGENT_TO_CLASS = {
|
||||||
"zero-shot-react-description": ZeroShotAgent,
|
AgentType.ZERO_SHOT_REACT_DESCRIPTION: ZeroShotAgent,
|
||||||
"react-docstore": ReActDocstoreAgent,
|
AgentType.REACT_DOCSTORE: ReActDocstoreAgent,
|
||||||
"self-ask-with-search": SelfAskWithSearchAgent,
|
AgentType.SELF_ASK_WITH_SEARCH: SelfAskWithSearchAgent,
|
||||||
"conversational-react-description": ConversationalAgent,
|
AgentType.CONVERSATIONAL_REACT_DESCRIPTION: ConversationalAgent,
|
||||||
"chat-zero-shot-react-description": ChatAgent,
|
AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION: ChatAgent,
|
||||||
"chat-conversational-react-description": ConversationalChatAgent,
|
AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION: ConversationalChatAgent,
|
||||||
}
|
}
|
||||||
|
|
||||||
URL_BASE = "https://raw.githubusercontent.com/hwchase17/langchain-hub/master/agents/"
|
URL_BASE = "https://raw.githubusercontent.com/hwchase17/langchain-hub/master/agents/"
|
||||||
|
@ -5,6 +5,7 @@ import re
|
|||||||
from typing import Any, Callable, List, NamedTuple, Optional, Sequence, Tuple
|
from typing import Any, Callable, List, NamedTuple, Optional, Sequence, Tuple
|
||||||
|
|
||||||
from langchain.agents.agent import Agent, AgentExecutor
|
from langchain.agents.agent import Agent, AgentExecutor
|
||||||
|
from langchain.agents.agent_types import AgentType
|
||||||
from langchain.agents.mrkl.prompt import FORMAT_INSTRUCTIONS, PREFIX, SUFFIX
|
from langchain.agents.mrkl.prompt import FORMAT_INSTRUCTIONS, PREFIX, SUFFIX
|
||||||
from langchain.agents.tools import Tool
|
from langchain.agents.tools import Tool
|
||||||
from langchain.callbacks.base import BaseCallbackManager
|
from langchain.callbacks.base import BaseCallbackManager
|
||||||
@ -56,7 +57,7 @@ class ZeroShotAgent(Agent):
|
|||||||
@property
|
@property
|
||||||
def _agent_type(self) -> str:
|
def _agent_type(self) -> str:
|
||||||
"""Return Identifier of agent type."""
|
"""Return Identifier of agent type."""
|
||||||
return "zero-shot-react-description"
|
return AgentType.ZERO_SHOT_REACT_DESCRIPTION
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def observation_prefix(self) -> str:
|
def observation_prefix(self) -> str:
|
||||||
|
@ -5,6 +5,7 @@ from typing import Any, List, Optional, Sequence, Tuple
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from langchain.agents.agent import Agent, AgentExecutor
|
from langchain.agents.agent import Agent, AgentExecutor
|
||||||
|
from langchain.agents.agent_types import AgentType
|
||||||
from langchain.agents.react.textworld_prompt import TEXTWORLD_PROMPT
|
from langchain.agents.react.textworld_prompt import TEXTWORLD_PROMPT
|
||||||
from langchain.agents.react.wiki_prompt import WIKI_PROMPT
|
from langchain.agents.react.wiki_prompt import WIKI_PROMPT
|
||||||
from langchain.agents.tools import Tool
|
from langchain.agents.tools import Tool
|
||||||
@ -21,7 +22,7 @@ class ReActDocstoreAgent(Agent, BaseModel):
|
|||||||
@property
|
@property
|
||||||
def _agent_type(self) -> str:
|
def _agent_type(self) -> str:
|
||||||
"""Return Identifier of agent type."""
|
"""Return Identifier of agent type."""
|
||||||
return "react-docstore"
|
return AgentType.REACT_DOCSTORE
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_prompt(cls, tools: Sequence[BaseTool]) -> BasePromptTemplate:
|
def create_prompt(cls, tools: Sequence[BaseTool]) -> BasePromptTemplate:
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
from typing import Any, Optional, Sequence, Tuple, Union
|
from typing import Any, Optional, Sequence, Tuple, Union
|
||||||
|
|
||||||
from langchain.agents.agent import Agent, AgentExecutor
|
from langchain.agents.agent import Agent, AgentExecutor
|
||||||
|
from langchain.agents.agent_types import AgentType
|
||||||
from langchain.agents.self_ask_with_search.prompt import PROMPT
|
from langchain.agents.self_ask_with_search.prompt import PROMPT
|
||||||
from langchain.agents.tools import Tool
|
from langchain.agents.tools import Tool
|
||||||
from langchain.llms.base import BaseLLM
|
from langchain.llms.base import BaseLLM
|
||||||
@ -17,7 +18,7 @@ class SelfAskWithSearchAgent(Agent):
|
|||||||
@property
|
@property
|
||||||
def _agent_type(self) -> str:
|
def _agent_type(self) -> str:
|
||||||
"""Return Identifier of agent type."""
|
"""Return Identifier of agent type."""
|
||||||
return "self-ask-with-search"
|
return AgentType.SELF_ASK_WITH_SEARCH
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_prompt(cls, tools: Sequence[BaseTool]) -> BasePromptTemplate:
|
def create_prompt(cls, tools: Sequence[BaseTool]) -> BasePromptTemplate:
|
||||||
|
@ -65,7 +65,7 @@ toolkit = ZapierToolkit.from_zapier_nla_wrapper(zapier)
|
|||||||
agent = initialize_agent(
|
agent = initialize_agent(
|
||||||
toolkit.get_tools(),
|
toolkit.get_tools(),
|
||||||
llm,
|
llm,
|
||||||
agent="zero-shot-react-description",
|
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
||||||
verbose=True
|
verbose=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ from typing import Any, List, Mapping, Optional
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from langchain.agents import AgentExecutor, initialize_agent
|
from langchain.agents import AgentExecutor, initialize_agent
|
||||||
|
from langchain.agents.agent_types import AgentType
|
||||||
from langchain.agents.tools import Tool
|
from langchain.agents.tools import Tool
|
||||||
from langchain.callbacks.base import CallbackManager
|
from langchain.callbacks.base import CallbackManager
|
||||||
from langchain.llms.base import LLM
|
from langchain.llms.base import LLM
|
||||||
@ -55,7 +56,11 @@ def _get_agent(**kwargs: Any) -> AgentExecutor:
|
|||||||
),
|
),
|
||||||
]
|
]
|
||||||
agent = initialize_agent(
|
agent = initialize_agent(
|
||||||
tools, fake_llm, agent="zero-shot-react-description", verbose=True, **kwargs
|
tools,
|
||||||
|
fake_llm,
|
||||||
|
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
||||||
|
verbose=True,
|
||||||
|
**kwargs,
|
||||||
)
|
)
|
||||||
return agent
|
return agent
|
||||||
|
|
||||||
@ -98,7 +103,7 @@ def test_agent_with_callbacks_global() -> None:
|
|||||||
agent = initialize_agent(
|
agent = initialize_agent(
|
||||||
tools,
|
tools,
|
||||||
fake_llm,
|
fake_llm,
|
||||||
agent="zero-shot-react-description",
|
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
||||||
verbose=True,
|
verbose=True,
|
||||||
callback_manager=manager,
|
callback_manager=manager,
|
||||||
)
|
)
|
||||||
@ -144,7 +149,7 @@ def test_agent_with_callbacks_local() -> None:
|
|||||||
agent = initialize_agent(
|
agent = initialize_agent(
|
||||||
tools,
|
tools,
|
||||||
fake_llm,
|
fake_llm,
|
||||||
agent="zero-shot-react-description",
|
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
||||||
verbose=True,
|
verbose=True,
|
||||||
callback_manager=manager,
|
callback_manager=manager,
|
||||||
)
|
)
|
||||||
@ -191,7 +196,7 @@ def test_agent_with_callbacks_not_verbose() -> None:
|
|||||||
agent = initialize_agent(
|
agent = initialize_agent(
|
||||||
tools,
|
tools,
|
||||||
fake_llm,
|
fake_llm,
|
||||||
agent="zero-shot-react-description",
|
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
||||||
callback_manager=manager,
|
callback_manager=manager,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -223,7 +228,7 @@ def test_agent_tool_return_direct() -> None:
|
|||||||
agent = initialize_agent(
|
agent = initialize_agent(
|
||||||
tools,
|
tools,
|
||||||
fake_llm,
|
fake_llm,
|
||||||
agent="zero-shot-react-description",
|
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
||||||
)
|
)
|
||||||
|
|
||||||
output = agent.run("when was langchain made")
|
output = agent.run("when was langchain made")
|
||||||
@ -249,7 +254,7 @@ def test_agent_tool_return_direct_in_intermediate_steps() -> None:
|
|||||||
agent = initialize_agent(
|
agent = initialize_agent(
|
||||||
tools,
|
tools,
|
||||||
fake_llm,
|
fake_llm,
|
||||||
agent="zero-shot-react-description",
|
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
||||||
return_intermediate_steps=True,
|
return_intermediate_steps=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -280,7 +285,7 @@ def test_agent_with_new_prefix_suffix() -> None:
|
|||||||
agent = initialize_agent(
|
agent = initialize_agent(
|
||||||
tools=tools,
|
tools=tools,
|
||||||
llm=fake_llm,
|
llm=fake_llm,
|
||||||
agent="zero-shot-react-description",
|
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
||||||
agent_kwargs={"prefix": prefix, "suffix": suffix},
|
agent_kwargs={"prefix": prefix, "suffix": suffix},
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -307,7 +312,7 @@ def test_agent_lookup_tool() -> None:
|
|||||||
agent = initialize_agent(
|
agent = initialize_agent(
|
||||||
tools=tools,
|
tools=tools,
|
||||||
llm=fake_llm,
|
llm=fake_llm,
|
||||||
agent="zero-shot-react-description",
|
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert agent.lookup_tool("Search") == tools[0]
|
assert agent.lookup_tool("Search") == tools[0]
|
||||||
|
Loading…
Reference in New Issue
Block a user