Using Opik with OpenAI

Opik integrates with OpenAI to provide a simple way to log traces for all OpenAI LLM calls. This works for all OpenAI models, including if you are using the streaming API.

Creating an account on Comet.com

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.

1%pip install --upgrade opik openai
1import opik
2
3opik.configure(use_local=False)

Preparing our environment

First, we will set up our OpenAI API keys.

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: ")

Logging traces

In order to log traces to Opik, we need to wrap our OpenAI calls with the track_openai function:

1from opik.integrations.openai import track_openai
2from openai import OpenAI
3
4os.environ["OPIK_PROJECT_NAME"] = "openai-integration-demo"
5
6client = OpenAI()
7openai_client = track_openai(client)
1prompt = """
2Write a short two sentence story about Opik.
3"""
4
5completion = openai_client.chat.completions.create(
6 model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}]
7)
8
9print(completion.choices[0].message.content)

The prompt and response messages are automatically logged to Opik and can be viewed in the UI.

OpenAI Integration

Using it with the track decorator

If you have multiple steps in your LLM pipeline, you can use the track decorator to log the traces for each step. If OpenAI is called within one of these steps, the LLM call with be associated with that corresponding step:

1from opik import track
2from opik.integrations.openai import track_openai
3from openai import OpenAI
4
5os.environ["OPIK_PROJECT_NAME"] = "openai-integration-demo"
6
7client = OpenAI()
8openai_client = track_openai(client)
9
10
11@track
12def generate_story(prompt):
13 res = openai_client.chat.completions.create(
14 model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}]
15 )
16 return res.choices[0].message.content
17
18
19@track
20def generate_topic():
21 prompt = "Generate a topic for a story about Opik."
22 res = openai_client.chat.completions.create(
23 model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}]
24 )
25 return res.choices[0].message.content
26
27
28@track
29def generate_opik_story():
30 topic = generate_topic()
31 story = generate_story(topic)
32 return story
33
34
35generate_opik_story()

The trace can now be viewed in the UI:

OpenAI Integration

Built with