-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdemo.py
More file actions
164 lines (147 loc) · 4.31 KB
/
demo.py
File metadata and controls
164 lines (147 loc) · 4.31 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import openai
import ast
import json
import os
from typing_extensions import override
from instructions import instructions
from functions import zero_shot_predict
client = openai.OpenAI(
api_key = "COPY-API-KEY-HERE"
)
def create_assistant(name="", instructions="", model="gpt-4-turbo", tools=None, debug=False):
assistant_file_path = "assistant.json"
assistant_json = []
# load existing assistant json
if os.path.exists(assistant_file_path):
with open(assistant_file_path, "r") as file:
assistant_json = json.load(file)
for assistant_data in assistant_json:
assistant_name = assistant_data["assistant_name"]
if assistant_name == name:
assistant_id = assistant_data["assistant_id"]
if debug:
print("Loading existing Assistant ID")
if debug:
print("Assistant ID: ", assistant_id)
return assistant_id
# create Assistant
assistant = client.beta.assistants.create(
name=name,
instructions=instructions,
model=model,
tools=tools
)
assistant_id = assistant.id
assistant_name = assistant.name
# save Assistant info
assistant_json.append(
{
"assistant_name": assistant_name,
"assistant_id": assistant_id
}
)
with open(assistant_file_path, "w", encoding="utf-8") as file:
json.dump(assistant_json, file, ensure_ascii=False, indent=4)
print("New Assistant info saved")
return assistant_id
DEBUG = False
assistant_id = create_assistant(
name="iBioFAB co-pilot",
instructions=instructions,
model="gpt-4-turbo",
tools=[
{
"type": "file_search"
},
# {
# "type": "code_interpreter"
# },
{
"type": "function",
"function": {
"name": "get-protein-zero-shot",
"description": "Obtain user input protein for improvement and for zero-shot prediction",
"parameters": {
"type": "object",
"properties": {
"protein_name":{
"type": "string",
"description": "The name of the user input protein"
}
},
"required": ["protein_name"]
}
}
}
# {
# "type": "function", add more funtions here
# },
],
debug=DEBUG
)
funcs = [zero_shot_predict]
assistant = client.beta.assistants.update(
assistant_id=assistant_id
)
thread = client.beta.threads.create(
messages=[
{
"role": "user",
'content': input("How can I help?")
}
]
)
thread_id = thread.id
run = client.beta.threads.runs.create_and_poll(
thread_id=thread.id,
assistant_id=assistant_id,
)
if run.status == 'completed':
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
if DEBUG:
print(messages)
print(messages.data[0].content[0].text.value)
else:
if DEBUG:
print(run.status)
tool_outputs = []
for tool in run.required_action.submit_tool_outputs.tool_calls:
if tool.function.name == "get-protein-zero-shot":
try:
args = ast.literal_eval(tool.function.arguments)
zero_shot_predict(args['protein_name'])
tool_outputs.append({
"tool_call_id": tool.id,
"output": "success"
})
except:
tool_outputs.append({
"tool_call_id": tool.id,
"output": "failed"
})
# elif tool.function.name == 'add more functions here'
else:
pass
# Submit all tool outputs at once after collecting them in a list
if tool_outputs:
try:
run = client.beta.threads.runs.submit_tool_outputs_and_poll(
thread_id=thread.id,
run_id=run.id,
tool_outputs=tool_outputs
)
if DEBUG:
print("Tool outputs submitted successfully.")
except Exception as e:
print("Failed to submit tool outputs:", e)
else:
print("No tool outputs to submit.")
if run.status == 'completed':
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
print(messages.data[0].content[0].text.value)
else:
print('not completed')