-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathutils.py
90 lines (69 loc) · 3.06 KB
/
utils.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
90
import zipfile
import os
import yaml
import openai
import streamlit as st
def clean_environment():
"""Clean up environment variables."""
if "OPENAI_API_KEY" in os.environ:
del os.environ["OPENAI_API_KEY"]
if "OPENAI_ORGANIZATION_ID" in os.environ:
del os.environ["OPENAI_ORGANIZATION_ID"]
if "OPENAI_DEFAULT_ORGANIZATION_ID" in os.environ:
del os.environ["OPENAI_DEFAULT_ORGANIZATION_ID"]
def upload_to_openai(file):
"""Upload a file to OpenAI and return its file ID."""
with open(file.name, "rb") as f:
response = openai.files.create(file=f.read(), purpose="assistants")
return response.id if response else None
def export_assistant(nome_assistente, modello_assistente, prompt_sistema, file_up):
file_yaml = open("config_assistente.yaml", "w")
file_yaml.write("name: " + nome_assistente + "\n")
file_yaml.write("model: " + modello_assistente + "\n")
file_yaml.close()
#Crea file.txt per sistem_prompt
file_prompt = open("prompt.txt", "w")
file_prompt.write(prompt_sistema)
file_prompt.close()
#CREO IL FILE ZIP
zip_file = zipfile.ZipFile("config_assistente.zip", "w")
zip_file.write("config_assistente.yaml")
zip_file.write("prompt.txt")
if file_up:
for file in file_up:
with open(file.name, "rb") as f:
zip_file.write(file.name)
zip_file.close()
return open("config_assistente.zip", "rb")
def create_assistant_from_config_file(file_up, client):
stored_file = []
with st.spinner("Estrazione e caricamento file in corso..."):
#cambia l'estensione del file da .iaItaliaBotConfig a .zip
with open("config_assistente.zip", "wb") as f:
f.write(file_up.getbuffer())
f.close()
with zipfile.ZipFile("config_assistente.zip", "r") as zip_ref:
zip_ref.extractall("temp_folder")
with open("temp_folder/config_assistente.yaml", "r") as yaml_file:
config_data = yaml.safe_load(yaml_file)
nome_assistente = config_data.get('name', '')
modello_assistente = config_data.get('model', '')
st.write("Nome Assistente: " + nome_assistente)
st.write("Modello Assistente: " + modello_assistente)
with open("temp_folder/prompt.txt", "r") as prompt_file:
prompt_sistema = prompt_file.read()
if os.path.exists("temp_folder"):
for root, dirs, files in os.walk("temp_folder"):
for file in files:
if file != "config_assistente.yaml" and file != "prompt.txt":
additional_file_id = upload_to_openai(open(os.path.join(root, file), "rb"))
if additional_file_id:
stored_file.append(additional_file_id)
my_assistant = client.beta.assistants.create(
instructions=prompt_sistema,
name=nome_assistente,
tools=[{"type": "retrieval"}],
model=modello_assistente,
file_ids=stored_file,
)
return my_assistant