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"}
More examples
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openai_otel-0.1.0a3.tar.gz.
File metadata
- Download URL: openai_otel-0.1.0a3.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.6 Linux/6.8.0-1014-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12969c3055b55027aa86b9854de8ccfabb014f55f2b37796d2d2d1672190d6a0
|
|
| MD5 |
9f6c77396301ecfc04c8b58536804364
|
|
| BLAKE2b-256 |
a4c5d110462a74e08f32549fe32e8802554934bdab719b0636010c22bd0c2cc2
|
File details
Details for the file openai_otel-0.1.0a3-py3-none-any.whl.
File metadata
- Download URL: openai_otel-0.1.0a3-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.6 Linux/6.8.0-1014-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1dc52e6d7a4933319bd3650bf7207cc8e819a868d8ed183beda1547f4b417928
|
|
| MD5 |
bfd7b07811beea190b534d45e878608d
|
|
| BLAKE2b-256 |
76f83c2bd8156843ad5e5a5a4e43ae9e989aa4a01d7f839dda415916c6cc24b1
|