From 395f0572435ffd892b8c8b70197d2c4c2ad78a1a Mon Sep 17 00:00:00 2001 From: Michael Li Date: Wed, 14 May 2025 04:56:11 +1000 Subject: [PATCH] docs: replace deprecated load_tools in google_finance.ipynb (#31220) Thank you for contributing to LangChain! - [x] **PR title**: "package: description" - Where "package" is whichever of langchain, core, etc. is being modified. Use "docs: ..." for purely docs changes, "infra: ..." for CI changes. - Example: "core: add foobar LLM" - [x] **PR message**: ***Delete this entire checklist*** and replace with - **Description:** a description of the change - **Issue:** the issue # it fixes, if applicable - **Dependencies:** any dependencies required for this change - **Twitter handle:** if your PR gets announced, and you'd like a mention, we'll gladly shout you out! - [x] **Add tests and docs**: If you're adding a new integration, please include 1. a test for the integration, preferably unit tests that do not rely on network access, 2. an example notebook showing its use. It lives in `docs/docs/integrations` directory. - [x] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/ Additional guidelines: - Make sure optional dependencies are imported within a function. - Please do not add dependencies to pyproject.toml files (even optional ones) unless they are required for unit tests. - Most PRs should not touch more than one package. - Changes should be backwards compatible. If no one reviews your PR within a few days, please @-mention one of baskaryan, eyurtsev, ccurme, vbarda, hwchase17. --- .../integrations/tools/google_finance.ipynb | 121 +++++++++++++----- 1 file changed, 90 insertions(+), 31 deletions(-) diff --git a/docs/docs/integrations/tools/google_finance.ipynb b/docs/docs/integrations/tools/google_finance.ipynb index 2a24395615d..fa43044a41c 100644 --- a/docs/docs/integrations/tools/google_finance.ipynb +++ b/docs/docs/integrations/tools/google_finance.ipynb @@ -15,11 +15,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], "source": [ - "%pip install --upgrade --quiet google-search-results langchain-community" + "%pip install --upgrade --quiet google-search-results langchain-community" ] }, { @@ -31,31 +39,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", - "from langchain_community.tools.google_finance import GoogleFinanceQueryRun\n", - "from langchain_community.utilities.google_finance import GoogleFinanceAPIWrapper\n", - "\n", - "os.environ[\"SERPAPI_API_KEY\"] = \"[your serpapi key]\"\n", - "tool = GoogleFinanceQueryRun(api_wrapper=GoogleFinanceAPIWrapper())" + "os.environ[\"SERPAPI_API_KEY\"] = \"\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, + "outputs": [], + "source": [ + "from langchain_community.tools.google_finance import GoogleFinanceQueryRun\n", + "from langchain_community.utilities.google_finance import GoogleFinanceAPIWrapper\n", + "\n", + "tool = GoogleFinanceQueryRun(api_wrapper=GoogleFinanceAPIWrapper())" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'\\nQuery: Google\\nstock: GOOGL:NASDAQ\\nprice: $161.96\\npercentage: 1.68\\nmovement: Up\\n'" + "'\\nQuery: Google\\nstock: GOOGL:NASDAQ\\nprice: $159.96\\npercentage: 0.94\\nmovement: Up\\nus: price = 42210.57, movement = Down\\neurope: price = 23638.56, movement = Up\\nasia: price = 38183.26, movement = Up\\n'" ] }, - "execution_count": 3, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -73,9 +89,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], "source": [ "%pip install --upgrade --quiet langgraph langchain-openai" ] @@ -89,7 +113,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "\n", + "os.environ[\"OPENAI_API_KEY\"] = \"\"\n", + "os.environ[\"SERP_API_KEY\"] = \"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.chat_models import init_chat_model\n", + "\n", + "llm = init_chat_model(\"gpt-4o-mini\", model_provider=\"openai\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_community.agent_toolkits.load_tools import load_tools\n", + "\n", + "tools = load_tools([\"google-scholar\", \"google-finance\"], llm=llm)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -101,8 +159,8 @@ "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", + " google_finance (call_8m0txCtxNuQaAv9UlomPhSA1)\n", + " Call ID: call_8m0txCtxNuQaAv9UlomPhSA1\n", " Args:\n", " query: Google\n", "=================================\u001b[1m Tool Message \u001b[0m=================================\n", @@ -111,28 +169,22 @@ "\n", "Query: Google\n", "stock: GOOGL:NASDAQ\n", - "price: $161.96\n", - "percentage: 1.68\n", + "price: $159.96\n", + "percentage: 0.94\n", "movement: Up\n", + "us: price = 42210.57, movement = Down\n", + "europe: price = 23638.56, movement = Up\n", + "asia: price = 38183.26, 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" + "Google's stock, listed as GOOGL on NASDAQ, is currently priced at $159.96, with a movement up by 0.94%.\n" ] } ], "source": [ - "import os\n", - "\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\"] = \"[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 = create_react_agent(llm, tools)\n", "\n", "events = agent.stream(\n", @@ -142,11 +194,18 @@ "for event in events:\n", " event[\"messages\"][-1].pretty_print()" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "venv", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -160,9 +219,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.7" + "version": "3.12.4" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 }