Spaces:
Running
Running
| from datetime import datetime | |
| from _utils.custom_exception_handler import custom_exception_handler_without_api_handler | |
| from _utils.gerar_documento import gerar_documento | |
| from _utils.gerar_documento_utils.GerarDocumento import ( | |
| GerarDocumento, | |
| GerarDocumentoUtils, | |
| ) | |
| from _utils.langchain_utils.Prompt_class import Prompt | |
| from _utils.utils import convert_markdown_to_HTML | |
| from setup.logging import Axiom | |
| from setup.easy_imports import ( | |
| Response, | |
| AsyncAPIView, | |
| extend_schema, | |
| ) | |
| from simple_llm.serializer import SimpleLLMSerializer | |
| class SimpleLLMView(AsyncAPIView): | |
| # parser_classes = [MultiPartParser] | |
| serializer = {} | |
| axiom_instance = Axiom() | |
| async def post(self, request): | |
| try: | |
| self.axiom_instance.generate_new_uuid() | |
| print(f"\n\nDATA E HORA DA REQUISIÇÃO: {datetime.now()}") | |
| self.axiom_instance.send_axiom( | |
| f"COMEÇOU NOVA REQUISIÇÃO - request.data: {request.data}" | |
| ) | |
| serializer = SimpleLLMSerializer(data=request.data) | |
| if serializer.is_valid(raise_exception=True): | |
| obj = serializer.get_obj() # type: ignore | |
| if not serializer.validated_data: | |
| raise ValueError("Erro no validated_data") | |
| self.serializer = obj | |
| listaPDFs = [l.link_arquivo for l in obj.files] | |
| self.axiom_instance.send_axiom(f"listaPDFs: {listaPDFs}") | |
| summarizer = GerarDocumentoUtils(self.axiom_instance) | |
| prompt_instance = Prompt() | |
| prompt = prompt_instance.create_and_invoke_prompt( | |
| obj.prompt, | |
| dynamic_dict={"context": obj.user_text}, | |
| ) | |
| resposta_llm = ( | |
| await summarizer.checar_se_resposta_vazia_do_documento_final( | |
| obj.llm_ultimas_requests, prompt.to_string() | |
| ) | |
| ) | |
| self.axiom_instance.send_axiom(f"resposta_llm: {resposta_llm}") | |
| texto_completo_como_html = convert_markdown_to_HTML( | |
| resposta_llm | |
| ).replace("resposta_segunda_etapa:", "<br><br>") | |
| self.axiom_instance.send_axiom( | |
| f"texto_completo_como_html: {texto_completo_como_html}" | |
| ) | |
| return Response({"resposta": texto_completo_como_html}) | |
| except Exception as e: | |
| custom_exception_handler_without_api_handler( | |
| e, serializer, self.axiom_instance | |
| ) | |
| raise | |