Spaces:
Running
Running
luanpoppe
commited on
Commit
·
095b5f1
1
Parent(s):
de78af1
feat: parando de esperar todo processamento terminar para enviar a resposta da requisição
Browse files
_utils/bubble_integrations/enviar_resposta_final.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
def enviar_resposta_final(doc_id: str, form_response_id: str, version: str, texto_completo: str, error: bool = False):
|
| 5 |
+
url = f"https://vella.app.br/version-{version}/api/1.1/wf/texto_completo"
|
| 6 |
+
headers = {"Authorization": f"Bearer {os.environ.get("BUBBLE_TOKEN")}"}
|
| 7 |
+
|
| 8 |
+
body = {
|
| 9 |
+
"doc_id": doc_id,
|
| 10 |
+
"form_response_id": form_response_id,
|
| 11 |
+
"texto_completo": texto_completo,
|
| 12 |
+
"erro": error,
|
| 13 |
+
}
|
| 14 |
+
requests.post(url, body, headers=headers)
|
_utils/custom_exception_handler.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# myapp/exception_handler.py
|
| 2 |
+
|
| 3 |
+
from typing import Dict
|
| 4 |
+
from rest_framework.views import exception_handler
|
| 5 |
+
import logging
|
| 6 |
+
|
| 7 |
+
from _utils.bubble_integrations.enviar_resposta_final import enviar_resposta_final
|
| 8 |
+
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def custom_exception_handler(exc, context):
|
| 13 |
+
# Call REST framework's default exception handler first
|
| 14 |
+
response = exception_handler(exc, context)
|
| 15 |
+
serializer: Dict = context["view"].serializer
|
| 16 |
+
|
| 17 |
+
enviar_resposta_final(
|
| 18 |
+
serializer.get("doc_id", ""),
|
| 19 |
+
serializer.get("form_response_id", ""),
|
| 20 |
+
serializer.get("version", ""),
|
| 21 |
+
serializer.get("texto_completo", ""),
|
| 22 |
+
True,
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
return response
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def your_additional_function(exc, context):
|
| 29 |
+
# Implement your custom logic here
|
| 30 |
+
# For example, send an email notification or perform other actions
|
| 31 |
+
pass
|
_utils/gerar_relatorio_modelo_usuario/utils.py
CHANGED
|
@@ -29,7 +29,7 @@ def gerar_resposta_compilada(serializer):
|
|
| 29 |
"chunk_size": serializer["chunk_size"],
|
| 30 |
"chunk_overlap": serializer["chunk_overlap"],
|
| 31 |
# "prompt_auxiliar": serializer["prompt_auxiliar"],
|
| 32 |
-
"prompt_gerar_documento": serializer["prompt_gerar_documento"],
|
| 33 |
}
|
| 34 |
|
| 35 |
|
|
|
|
| 29 |
"chunk_size": serializer["chunk_size"],
|
| 30 |
"chunk_overlap": serializer["chunk_overlap"],
|
| 31 |
# "prompt_auxiliar": serializer["prompt_auxiliar"],
|
| 32 |
+
"prompt_gerar_documento": serializer["prompt_gerar_documento"][0:200],
|
| 33 |
}
|
| 34 |
|
| 35 |
|
_utils/resumo_completo_cursor.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from _utils.gerar_relatorio_modelo_usuario.prompts import prompt_auxiliar_SEM_CONTEXT
|
| 3 |
from _utils.gerar_relatorio_modelo_usuario.GerarDocumento import (
|
| 4 |
GerarDocumento,
|
|
@@ -101,7 +102,6 @@ async def get_llm_summary_answer_by_cursor_complete(
|
|
| 101 |
)
|
| 102 |
|
| 103 |
llm_ultimas_requests = serializer["llm_ultimas_requests"]
|
| 104 |
-
# Generate enhanced summary
|
| 105 |
print("\nCOMEÇANDO A FAZER ÚLTIMA REQUISIÇÃO")
|
| 106 |
structured_summaries = await summarizer.gerar_documento_final(
|
| 107 |
vector_store, bm25, chunk_ids, llm_ultimas_requests, prompt_auxiliar_SEM_CONTEXT
|
|
@@ -117,9 +117,22 @@ async def get_llm_summary_answer_by_cursor_complete(
|
|
| 117 |
|
| 118 |
for x in structured_summaries:
|
| 119 |
texto_completo = texto_completo + x["content"] + "\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
return {
|
| 122 |
-
"resultado": structured_summaries,
|
| 123 |
"texto_completo": texto_completo,
|
|
|
|
| 124 |
"parametros-utilizados": gerar_resposta_compilada(serializer),
|
| 125 |
}
|
|
|
|
| 1 |
import os
|
| 2 |
+
from _utils.bubble_integrations.enviar_resposta_final import enviar_resposta_final
|
| 3 |
from _utils.gerar_relatorio_modelo_usuario.prompts import prompt_auxiliar_SEM_CONTEXT
|
| 4 |
from _utils.gerar_relatorio_modelo_usuario.GerarDocumento import (
|
| 5 |
GerarDocumento,
|
|
|
|
| 102 |
)
|
| 103 |
|
| 104 |
llm_ultimas_requests = serializer["llm_ultimas_requests"]
|
|
|
|
| 105 |
print("\nCOMEÇANDO A FAZER ÚLTIMA REQUISIÇÃO")
|
| 106 |
structured_summaries = await summarizer.gerar_documento_final(
|
| 107 |
vector_store, bm25, chunk_ids, llm_ultimas_requests, prompt_auxiliar_SEM_CONTEXT
|
|
|
|
| 117 |
|
| 118 |
for x in structured_summaries:
|
| 119 |
texto_completo = texto_completo + x["content"] + "\n"
|
| 120 |
+
x["source"]["text"] = x["source"]["text"][0:200]
|
| 121 |
+
x["source"]["context"] = x["source"]["context"][0:200]
|
| 122 |
+
|
| 123 |
+
if isBubble:
|
| 124 |
+
print("COMEÇANDO A REQUISIÇÃO FINAL PARA O BUBBLE")
|
| 125 |
+
enviar_resposta_final(
|
| 126 |
+
serializer["doc_id"],
|
| 127 |
+
serializer["form_response_id"],
|
| 128 |
+
serializer["version"],
|
| 129 |
+
texto_completo,
|
| 130 |
+
False,
|
| 131 |
+
)
|
| 132 |
+
print("TERMINOU A REQUISIÇÃO FINAL PARA O BUBBLE")
|
| 133 |
|
| 134 |
return {
|
|
|
|
| 135 |
"texto_completo": texto_completo,
|
| 136 |
+
"resultado": structured_summaries,
|
| 137 |
"parametros-utilizados": gerar_resposta_compilada(serializer),
|
| 138 |
}
|
gerar_documento/serializer.py
CHANGED
|
@@ -49,7 +49,9 @@ class GerarDocumentoSerializer(ResumoCursorSerializer):
|
|
| 49 |
llm_ultimas_requests = serializers.CharField(
|
| 50 |
required=False, default="gemini-2.0-flash"
|
| 51 |
)
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
|
| 54 |
class GerarDocumentoComPDFProprioSerializer(ResumoCursorSerializer):
|
| 55 |
system_prompt = None
|
|
|
|
| 49 |
llm_ultimas_requests = serializers.CharField(
|
| 50 |
required=False, default="gemini-2.0-flash"
|
| 51 |
)
|
| 52 |
+
doc_id = serializers.CharField(required=True)
|
| 53 |
+
form_response_id = serializers.CharField(required=True)
|
| 54 |
+
version = serializers.CharField(required=True)
|
| 55 |
|
| 56 |
class GerarDocumentoComPDFProprioSerializer(ResumoCursorSerializer):
|
| 57 |
system_prompt = None
|
gerar_documento/views.py
CHANGED
|
@@ -16,10 +16,12 @@ from .serializer import (
|
|
| 16 |
GerarDocumentoComPDFProprioSerializer,
|
| 17 |
GerarDocumentoSerializer,
|
| 18 |
)
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
class GerarDocumentoView(AsyncAPIView):
|
| 22 |
# parser_classes = [MultiPartParser]
|
|
|
|
| 23 |
|
| 24 |
@extend_schema(
|
| 25 |
request=GerarDocumentoSerializer,
|
|
@@ -30,31 +32,41 @@ class GerarDocumentoView(AsyncAPIView):
|
|
| 30 |
if serializer.is_valid(raise_exception=True):
|
| 31 |
if not serializer.validated_data:
|
| 32 |
raise ValueError("Erro no validated_data")
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
|
| 43 |
-
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
class GerarDocumentoComPDFProprioView(AsyncAPIView):
|
| 57 |
parser_classes = [MultiPartParser]
|
|
|
|
| 58 |
|
| 59 |
@extend_schema(
|
| 60 |
request=GerarDocumentoComPDFProprioSerializer,
|
|
@@ -65,6 +77,7 @@ class GerarDocumentoComPDFProprioView(AsyncAPIView):
|
|
| 65 |
if serializer.is_valid(raise_exception=True):
|
| 66 |
data = cast(Dict[str, Any], serializer.validated_data)
|
| 67 |
print("\n\ndata: ", data)
|
|
|
|
| 68 |
|
| 69 |
listaPDFs = handle_pdf_files_from_serializer(data["files"])
|
| 70 |
|
|
|
|
| 16 |
GerarDocumentoComPDFProprioSerializer,
|
| 17 |
GerarDocumentoSerializer,
|
| 18 |
)
|
| 19 |
+
import asyncio
|
| 20 |
|
| 21 |
|
| 22 |
class GerarDocumentoView(AsyncAPIView):
|
| 23 |
# parser_classes = [MultiPartParser]
|
| 24 |
+
serializer = {}
|
| 25 |
|
| 26 |
@extend_schema(
|
| 27 |
request=GerarDocumentoSerializer,
|
|
|
|
| 32 |
if serializer.is_valid(raise_exception=True):
|
| 33 |
if not serializer.validated_data:
|
| 34 |
raise ValueError("Erro no validated_data")
|
| 35 |
+
|
| 36 |
+
async def proccess_data_after_response():
|
| 37 |
+
# await asyncio.sleep(0)
|
| 38 |
+
data = cast(Dict[str, Any], serializer.validated_data)
|
| 39 |
+
print("\ndata: ", data)
|
| 40 |
+
self.serializer = data
|
| 41 |
|
| 42 |
+
# data["prompt_auxiliar"] = (
|
| 43 |
+
# prompt_auxiliar_inicio + "\n" + data["prompt_auxiliar"]
|
| 44 |
+
# )
|
| 45 |
|
| 46 |
+
# listaPDFs = handle_pdf_files_from_serializer(data["files"])
|
| 47 |
+
listaPDFs = [l["link_arquivo"] for l in data["files"]]
|
| 48 |
|
| 49 |
+
print("\n\nlistaPDFs: ", listaPDFs)
|
| 50 |
|
| 51 |
+
resposta_llm = await get_llm_summary_answer_by_cursor_complete(
|
| 52 |
+
data, listaPDFs, True
|
| 53 |
+
)
|
| 54 |
+
print("\n\nresposta_llm: ", resposta_llm)
|
| 55 |
|
| 56 |
+
# remove_pdf_temp_files(listaPDFs)
|
| 57 |
|
| 58 |
+
# print("PRÓXIMA LINHA ENVIA A RESPOSTA A QUEM FEZ A REQUISIÇÃO")
|
| 59 |
+
|
| 60 |
+
# asyncio.create_task(proccess_data_after_response())
|
| 61 |
+
loop = asyncio.get_running_loop()
|
| 62 |
+
loop.run_in_executor(None, lambda: asyncio.run(proccess_data_after_response()))
|
| 63 |
+
|
| 64 |
+
return Response({"resposta": "Requisição está sendo processada em segundo plano"})
|
| 65 |
|
| 66 |
|
| 67 |
class GerarDocumentoComPDFProprioView(AsyncAPIView):
|
| 68 |
parser_classes = [MultiPartParser]
|
| 69 |
+
serializer = {}
|
| 70 |
|
| 71 |
@extend_schema(
|
| 72 |
request=GerarDocumentoComPDFProprioSerializer,
|
|
|
|
| 77 |
if serializer.is_valid(raise_exception=True):
|
| 78 |
data = cast(Dict[str, Any], serializer.validated_data)
|
| 79 |
print("\n\ndata: ", data)
|
| 80 |
+
self.serializer = data
|
| 81 |
|
| 82 |
listaPDFs = handle_pdf_files_from_serializer(data["files"])
|
| 83 |
|
setup/settings.py
CHANGED
|
@@ -142,6 +142,7 @@ CORS_ORIGIN_WHITELIST = [
|
|
| 142 |
]
|
| 143 |
|
| 144 |
REST_FRAMEWORK = {
|
|
|
|
| 145 |
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
|
| 146 |
"PAGE_SIZE": 10,
|
| 147 |
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
|
|
|
|
| 142 |
]
|
| 143 |
|
| 144 |
REST_FRAMEWORK = {
|
| 145 |
+
'EXCEPTION_HANDLER': "_utils.custom_exception_handler.custom_exception_handler",
|
| 146 |
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
|
| 147 |
"PAGE_SIZE": 10,
|
| 148 |
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
|