openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
examples/assistant_stream.py
33lines · modecode
| 1 | import openai |
| 2 | |
| 3 | # gets API Key from environment variable OPENAI_API_KEY |
| 4 | client = openai.OpenAI() |
| 5 | |
| 6 | assistant = 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 | |
| 13 | thread = client.beta.threads.create() |
| 14 | |
| 15 | message = 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 | |
| 21 | print("starting run stream") |
| 22 | |
| 23 | stream = 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 | |
| 30 | for event in stream: |
| 31 | print(event.model_dump_json(indent=2, exclude_unset=True)) |
| 32 | |
| 33 | client.beta.assistants.delete(assistant.id) |
| 34 | |