import time import json from openai import OpenAI import gradio as gr class Backend: def __init__(self): self.return_instruction = """ Please only return in the following Json format: {{ "Answer": "", "Reference Sentences": [""] }}""" self.chat_history = [] def load_agent(self, openai_api_key, assistant_id): client = OpenAI(api_key=openai_api_key) assistant = client.beta.assistants.retrieve(assistant_id=assistant_id) return client, assistant def update_file(self, file_path): file = open(file_path, 'rb') file = self.client.files.create(file=file, purpose='assistants') return file def create_thread(self): thread = self.client.beta.threads.create() return thread def delate_thread(self, thread): self.client.beta.threads.delete(thread.id) def create_message(self, question, thread, file): message = self.client.beta.threads.messages.create( thread_id=thread.id, role="user", content= question + self.return_instruction, file_ids=[file.id] ) return message def delate_message(self, message): self.client.beta.threads.messages.delete(message.id) def create_run(self, thread, assistant): run = self.client.beta.threads.runs.create( thread_id=thread.id, assistant_id=assistant.id, # instructions="""Please read PDF and answer the qusetions asked by users with professional knowledge.""" ) return run def delate_run(self, run): self.client.beta.threads.runs.delete(run.id) def get_massage(self, thread): messages = self.client.beta.threads.messages.list( thread_id=thread.id ) return messages def phrase_massage(self, question, messages): mess = json.loads(messages.json()) output = mess['data'][0]['content'][0]['text']['value'] print(output) try: output = output.split("{")[1:] output = "{" + "".join(output) output = output.split("}")[:-1] output = "".join(output) + "}" print(output) output = eval(output) answer = output['Answer'] reference = output['Reference Sentences'] except: self.detete_message(message) answer = output reference = [] reference = ' '.join(reference) reference = self.processing_html(reference) self.chat_history.append([question, answer]) return self.chat_history, reference def phrase_massage_1(self, question, messages): mess = json.loads(messages.json()) output = mess['data'][0]['content'][0]['text']['value'] self.chat_history.append([question, output]) return self.chat_history def processing_html(self, text): return f'
{text}