Skip to content

Commit f256e80

Browse files
Merge pull request #73 from openai/dev/logan/add-assistants
Add assistants example and move to the multi example structure
2 parents 88fb67f + 1e4b75c commit f256e80

File tree

5 files changed

+93
-7
lines changed

5 files changed

+93
-7
lines changed

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# OpenAI API Quickstart - Python example app
1+
# OpenAI API Quickstart - Python
22

3-
This is an example chat app intended to get you started with your first OpenAI API project. It uses the [Chat Completions API](https://platform.openai.com/docs/api-reference/chat) to create a simple general purpose chat app with streaming.
3+
This repository hosts multiple quickstart apps for different OpenAI API endpoints (chat, assistants, etc). Check out the `examples` folder to try out different examples and get started using the OpenAI API.
44

55
## Basic request
66

@@ -21,8 +21,6 @@ completion = client.chat.completions.create(
2121
print(completion.choices[0].message)
2222
```
2323

24-
This quickstart app builds on top of the example code above, with streaming and a UI to visualize messages.
25-
2624
## Setup
2725

2826
1. If you don’t have Python installed, install it [from Python.org](https://www.python.org/downloads/).
@@ -38,6 +36,7 @@ This quickstart app builds on top of the example code above, with streaming and
3836
4. Create a new virtual environment:
3937

4038
- macOS:
39+
4140
```bash
4241
$ python -m venv venv
4342
$ . venv/bin/activate
@@ -65,8 +64,16 @@ This quickstart app builds on top of the example code above, with streaming and
6564

6665
8. Run the app:
6766

68-
```bash
69-
$ flask run
70-
```
67+
This step depends on the app itself. If the code uses flask (like the chat-basic example), you can run:
68+
69+
```bash
70+
$ flask run
71+
```
7172

7273
You should now be able to access the app from your browser at the following URL: [http://localhost:5000](http://localhost:5000)!
74+
75+
If the code is just a simple Python script, you can run it with:
76+
77+
```bash
78+
$ python my_file.py
79+
```

examples/assistant-basic/assistant.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from openai import OpenAI
2+
from time import sleep
3+
4+
client = OpenAI()
5+
starting_assistant = ""
6+
starting_thread = ""
7+
8+
9+
def create_assistant():
10+
if starting_assistant == "":
11+
my_assistant = client.beta.assistants.create(
12+
instructions="You are a helpful assistant.",
13+
name="MyQuickstartAssistant",
14+
model="gpt-3.5-turbo",
15+
)
16+
else:
17+
my_assistant = client.beta.assistants.retrieve(starting_assistant)
18+
19+
return my_assistant
20+
21+
22+
def create_thread():
23+
if starting_thread == "":
24+
thread = client.beta.threads.create()
25+
else:
26+
thread = client.beta.threads.retrieve(starting_thread)
27+
28+
return thread
29+
30+
31+
def send_message(thread_id, message):
32+
thread_message = client.beta.threads.messages.create(
33+
thread_id,
34+
role="user",
35+
content=message,
36+
)
37+
return thread_message
38+
39+
40+
def run_assistant(thread_id, assistant_id):
41+
run = client.beta.threads.runs.create(
42+
thread_id=thread_id, assistant_id=assistant_id
43+
)
44+
return run
45+
46+
47+
def get_newest_message(thread_id):
48+
thread_messages = client.beta.threads.messages.list(thread_id)
49+
return thread_messages.data[0]
50+
51+
52+
def get_run_status(thread_id, run_id):
53+
run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run_id)
54+
return run.status
55+
56+
57+
def main():
58+
my_assistant = create_assistant()
59+
my_thread = create_thread()
60+
61+
while True:
62+
user_message = input("Enter your message: ")
63+
if user_message.lower() == "exit":
64+
break
65+
66+
send_message(my_thread.id, user_message)
67+
run = run_assistant(my_thread.id, my_assistant.id)
68+
while run.status != "completed":
69+
run.status = get_run_status(my_thread.id, run.id)
70+
sleep(1)
71+
print("⏳", end="\r", flush=True)
72+
73+
sleep(0.5)
74+
response = get_newest_message(my_thread.id)
75+
print("Response:", response.content[0].text.value)
76+
77+
78+
if __name__ == "__main__":
79+
main()
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)