Revert "docs: replace deprecated initialize_agent with create_react_agent and non-deprecated functions" (#31492)

Reverts langchain-ai/langchain#31361
This commit is contained in:
Eugene Yurtsev 2025-06-04 10:42:47 -04:00 committed by GitHub
parent 222578b296
commit b149cce5f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 32 additions and 51 deletions

View File

@ -22,16 +22,16 @@ You can use it as part of a Self Ask chain:
```python
from langchain_community.utilities import GoogleSerperAPIWrapper
from langchain_core.tools import Tool
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
from langchain_openai import OpenAI
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
import os
os.environ["SERPER_API_KEY"] = ""
os.environ['OPENAI_API_KEY'] = ""
llm = ChatOpenAI(temperature=0)
llm = OpenAI(temperature=0)
search = GoogleSerperAPIWrapper()
tools = [
Tool(
@ -41,13 +41,8 @@ tools = [
)
]
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)
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?")
```
#### Output

View File

@ -22,12 +22,13 @@ 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 Tool, create_openai_functions_agent, AgentExecutor
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
import os
os.environ["SEARCHAPI_API_KEY"] = "<your-searchapi-key>"
os.environ['OPENAI_API_KEY'] = "<your-openai-key>"
os.environ["SEARCHAPI_API_KEY"] = ""
os.environ['OPENAI_API_KEY'] = ""
llm = OpenAI(temperature=0)
search = SearchApiAPIWrapper()
@ -39,13 +40,8 @@ tools = [
)
]
# 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"])
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?")
```
#### Output

View File

@ -52,18 +52,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "d41405b5",
"metadata": {},
"outputs": [],
"source": [
"from langchain.agents import AgentExecutor, create_react_agent, load_tools\n",
"from langchain.agents import AgentType, initialize_agent, load_tools\n",
"from langchain_openai import ChatOpenAI"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "d9e61df5",
"metadata": {},
"outputs": [],
@ -73,7 +73,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "edc0ea0e",
"metadata": {},
"outputs": [
@ -113,15 +113,13 @@
],
"source": [
"llm = ChatOpenAI(temperature=0)\n",
"\n",
"tools = load_tools([\"requests_all\"])\n",
"tools += [tool]\n",
"\n",
"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?\"})"
"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?\")"
]
},
{
@ -135,7 +133,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "AI_env",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@ -149,7 +147,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
"version": "3.10.12"
}
},
"nbformat": 4,

View File

@ -32,19 +32,6 @@
"%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,
@ -52,12 +39,17 @@
"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",
"tools = toolkit.get_tools()\n",
"\n",
"agent = create_react_agent(llm, tools)"
"agent = initialize_agent(\n",
" toolkit.get_tools(), llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True\n",
")"
]
},
{
@ -104,7 +96,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "AI_env",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@ -118,7 +110,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
"version": "3.11.5"
}
},
"nbformat": 4,