Spaces:
Sleeping
Sleeping
Commit
·
4c81a8d
1
Parent(s):
f77cd10
OpenAI no Lugar de GRONk - Versão Estável
Browse files
.gitignore
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
# Arquivos de ambiente virtual
|
| 2 |
.venv311/
|
|
|
|
| 3 |
__pycache__/
|
| 4 |
|
| 5 |
# Arquivo de segredos
|
|
|
|
| 1 |
# Arquivos de ambiente virtual
|
| 2 |
.venv311/
|
| 3 |
+
.venvLocal
|
| 4 |
__pycache__/
|
| 5 |
|
| 6 |
# Arquivo de segredos
|
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import os
|
|
| 5 |
import subprocess
|
| 6 |
import importlib.util
|
| 7 |
|
|
|
|
| 8 |
print("--- INÍCIO DO DEBUG ---", flush=True)
|
| 9 |
|
| 10 |
# 1. Onde o Python está procurando por pacotes?
|
|
@@ -50,7 +51,7 @@ except Exception as e:
|
|
| 50 |
|
| 51 |
|
| 52 |
print("\n--- FIM DO DEBUG ---", flush=True)
|
| 53 |
-
|
| 54 |
|
| 55 |
from flask import Flask, render_template, request, Response, jsonify
|
| 56 |
import json
|
|
@@ -72,7 +73,7 @@ from langchain.prompts import PromptTemplate
|
|
| 72 |
from langchain_core.output_parsers import StrOutputParser
|
| 73 |
|
| 74 |
# Importa os LLMs
|
| 75 |
-
from llms import claude_llm,
|
| 76 |
|
| 77 |
# Importa os prompts
|
| 78 |
from config import *
|
|
@@ -294,14 +295,15 @@ def process():
|
|
| 294 |
results[key] = f"Erro ao processar {key.upper()}: {e}"
|
| 295 |
|
| 296 |
claude_atomic_llm = claude_llm.bind(max_tokens=60000)
|
| 297 |
-
models = {'grok': grok_llm, 'sonnet': claude_atomic_llm, 'gemini': gemini_llm, 'openai': openai_llm}
|
|
|
|
| 298 |
|
| 299 |
# Substituir os placeholders no template
|
| 300 |
updated_prompt_template = PROMPT_ATOMICO_INICIAL.replace(
|
| 301 |
"MIN_CHARS_PLACEHOLDER", str(min_chars)
|
| 302 |
).replace(
|
| 303 |
"MAX_CHARS_PLACEHOLDER", str(max_chars)
|
| 304 |
-
)
|
| 305 |
|
| 306 |
# --- renderiza e loga o prompt final Atomico ---
|
| 307 |
# ——— log do prompt atômico já formatado ———
|
|
@@ -376,7 +378,7 @@ def process():
|
|
| 376 |
"MIN_CHARS_PLACEHOLDER", str(min_chars)
|
| 377 |
).replace(
|
| 378 |
"MAX_CHARS_PLACEHOLDER", str(max_chars)
|
| 379 |
-
)
|
| 380 |
|
| 381 |
# --- renderiza e loga o prompt final Hierárquico Grok ---
|
| 382 |
##log_print(f"[DEBUG] PROMPT HIERÁRQUICO GROK RENDERED:\n"
|
|
@@ -390,7 +392,7 @@ def process():
|
|
| 390 |
"MIN_CHARS_PLACEHOLDER", str(min_chars)
|
| 391 |
).replace(
|
| 392 |
"MAX_CHARS_PLACEHOLDER", str(max_chars)
|
| 393 |
-
)
|
| 394 |
|
| 395 |
# --- renderiza e loga o prompt final Hierárquico Sonnet ---
|
| 396 |
##log_print(f"[DEBUG] PROMPT HIERÁRQUICO SONNET RENDERED:\n"
|
|
@@ -403,7 +405,7 @@ def process():
|
|
| 403 |
"MIN_CHARS_PLACEHOLDER", str(min_chars)
|
| 404 |
).replace(
|
| 405 |
"MAX_CHARS_PLACEHOLDER", str(max_chars)
|
| 406 |
-
)
|
| 407 |
|
| 408 |
# --- renderiza e loga o prompt final Hierárquico Gemini ---
|
| 409 |
log_print(f"[DEBUG] PROMPT HIERÁRQUICO GEMINI RENDERED:\n"
|
|
@@ -425,8 +427,9 @@ def process():
|
|
| 425 |
#log_print(f"=== GROK TERMINOU: {len(resposta_grok)} chars ===")
|
| 426 |
|
| 427 |
log_print("=== PROCESSANDO OPEN AI ===")
|
|
|
|
| 428 |
prompt_openai = PromptTemplate(template=updated_openai_template, input_variables=["contexto", "solicitacao_usuario", "rag_context"])
|
| 429 |
-
chain_openai = prompt_openai |
|
| 430 |
resposta_openai = chain_openai.invoke({"contexto": contexto, "solicitacao_usuario": solicitacao_usuario, "rag_context": rag_context})
|
| 431 |
|
| 432 |
log_print(f"=== OPEN AI TERMINOU: {len(resposta_openai)} chars ===")
|
|
@@ -559,7 +562,7 @@ def merge():
|
|
| 559 |
"MIN_CHARS_PLACEHOLDER", str(min_chars)
|
| 560 |
).replace(
|
| 561 |
"MAX_CHARS_PLACEHOLDER", str(max_chars)
|
| 562 |
-
)
|
| 563 |
|
| 564 |
# --- renderiza e loga o prompt final Atomico Merge --
|
| 565 |
##log_print(f"[DEBUG] PROMPT MERGE RENDERED:\n"
|
|
|
|
| 5 |
import subprocess
|
| 6 |
import importlib.util
|
| 7 |
|
| 8 |
+
'''
|
| 9 |
print("--- INÍCIO DO DEBUG ---", flush=True)
|
| 10 |
|
| 11 |
# 1. Onde o Python está procurando por pacotes?
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
print("\n--- FIM DO DEBUG ---", flush=True)
|
| 54 |
+
'''
|
| 55 |
|
| 56 |
from flask import Flask, render_template, request, Response, jsonify
|
| 57 |
import json
|
|
|
|
| 73 |
from langchain_core.output_parsers import StrOutputParser
|
| 74 |
|
| 75 |
# Importa os LLMs
|
| 76 |
+
from llms import claude_llm, gemini_llm, openai_llm#, grok_llm
|
| 77 |
|
| 78 |
# Importa os prompts
|
| 79 |
from config import *
|
|
|
|
| 295 |
results[key] = f"Erro ao processar {key.upper()}: {e}"
|
| 296 |
|
| 297 |
claude_atomic_llm = claude_llm.bind(max_tokens=60000)
|
| 298 |
+
#models = {'grok': grok_llm, 'sonnet': claude_atomic_llm, 'gemini': gemini_llm, 'openai': openai_llm}
|
| 299 |
+
models = {'sonnet': claude_atomic_llm, 'gemini': gemini_llm, 'openai': openai_llm}
|
| 300 |
|
| 301 |
# Substituir os placeholders no template
|
| 302 |
updated_prompt_template = PROMPT_ATOMICO_INICIAL.replace(
|
| 303 |
"MIN_CHARS_PLACEHOLDER", str(min_chars)
|
| 304 |
).replace(
|
| 305 |
"MAX_CHARS_PLACEHOLDER", str(max_chars)
|
| 306 |
+
)
|
| 307 |
|
| 308 |
# --- renderiza e loga o prompt final Atomico ---
|
| 309 |
# ——— log do prompt atômico já formatado ———
|
|
|
|
| 378 |
"MIN_CHARS_PLACEHOLDER", str(min_chars)
|
| 379 |
).replace(
|
| 380 |
"MAX_CHARS_PLACEHOLDER", str(max_chars)
|
| 381 |
+
)
|
| 382 |
|
| 383 |
# --- renderiza e loga o prompt final Hierárquico Grok ---
|
| 384 |
##log_print(f"[DEBUG] PROMPT HIERÁRQUICO GROK RENDERED:\n"
|
|
|
|
| 392 |
"MIN_CHARS_PLACEHOLDER", str(min_chars)
|
| 393 |
).replace(
|
| 394 |
"MAX_CHARS_PLACEHOLDER", str(max_chars)
|
| 395 |
+
)
|
| 396 |
|
| 397 |
# --- renderiza e loga o prompt final Hierárquico Sonnet ---
|
| 398 |
##log_print(f"[DEBUG] PROMPT HIERÁRQUICO SONNET RENDERED:\n"
|
|
|
|
| 405 |
"MIN_CHARS_PLACEHOLDER", str(min_chars)
|
| 406 |
).replace(
|
| 407 |
"MAX_CHARS_PLACEHOLDER", str(max_chars)
|
| 408 |
+
)
|
| 409 |
|
| 410 |
# --- renderiza e loga o prompt final Hierárquico Gemini ---
|
| 411 |
log_print(f"[DEBUG] PROMPT HIERÁRQUICO GEMINI RENDERED:\n"
|
|
|
|
| 427 |
#log_print(f"=== GROK TERMINOU: {len(resposta_grok)} chars ===")
|
| 428 |
|
| 429 |
log_print("=== PROCESSANDO OPEN AI ===")
|
| 430 |
+
openai_with_max_tokens = openai_llm.bind(max_completion_tokens=100000)
|
| 431 |
prompt_openai = PromptTemplate(template=updated_openai_template, input_variables=["contexto", "solicitacao_usuario", "rag_context"])
|
| 432 |
+
chain_openai = prompt_openai | openai_with_max_tokens | output_parser
|
| 433 |
resposta_openai = chain_openai.invoke({"contexto": contexto, "solicitacao_usuario": solicitacao_usuario, "rag_context": rag_context})
|
| 434 |
|
| 435 |
log_print(f"=== OPEN AI TERMINOU: {len(resposta_openai)} chars ===")
|
|
|
|
| 562 |
"MIN_CHARS_PLACEHOLDER", str(min_chars)
|
| 563 |
).replace(
|
| 564 |
"MAX_CHARS_PLACEHOLDER", str(max_chars)
|
| 565 |
+
)
|
| 566 |
|
| 567 |
# --- renderiza e loga o prompt final Atomico Merge --
|
| 568 |
##log_print(f"[DEBUG] PROMPT MERGE RENDERED:\n"
|
config.py
CHANGED
|
@@ -9,10 +9,10 @@ PROMPT_HIERARQUICO_GROK = """
|
|
| 9 |
</role>
|
| 10 |
|
| 11 |
<requirements>
|
| 12 |
-
<
|
| 13 |
-
<
|
| 14 |
-
<
|
| 15 |
-
</
|
| 16 |
<language>Português do Brasil</language>
|
| 17 |
<paragraph_structure>Parágrafos curtos para facilitar a leitura</paragraph_structure>
|
| 18 |
<language_style>
|
|
@@ -47,8 +47,8 @@ PROMPT_HIERARQUICO_GROK = """
|
|
| 47 |
7. Evite usar um estilo de escrita muito característico de textos gerados com IA, como por exemplo: "Não é mera..., mas é...". Coisas assim. Seja mais direto.
|
| 48 |
8. Todo o texto, incluindo citações, devem estar na lingua Português do Brasil.
|
| 49 |
|
| 50 |
-
<forbidden>Que o texto tenha menos de MIN_CHARS_PLACEHOLDER caracteres
|
| 51 |
-
<forbidden>Que o texto tenha mais de MAX_CHARS_PLACEHOLDER caracteres
|
| 52 |
</instructions>
|
| 53 |
</prompt>
|
| 54 |
"""
|
|
@@ -67,10 +67,10 @@ PROMPT_HIERARQUICO_SONNET = """
|
|
| 67 |
</entrada>
|
| 68 |
|
| 69 |
<tamanhoDoTexto>
|
| 70 |
-
<
|
| 71 |
-
<
|
| 72 |
-
<
|
| 73 |
-
</
|
| 74 |
</tamanhoDoTexto>
|
| 75 |
|
| 76 |
<instrucoes>
|
|
@@ -93,8 +93,8 @@ PROMPT_HIERARQUICO_SONNET = """
|
|
| 93 |
<acao>corrigir problemas argumentativos</acao>
|
| 94 |
</correcoes>
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
|
| 99 |
</instrucoes>
|
| 100 |
|
|
@@ -134,10 +134,10 @@ PROMPT_HIERARQUICO_GEMINI = """
|
|
| 134 |
</entrada>
|
| 135 |
|
| 136 |
<tamanhoDoTexto>
|
| 137 |
-
<
|
| 138 |
-
<
|
| 139 |
-
<
|
| 140 |
-
</
|
| 141 |
</tamanhoDoTexto>
|
| 142 |
|
| 143 |
<instrucoes>
|
|
@@ -173,8 +173,8 @@ PROMPT_HIERARQUICO_GEMINI = """
|
|
| 173 |
<proibido>encurtar o conteúdo original</proibido>
|
| 174 |
<proibido>elaborar um texto com mais de 30000 caracteres</proibido>
|
| 175 |
<proibido>usar expressões características de IA como "Não é mera..., mas é..."</proibido>
|
| 176 |
-
<proibido>Que o texto tenha menos de MIN_CHARS_PLACEHOLDER caracteres
|
| 177 |
-
<proibido>Que o texto tenha mais de MAX_CHARS_PLACEHOLDER caracteres
|
| 178 |
</restricoes>
|
| 179 |
|
| 180 |
<requisitos>
|
|
@@ -200,10 +200,10 @@ PROMPT_ATOMICO_INICIAL = """
|
|
| 200 |
{contexto}
|
| 201 |
</role>
|
| 202 |
<requirements>
|
| 203 |
-
<
|
| 204 |
-
<
|
| 205 |
-
<
|
| 206 |
-
</
|
| 207 |
<language>Português do Brasil</language>
|
| 208 |
<paragraph_structure>Parágrafos curtos para facilitar a leitura</paragraph_structure>
|
| 209 |
<language_style>
|
|
@@ -235,8 +235,8 @@ PROMPT_ATOMICO_INICIAL = """
|
|
| 235 |
7. Evite usar um estilo de escrita muito característico de textos gerados com IA, como por exemplo: "Não é mera..., mas é...". Coisas assim. Seja mais direto.
|
| 236 |
8. Todo o texto, incluindo citações, devem estar na lingua Português do Brasil.
|
| 237 |
|
| 238 |
-
<forbidden>Que o texto tenha menos de MIN_CHARS_PLACEHOLDER caracteres
|
| 239 |
-
<forbidden>Que o texto tenha mais de MAX_CHARS_PLACEHOLDER caracteres
|
| 240 |
</instructions>
|
| 241 |
</prompt>
|
| 242 |
"""
|
|
@@ -259,7 +259,8 @@ PROMPT_ATOMICO_MERGE = """
|
|
| 259 |
|
| 260 |
<text_grok>
|
| 261 |
<title>Texto Gerado pelo GROK:</title>
|
| 262 |
-
<content>{
|
|
|
|
| 263 |
</text_grok>
|
| 264 |
|
| 265 |
<text_sonnet>
|
|
@@ -278,10 +279,10 @@ PROMPT_ATOMICO_MERGE = """
|
|
| 278 |
Analise e escolha a melhor estrutura de seções entre os 3 textos e aplique no texto consolidado. A melhor estrutura de seções é aquela que melhor entendeu o objetivo da solicitação do usuário e que mais conseguir se aprofundar na abordagem do tema.
|
| 279 |
</structure>
|
| 280 |
|
| 281 |
-
<
|
| 282 |
-
<
|
| 283 |
-
<
|
| 284 |
-
</
|
| 285 |
|
| 286 |
<analysis>
|
| 287 |
Verifique a coesão, coerência e profundidade dos argumentos.
|
|
@@ -312,8 +313,8 @@ PROMPT_ATOMICO_MERGE = """
|
|
| 312 |
Verificar se todo o texto, incluindo citações, estão na lingua Português do Brasil. Traduza as que não estiverem.
|
| 313 |
</language>
|
| 314 |
|
| 315 |
-
<forbidden>Que o texto tenha menos de MIN_CHARS_PLACEHOLDER caracteres
|
| 316 |
-
<forbidden>Que o texto tenha mais de MAX_CHARS_PLACEHOLDER caracteres
|
| 317 |
</instructions>
|
| 318 |
|
| 319 |
<output>
|
|
|
|
| 9 |
</role>
|
| 10 |
|
| 11 |
<requirements>
|
| 12 |
+
<length_requirements>
|
| 13 |
+
<min_characters_no_spaces>MIN_CHARS_PLACEHOLDER</min_characters_no_spaces>
|
| 14 |
+
<max_characters_no_spaces>MAX_CHARS_PLACEHOLDER</max_characters_no_spaces>
|
| 15 |
+
</length_requirements>
|
| 16 |
<language>Português do Brasil</language>
|
| 17 |
<paragraph_structure>Parágrafos curtos para facilitar a leitura</paragraph_structure>
|
| 18 |
<language_style>
|
|
|
|
| 47 |
7. Evite usar um estilo de escrita muito característico de textos gerados com IA, como por exemplo: "Não é mera..., mas é...". Coisas assim. Seja mais direto.
|
| 48 |
8. Todo o texto, incluindo citações, devem estar na lingua Português do Brasil.
|
| 49 |
|
| 50 |
+
<forbidden>Que o texto tenha menos de MIN_CHARS_PLACEHOLDER caracteres *desconsiderando os espaços*.</forbidden>
|
| 51 |
+
<forbidden>Que o texto tenha mais de MAX_CHARS_PLACEHOLDER caracteres *desconsiderando os espaços*.</forbidden>
|
| 52 |
</instructions>
|
| 53 |
</prompt>
|
| 54 |
"""
|
|
|
|
| 67 |
</entrada>
|
| 68 |
|
| 69 |
<tamanhoDoTexto>
|
| 70 |
+
<length_requirements>
|
| 71 |
+
<min_characters_no_spaces>MIN_CHARS_PLACEHOLDER</min_characters_no_spaces>
|
| 72 |
+
<max_characters_no_spaces>MAX_CHARS_PLACEHOLDER</max_characters_no_spaces>
|
| 73 |
+
</length_requirements>
|
| 74 |
</tamanhoDoTexto>
|
| 75 |
|
| 76 |
<instrucoes>
|
|
|
|
| 93 |
<acao>corrigir problemas argumentativos</acao>
|
| 94 |
</correcoes>
|
| 95 |
|
| 96 |
+
<forbidden>Que o texto tenha menos de MIN_CHARS_PLACEHOLDER caracteres *desconsiderando os espaços*.</forbidden>
|
| 97 |
+
<forbidden>Que o texto tenha mais de MAX_CHARS_PLACEHOLDER caracteres *desconsiderando os espaços*.</forbidden>
|
| 98 |
|
| 99 |
</instrucoes>
|
| 100 |
|
|
|
|
| 134 |
</entrada>
|
| 135 |
|
| 136 |
<tamanhoDoTexto>
|
| 137 |
+
<length_requirements>
|
| 138 |
+
<min_characters_no_spaces>MIN_CHARS_PLACEHOLDER</min_characters_no_spaces>
|
| 139 |
+
<max_characters_no_spaces>MAX_CHARS_PLACEHOLDER</max_characters_no_spaces>
|
| 140 |
+
</length_requirements>
|
| 141 |
</tamanhoDoTexto>
|
| 142 |
|
| 143 |
<instrucoes>
|
|
|
|
| 173 |
<proibido>encurtar o conteúdo original</proibido>
|
| 174 |
<proibido>elaborar um texto com mais de 30000 caracteres</proibido>
|
| 175 |
<proibido>usar expressões características de IA como "Não é mera..., mas é..."</proibido>
|
| 176 |
+
<proibido>Que o texto tenha menos de MIN_CHARS_PLACEHOLDER caracteres *desconsiderando os espaços*.</proibido>
|
| 177 |
+
<proibido>Que o texto tenha mais de MAX_CHARS_PLACEHOLDER caracteres *desconsiderando os espaços*.</proibido>
|
| 178 |
</restricoes>
|
| 179 |
|
| 180 |
<requisitos>
|
|
|
|
| 200 |
{contexto}
|
| 201 |
</role>
|
| 202 |
<requirements>
|
| 203 |
+
<length_requirements>
|
| 204 |
+
<min_characters_no_spaces>MIN_CHARS_PLACEHOLDER</min_characters_no_spaces>
|
| 205 |
+
<max_characters_no_spaces>MAX_CHARS_PLACEHOLDER</max_characters_no_spaces>
|
| 206 |
+
</length_requirements>
|
| 207 |
<language>Português do Brasil</language>
|
| 208 |
<paragraph_structure>Parágrafos curtos para facilitar a leitura</paragraph_structure>
|
| 209 |
<language_style>
|
|
|
|
| 235 |
7. Evite usar um estilo de escrita muito característico de textos gerados com IA, como por exemplo: "Não é mera..., mas é...". Coisas assim. Seja mais direto.
|
| 236 |
8. Todo o texto, incluindo citações, devem estar na lingua Português do Brasil.
|
| 237 |
|
| 238 |
+
<forbidden>Que o texto tenha menos de MIN_CHARS_PLACEHOLDER caracteres *desconsiderando os espaços*.</forbidden>
|
| 239 |
+
<forbidden>Que o texto tenha mais de MAX_CHARS_PLACEHOLDER caracteres *desconsiderando os espaços*.</forbidden>
|
| 240 |
</instructions>
|
| 241 |
</prompt>
|
| 242 |
"""
|
|
|
|
| 259 |
|
| 260 |
<text_grok>
|
| 261 |
<title>Texto Gerado pelo GROK:</title>
|
| 262 |
+
<content>{texto_para_analise_openai}</content>
|
| 263 |
+
|
| 264 |
</text_grok>
|
| 265 |
|
| 266 |
<text_sonnet>
|
|
|
|
| 279 |
Analise e escolha a melhor estrutura de seções entre os 3 textos e aplique no texto consolidado. A melhor estrutura de seções é aquela que melhor entendeu o objetivo da solicitação do usuário e que mais conseguir se aprofundar na abordagem do tema.
|
| 280 |
</structure>
|
| 281 |
|
| 282 |
+
<length_requirements>
|
| 283 |
+
<min_characters_no_spaces>MIN_CHARS_PLACEHOLDER</min_characters_no_spaces>
|
| 284 |
+
<max_characters_no_spaces>MAX_CHARS_PLACEHOLDER</max_characters_no_spaces>
|
| 285 |
+
</length_requirements>
|
| 286 |
|
| 287 |
<analysis>
|
| 288 |
Verifique a coesão, coerência e profundidade dos argumentos.
|
|
|
|
| 313 |
Verificar se todo o texto, incluindo citações, estão na lingua Português do Brasil. Traduza as que não estiverem.
|
| 314 |
</language>
|
| 315 |
|
| 316 |
+
<forbidden>Que o texto tenha menos de MIN_CHARS_PLACEHOLDER caracteres *desconsiderando os espaços*.</forbidden>
|
| 317 |
+
<forbidden>Que o texto tenha mais de MAX_CHARS_PLACEHOLDER caracteres *desconsiderando os espaços*.</forbidden>
|
| 318 |
</instructions>
|
| 319 |
|
| 320 |
<output>
|
llms.py
CHANGED
|
@@ -26,11 +26,11 @@ def format_assistant_input(prompt_value):
|
|
| 26 |
|
| 27 |
def parse_assistant_output(assistant_finish_object):
|
| 28 |
"""
|
| 29 |
-
|
| 30 |
O resultado final do assistente está no dicionário `return_values`.
|
| 31 |
"""
|
| 32 |
-
#
|
| 33 |
-
if isinstance(assistant_finish_object,
|
| 34 |
# A resposta final em string está na chave 'output' do dicionário return_values
|
| 35 |
return assistant_finish_object.return_values.get('output', '')
|
| 36 |
|
|
|
|
| 26 |
|
| 27 |
def parse_assistant_output(assistant_finish_object):
|
| 28 |
"""
|
| 29 |
+
Extrai a string de saída de um objeto AgentFinish.
|
| 30 |
O resultado final do assistente está no dicionário `return_values`.
|
| 31 |
"""
|
| 32 |
+
# 2. A verificação de tipo aqui deve usar 'AgentFinish', e não o nome antigo.
|
| 33 |
+
if isinstance(assistant_finish_object, AgentFinish):
|
| 34 |
# A resposta final em string está na chave 'output' do dicionário return_values
|
| 35 |
return assistant_finish_object.return_values.get('output', '')
|
| 36 |
|