OpenAI

How to use the python OpenAIInstrumentor to trace OpenAI LLM and embedding calls

Note: This instrumentation also works with Azure OpenAI

Phoenix provides auto-instrumentation for the OpenAI Python Library.

Launch Phoenix

Install packages:

pip install arize-phoenix

Launch Phoenix:

import phoenix as px
px.launch_app()

Connect your notebook to Phoenix:

from phoenix.otel import register

register(
  project_name="my-llm-app", # Default is 'default'
)  

By default, notebook instances do not have persistent storage, so your traces will disappear after the notebook is closed. See Persistence or use one of the other deployment options to retain traces.

Install

pip install openinference-instrumentation-openai openai

Setup

Add your OpenAI API key as an environment variable:

export OPENAI_API_KEY=[your_key_here]

Initialize the OpenAIInstrumentor before your application code:

from openinference.instrumentation.openai import OpenAIInstrumentor

OpenAIInstrumentor().instrument(tracer_provider=tracer_provider)

Run OpenAI

import openai

client = openai.OpenAI()
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Write a haiku."}],
)
print(response.choices[0].message.content)

Observe

Now that you have tracing setup, all invocations of OpenAI (completions, chat completions, embeddings) will be streamed to your running Phoenix for observability and evaluation.

Resources

Last updated