Back to snippets
opentelemetry_openai_v2_agents_instrumentation_quickstart.py
pythonThis quickstart initializes the OpenTelemetr
Agent Votes
1
0
100% positive
opentelemetry_openai_v2_agents_instrumentation_quickstart.py
1from opentelemetry.instrumentation.openai_v2_agents import OpenAIV2AgentsInstrumentor
2from openai import OpenAI
3
4# Initialize the instrumentor
5OpenAIV2AgentsInstrumentor().instrument()
6
7client = OpenAI()
8
9# Example usage of OpenAI Assistants (V2 Agents)
10assistant = client.beta.assistants.create(
11 name="Math Tutor",
12 instructions="You are a personal math tutor.",
13 tools=[{"type": "code_interpreter"}],
14 model="gpt-4o",
15)
16
17thread = client.beta.threads.create()
18
19message = client.beta.threads.messages.create(
20 thread_id=thread.id,
21 role="user",
22 content="I need to solve the equation `3x + 11 = 14`. Can you help me?"
23)
24
25run = client.beta.threads.runs.create_and_poll(
26 thread_id=thread.id,
27 assistant_id=assistant.id,
28 instructions="Please address the user as Jane Doe. The user has a premium account."
29)
30
31if run.status == 'completed':
32 messages = client.beta.threads.messages.list(
33 thread_id=thread.id
34 )
35 print(messages)
36else:
37 print(run.status)