mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-28 17:38:36 +00:00
docs: replace deprecated initialize_agent
with create_react_agent
and non-deprecated functions (#31361)
**Description:** This PR updates approximately 4' occurences of the deprecated `initialize_agent` function in LangChain documentation and examples, replacing it with the recommended `create_react_agent` and pattern. It also refactors related examples to align with current best practices. **Issue:** Partially Fixes #29277 **Dependencies:** None **X handle:** @TK1475 --------- Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
This commit is contained in:
parent
4ec46aeb73
commit
81db124351
@ -22,16 +22,16 @@ You can use it as part of a Self Ask chain:
|
||||
|
||||
```python
|
||||
from langchain_community.utilities import GoogleSerperAPIWrapper
|
||||
from langchain_openai import OpenAI
|
||||
from langchain.agents import initialize_agent, Tool
|
||||
from langchain.agents import AgentType
|
||||
from langchain_core.tools import Tool
|
||||
from langchain_openai import ChatOpenAI
|
||||
from langgraph.prebuilt import create_react_agent
|
||||
|
||||
import os
|
||||
|
||||
os.environ["SERPER_API_KEY"] = ""
|
||||
os.environ['OPENAI_API_KEY'] = ""
|
||||
|
||||
llm = OpenAI(temperature=0)
|
||||
llm = ChatOpenAI(temperature=0)
|
||||
search = GoogleSerperAPIWrapper()
|
||||
tools = [
|
||||
Tool(
|
||||
@ -41,8 +41,13 @@ tools = [
|
||||
)
|
||||
]
|
||||
|
||||
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?")
|
||||
agent = create_react_agent(llm, tools)
|
||||
|
||||
result = agent.invoke({
|
||||
"messages": [("human", "What is the hometown of the reigning men's U.S. Open champion?")]
|
||||
})
|
||||
|
||||
print(result)
|
||||
```
|
||||
|
||||
#### Output
|
||||
|
@ -22,13 +22,12 @@ You can use it as part of a Self Ask chain:
|
||||
```python
|
||||
from langchain_community.utilities import SearchApiAPIWrapper
|
||||
from langchain_openai import OpenAI
|
||||
from langchain.agents import initialize_agent, Tool
|
||||
from langchain.agents import AgentType
|
||||
from langchain.agents import Tool, create_openai_functions_agent, AgentExecutor
|
||||
|
||||
import os
|
||||
|
||||
os.environ["SEARCHAPI_API_KEY"] = ""
|
||||
os.environ['OPENAI_API_KEY'] = ""
|
||||
os.environ["SEARCHAPI_API_KEY"] = "<your-searchapi-key>"
|
||||
os.environ['OPENAI_API_KEY'] = "<your-openai-key>"
|
||||
|
||||
llm = OpenAI(temperature=0)
|
||||
search = SearchApiAPIWrapper()
|
||||
@ -40,8 +39,13 @@ tools = [
|
||||
)
|
||||
]
|
||||
|
||||
self_ask_with_search = initialize_agent(tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True)
|
||||
self_ask_with_search.run("Who lived longer: Plato, Socrates, or Aristotle?")
|
||||
# Create agent and executor
|
||||
agent = create_openai_functions_agent(llm=llm, tools=tools)
|
||||
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
|
||||
|
||||
# Run the agent
|
||||
response = agent_executor.invoke({"input": "Who lived longer: Plato, Socrates, or Aristotle?"})
|
||||
print(response["output"])
|
||||
```
|
||||
|
||||
#### Output
|
||||
|
@ -52,18 +52,18 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": null,
|
||||
"id": "d41405b5",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.agents import AgentType, initialize_agent, load_tools\n",
|
||||
"from langchain.agents import AgentExecutor, create_react_agent, load_tools\n",
|
||||
"from langchain_openai import ChatOpenAI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": null,
|
||||
"id": "d9e61df5",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -73,7 +73,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": null,
|
||||
"id": "edc0ea0e",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -113,13 +113,15 @@
|
||||
],
|
||||
"source": [
|
||||
"llm = ChatOpenAI(temperature=0)\n",
|
||||
"\n",
|
||||
"tools = load_tools([\"requests_all\"])\n",
|
||||
"tools += [tool]\n",
|
||||
"\n",
|
||||
"agent_chain = initialize_agent(\n",
|
||||
" tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True\n",
|
||||
")\n",
|
||||
"agent_chain.run(\"what t shirts are available in klarna?\")"
|
||||
"agent = create_react_agent(llm=llm, tools=tools)\n",
|
||||
"\n",
|
||||
"agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)\n",
|
||||
"\n",
|
||||
"agent_executor.invoke({\"input\": \"what t shirts are available in klarna?\"})"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -133,7 +135,7 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"display_name": "AI_env",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
@ -147,7 +149,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.12"
|
||||
"version": "3.12.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
@ -32,6 +32,19 @@
|
||||
"%pip install -qU langchain-community"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "cf0cb2e5",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.agent_toolkits.nasa.toolkit import NasaToolkit\n",
|
||||
"from langchain_community.utilities.nasa import NasaAPIWrapper\n",
|
||||
"from langchain_openai import OpenAI\n",
|
||||
"from langgraph.prebuilt import create_react_agent"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@ -39,17 +52,12 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.agents import AgentType, initialize_agent\n",
|
||||
"from langchain_community.agent_toolkits.nasa.toolkit import NasaToolkit\n",
|
||||
"from langchain_community.utilities.nasa import NasaAPIWrapper\n",
|
||||
"from langchain_openai import OpenAI\n",
|
||||
"\n",
|
||||
"llm = OpenAI(temperature=0, openai_api_key=\"\")\n",
|
||||
"nasa = NasaAPIWrapper()\n",
|
||||
"toolkit = NasaToolkit.from_nasa_api_wrapper(nasa)\n",
|
||||
"agent = initialize_agent(\n",
|
||||
" toolkit.get_tools(), llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True\n",
|
||||
")"
|
||||
"tools = toolkit.get_tools()\n",
|
||||
"\n",
|
||||
"agent = create_react_agent(llm, tools)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -96,7 +104,7 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"display_name": "AI_env",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
@ -110,7 +118,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.5"
|
||||
"version": "3.12.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
Loading…
Reference in New Issue
Block a user