-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtool_user.py
89 lines (71 loc) · 3.03 KB
/
tool_user.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
Create an assistant using the tools from tool_creator using the assistant creation API
"""
import os
import json
from user_config import AssistantConfig as UserConfig
from utils import chat as chat_loop
from openai import OpenAI
client = OpenAI() # be sure to set your OPENAI_API_KEY environment variable
def create_tool_user(assistant_details):
# create the assistant
tool_user = client.beta.assistants.create(**assistant_details["build_params"])
print(f"Created assistant {tool_user.id} to use tools\n\n" + 90*"-" + "\n\n", flush=True)
# save the assistant info to a json file
info_to_export = {
"assistant_id": tool_user.id,
"assistant_details": assistant_details,
}
os.makedirs('assistants', exist_ok=True)
with open('assistants/tool_user.json', 'w') as f:
json.dump(info_to_export, f, indent=4)
return tool_user
def talk_to_tool_user(assistant_details):
"""
talk to the assistant to use the tools
"""
# check if json file exists
try:
os.makedirs('assistants', exist_ok=True)
with open('assistants/tool_user.json') as f:
create_new = input(f'Assistant details found in tool_user.json. Create a new assistant? [y/N]')
if create_new == 'y':
raise Exception("User wants a new assistant")
assistant_from_json = json.load(f)
tool_user = client.beta.assistants.retrieve(assistant_from_json['assistant_id'])
print(f"Loaded assistant details from tool_user.json\n\n" + 90*"-" + "\n\n", flush=True)
print(f'Assistant {tool_user.id}:\n')
assistant_details = assistant_from_json["assistant_details"]
except:
# create the assistant first
tool_user = create_tool_user(assistant_details)
# gather the dependencies
dependencies = assistant_details["dependencies"]
if dependencies:
print(f"Installing dependencies...", flush=True)
for d in dependencies:
os.system(f"pip install {d}")
print(f"Installed dependencies\n\n" + 90*"-" + "\n\n", flush=True)
# exec the functions from the py files
os.makedirs('tools', exist_ok=True)
functions = assistant_details["functions"]
for func in functions:
print(f"Loading function {func} into execution environment", flush=True)
try:
with open('tools/' + func + '.py') as f:
exec(f.read(), globals())
functions.update({func: eval(func)})
except Exception as e:
print(f"Exception loading function {func}: {e}", flush=True)
print(f"Continuing without {func}...", flush=True)
print(f"Loaded functions\n\n" + 90*"-" + "\n\n", flush=True)
# Create thread
thread = client.beta.threads.create()
# chat with the assistant
chat_loop(client, thread, tool_user, functions)
def main():
# create the tool user assistant and chat to test your tools
user_details = UserConfig().assistant_details
talk_to_tool_user(user_details)
if __name__ == '__main__':
main()