.ipynb
.pdf
GraphQL tool
GraphQL tool#
This Jupyter Notebook demonstrates how to use the BaseGraphQLTool component with an Agent.
GraphQL is a query language for APIs and a runtime for executing those queries against your data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.
By including a BaseGraphQLTool in the list of tools provided to an Agent, you can grant your Agent the ability to query data from GraphQL APIs for any purposes you need.
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.
First, you need to install httpx and gql Python packages.
pip install httpx gql > /dev/null
Now, let’s create a BaseGraphQLTool instance with the specified Star Wars API endpoint and initialize an Agent with the tool.
from langchain import OpenAI
from langchain.agents import load_tools, initialize_agent, AgentType
from langchain.utilities import GraphQLAPIWrapper
llm = OpenAI(temperature=0)
tools = load_tools(["graphql"], graphql_endpoint="https://swapi-graphql.netlify.app/.netlify/functions/index", llm=llm)
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
Now, we can use the Agent to run queries against the Star Wars GraphQL API. Let’s ask the Agent to list all the Star Wars films and their release dates.
graphql_fields = """allFilms {
    films {
      title
      director
      releaseDate
      speciesConnection {
        species {
          name
          classification
          homeworld {
            name
          }
rtdocs_stable/api.python.langchain.com/en/stable/modules/agents/tools/examples/graphql.html