OpenAI

Both SDKs ship with a function that enables automatic tracing of all OpenAI calls.

Quick Start

In order to run the examples below, you need:

  • to have openai installed:
pip install openai
export OPENAI_API_KEY=<your-api-key>
export AUTOBLOCKS_INGESTION_KEY=<your-ingestion-key>

Then run the following:

import openai
from autoblocks.vendor.openai import trace_openai

# Call at the entrypoint to your application
trace_openai()

# The request and response will be automatically
# logged to Autoblocks
openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  temperature=0,
  messages=[
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": "Hello, World!"
    }
  ],
)

After running the above script, you should eventually see a trace on the explore page with events from the OpenAI call.

Sending custom events alongside OpenAI events

If you want to send custom events in addition to the events that are sent automatically, such as user feedback events, you can access the underlying AutoblocksTracer instance via the return value of the trace_openai or traceOpenAI functions.

import uuid

import openai
from autoblocks.vendor.openai import trace_openai

# Call at the entrypoint to your application
tracer = trace_openai()

# Set the trace ID on the tracer directly
tracer.set_trace_id(trace_id)

# The events that are sent automatically
# from the openai call below will have
# the trace ID set above
openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  temperature=0,
  messages=[
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": "Hello, World!"
    }
  ],
)

# This custom feedback event will also
# have the same trace ID set above
tracer.send_event(
  "user.feedback", 
  properties=dict(feedback="good"),
)

On the explore page you should eventually see a new trace with the same events from the previous example and also a user.feedback event.

Examples

See the OpenAI examples in our examples repository.