Skip to content

Commit df2dd1f

Browse files
committed
Add embeddings example
1 parent ab95ef6 commit df2dd1f

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Update this with your real OpenAI API key
2+
OPENAI_API_KEY=sk-YOUR_API_KEY
3+
4+
# Uncomment to use Ollama instead of OpenAI
5+
# OPENAI_BASE_URL=http://localhost:11434/v1
6+
# OPENAI_API_KEY=unused
7+
# CHAT_MODEL=qwen2.5:0.5b
8+
9+
# Uncomment and change to your OTLP endpoint
10+
# OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
11+
# OTEL_EXPORTER_OTLP_PROTOCOL=grpc
12+
13+
OTEL_SERVICE_NAME=opentelemetry-python-openai
14+
15+
# Change to 'false' to disable collection of python logging logs
16+
OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true
17+
18+
# Uncomment if your OTLP endpoint doesn't support logs
19+
# OTEL_LOGS_EXPORTER=console
20+
21+
# Change to 'false' to hide prompt and completion content
22+
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
OpenTelemetry OpenAI Embeddings API Instrumentation Example
2+
========================================================
3+
4+
This is an example of how to instrument OpenAI Embeddings API calls with zero code changes,
5+
using ``opentelemetry-instrument``.
6+
7+
When ``main.py`` is run, it exports traces and metrics to an OTLP
8+
compatible endpoint. Traces include details such as the model used,
9+
dimensions of embeddings, and the duration of the embedding request.
10+
Metrics capture token usage and performance data.
11+
12+
Note: ``.env`` file configures additional environment variables:
13+
14+
- ``OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true`` configures OpenTelemetry SDK to export logs and events.
15+
- ``OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true`` configures OpenAI instrumentation to capture content on events.
16+
- ``OTEL_LOGS_EXPORTER=otlp`` to specify exporter type.
17+
18+
Setup
19+
-----
20+
21+
Minimally, update the ``.env`` file with your ``OPENAI_API_KEY``. An
22+
OTLP compatible endpoint should be listening for traces and logs on
23+
http://localhost:4317. If not, update ``OTEL_EXPORTER_OTLP_ENDPOINT`` as well.
24+
25+
Next, set up a virtual environment like this:
26+
27+
::
28+
29+
python3 -m venv .venv
30+
source .venv/bin/activate
31+
pip install "python-dotenv[cli]"
32+
pip install -r requirements.txt
33+
34+
Run
35+
---
36+
37+
Run the example like this:
38+
39+
::
40+
41+
dotenv run -- opentelemetry-instrument python main.py
42+
43+
You should see embedding information printed while traces and metrics export to your
44+
configured observability tool.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import os
2+
3+
from openai import OpenAI
4+
5+
6+
def main():
7+
client = OpenAI()
8+
9+
# Create embeddings with OpenAI API
10+
embedding_response = client.embeddings.create(
11+
model=os.getenv("EMBEDDING_MODEL", "text-embedding-3-small"),
12+
input="OpenTelemetry provides observability for your applications.",
13+
)
14+
15+
# Print embedding information
16+
print(f"Model: {embedding_response.model}")
17+
print(f"Dimensions: {len(embedding_response.data[0].embedding)}")
18+
print(
19+
f"Token usage - Prompt: {embedding_response.usage.prompt_tokens}, Total: {embedding_response.usage.total_tokens}"
20+
)
21+
22+
# Print a sample of the embedding vector (first 5 dimensions)
23+
print(
24+
f"Embedding sample (first 5 dimensions): {embedding_response.data[0].embedding[:5]}"
25+
)
26+
27+
28+
if __name__ == "__main__":
29+
main()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
openai~=1.57.3
2+
3+
opentelemetry-sdk~=1.30.0
4+
opentelemetry-exporter-otlp-proto-grpc~=1.30.0
5+
opentelemetry-distro~=0.51b0
6+
opentelemetry-instrumentation-openai-v2~=2.1b0

0 commit comments

Comments
 (0)