openai-otel is a library that automatically instrument your OpenAI python code using OpenTelemetry
Project description
OpenAI OTEL
OpenAI OTEL is a library that allows you to instrument your OpenAI Python client using OpenTelemetry.
Installation
pip install openai-otel
Or
poetry add openai-otel
if you are using poetry.
Usage
Make sure that you have OPENAI_API_KEY
set in your environment variables.
import openai
from openai_otel import OpenAIAutoInstrumentor, tracer
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor, ConsoleSpanExporter
trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(
SimpleSpanProcessor(ConsoleSpanExporter())
)
OpenAIAutoInstrumentor().instrument()
resp = openai.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "user", "content": "what's the meaning of life?"},
],
)
print(resp.choices[0].message.content)
Examples
Start up tempo using docker compose:
(
cd examples/tempo
docker compose up -d
)
Run the example:
(
cd examples/fastapi
pip install -r requirements.txt
fastapi run
)
Then open http://localhost:8000 - you shall get the meaning of life.
Afterwards you can check out the traces on Grafana. You can do it via:
- Visit http://localhost:3000
- Go to the explorer view
- Click
TraceQL
- Run
{.service.name="fastapi-demo" && span.create.request.model="gpt-4o"}
You will get view like this:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
openai_otel-0.1.0a2.tar.gz
(3.4 kB
view details)
Built Distribution
File details
Details for the file openai_otel-0.1.0a2.tar.gz
.
File metadata
- Download URL: openai_otel-0.1.0a2.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a68be2dfc6f9ceb8bc2cbf63d0ea3a6a7972959d0b05698e0c93c33dcfd54b0f |
|
MD5 | 5fa9820b6c0f85d73458ee9117695b8a |
|
BLAKE2b-256 | 84a003ceb47344e471ad750e8cd8cbdd7803e6c273031f36fe42827d1dfffaa7 |
File details
Details for the file openai_otel-0.1.0a2-py3-none-any.whl
.
File metadata
- Download URL: openai_otel-0.1.0a2-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0ef53511fc82e37909176449887e0a0d10f330a7fecf4999f169df3e428e1b4 |
|
MD5 | 6dcb5c4df28d83a0e554bb10345f9846 |
|
BLAKE2b-256 | 8ed50b846ec4dcc3c93423da011b44f43494f22264f376f6765d63aadfd67303 |