-
Notifications
You must be signed in to change notification settings - Fork 0
/
summary_chatgpt.py
66 lines (40 loc) · 1.15 KB
/
summary_chatgpt.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
from openai import OpenAI
import json
api_key = ''
summary_assistant_id = ''
def run_summary(contents):
summary = ''
client = OpenAI(api_key=api_key)
thread = client.beta.threads.create()
message = client.beta.threads.messages.create(
thread_id=thread.id,
role='user',
content=contents
)
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=summary_assistant_id
)
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
while True:
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
if run.status != 'in_progress':
if run.status == 'completed':
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
summary = messages.data[0].content[0].text.value
client.beta.threads.delete(thread_id=thread.id)
break
else:
# print(run)
summary = ''
break
json_data = {'summary': summary}
return json.dumps(json_data, ensure_ascii=False)