from typing import Any, Dict, cast from setup.easy_imports import ( Response, AsyncAPIView, APIView, MultiPartParser, extend_schema, ) from datetime import datetime from _utils.handle_files import handle_pdf_files_from_serializer, remove_pdf_temp_files from _utils.resumo_completo_cursor import ( get_llm_summary_answer_by_cursor_complete, ) from _utils.gerar_relatorio_modelo_usuario.prompts import prompt_auxiliar_inicio from .serializer import ( GerarDocumentoComPDFProprioSerializer, GerarDocumentoSerializer, ) class GerarDocumentoView(AsyncAPIView): # parser_classes = [MultiPartParser] @extend_schema( request=GerarDocumentoSerializer, ) async def post(self, request): print(f"\n\nDATA E HORA DA REQUISIÇÃO: {datetime.now()}") serializer = GerarDocumentoSerializer(data=request.data) if serializer.is_valid(raise_exception=True): if not serializer.validated_data: raise ValueError("Erro no validated_data") data = cast(Dict[str, Any], serializer.validated_data) print("\n\ndata: ", data) data["prompt_auxiliar"] = ( prompt_auxiliar_inicio + "\n" + data["prompt_auxiliar"] ) # listaPDFs = handle_pdf_files_from_serializer(data["files"]) listaPDFs = [l["link_arquivo"] for l in data["files"]] print("\n\nlistaPDFs: ", listaPDFs) resposta_llm = await get_llm_summary_answer_by_cursor_complete( data, listaPDFs ) print("\n\nresposta_llm: ", resposta_llm) # remove_pdf_temp_files(listaPDFs) return Response({"resposta": resposta_llm}) class GerarDocumentoComPDFProprioView(AsyncAPIView): parser_classes = [MultiPartParser] @extend_schema( request=GerarDocumentoComPDFProprioSerializer, ) async def post(self, request): print(f"\n\nDATA E HORA DA REQUISIÇÃO: {datetime.now()}") serializer = GerarDocumentoComPDFProprioSerializer(data=request.data) if serializer.is_valid(raise_exception=True): data = cast(Dict[str, Any], serializer.validated_data) print("\n\ndata: ", data) listaPDFs = handle_pdf_files_from_serializer(data["files"]) resposta_llm = await get_llm_summary_answer_by_cursor_complete( data, listaPDFs ) print("\n\nresposta_llm: ", resposta_llm) remove_pdf_temp_files(listaPDFs) return Response({"resposta": resposta_llm})