-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface_chat.py
More file actions
78 lines (61 loc) · 3.45 KB
/
interface_chat.py
File metadata and controls
78 lines (61 loc) · 3.45 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from openai import OpenAI
from assistente_base import AssistenteBase
from assistente_commit import AssistenteCommit
from assistente_documentacao import AssistenteDocumentacao
from assistente_revisao import AssistenteRevisao
from assistente_teste_unitario import AssistenteTesteUnitario
from util_io import salvar_resposta_ia, salvar_resposta_ia_binario
import os
STATUS_COMPLETED = "completed"
class InterfaceChat():
def __init__(self, assistente):
self.chat : AssistenteBase = assistente
self.mensagens = []
def salvar_resposta(self):
lista_respostas = []
for mensagem in self.mensagens:
lista_respostas.append(mensagem.text.value)
dados_salvos = "".join(lista_respostas)
for mensagem in self.mensagens:
if hasattr(mensagem, "text") and mensagem.text is not None:
for annotation in mensagem.text.annotations:
if hasattr(annotation, "file_path") and annotation.file_path is not None:
file_id = annotation.file_path.file_id
if file_id is not None:
resposta_open_ai = self.chat.cliente.files.retrieve(file_id)
arquivo_binario_dados = self.chat.cliente.files.content(resposta_open_ai.id)
arquivo_dados = arquivo_binario_dados.read()
if isinstance(self.chat, AssistenteDocumentacao):
salvar_resposta_ia_binario(self.chat.caminho_arquivo, arquivo_dados, "Codigo-Documentacao")
elif isinstance(self.chat, AssistenteRevisao):
salvar_resposta_ia_binario(self.chat.caminho_arquivo, arquivo_dados, "Codigo-Revisao")
elif isinstance(self.chat, AssistenteTesteUnitario):
salvar_resposta_ia_binario(self.chat.caminho_arquivo, arquivo_dados, "Codigo-Teste")
lista_respostas.append(mensagem.text.value)
dados_salvos = "".join(lista_respostas)
if isinstance(self.chat, AssistenteCommit):
salvar_resposta_ia(self.chat.caminho_arquivo, dados_salvos, "Commit")
elif isinstance(self.chat, AssistenteDocumentacao):
salvar_resposta_ia(self.chat.caminho_arquivo, dados_salvos, "Documentacao")
elif isinstance(self.chat, AssistenteRevisao):
salvar_resposta_ia(self.chat.caminho_arquivo, dados_salvos, "Documentacao")
def conversar(self, mensagem_usuario : str):
self.chat.cliente.beta.threads.messages.create(
thread_id=self.chat.thread.id,
role="user",
content=mensagem_usuario
)
run = self.chat.cliente.beta.threads.runs.create_and_poll(
thread_id=self.chat.thread.id,
assistant_id=self.chat.assistente.id
)
if run.status == STATUS_COMPLETED:
self.mensagens = self.chat.cliente.beta.threads.messages.list(
thread_id=self.chat.thread.id
).data[0].content
self.salvar_resposta()
return self.mensagens
def apagar_assistente_completamente(self):
self.chat.apagar_arquivo_openai()
self.chat.apagar_thread()
self.chat.apagar_assistente()