OpenAI Instrumentation Package
Project description
OpenTelemetry Instrumentation for OpenAI
An OpenTelemetry instrumentation for the openai client library.
This instrumentation currently supports instrumenting the chat completions (create and parse (beta) APIs) and the embeddings API.
We currently support the following features:
syncandasyncchat completions- Streaming support for chat completions
- Functions calling with tools for chat completions
- Client side metrics
- Embeddings API calls
- Following 1.29.0 Gen AI Semantic Conventions
Installation
pip install elastic-opentelemetry-instrumentation-openai
Usage
This instrumentation supports zero-code / autoinstrumentation:
Set up a virtual environment with this package, the dependencies it requires
and dotenv (a portable way to load environment variables).
python3 -m venv .venv
source .venv/bin/activate
pip install -r dev-requirements.txt
pip install python-dotenv[cli]
Create a .env file containing the OpenAI API key:
echo "OPENAI_API_KEY=sk-..." > .env
Run the script with telemetry setup to use the instrumentation.
dotenv run -- opentelemetry-instrument python examples/chat.py
You can record more information about prompts as log events by enabling content capture.
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true dotenv run -- \
opentelemetry-instrument python examples/chat.py
Using a local model
Ollama may be used to run examples without a cloud account. After you have set it up need to install the models in order to run the examples:
# for chat
ollama pull qwen2.5:0.5b
# for embeddings
ollama pull all-minilm:33m
Finally run the examples using ollama.env variables to point to Ollama instead of OpenAI:
dotenv -f ollama.env run -- opentelemetry-instrument python examples/chat.py
Instrumentation specific environment variable configuration
None
Elastic specific semantic conventions
None at the moment
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
To run integration tests doing real requests:
OPENAI_API_KEY=unused pytest --integration-tests
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 environment variables set for recordings, detailed later.
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.
OpenAI Environment Variables
OPENAI_API_KEY- from https://platform.openai.com/settings/profile?tab=api-keys- It should look like
sk-...
- It should look like
Azure OpenAI Environment Variables
The AzureOpenAI client extends OpenAI with parameters specific to the Azure OpenAI Service.
AZURE_OPENAI_ENDPOINT- "Azure OpenAI Endpoint" in https://oai.azure.com/resource/overview- It should look like
https://<your-resource-name>.openai.azure.com/
- It should look like
AZURE_OPENAI_API_KEY- "API key 1 (or 2)" in https://oai.azure.com/resource/overview- It should look be a hex string like
abc01...
- It should look be a hex string like
OPENAI_API_VERSION= "Inference version" from https://learn.microsoft.com/en-us/azure/ai-services/openai/api-version-deprecation- It should look like
2024-10-01-preview
- It should look like
TEST_CHAT_MODEL= "Name" from https://oai.azure.com/resource/deployments that deployed a model that supports tool calling, such as "gpt-4o-mini".TEST_EMBEDDINGS_MODEL= "Name" from https://oai.azure.com/resource/deployments that deployed a model that supports embeddings, such as "text-embedding-3-small".
Note: The model parameter of a chat completion or embeddings request is substituted for an identical
deployment name. As deployment names are arbitrary they may have no correlation with a real model
like gpt-4o
License
This software is licensed under the Apache License, version 2 ("Apache-2.0").
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 elastic_opentelemetry_instrumentation_openai-0.7.0.tar.gz.
File metadata
- Download URL: elastic_opentelemetry_instrumentation_openai-0.7.0.tar.gz
- Upload date:
- Size: 33.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e64a739e9def8d4f09f6c623fc9cadfeec6773c89e5cc0dcb1a7e2ecf0041564
|
|
| MD5 |
d2b77b9a971667853f87f394af1993de
|
|
| BLAKE2b-256 |
799746d9ec57860e960deb4963f45067a1e809cb93ec4a4e4fcde7301c275a29
|
Provenance
The following attestation bundles were made for elastic_opentelemetry_instrumentation_openai-0.7.0.tar.gz:
Publisher:
release-openai.yml on elastic/elastic-otel-python-instrumentations
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
elastic_opentelemetry_instrumentation_openai-0.7.0.tar.gz -
Subject digest:
e64a739e9def8d4f09f6c623fc9cadfeec6773c89e5cc0dcb1a7e2ecf0041564 - Sigstore transparency entry: 184783816
- Sigstore integration time:
-
Permalink:
elastic/elastic-otel-python-instrumentations@a1a008c985052795733570647dbd4f6e0ec9e9e5 -
Branch / Tag:
refs/tags/openai-v0.7.0 - Owner: https://github.com/elastic
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-openai.yml@a1a008c985052795733570647dbd4f6e0ec9e9e5 -
Trigger Event:
push
-
Statement type:
File details
Details for the file elastic_opentelemetry_instrumentation_openai-0.7.0-py3-none-any.whl.
File metadata
- Download URL: elastic_opentelemetry_instrumentation_openai-0.7.0-py3-none-any.whl
- Upload date:
- Size: 22.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
877c5ea014f5b08f8bb0dc6ff4b4612b9603177de8ca327d28da3e14c0aa2a54
|
|
| MD5 |
46e1c0b883281f60aa54ef4481831939
|
|
| BLAKE2b-256 |
85575f69d240b5bdff62cd47d51a6c8b794aa9333b21f0771515c643fa27d4a0
|
Provenance
The following attestation bundles were made for elastic_opentelemetry_instrumentation_openai-0.7.0-py3-none-any.whl:
Publisher:
release-openai.yml on elastic/elastic-otel-python-instrumentations
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
elastic_opentelemetry_instrumentation_openai-0.7.0-py3-none-any.whl -
Subject digest:
877c5ea014f5b08f8bb0dc6ff4b4612b9603177de8ca327d28da3e14c0aa2a54 - Sigstore transparency entry: 184783818
- Sigstore integration time:
-
Permalink:
elastic/elastic-otel-python-instrumentations@a1a008c985052795733570647dbd4f6e0ec9e9e5 -
Branch / Tag:
refs/tags/openai-v0.7.0 - Owner: https://github.com/elastic
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-openai.yml@a1a008c985052795733570647dbd4f6e0ec9e9e5 -
Trigger Event:
push
-
Statement type: