-
Notifications
You must be signed in to change notification settings - Fork 798
Add OpenAI embeddings instrumentation #3461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
drewby
wants to merge
29
commits into
open-telemetry:main
Choose a base branch
from
drewby:openai-embeddings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
ab95ef6
Initial implementation and tests
drewby df2dd1f
Add embeddings example
drewby a360307
Update documentation
drewby 92ccc30
Changelog entry
drewby 3929fa9
Add comment about custom attributes
drewby ced260a
Update PR link in Changelog
drewby 1966c7a
Add input and output events for embeddings
drewby f9c2a1e
Fix changelog
drewby 4d1497e
Use gen_ai.embeddings.dimension.count
drewby ebb728a
Remove total_tokens
drewby 5b5743b
Use end_on_exit
drewby 6840646
Don't import conditionally
drewby 03e1bba
Fix heading
drewby 94b4ad3
Use gen_ai.request.encoding_formats
drewby 1c12412
Use gen_ai.request.encoding_formats
drewby 168d40e
Rename span_attr to request_attr
drewby 3e286e2
Merge branch 'open-telemetry:main' into openai-embeddings
drewby 1379841
Remove embeddings capture via events
drewby cda776d
bump versions
drewby d3061cb
Remove unused event code
drewby 543d6b8
Updates for PR feedback
drewby a2efaf4
Merge branch 'main' into openai-embeddings
xrmx d4e5279
Merge branch 'open-telemetry:main' into openai-embeddings
drewby 14f5a45
Update requirements for example
drewby 3dd15c0
dimension.count is added in get_llm_request_attributes
drewby abaa373
Use a shared function to reduce duplicate code
drewby ad216f3
Merge branch 'main' into openai-embeddings
drewby debb47c
remove return
drewby 47807f4
Merge branch 'main' into openai-embeddings
drewby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
instrumentation-genai/opentelemetry-instrumentation-openai-v2/examples/embeddings/.env
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Update this with your real OpenAI API key | ||
OPENAI_API_KEY=sk-YOUR_API_KEY | ||
|
||
# Uncomment to use Ollama instead of OpenAI | ||
# OPENAI_BASE_URL=http://localhost:11434/v1 | ||
# OPENAI_API_KEY=unused | ||
# CHAT_MODEL=qwen2.5:0.5b | ||
|
||
# Uncomment and change to your OTLP endpoint | ||
# OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317 | ||
# OTEL_EXPORTER_OTLP_PROTOCOL=grpc | ||
|
||
OTEL_SERVICE_NAME=opentelemetry-python-openai | ||
|
||
# Change to 'false' to disable collection of python logging logs | ||
OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true | ||
|
||
# Uncomment if your OTLP endpoint doesn't support logs | ||
# OTEL_LOGS_EXPORTER=console | ||
|
||
# Change to 'false' to hide prompt and completion content | ||
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true |
44 changes: 44 additions & 0 deletions
44
...on-genai/opentelemetry-instrumentation-openai-v2/examples/embeddings/README.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
OpenTelemetry OpenAI Embeddings API Instrumentation Example | ||
=========================================================== | ||
|
||
This is an example of how to instrument OpenAI Embeddings API calls with zero code changes, | ||
using ``opentelemetry-instrument``. | ||
|
||
When ``main.py`` is run, it exports traces and metrics to an OTLP | ||
compatible endpoint. Traces include details such as the model used, | ||
dimensions of embeddings, and the duration of the embedding request. | ||
Metrics capture token usage and performance data. | ||
|
||
Note: ``.env`` file configures additional environment variables: | ||
|
||
- ``OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true`` configures OpenTelemetry SDK to export logs and events. | ||
- ``OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true`` configures OpenAI instrumentation to capture content on events. | ||
- ``OTEL_LOGS_EXPORTER=otlp`` to specify exporter type. | ||
|
||
Setup | ||
----- | ||
|
||
Minimally, update the ``.env`` file with your ``OPENAI_API_KEY``. An | ||
OTLP compatible endpoint should be listening for traces and logs on | ||
http://localhost:4317. If not, update ``OTEL_EXPORTER_OTLP_ENDPOINT`` as well. | ||
|
||
Next, set up a virtual environment like this: | ||
|
||
:: | ||
|
||
python3 -m venv .venv | ||
source .venv/bin/activate | ||
pip install "python-dotenv[cli]" | ||
pip install -r requirements.txt | ||
|
||
Run | ||
--- | ||
|
||
Run the example like this: | ||
|
||
:: | ||
|
||
dotenv run -- opentelemetry-instrument python main.py | ||
|
||
You should see embedding information printed while traces and metrics export to your | ||
configured observability tool. |
29 changes: 29 additions & 0 deletions
29
instrumentation-genai/opentelemetry-instrumentation-openai-v2/examples/embeddings/main.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import os | ||
|
||
from openai import OpenAI | ||
|
||
|
||
def main(): | ||
client = OpenAI() | ||
|
||
# Create embeddings with OpenAI API | ||
embedding_response = client.embeddings.create( | ||
model=os.getenv("EMBEDDING_MODEL", "text-embedding-3-small"), | ||
input="OpenTelemetry provides observability for your applications.", | ||
) | ||
|
||
# Print embedding information | ||
print(f"Model: {embedding_response.model}") | ||
print(f"Dimensions: {len(embedding_response.data[0].embedding)}") | ||
print( | ||
f"Token usage - Prompt: {embedding_response.usage.prompt_tokens}, Total: {embedding_response.usage.total_tokens}" | ||
) | ||
|
||
# Print a sample of the embedding vector (first 5 dimensions) | ||
print( | ||
f"Embedding sample (first 5 dimensions): {embedding_response.data[0].embedding[:5]}" | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
6 changes: 6 additions & 0 deletions
6
...tation-genai/opentelemetry-instrumentation-openai-v2/examples/embeddings/requirements.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
openai~=1.57.3 | ||
|
||
opentelemetry-sdk~=1.36.0 | ||
opentelemetry-exporter-otlp-proto-grpc~=1.36.0 | ||
opentelemetry-distro~=0.57b0 | ||
opentelemetry-instrumentation-openai-v2~=2.1b0 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.