Docs: replace initialize agent with create react agent for google tools (#31043)

- **Description:** The deprecated initialize_agent functionality is
replaced with create_react_agent for the google tools. Also noticed a
potential issue with the non-existent "google-drive-search" which was
used in the old `google-drive.ipynb`. If this should be a by default
available tool, an issue should be opened to modify
langchain-community's `load_tools` accordingly.
- **Issue:**  #29277
- **Dependencies:** No added dependencies
- **Twitter handle:** No Twitter account
This commit is contained in:
Haris Colic 2025-05-03 21:20:07 +02:00 committed by GitHub
parent 325f729a92
commit 3e25a93136
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 757 additions and 672 deletions

View File

@ -16,7 +16,7 @@
"1. `pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib`\n",
"\n",
"## Instructions for retrieving your Google Docs data\n",
"By default, the `GoogleDriveTools` and `GoogleDriveWrapper` expects the `credentials.json` file to be `~/.credentials/credentials.json`, but this is configurable using the `GOOGLE_ACCOUNT_FILE` environment variable. \n",
"By default, the `GoogleDriveTools` and `GoogleDriveWrapper` expects the `credentials.json` file to be `~/.credentials/credentials.json`, but this is configurable by setting the `GOOGLE_ACCOUNT_FILE` environment variable to your `custom/path/to/credentials.json`. \n",
"The location of `token.json` use the same directory (or use the parameter `token_path`). Note that `token.json` will be created automatically the first time you use the tool.\n",
"\n",
"`GoogleDriveSearchTool` can retrieve a selection of files with some requests. \n",
@ -47,7 +47,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
@ -88,7 +88,7 @@
"metadata": {},
"outputs": [],
"source": [
"%pip install --upgrade --quiet unstructured"
"%pip install --upgrade --quiet unstructured langchain-googledrive"
]
},
{
@ -99,9 +99,13 @@
},
"outputs": [],
"source": [
"import os\n",
"\n",
"from langchain_googledrive.tools.google_drive.tool import GoogleDriveSearchTool\n",
"from langchain_googledrive.utilities.google_drive import GoogleDriveAPIWrapper\n",
"\n",
"os.environ[\"GOOGLE_ACCOUNT_FILE\"] = \"custom/path/to/credentials.json\"\n",
"\n",
"# By default, search only in the filename.\n",
"tool = GoogleDriveSearchTool(\n",
" api_wrapper=GoogleDriveAPIWrapper(\n",
@ -114,7 +118,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
@ -134,33 +138,52 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"\"A wrapper around Google Drive Search. Useful for when you need to find a document in google drive. The input should be formatted as a list of entities separated with a space. As an example, a list of keywords is 'hello word'.\""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tool.description"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain.agents import load_tools\n",
"\n",
"tools = load_tools(\n",
" [\"google-drive-search\"],\n",
" folder_id=folder_id,\n",
" template=\"gdrive-query-in-folder\",\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use within an Agent"
"## Use the tool within a ReAct agent"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In order to create an agent that uses the Google Jobs tool install Langgraph"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install --upgrade --quiet langgraph langchain-openai"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and use the `create_react_agent` functionality to initialize a ReAct agent. You will also need to set up your OPEN_API_KEY (visit https://platform.openai.com) in order to access OpenAI's chat models."
]
},
{
@ -171,32 +194,29 @@
},
"outputs": [],
"source": [
"from langchain.agents import AgentType, initialize_agent\n",
"from langchain_openai import OpenAI\n",
"import os\n",
"\n",
"llm = OpenAI(temperature=0)\n",
"agent = initialize_agent(\n",
" tools=tools,\n",
" llm=llm,\n",
" agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"agent.run(\"Search in google drive, who is 'Yann LeCun' ?\")"
"from langchain.chat_models import init_chat_model\n",
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"your-openai-api-key\"\n",
"\n",
"\n",
"llm = init_chat_model(\"gpt-4o-mini\", model_provider=\"openai\", temperature=0)\n",
"agent = create_react_agent(llm, tools=[tool])\n",
"\n",
"events = agent.stream(\n",
" {\"messages\": [(\"user\", \"Search in google drive, who is 'Yann LeCun' ?\")]},\n",
" stream_mode=\"values\",\n",
")\n",
"for event in events:\n",
" event[\"messages\"][-1].pretty_print()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "venv",
"language": "python",
"name": "python3"
},
@ -210,7 +230,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.12.7"
}
},
"nbformat": 4,

