-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc5d2d3
commit 393a38b
Showing
1 changed file
with
39 additions
and
13 deletions.
There are no files selected for viewing
52 changes: 39 additions & 13 deletions
52
python/instrumentation/openinference-instrumentation-openai/examples/chat_completions.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 |
---|---|---|
@@ -1,18 +1,44 @@ | ||
import openai | ||
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter | ||
from opentelemetry.sdk import trace as trace_sdk | ||
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor | ||
|
||
from openinference.instrumentation import using_attributes | ||
from openinference.instrumentation.openai import OpenAIInstrumentor | ||
from phoenix.otel import register | ||
|
||
tracer_provider = register(endpoint="http://localhost:6006/v1/traces") | ||
endpoint = "http://127.0.0.1:6006/v1/traces" | ||
tracer_provider = trace_sdk.TracerProvider() | ||
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint))) | ||
tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter())) | ||
|
||
OpenAIInstrumentor().instrument(tracer_provider=tracer_provider) | ||
client = openai.OpenAI() | ||
with using_attributes( | ||
session_id="session-id", | ||
user_id="user-id", | ||
): | ||
response = client.chat.completions.create( | ||
model="gpt-3.5-turbo", | ||
messages=[{"role": "user", "content": "Write a haiku."}], | ||
max_tokens=20, | ||
) | ||
print(response.choices[0].message.content) | ||
|
||
|
||
if __name__ == "__main__": | ||
client = openai.OpenAI() | ||
with using_attributes( | ||
session_id="my-test-session", | ||
user_id="my-test-user", | ||
metadata={ | ||
"test-int": 1, | ||
"test-str": "string", | ||
"test-list": [1, 2, 3], | ||
"test-dict": { | ||
"key-1": "val-1", | ||
"key-2": "val-2", | ||
}, | ||
}, | ||
tags=["tag-1", "tag-2"], | ||
prompt_template="Who won the soccer match in {city} on {date}", | ||
prompt_template_version="v1.0", | ||
prompt_template_variables={ | ||
"city": "Johannesburg", | ||
"date": "July 11th", | ||
}, | ||
): | ||
response = client.chat.completions.create( | ||
model="gpt-3.5-turbo", | ||
messages=[{"role": "user", "content": "Write a haiku."}], | ||
max_tokens=20, | ||
) | ||
print(response.choices[0].message.content) |