-
Notifications
You must be signed in to change notification settings - Fork 612
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add manual sample, add no-content tests
- Loading branch information
Showing
14 changed files
with
493 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
15 changes: 15 additions & 0 deletions
15
instrumentation-genai/opentelemetry-instrumentation-openai-v2/examples/manual/.env
This file contains 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,15 @@ | ||
# 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:4318 | ||
# OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf | ||
OTEL_SERVICE_NAME=opentelemetry-python-openai | ||
|
||
# Change to 'false' to hide prompt and completion content | ||
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true |
38 changes: 38 additions & 0 deletions
38
...tation-genai/opentelemetry-instrumentation-openai-v2/examples/manual/README.rst
This file contains 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,38 @@ | ||
OpenTelemetry OpenAI Instrumentation Example | ||
============================================ | ||
|
||
This is an example of how to instrument OpenAI calls. | ||
|
||
When `main.py <main.py>`_ is run, it exports traces and logs to an OTLP | ||
compatible endpoint. Traces include details such as the model used and the | ||
duration of the chat request. Logs capture the chat request and the generated | ||
response, providing a comprehensive view of the performance and behavior of | ||
your OpenAI requests. | ||
|
||
Setup | ||
----- | ||
|
||
Minimally, update the `.env <.env>`_ file with your "OPENAI_API_KEY". An | ||
OTLP compatible endpoint should be listening for traces and logs on | ||
http://localhost:4318. 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 -- python main.py | ||
|
||
You should see a poem generated by OpenAI while traces and logs export to your | ||
configured observability tool. |
48 changes: 48 additions & 0 deletions
48
instrumentation-genai/opentelemetry-instrumentation-openai-v2/examples/manual/main.py
This file contains 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,48 @@ | ||
import os | ||
|
||
from openai import OpenAI | ||
|
||
from opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentor | ||
|
||
# NOTE: OpenTelemetry Python Logs and Events APIs are in beta | ||
from opentelemetry import trace, _logs, _events | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk._logs import LoggerProvider | ||
from opentelemetry.sdk._events import EventLoggerProvider | ||
|
||
from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor | ||
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter | ||
from opentelemetry.exporter.otlp.proto.http._log_exporter import OTLPLogExporter | ||
|
||
# configure tracing | ||
trace.set_tracer_provider(TracerProvider()) | ||
trace.get_tracer_provider().add_span_processor( | ||
BatchSpanProcessor(OTLPSpanExporter()) | ||
) | ||
|
||
# configure logging and events | ||
_logs.set_logger_provider(LoggerProvider()) | ||
_logs.get_logger_provider().add_log_record_processor(BatchLogRecordProcessor(OTLPLogExporter())) | ||
_events.set_event_logger_provider(EventLoggerProvider()) | ||
|
||
# instrument OpenAI | ||
OpenAIInstrumentor().instrument() | ||
|
||
def main(): | ||
|
||
client = OpenAI() | ||
chat_completion = client.chat.completions.create( | ||
model=os.getenv("CHAT_MODEL", "gpt-4o-mini"), | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "Write a short poem on OpenTelemetry.", | ||
}, | ||
], | ||
) | ||
print(chat_completion.choices[0].message.content) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
5 changes: 5 additions & 0 deletions
5
...umentation-genai/opentelemetry-instrumentation-openai-v2/examples/manual/requirements.txt
This file contains 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,5 @@ | ||
openai~=1.54.4 | ||
|
||
opentelemetry-sdk~=1.28.2 | ||
opentelemetry-exporter-otlp-proto-http~=1.28.2 | ||
opentelemetry-instrumentation-openai-v2~=2.0b0 |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
132 changes: 132 additions & 0 deletions
132
...etry-instrumentation-openai-v2/tests/cassettes/test_async_chat_completion_no_content.yaml
This file contains 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,132 @@ | ||
interactions: | ||
- request: | ||
body: |- | ||
{ | ||
"messages": [ | ||
{ | ||
"role": "user", | ||
"content": "Say this is a test" | ||
} | ||
], | ||
"model": "gpt-4o-mini", | ||
"stream": false | ||
} | ||
headers: | ||
accept: | ||
- application/json | ||
accept-encoding: | ||
- gzip, deflate | ||
authorization: | ||
- Bearer test_openai_api_key | ||
connection: | ||
- keep-alive | ||
content-length: | ||
- '106' | ||
content-type: | ||
- application/json | ||
host: | ||
- api.openai.com | ||
user-agent: | ||
- AsyncOpenAI/Python 1.26.0 | ||
x-stainless-arch: | ||
- arm64 | ||
x-stainless-async: | ||
- async:asyncio | ||
x-stainless-lang: | ||
- python | ||
x-stainless-os: | ||
- MacOS | ||
x-stainless-package-version: | ||
- 1.26.0 | ||
x-stainless-runtime: | ||
- CPython | ||
x-stainless-runtime-version: | ||
- 3.12.5 | ||
method: POST | ||
uri: https://api.openai.com/v1/chat/completions | ||
response: | ||
body: | ||
string: |- | ||
{ | ||
"id": "chatcmpl-ASv9R2E7Yhb2e7bj4Xl0qm9s3J42Y", | ||
"object": "chat.completion", | ||
"created": 1731456237, | ||
"model": "gpt-4o-mini-2024-07-18", | ||
"choices": [ | ||
{ | ||
"index": 0, | ||
"message": { | ||
"role": "assistant", | ||
"content": "This is a test. How can I assist you further?", | ||
"refusal": null | ||
}, | ||
"logprobs": null, | ||
"finish_reason": "stop" | ||
} | ||
], | ||
"usage": { | ||
"prompt_tokens": 12, | ||
"completion_tokens": 12, | ||
"total_tokens": 24, | ||
"prompt_tokens_details": { | ||
"cached_tokens": 0, | ||
"audio_tokens": 0 | ||
}, | ||
"completion_tokens_details": { | ||
"reasoning_tokens": 0, | ||
"audio_tokens": 0, | ||
"accepted_prediction_tokens": 0, | ||
"rejected_prediction_tokens": 0 | ||
} | ||
}, | ||
"system_fingerprint": "fp_0ba0d124f1" | ||
} | ||
headers: | ||
CF-Cache-Status: | ||
- DYNAMIC | ||
CF-RAY: | ||
- 8e1a80679a8311a6-MRS | ||
Connection: | ||
- keep-alive | ||
Content-Type: | ||
- application/json | ||
Date: | ||
- Wed, 13 Nov 2024 00:03:58 GMT | ||
Server: | ||
- cloudflare | ||
Set-Cookie: test_set_cookie | ||
Transfer-Encoding: | ||
- chunked | ||
X-Content-Type-Options: | ||
- nosniff | ||
access-control-expose-headers: | ||
- X-Request-ID | ||
alt-svc: | ||
- h3=":443"; ma=86400 | ||
content-length: | ||
- '796' | ||
openai-organization: test_openai_org_id | ||
openai-processing-ms: | ||
- '359' | ||
openai-version: | ||
- '2020-10-01' | ||
strict-transport-security: | ||
- max-age=31536000; includeSubDomains; preload | ||
x-ratelimit-limit-requests: | ||
- '30000' | ||
x-ratelimit-limit-tokens: | ||
- '150000000' | ||
x-ratelimit-remaining-requests: | ||
- '29999' | ||
x-ratelimit-remaining-tokens: | ||
- '149999978' | ||
x-ratelimit-reset-requests: | ||
- 2ms | ||
x-ratelimit-reset-tokens: | ||
- 0s | ||
x-request-id: | ||
- req_41ea134c1fc450d4ca4cf8d0c6a7c53a | ||
status: | ||
code: 200 | ||
message: OK | ||
version: 1 |
Oops, something went wrong.