openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.24.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/assistant_stream.py

33lines · modeblame

5429f696Stainless Bot2 years ago1import openai
2
3# gets API Key from environment variable OPENAI_API_KEY
4client = openai.OpenAI()
5
6assistant = client.beta.assistants.create(
7name="Math Tutor",
8instructions="You are a personal math tutor. Write and run code to answer math questions.",
9tools=[{"type": "code_interpreter"}],
10model="gpt-4-1106-preview",
11)
12
13thread = client.beta.threads.create()
14
15message = client.beta.threads.messages.create(
16thread_id=thread.id,
17role="user",
18content="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(
24thread_id=thread.id,
25assistant_id=assistant.id,
26instructions="Please address the user as Jane Doe. The user has a premium account.",
27stream=True,
28)
29
30for event in stream:
31print(event.model_dump_json(indent=2, exclude_unset=True))
32
33client.beta.assistants.delete(assistant.id)