Describes how to track OpenAI LLM calls using Opik

This guide explains how to integrate Opik with the OpenAI Python SDK. By using the track_openai method provided by opik, you can easily track and evaluate your OpenAI API calls within your Opik projects as Opik will automatically log the input prompt, model used, token usage, and response generated.

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

Open In Colab

Getting started

First, ensure you have both opik and openai packages installed:

$pip install opik openai

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

Tracking OpenAI API calls

1from opik.integrations.openai import track_openai
2from openai import OpenAI
3
4openai_client = OpenAI()
5openai_client = track_openai(openai_client)
6
7prompt="Hello, world!"
8
9response = openai_client.chat.completions.create(
10 model="gpt-3.5-turbo",
11 messages=[
12 {"role":"user", "content":prompt}
13 ],
14 temperature=0.7,
15 max_tokens=100,
16 top_p=1,
17 frequency_penalty=0,
18 presence_penalty=0
19)
20
21print(response.choices[0].message.content)

The track_openai will automatically track and log the API call, including the input prompt, model used, and response generated. You can view these logs in your Opik project dashboard.

Using Azure OpenAI

The OpenAI integration also supports Azure OpenAI Services. To use Azure OpenAI, initialize your client with Azure configuration and use it with track_openai just like the standard OpenAI client:

1from opik.integrations.openai import track_openai
2from openai import AzureOpenAI
3
4# gets the API Key from environment variable AZURE_OPENAI_API_KEY
5azure_client = AzureOpenAI(
6 # https://learn.microsoft.com/azure/ai-services/openai/reference#rest-api-versioning
7 api_version="2023-07-01-preview",
8 # https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal#create-a-resource
9 azure_endpoint="https://example-endpoint.openai.azure.com",
10)
11azure_client = track_openai(azure_client)
12
13completion = azure_client.chat.completions.create(
14 model="deployment-name", # e.g. gpt-35-instant
15 messages=[
16 {
17 "role": "user",
18 "content": "How do I output all files in a directory using Python?",
19 },
20 ],
21)

Supported OpenAI methods

The track_openai wrapper supports the following OpenAI methods:

  • openai_client.chat.completions.create()
  • openai_client.beta.chat.completions.parse()

If you would like to track another OpenAI method, please let us know by opening an issue on GitHub.

Built with