-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.py
65 lines (55 loc) · 1.77 KB
/
functions.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
import json
import os
from openai import OpenAI
from prompts import assistant_instructions
from dotenv import load_dotenv
load_dotenv()
from tools.mathematica import tool_wolframalpha
# OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
OPENAI_API_KEY = "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"
# Init OpenAI Client
client = OpenAI(api_key=OPENAI_API_KEY)
def create_assistant(client, name, description, file_path, tools, prompt):
assistant_file_path = 'preset.json'
# Load existing data
if os.path.exists(assistant_file_path):
with open(assistant_file_path, 'r') as file:
data = json.load(file)
else:
data = {}
# Check if assistant already exists
for key, assistant_data in data.items():
if assistant_data['name'] == name:
print(f"Loaded existing assistant ID for {name}.")
return assistant_data['assistant_id']
# If no assistant.json is present, create a new assistant using the below specifications
if not os.path.exists(file_path):
file = client.files.retrieve("file-Wp3snAIbfaGk3Riy6TAwiffZ")
else:
file = client.files.create(file=open(file_path, "rb"), purpose='assistants')
assistant = client.beta.assistants.create(
name=name,
description=description,
instructions=prompt,
model="gpt-3.5-turbo-1106",
tools=[
{
'type': tools,
},
tool_wolframalpha,
],
file_ids=[file.id])
# Add new assistant data
data[str(len(data) + 1)] = {
'name': name,
'assistant_id': assistant.id,
'tools': tools,
'file_path': file_path,
'description': description,
'prompt': prompt
}
# Save updated data
with open(assistant_file_path, 'w') as file:
json.dump(data, file)
print(f"Created a new assistant named {name} and saved the ID.")
return assistant.id