LlamaIndex

How to use the python LlamaIndexInstrumentor to trace LlamaIndex

LlamaIndex is a data framework for your LLM application. It's a powerful framework by which you can build an application that leverages RAG (retrieval-augmented generation) to super-charge an LLM with your own data. RAG is an extremely powerful LLM application model because it lets you harness the power of LLMs such as OpenAI's GPT but tuned to your data and use-case.

For LlamaIndex, tracing instrumentation is added via an OpenTelemetry instrumentor aptly named the LlamaIndexInstrumentor . This callback is what is used to create spans and send them to the Phoenix collector.

Phoenix supports LlamaIndex's latest instrumentation paradigm.

To get started, pip install the following.

pip install "llama-index-core>=0.10.43" "openinference-instrumentation-llama-index>=2" "opentelemetry-proto>=1.12.0" opentelemetry-exporter-otlp opentelemetry-sdk

Use the following code snippet to activate the instrumentation.

Note that the endpoint variable below should the address of the Phoenix receiver.

from openinference.instrumentation.llama_index import LlamaIndexInstrumentor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

endpoint = "http://127.0.0.1:6006/v1/traces"  # Phoenix receiver address

tracer_provider = trace_sdk.TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))

LlamaIndexInstrumentor().instrument(tracer_provider=tracer_provider)

Note that the legacy One-Click system of spans can still be used instead by setting use_legacy_callback_handler=True as shown below.

LlamaIndexInstrumentor().instrument(
    tracer_provider=tracer_provider,
    use_legacy_callback_handler=True,
)

By adding the callback to the callback manager of LlamaIndex, we've created a one-way data connection between your LLM application and Phoenix Server.

To view the traces in Phoenix, simply open the UI in your browser.

px.active_session().url

For a fully working example of tracing with LlamaIndex, checkout our colab notebook.

Last updated