Skip to content

Logging traces with Opik

For this guide we will be downloading the essays from Paul Graham and use them as our data source. We will then start querying these essays with LlamaIndex and logging the traces to Opik.

Comet provides a hosted version of the Opik platform, simply create an account and grab you API Key.

You can also run the Opik platform locally, see the installation guide for more information.

import os
import getpass
if "OPIK_API_KEY" not in os.environ:
os.environ["OPIK_API_KEY"] = getpass.getpass("Opik API Key: ")
if "OPIK_WORKSPACE" not in os.environ:
os.environ["OPIK_WORKSPACE"] = input(
"Comet workspace (often the same as your username): "
)

If you are running the Opik platform locally, simply set:

# import os
# os.environ["OPIK_URL_OVERRIDE"] = "http://localhost:5173/api"

First, we will install the necessary libraries, download the Chinook database and set up our different API keys.

%pip install opik llama-index llama-index-agent-openai llama-index-llms-openai --upgrade --quiet

And configure the required environment variables:

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

In addition, we will download the Paul Graham essays:

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

You can use the Opik callback directly by calling:

from llama_index.core import set_global_handler
# You should provide your OPIK API key and Workspace using the following environment variables:
# OPIK_API_KEY, OPIK_WORKSPACE
set_global_handler(
"opik",
)

Now that the callback handler is configured, all traces will automatically be logged to Opik.

The first step is to load the data into LlamaIndex. We will use the SimpleDirectoryReader to load the data from the data/paul_graham directory. We will also create the vector store to index all the loaded documents.

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
documents = SimpleDirectoryReader("./data/paul_graham").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()

We can now query the index using the query_engine object:

response = query_engine.query("What did the author do growing up?")
print(response)

You can now go to the Opik app to see the trace:

LlamaIndex trace in Opik

Note for AI agents: this documentation is built for programmatic access. - Overview of all docs: https://developers.llamaindex.ai/llms.txt - Any page is available as raw Markdown by appending index.md to its URL — e.g. https://developers.llamaindex.ai/llamaparse/parse/getting_started/index.md - Agent-friendly REST search APIs live under https://developers.llamaindex.ai/api/ — search (BM25 full-text), grep (regex), read (fetch a page), and list (browse the doc tree). See https://developers.llamaindex.ai/llms.txt for parameters. - A hosted documentation MCP server is available at https://developers.llamaindex.ai/mcp. If you support MCP, you can ask the user to install it for browsing these docs directly (an alternative to the REST API). Setup: https://developers.llamaindex.ai/python/shared/mcp/