Skip to main content

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:

  • sync and async chat 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

Azure OpenAI Environment Variables

The AzureOpenAI client extends OpenAI with parameters specific to the Azure OpenAI Service.

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


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

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file elastic_opentelemetry_instrumentation_openai-1.2.0.tar.gz.

File metadata

File hashes

Hashes for elastic_opentelemetry_instrumentation_openai-1.2.0.tar.gz
Algorithm Hash digest
SHA256 b393bb3816c91ba2492adf5b87e2665b30e481facbf1f95909ebd658b0ea5d20
MD5 78a99304cc975c33640aa01c2369d89a
BLAKE2b-256 e0c4a80de71f1f02f2ae278a7cd5850ccaa4512ca81d7b57d9ca7318d43d176f

See more details on using hashes here.

Provenance

The following attestation bundles were made for elastic_opentelemetry_instrumentation_openai-1.2.0.tar.gz:

Publisher: release-openai.yml on elastic/elastic-otel-python-instrumentations

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file elastic_opentelemetry_instrumentation_openai-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for elastic_opentelemetry_instrumentation_openai-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 efd8ab1127d48207ae6db2b250e8bc7aca44f264c606a2acea05ff5a697d5ee9
MD5 d715fedecbdf40bc70e6d40fa0eee876
BLAKE2b-256 dccaff93835b2c2471a91c28279299a77e6ba57aa03bafd0619c356c5bc370a8

See more details on using hashes here.

Provenance

The following attestation bundles were made for elastic_opentelemetry_instrumentation_openai-1.2.0-py3-none-any.whl:

Publisher: release-openai.yml on elastic/elastic-otel-python-instrumentations

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page