forked from tommasov03/N.O.V.A.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.py
45 lines (37 loc) · 1.38 KB
/
ai.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
import time
from openai import OpenAI
import os
import configparser
import re
config = configparser.ConfigParser()
config.read('config.cfg')
KEY = config.get('OpenAIAPI', 'key').strip('"')
ASSID = config.get('OpenAIAPI', 'assistant_id').strip('"')
class Ai:
def __init__(self):
self.client = OpenAI(api_key=KEY)
self.ASSISTANT_ID = ASSID
def ask(self, domanda):
os.environ['OPENAI_API_KEY'] = KEY
thread = self.client.beta.threads.create(
messages=[
{
"role": "user",
"content": domanda,
}
]
)
run = self.client.beta.threads.runs.create(thread_id=thread.id, assistant_id=self.ASSISTANT_ID)
print(f"thread creato: {run.id}")
while run.status != "completed":
run = self.client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
print(f"stato: {run.status}")
time.sleep(1)
else:
print(f"thread completato")
message_response = self.client.beta.threads.messages.list(thread_id=thread.id)
messages = message_response.data
latest_message = messages[0]
# Correzione dell'espressione regolare
out = re.sub(r'【\d+(:\d+)?†source】', '', latest_message.content[0].text.value)
return out