-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt_analysis.py
59 lines (46 loc) · 1.6 KB
/
gpt_analysis.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
from openai import OpenAI
import time
def gpt_parse(assistant_prompt, prompt, paper):
client = OpenAI()
assistant = client.beta.assistants.create(
name=assistant_prompt[0],
instructions=assistant_prompt[1],
model=assistant_prompt[2],
tools=[{"type": "file_search"}],
temperature=.1
)
# Upload the user provided file to OpenAI
message_file = client.files.create(
file=open(paper, "rb"),
purpose="assistants"
)
# Create a thread and attach the file to the message
thread = client.beta.threads.create(
messages=[
{
"role": "user",
"content": prompt,
# Attach the new file to the message.
"attachments": [
{"file_id": message_file.id, "tools": [{"type": "file_search"}]}
],
}
]
)
# Then, we use the stream SDK helper
# with the EventHandler class to create the Run
# and stream the response.
with client.beta.threads.runs.stream(
thread_id=thread.id,
assistant_id=assistant.id,
) as stream:
stream.until_done()
messages = client.beta.threads.messages.list(
thread_id=thread.id)
while messages.data[0].role == 'user':
time.sleep(1)
messages = client.beta.threads.messages.list(
thread_id=thread.id)
return "user" #terrible workaround that does not work... not sure why program sometimes returns message from user
#pls fix
return (messages.data[0].content[0].text.value)