View File

@ -6,24 +6,11 @@
"source": [
"# Google Finance\n",
"\n",
"This notebook goes over how to use the Google Finance Tool to get information from the Google Finance page\n",
"This notebook goes over how to use the Google Finance Tool to get information from the Google Finance page.\n",
"\n",
"To get an SerpApi key key, sign up at: https://serpapi.com/users/sign_up.\n",
"\n",
"Then install google-search-results with the command: \n",
"\n",
"pip install google-search-results\n",
"\n",
"Then set the environment variable SERPAPI_API_KEY to your SerpApi key\n",
"\n",
"Or pass the key in as a argument to the wrapper serp_api_key=\"your secret key\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use the Tool"
"To use the tool with Langchain install following packages"
]
},
{
@ -35,9 +22,16 @@
"%pip install --upgrade --quiet google-search-results langchain-community"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then set the environment variable SERPAPI_API_KEY to your SerpApi key or pass the key in as a argument to the wrapper serp_api_key=\"your secret key\"."
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -46,15 +40,26 @@
"from langchain_community.tools.google_finance import GoogleFinanceQueryRun\n",
"from langchain_community.utilities.google_finance import GoogleFinanceAPIWrapper\n",
"\n",
"os.environ[\"SERPAPI_API_KEY\"] = \"\"\n",
"os.environ[\"SERPAPI_API_KEY\"] = \"[your serpapi key]\"\n",
"tool = GoogleFinanceQueryRun(api_wrapper=GoogleFinanceAPIWrapper())"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'\\nQuery: Google\\nstock: GOOGL:NASDAQ\\nprice: $161.96\\npercentage: 1.68\\nmovement: Up\\n'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tool.run(\"Google\")"
]
@ -63,7 +68,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Using it with Langchain"
"In order to create an agent that uses the Google Finance tool install Langgraph"
]
},
{
@ -71,26 +76,77 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install --upgrade --quiet langgraph langchain-openai"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and use the `create_react_agent` functionality to initialize a ReAct agent. You will also need to set up your OPEN_API_KEY (visit https://platform.openai.com) in order to access OpenAI's chat models."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"================================\u001b[1m Human Message \u001b[0m=================================\n",
"\n",
"What is Google's stock?\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"Tool Calls:\n",
" google_finance (call_u676mJAkdojgkW806ZGSE8mF)\n",
" Call ID: call_u676mJAkdojgkW806ZGSE8mF\n",
" Args:\n",
" query: Google\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"Name: google_finance\n",
"\n",
"\n",
"Query: Google\n",
"stock: GOOGL:NASDAQ\n",
"price: $161.96\n",
"percentage: 1.68\n",
"movement: Up\n",
"\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"Google's stock (Ticker: GOOGL) is currently priced at $161.96, showing an increase of 1.68%.\n"
]
}
],
"source": [
"import os\n",
"\n",
"from langchain.agents import AgentType, initialize_agent, load_tools\n",
"from langchain_openai import OpenAI\n",
"from langchain.agents import load_tools\n",
"from langchain.chat_models import init_chat_model\n",
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"\"\n",
"os.environ[\"SERP_API_KEY\"] = \"\"\n",
"llm = OpenAI()\n",
"os.environ[\"OPENAI_API_KEY\"] = \"[your openai key]\"\n",
"os.environ[\"SERP_API_KEY\"] = \"[your serpapi key]\"\n",
"\n",
"llm = init_chat_model(\"gpt-4o-mini\", model_provider=\"openai\")\n",
"tools = load_tools([\"google-scholar\", \"google-finance\"], llm=llm)\n",
"agent = initialize_agent(\n",
" tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True\n",
"agent = create_react_agent(llm, tools)\n",
"\n",
"events = agent.stream(\n",
" {\"messages\": [(\"user\", \"What is Google's stock?\")]},\n",
" stream_mode=\"values\",\n",
")\n",
"agent.run(\"what is google's stock\")"
"for event in events:\n",
" event[\"messages\"][-1].pretty_print()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "venv",
"language": "python",
"name": "python3"
},
@ -104,7 +160,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.5"
"version": "3.12.7"
}
},
"nbformat": 4,

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff