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

File metadata

File hashes

Hashes for elastic_opentelemetry_instrumentation_openai-0.3.0.tar.gz
Algorithm Hash digest
SHA256 6acc74ed383d99bdbab028dbfe5b6db06febdfe0d6e6d627d4f3b5f931c5514c
MD5 d33a55a381e49dbfe4495c3ffe40f297
BLAKE2b-256 b1b5c3636aff31923dcefd0398faac125d381cb6bc0945d2e63b3a4a4300c010

See more details on using hashes here.

Provenance

The following attestation bundles were made for elastic_opentelemetry_instrumentation_openai-0.3.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.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for elastic_opentelemetry_instrumentation_openai-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3a922da07e933bdcbebf6df1b8f925f2f711dadbee3ae65fbcbd25af43f2c703
MD5 1722a3a4007a1a9e59b5a59595f08317
BLAKE2b-256 08d73c551c842c62dbcbc8c31c8e1d9bb3a0dd930055466ddc8ede030c08c815

See more details on using hashes here.

Provenance

The following attestation bundles were made for elastic_opentelemetry_instrumentation_openai-0.3.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