openai/openai-python

Public

mirrored fromhttps://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
7a6517d81e4ae9e9e9527cd401bb76937983dfef

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

examples/assistant_stream.py

33lines · modecode

1import openai
2
3# gets API Key from environment variable OPENAI_API_KEY
4client = openai.OpenAI()
5
6assistant = client.beta.assistants.create(
7 name="Math Tutor",
8 instructions="You are a personal math tutor. Write and run code to answer math questions.",
9 tools=[{"type": "code_interpreter"}],
10 model="gpt-4-1106-preview",
11)
12
13thread = client.beta.threads.create()
14
15message = client.beta.threads.messages.create(
16 thread_id=thread.id,
17 role="user",
18 content="I need to solve the equation `3x + 11 = 14`. Can you help me?",
19)
20
21print("starting run stream")
22
23stream = client.beta.threads.runs.create(
24 thread_id=thread.id,
25 assistant_id=assistant.id,
26 instructions="Please address the user as Jane Doe. The user has a premium account.",
27 stream=True,
28)
29
30for event in stream:
31 print(event.model_dump_json(indent=2, exclude_unset=True))
32
33client.beta.assistants.delete(assistant.id)
34