Use Stores with LangChain (Native Tool Calls)

In this quickstart, we will be creating a simple agent that can get the top posts on Hacker News.

While large language models can generate text, they need additional tools to perform actions like fetching data from Hacker News. Using Stores, we will add tools for querying the Hacker News API.

Hacker News agent

from langchain_google_genai import ChatGoogleGenerativeAI
import stores

# Load the Hacker News tool index
index = stores.Index(["silanthro/hackernews"])

# Initialize the model with tools
model = ChatGoogleGenerativeAI(model="gemini-2.0-flash-001")
model_with_tools = model.bind_tools(index.tools)

# Get the response from the model
response = model_with_tools.invoke("What are the top 10 posts on Hacker News today?")

# Execute the tool call
tool_call = response.tool_calls[0]
result = index.execute(tool_call["name"], tool_call["args"])
print(f"Tool output: {result}")

Tool calling steps

1. Load the tools

First, we will load the Hacker News tools from the silanthro/hackernews tool index.

index = stores.Index(["silanthro/hackernews"])

You can also load a tool index from a public GitHub repository or load your own custom tools from your repository. Learn more about what a tool index is here.

The Hacker News API doesn't require any API key. If a tool requires an API key, you can pass it via the env_var parameter.

2. Bind the tools to the model

Remember to add your Gemini API key (GOOGLE_API_KEY) to your .env file.

index.tools is a list of functions loaded in the index. This can be used directly in model.bind_tools because LangChain will automatically create the required function declaration JSON schema for us.

model = ChatGoogleGenerativeAI(model="gemini-2.0-flash-001")
model_with_tools = model.bind_tools(index.tools)

3. Call the model with tools

response = model_with_tools.invoke("What are the top 10 posts on Hacker News today?")

4. Get the tool name and arguments from the model

We can then parse response.tool_calls to retrieve the tool name and arguments.

response.tool_calls[0]
{
    "name": "tools.get_top_stories",
    "args": {
        "num": 10,
    },
    "id": "random-tool-use-uuid",
    "type": "tool_call"
}

5. Execute the function

Finally, we will use index.execute with the tool name and arguments to run the tool.

tool_call = response.tool_calls[0]
result = index.execute(tool_call["name"], tool_call["args"])

This gives us the tool call result. You can then supply the result to the model and call the model again to get its final response with the supplied information.

Next steps