diff --git a/docs/docs/integrations/tools/graphql.ipynb b/docs/docs/integrations/tools/graphql.ipynb index bc0ded37bd0..cbc67d7fcf5 100644 --- a/docs/docs/integrations/tools/graphql.ipynb +++ b/docs/docs/integrations/tools/graphql.ipynb @@ -12,33 +12,49 @@ "\n", "This Jupyter Notebook demonstrates how to use the `GraphQLAPIWrapper` component with an Agent.\n", "\n", - "In this example, we'll be using the public `Star Wars GraphQL API` available at the following endpoint: https://swapi-graphql.netlify.app/.netlify/functions/index.\n", + "In this example, we'll be using the public `Star Wars GraphQL API` available at the following endpoint: https://swapi-graphql.netlify.app/graphql .\n", "\n", "First, you need to install `httpx` and `gql` Python packages." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "vscode": { "languageId": "shellscript" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], "source": [ "pip install httpx gql > /dev/null" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "vscode": { "languageId": "shellscript" } }, - "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 langchain-community" ] @@ -56,21 +72,36 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain.agents import AgentType, initialize_agent, load_tools\n", - "from langchain_openai import OpenAI\n", + "import os\n", "\n", - "llm = OpenAI(temperature=0)\n", + "os.environ[\"OPENAI_API_KEY\"] = \"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.agents import load_tools\n", "\n", "tools = load_tools(\n", " [\"graphql\"],\n", - " graphql_endpoint=\"https://swapi-graphql.netlify.app/.netlify/functions/index\",\n", - ")\n", - "\n", - "agent = initialize_agent(\n", - " tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True\n", + " graphql_endpoint=\"https://swapi-graphql.netlify.app/graphql\",\n", ")" ] }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "from langgraph.prebuilt import create_react_agent\n", + "\n", + "agent = create_react_agent(\"openai:gpt-4.1-mini\", tools)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -80,35 +111,55 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "================================\u001b[1m Human Message \u001b[0m=================================\n", + "\n", + "Search for the titles of all the stawars films stored in the graphql database that has this schema allFilms {\n", + " films {\n", + " title\n", + " director\n", + " releaseDate\n", + " speciesConnection {\n", + " species {\n", + " name\n", + " classification\n", + " homeworld {\n", + " name\n", + " }\n", + " }\n", + " }\n", + " }\n", + " }\n", "\n", "\n", - "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3m I need to query the graphql database to get the titles of all the star wars films\n", - "Action: query_graphql\n", - "Action Input: query { allFilms { films { title } } }\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3m\"{\\n \\\"allFilms\\\": {\\n \\\"films\\\": [\\n {\\n \\\"title\\\": \\\"A New Hope\\\"\\n },\\n {\\n \\\"title\\\": \\\"The Empire Strikes Back\\\"\\n },\\n {\\n \\\"title\\\": \\\"Return of the Jedi\\\"\\n },\\n {\\n \\\"title\\\": \\\"The Phantom Menace\\\"\\n },\\n {\\n \\\"title\\\": \\\"Attack of the Clones\\\"\\n },\\n {\\n \\\"title\\\": \\\"Revenge of the Sith\\\"\\n }\\n ]\\n }\\n}\"\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I now know the titles of all the star wars films\n", - "Final Answer: The titles of all the star wars films are: A New Hope, The Empire Strikes Back, Return of the Jedi, The Phantom Menace, Attack of the Clones, and Revenge of the Sith.\u001b[0m\n", + "==================================\u001b[1m Ai Message \u001b[0m==================================\n", + "Tool Calls:\n", + " query_graphql (call_tN5A0dBbfOMewuw8Yy13bYpW)\n", + " Call ID: call_tN5A0dBbfOMewuw8Yy13bYpW\n", + " Args:\n", + " tool_input: query { allFilms { films { title } } }\n", + "=================================\u001b[1m Tool Message \u001b[0m=================================\n", + "Name: query_graphql\n", "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" + "\"{\\n \\\"allFilms\\\": {\\n \\\"films\\\": [\\n {\\n \\\"title\\\": \\\"A New Hope\\\"\\n },\\n {\\n \\\"title\\\": \\\"The Empire Strikes Back\\\"\\n },\\n {\\n \\\"title\\\": \\\"Return of the Jedi\\\"\\n },\\n {\\n \\\"title\\\": \\\"The Phantom Menace\\\"\\n },\\n {\\n \\\"title\\\": \\\"Attack of the Clones\\\"\\n },\\n {\\n \\\"title\\\": \\\"Revenge of the Sith\\\"\\n }\\n ]\\n }\\n}\"\n", + "==================================\u001b[1m Ai Message \u001b[0m==================================\n", + "\n", + "The titles of all the Star Wars films stored in the database are:\n", + "1. A New Hope\n", + "2. The Empire Strikes Back\n", + "3. Return of the Jedi\n", + "4. The Phantom Menace\n", + "5. Attack of the Clones\n", + "6. Revenge of the Sith\n", + "\n", + "If you would like more information about any of these films, please let me know!\n" ] - }, - { - "data": { - "text/plain": [ - "'The titles of all the star wars films are: A New Hope, The Empire Strikes Back, Return of the Jedi, The Phantom Menace, Attack of the Clones, and Revenge of the Sith.'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" } ], "source": [ @@ -133,9 +184,24 @@ "\n", "suffix = \"Search for the titles of all the stawars films stored in the graphql database that has this schema \"\n", "\n", + "input_message = {\n", + " \"role\": \"user\",\n", + " \"content\": suffix + graphql_fields,\n", + "}\n", "\n", - "agent.run(suffix + graphql_fields)" + "for step in agent.stream(\n", + " {\"messages\": [input_message]},\n", + " stream_mode=\"values\",\n", + "):\n", + " step[\"messages\"][-1].pretty_print()" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -157,7 +223,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.12" + "version": "3.13.2" } }, "nbformat": 4, diff --git a/docs/docs/integrations/tools/openweathermap.ipynb b/docs/docs/integrations/tools/openweathermap.ipynb index caf3c41f8ef..46dd9541108 100644 --- a/docs/docs/integrations/tools/openweathermap.ipynb +++ b/docs/docs/integrations/tools/openweathermap.ipynb @@ -153,7 +153,7 @@ "source": [ "input_message = {\n", " \"role\": \"user\",\n", - " \"content\": (\"What's the weather like in London?\"),\n", + " \"content\": \"What's the weather like in London?\",\n", "}\n", "\n", "for step in agent.stream(\n", @@ -188,7 +188,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.13.3" + "version": "3.13.2" } }, "nbformat": 4, diff --git a/docs/docs/integrations/tools/yahoo_finance_news.ipynb b/docs/docs/integrations/tools/yahoo_finance_news.ipynb index a2da3dd4064..76613b0bf51 100644 --- a/docs/docs/integrations/tools/yahoo_finance_news.ipynb +++ b/docs/docs/integrations/tools/yahoo_finance_news.ipynb @@ -108,7 +108,7 @@ "source": [ "input_message = {\n", " \"role\": \"user\",\n", - " \"content\": (\"What happened today with Microsoft stocks?\"),\n", + " \"content\": \"What happened today with Microsoft stocks?\",\n", "}\n", "\n", "for step in agent.stream(\n", @@ -165,7 +165,7 @@ "source": [ "input_message = {\n", " \"role\": \"user\",\n", - " \"content\": (\"How does Microsoft feels today comparing with Nvidia?\"),\n", + " \"content\": \"How does Microsoft feels today comparing with Nvidia?\",\n", "}\n", "\n", "for step in agent.stream(\n", @@ -258,7 +258,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.13.3" + "version": "3.13.2" } }, "nbformat": 4,