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.0a0.tar.gz
(2.6 kB
view details)
Built Distribution
File details
Details for the file openai_otel-0.1.0a0.tar.gz
.
File metadata
- Download URL: openai_otel-0.1.0a0.tar.gz
- Upload date:
- Size: 2.6 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 | a60fcab25b89031cd01d3d080125c16139d1a571167ae88d0fedcb52d039d77c |
|
MD5 | 004560323540bfeb0434146f4c87e8c0 |
|
BLAKE2b-256 | e18fb929cddf565af1a7f38e1e7e6b1a120f01773060dd701fe020659e13c827 |
File details
Details for the file openai_otel-0.1.0a0-py3-none-any.whl
.
File metadata
- Download URL: openai_otel-0.1.0a0-py3-none-any.whl
- Upload date:
- Size: 3.1 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 | abc59c49f7d9cedd9b390d0a81c0bdec27a7549d2a0d78081dacf71f9e962cb9 |
|
MD5 | 418e8e3996703dc18921409dbc9231c6 |
|
BLAKE2b-256 | cac4ec9bdd8c866df68d5a34ee9df76b635a81eba311e8e4beb854609ac04ec0 |