Skip to main content

OpenAI API

Use the Weights & Biases OpenAI API integration to log requests, responses, token counts and model metadata with 1 line of code for all OpenAI models, including fine-tuned models.

info

The W&B autolog integration works with openai <= 0.28.1. Install the correct version of openai with pip install openai==0.28.1.

Try in a Colab Notebook here →

With just 1 line of code you can now automatically log inputs and outputs from the OpenAI Python SDK to Weights & Biases!

Once you start logging your API inputs and outputs you can quickly evaluate the performance of difference prompts, compare different model settings (such as temperature), and track other usage metrics such as token usage.

To get started, pip install the wandb library, then follow the steps below:

1. Import autolog and initialise it

First, import autolog from wandb.integration.openai and initialise it.

import os
import openai
from wandb.integration.openai import autolog

autolog({"project": "gpt5"})

You can optionally pass a dictionary with argument that wandb.init() accepts to autolog. This includes a project name, team name, entity, and more. For more information about wandb.init, see the API Reference Guide.

2. Call the OpenAI API

Each call you make to the OpenAI API will now be logged to Weights & Biases automatically.

os.environ["OPENAI_API_KEY"] = "XXX"

chat_request_kwargs = dict(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"},
{"role": "assistant", "content": "The Los Angeles Dodgers"},
{"role": "user", "content": "Where was it played?"},
],
)
response = openai.ChatCompletion.create(**chat_request_kwargs)

3. View your OpenAI API inputs and responses

Click on the Weights & Biases run link generated by autolog in step 1. This will redirect you to your project workspace in the W&B App.

Select a run you created to view the trace table, trace timeline and the model architecture of the OpenAI LLM used.

4. Disable autolog

We recommend that you call disable() to close all W&B processes when you are finished using the OpenAI API.

autolog.disable()

Now your inputs and completions will be logged to Weights & Biases, ready for analysis or to be shared with colleagues.

Was this page helpful?👍👎