-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassistente_base.py
More file actions
58 lines (50 loc) · 1.94 KB
/
assistente_base.py
File metadata and controls
58 lines (50 loc) · 1.94 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
from openai import OpenAI
from openai.types.beta.assistant import Assistant
from openai.types.beta.thread import Thread
from dotenv import load_dotenv
import os
from abc import ABC, abstractmethod
class AssistenteBase(ABC):
def __init__(self, nome : str, instrucoes : str, caminho_arquivo : str):
load_dotenv()
self.cliente : OpenAI = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
self.id_arquivo : str = self.associar_arquivo(caminho_arquivo=caminho_arquivo)
self.assistente : Assistant = self.criar_assistente(nome=nome, instrucoes=instrucoes, file_id=self.id_arquivo)
self.thread : Thread = self.criar_thread()
@abstractmethod
def gerar_resposta(self):
pass
def associar_arquivo(self, caminho_arquivo : str):
novo_arquivo = self.cliente.files.create(
file=open(caminho_arquivo, "rb"),
purpose="assistants"
)
return novo_arquivo.id
def criar_assistente(self, nome : str, instrucoes : str, modelo = "gpt-4o", file_id = ""):
novo_assistente = self.cliente.beta.assistants.create(
name=nome,
instructions=instrucoes,
model=modelo,
tools=[
{
"type":"code_interpreter"
}
],
tool_resources={
"code_interpreter":{
"file_ids":[file_id]
}
}
)
return novo_assistente
def criar_thread(self):
return self.cliente.beta.threads.create()
def apagar_arquivo_openai(self):
resposta = self.cliente.files.delete(self.id_arquivo)
return resposta
def apagar_assistente(self):
resposta = self.cliente.beta.assistants.delete(self.assistente.id)
return resposta
def apagar_thread(self):
resposta = self.cliente.beta.threads.delete(self.threads.id)
return resposta