Skip to main content

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 and async 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 to true 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:

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.2.0.tar.gz.

File metadata

File hashes

Hashes for elastic_opentelemetry_instrumentation_openai-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0bb0d1bd71fedd79fb6bb76871e49ae880665b4f05e9d5deffd166f8dc89558b
MD5 344a555220746519d87f771b0b0a8e71
BLAKE2b-256 1a883b8c159f4d5725a04fe4f00908dee39aa6ad4a90aea997b55a72d11d521e

See more details on using hashes here.

Provenance

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

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

Attestations:

File details

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

File metadata

File hashes

Hashes for elastic_opentelemetry_instrumentation_openai-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e6ab3a2e843f5c23deab113dd0b710e6d7d3f36203114d150bc4064769febf5
MD5 6ef2def71fb2e9f20a7282971fd29da7
BLAKE2b-256 a24cd5e446293186e69d4722832120133519c79397bd92943d2c8d347730c8a8

See more details on using hashes here.

Provenance

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

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

Attestations:

Supported by

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