openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.20.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/assistant.py

38lines · modecode

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