Analyzing Blockchain Data with LLMs and Subsquid

Using LLMs with indexing tools unlocks new ways to analyze blockchain data.

In this blog, I’ll show you how to use LlamaIndex, Langchain and Subsquid SDK to build your own blockchain data agent.

Subsquid is a blockchain indexing SDK and a distributed data lake optimized for serving large volumes of historical on-chain data from over 100+ chains. It also provides out-of-the-box GraphQL API endpoint, enabling the agent to query the deployed squid.

To follow this tutorial, you will need OpenAI API key. You can also find the repo with a Python notebook and try it yourself here.

First, we need to create and deploy a squid that indexes Uniswap swap events. You can use this squid to follow along. It indexes Uniswap pools and swaps with the following schema:

Now we are going to implement the tools for the agent to use. In this snippet, we define our agent that’s capable of holding a conversation and calling tools in under 50 lines of code. Source code can be found here.

We also implement a function, that makes GraphQL request to the squid’s GraphQL endpoint.

In addition, we implement the introspection function, that allows our agent to explore the schema of the GraphQl endpoint.

Next, in a few lines of code we turn the functions into tools that can be used by our agent and call the agent with the desired prompt.

The queries can be structured, unstructured or both. Here’s the sample result of calling the agent on a squid, that indexes swap events on Uniswap.

We can also integrate Langchain into the project. The workflow is similar and we can use the functions we implemented before to make tools for the agent.

Next, we initialize the agent with tools and run it with out prompt.

Once adapted to LangChain, you can use the agent to conduct in-depth analysis over any schema, providing insights on available entities, fields, and combinations for querying.

In this example, we use the LangChain agent to propose metrics for analysis of the swap events.

This tutorial was inspired by a thread on how to query Subgraphs using AI.

In short, combining Language Learning Models (LLMs) with tools like LlamaIndex, Langchain, and Subsquid SDK opens new possibilities for data analysis. We demonstrated creating a smart blockchain data agent that can deeply analyze Uniswap swap events across various chains. By integrating Subsquid's capabilities and GraphQL APIs, the agent efficiently extracted valuable insights. The addition of Langchain expanded the analysis possibilities, making the process versatile and adaptable. This approach showcases the practical fusion of AI and blockchain tech, enabling deeper insights without unnecessary complexity.