OpenAI Instrumentation Package
Project description
OpenTelemetry Instrumentation for OpenAI
An OpenTelemetry instrumentation for the openai
client library.
This instrumentation currently only supports instrumenting the Chat completions APIs.
We currently support the following features:
sync
andasync
chat completions- Streaming support
- Functions calling with tools
- Client side metrics
- Following 1.27.0 Gen AI Semantic Conventions
Installation
pip install elastic-opentelemetry-instrumentation-openai
Usage
This instrumentation supports 0-code / auto instrumentation:
opentelemetry-instrument python use_openai.py
Or manual instrumentation:
import openai
from opentelemetry.instrumentation.openai import OpenAIInstrumentor
OpenAIInstrumentor().instrument()
# assumes at least the OPENAI_API_KEY environment variable set
client = openai.Client()
messages = [
{
"role": "user",
"content": "Answer in up to 3 words: Which ocean contains the canarian islands?",
}
]
chat_completion = client.chat.completions.create(model="gpt-4o-mini", messages=messages)
Instrumentation specific environment variable configuration
ELASTIC_OTEL_GENAI_CAPTURE_CONTENT
(default:false
): when sets totrue
collect more informations about prompts and responses by enabling content capture
Development
We use pytest to execute tests written with the standard library unittest framework.
Test dependencies need to be installed before running.
python3 -m venv .venv
source .venv/bin/activate
pip install -r dev-requirements.txt
pytest
Refreshing HTTP payloads
We use VCR.py to automatically record HTTP responses from LLMs to reuse in tests without running the LLM. Refreshing HTTP payloads may be needed in these cases
- Adding a new unit test
- Extending a unit test with functionality that requires an up-to-date HTTP response
Integration tests default to using ollama, to avoid cost and leaking sensitive information. However, unit test recordings should use the authoritative OpenAI platform unless the test is about a specific portability corner case.
To refresh a test, delete its cassette file in tests/cassettes and make sure you have the following environment variables set:
OPENAI_API_KEY
- from https://platform.openai.com/settings/profile?tab=api-keys- It should look like
sk-...
- It should look like
OPENAI_ORG_ID
- from https://platform.openai.com/settings/organization/general- It should look like
org-...
- It should look like
OPENAI_PROJECT_ID
- from https://platform.openai.com/settings/profile (click Project)- It should look like
proj_...
- It should look like
If writing a new test, start with the test logic with no assertions. If extending an existing unit test rather than writing a new one, remove the corresponding recorded response from cassettes instead.
Then, run pytest
as normal. It will execute a request against the LLM and record it. Update the
test with correct assertions until it passes. Following executions of pytest
will use the recorded
response without querying the LLM.
License
This software is licensed under the Apache License, version 2 ("Apache-2.0").
Project details
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
File details
Details for the file elastic_opentelemetry_instrumentation_openai-0.1.0.tar.gz
.
File metadata
- Download URL: elastic_opentelemetry_instrumentation_openai-0.1.0.tar.gz
- Upload date:
- Size: 20.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6069a17bfa55ebe58c183768060fa666138498a33fa56d8e1225fe203ecbd14d |
|
MD5 | a70ac50233b6d88fdd62d0fbfea549df |
|
BLAKE2b-256 | 816eb5297235d5273af917db6904aa518a8e04181fe829b1098289106085f11e |
File details
Details for the file elastic_opentelemetry_instrumentation_openai-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: elastic_opentelemetry_instrumentation_openai-0.1.0-py3-none-any.whl
- Upload date:
- Size: 20.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07ff009aded01e8e75bdd124d348dee28dee7fbecaeac8f75f237f7d1a423540 |
|
MD5 | fc0df31eb9993adde8649a7659e96c98 |
|
BLAKE2b-256 | c4eeb9f82aea0007fd62e096f46a129e79c13c1d0c677a2a54f43f0c401f0aba |