mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-24 07:35:18 +00:00
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:
parent
325f729a92
commit
3e25a93136
@ -16,7 +16,7 @@
|
|||||||
"1. `pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib`\n",
|
"1. `pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib`\n",
|
||||||
"\n",
|
"\n",
|
||||||
"## Instructions for retrieving your Google Docs data\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",
|
"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",
|
"\n",
|
||||||
"`GoogleDriveSearchTool` can retrieve a selection of files with some requests. \n",
|
"`GoogleDriveSearchTool` can retrieve a selection of files with some requests. \n",
|
||||||
@ -47,7 +47,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 1,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@ -88,7 +88,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%pip install --upgrade --quiet unstructured"
|
"%pip install --upgrade --quiet unstructured langchain-googledrive"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -99,9 +99,13 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
|
"import os\n",
|
||||||
|
"\n",
|
||||||
"from langchain_googledrive.tools.google_drive.tool import GoogleDriveSearchTool\n",
|
"from langchain_googledrive.tools.google_drive.tool import GoogleDriveSearchTool\n",
|
||||||
"from langchain_googledrive.utilities.google_drive import GoogleDriveAPIWrapper\n",
|
"from langchain_googledrive.utilities.google_drive import GoogleDriveAPIWrapper\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"os.environ[\"GOOGLE_ACCOUNT_FILE\"] = \"custom/path/to/credentials.json\"\n",
|
||||||
|
"\n",
|
||||||
"# By default, search only in the filename.\n",
|
"# By default, search only in the filename.\n",
|
||||||
"tool = GoogleDriveSearchTool(\n",
|
"tool = GoogleDriveSearchTool(\n",
|
||||||
" api_wrapper=GoogleDriveAPIWrapper(\n",
|
" api_wrapper=GoogleDriveAPIWrapper(\n",
|
||||||
@ -114,7 +118,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 3,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@ -134,33 +138,52 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 5,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
"source": [
|
{
|
||||||
"tool.description"
|
"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,
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.agents import load_tools\n",
|
"tool.description"
|
||||||
"\n",
|
|
||||||
"tools = load_tools(\n",
|
|
||||||
" [\"google-drive-search\"],\n",
|
|
||||||
" folder_id=folder_id,\n",
|
|
||||||
" template=\"gdrive-query-in-folder\",\n",
|
|
||||||
")"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"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": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.agents import AgentType, initialize_agent\n",
|
"import os\n",
|
||||||
"from langchain_openai import OpenAI\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"llm = OpenAI(temperature=0)\n",
|
"from langchain.chat_models import init_chat_model\n",
|
||||||
"agent = initialize_agent(\n",
|
"from langgraph.prebuilt import create_react_agent\n",
|
||||||
" tools=tools,\n",
|
"\n",
|
||||||
" llm=llm,\n",
|
"os.environ[\"OPENAI_API_KEY\"] = \"your-openai-api-key\"\n",
|
||||||
" agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,\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",
|
||||||
"cell_type": "code",
|
"events = agent.stream(\n",
|
||||||
"execution_count": null,
|
" {\"messages\": [(\"user\", \"Search in google drive, who is 'Yann LeCun' ?\")]},\n",
|
||||||
"metadata": {
|
" stream_mode=\"values\",\n",
|
||||||
"tags": []
|
")\n",
|
||||||
},
|
"for event in events:\n",
|
||||||
"outputs": [],
|
" event[\"messages\"][-1].pretty_print()"
|
||||||
"source": [
|
|
||||||
"agent.run(\"Search in google drive, who is 'Yann LeCun' ?\")"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 3 (ipykernel)",
|
"display_name": "venv",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python3"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
@ -210,7 +230,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.10.12"
|
"version": "3.12.7"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
@ -6,24 +6,11 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# Google Finance\n",
|
"# Google Finance\n",
|
||||||
"\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",
|
"\n",
|
||||||
"To get an SerpApi key key, sign up at: https://serpapi.com/users/sign_up.\n",
|
"To get an SerpApi key key, sign up at: https://serpapi.com/users/sign_up.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Then install google-search-results with the command: \n",
|
"To use the tool with Langchain install following packages"
|
||||||
"\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"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -35,9 +22,16 @@
|
|||||||
"%pip install --upgrade --quiet google-search-results langchain-community"
|
"%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",
|
"cell_type": "code",
|
||||||
"execution_count": 9,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@ -46,15 +40,26 @@
|
|||||||
"from langchain_community.tools.google_finance import GoogleFinanceQueryRun\n",
|
"from langchain_community.tools.google_finance import GoogleFinanceQueryRun\n",
|
||||||
"from langchain_community.utilities.google_finance import GoogleFinanceAPIWrapper\n",
|
"from langchain_community.utilities.google_finance import GoogleFinanceAPIWrapper\n",
|
||||||
"\n",
|
"\n",
|
||||||
"os.environ[\"SERPAPI_API_KEY\"] = \"\"\n",
|
"os.environ[\"SERPAPI_API_KEY\"] = \"[your serpapi key]\"\n",
|
||||||
"tool = GoogleFinanceQueryRun(api_wrapper=GoogleFinanceAPIWrapper())"
|
"tool = GoogleFinanceQueryRun(api_wrapper=GoogleFinanceAPIWrapper())"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 3,
|
||||||
"metadata": {},
|
"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": [
|
"source": [
|
||||||
"tool.run(\"Google\")"
|
"tool.run(\"Google\")"
|
||||||
]
|
]
|
||||||
@ -63,7 +68,7 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"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,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"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": [
|
"source": [
|
||||||
"import os\n",
|
"import os\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from langchain.agents import AgentType, initialize_agent, load_tools\n",
|
"from langchain.agents import load_tools\n",
|
||||||
"from langchain_openai import OpenAI\n",
|
"from langchain.chat_models import init_chat_model\n",
|
||||||
|
"from langgraph.prebuilt import create_react_agent\n",
|
||||||
"\n",
|
"\n",
|
||||||
"os.environ[\"OPENAI_API_KEY\"] = \"\"\n",
|
"os.environ[\"OPENAI_API_KEY\"] = \"[your openai key]\"\n",
|
||||||
"os.environ[\"SERP_API_KEY\"] = \"\"\n",
|
"os.environ[\"SERP_API_KEY\"] = \"[your serpapi key]\"\n",
|
||||||
"llm = OpenAI()\n",
|
"\n",
|
||||||
|
"llm = init_chat_model(\"gpt-4o-mini\", model_provider=\"openai\")\n",
|
||||||
"tools = load_tools([\"google-scholar\", \"google-finance\"], llm=llm)\n",
|
"tools = load_tools([\"google-scholar\", \"google-finance\"], llm=llm)\n",
|
||||||
"agent = initialize_agent(\n",
|
"agent = create_react_agent(llm, tools)\n",
|
||||||
" tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True\n",
|
"\n",
|
||||||
|
"events = agent.stream(\n",
|
||||||
|
" {\"messages\": [(\"user\", \"What is Google's stock?\")]},\n",
|
||||||
|
" stream_mode=\"values\",\n",
|
||||||
")\n",
|
")\n",
|
||||||
"agent.run(\"what is google's stock\")"
|
"for event in events:\n",
|
||||||
|
" event[\"messages\"][-1].pretty_print()"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 3",
|
"display_name": "venv",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python3"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
@ -104,7 +160,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.9.5"
|
"version": "3.12.7"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user