Describes how to track LlamaIndex pipelines using Opik

LlamaIndex is a flexible data framework for building LLM applications:

LlamaIndex is a “data framework” to help you build LLM apps. It provides the following tools:

  • Offers data connectors to ingest your existing data sources and data formats (APIs, PDFs, docs, SQL, etc.).
  • Provides ways to structure your data (indices, graphs) so that this data can be easily used with LLMs.
  • Provides an advanced retrieval/query interface over your data: Feed in any LLM input prompt, get back retrieved context and knowledge-augmented output.
  • Allows easy integrations with your outer application framework (e.g. with LangChain, Flask, Docker, ChatGPT, anything else).

You can check out the Colab Notebook if you’d like to jump straight to the code:

Open In Colab

Getting Started

To use the Opik integration with LlamaIndex, you’ll need to have both the opik and llama_index packages installed. You can install them using pip:

$pip install opik llama-index llama-index-agent-openai llama-index-llms-openai llama-index-callbacks-opik

In addition, you can configure Opik using the opik configure command which will prompt you for the correct local server address or if you are using the Cloud platform your API key:

$opik configure

Using the Opik integration

To use the Opik integration with LLamaIndex, you can use the set_global_handler function from the LlamaIndex package to set the global tracer:

1from llama_index.core import global_handler, set_global_handler
2
3set_global_handler("opik")
4opik_callback_handler = global_handler

Now that the integration is set up, all the LlamaIndex runs will be traced and logged to Opik.

Example

To showcase the integration, we will create a new a query engine that will use Paul Graham’s essays as the data source.

First step: Configure the Opik integration:

1from llama_index.core import global_handler, set_global_handler
2
3set_global_handler("opik")
4opik_callback_handler = global_handler

Second step: Download the example data:

1import os
2import requests
3
4# Create directory if it doesn't exist
5os.makedirs('./data/paul_graham/', exist_ok=True)
6
7# Download the file using requests
8url = 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt'
9response = requests.get(url)
10with open('./data/paul_graham/paul_graham_essay.txt', 'wb') as f:
11 f.write(response.content)

Third step:

Configure the OpenAI API key:

1import os
2import getpass
3
4if "OPENAI_API_KEY" not in os.environ:
5 os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter your OpenAI API key: ")

Fourth step:

We can now load the data, create an index and query engine:

1from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
2
3documents = SimpleDirectoryReader("./data/paul_graham").load_data()
4index = VectorStoreIndex.from_documents(documents)
5query_engine = index.as_query_engine()
6
7response = query_engine.query("What did the author do growing up?")
8print(response)

Given that the integration with Opik has been set up, all the traces are logged to the Opik platform:

Built with