-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathchatcompletion_async_vs_sync.py
70 lines (57 loc) · 1.96 KB
/
chatcompletion_async_vs_sync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import sys
if "init_modules" in globals():
# second or subsequent run: remove all but initially loaded modules
for m in list(sys.modules.keys()):
if m not in init_modules:
del sys.modules[m]
else:
# first run: find out which modules were initially loaded
init_modules = list(sys.modules.keys())
import os
import openai
from log10.load import log10, log10_session
openai.api_key = os.getenv("OPENAI_API_KEY")
# Launch an async run
with log10_session():
log10(openai, DEBUG_=True, USE_ASYNC_=True)
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": "You are the most knowledgable Star Wars guru on the planet",
},
{
"role": "user",
"content": "Write the time period of all the Star Wars movies and spinoffs?",
},
],
)
print(completion.choices[0].message)
# reload modules to prevent double calling openAI
if "init_modules" in globals():
# second or subsequent run: remove all but initially loaded modules
for m in list(sys.modules.keys()):
if m not in init_modules:
del sys.modules[m]
else:
# first run: find out which modules were initially loaded
init_modules = list(sys.modules.keys())
import openai # noqa
# Compare to sync run - note there can be variability in the OpenAI calls
with log10_session():
log10(openai, DEBUG_=True, USE_ASYNC_=False)
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": "You are the most knowledgable Star Wars guru on the planet",
},
{
"role": "user",
"content": "Write the time period of all the Star Wars movies and spinoffs?",
},
],
)
print(completion.choices[0].message)