Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -11,33 +11,44 @@ def call_openrouter(passage, mode, category):
|
|
| 11 |
api_key = os.getenv("OPENROUTER_API_KEY")
|
| 12 |
lang = detect_language(passage)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
url = DOCS.get(category)
|
| 16 |
-
|
| 17 |
try:
|
| 18 |
-
resp = requests.get(
|
| 19 |
resp.raise_for_status()
|
| 20 |
-
# Captura as perguntas (linhas que terminam com ?)
|
| 21 |
questions = [l.strip() for l in resp.text.splitlines() if l.strip().endswith('?')]
|
| 22 |
except Exception as e:
|
| 23 |
-
return f"Erro ao acessar Google Docs
|
| 24 |
-
|
| 25 |
-
if not questions:
|
| 26 |
-
return f"Documento de {category} lido, mas nenhuma pergunta encontrada.", "Nenhum"
|
| 27 |
|
| 28 |
-
#
|
| 29 |
model_chain = MODEL_PRIORITY_A if "Alta" in mode else MODEL_PRIORITY_B
|
| 30 |
-
model = model_chain[0]
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
api_key = os.getenv("OPENROUTER_API_KEY")
|
| 12 |
lang = detect_language(passage)
|
| 13 |
|
| 14 |
+
url_doc = DOCS.get(category)
|
|
|
|
|
|
|
| 15 |
try:
|
| 16 |
+
resp = requests.get(url_doc, timeout=15)
|
| 17 |
resp.raise_for_status()
|
|
|
|
| 18 |
questions = [l.strip() for l in resp.text.splitlines() if l.strip().endswith('?')]
|
| 19 |
except Exception as e:
|
| 20 |
+
return f"Erro ao acessar Google Docs: {str(e)}", "Nenhum"
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Escolha da cadeia de modelos do config.py
|
| 23 |
model_chain = MODEL_PRIORITY_A if "Alta" in mode else MODEL_PRIORITY_B
|
|
|
|
| 24 |
|
| 25 |
+
# NOVO PROMPT: Forçando a repetição da pergunta
|
| 26 |
+
prompt_completo = f"""Atue como um Professor de Filologia Clássica ({lang}).
|
| 27 |
+
Analise a passagem: "{passage}"
|
| 28 |
|
| 29 |
+
INSTRUÇÃO OBRIGATÓRIA: Para cada pergunta abaixo, você deve primeiro ESCREVER A PERGUNTA NA ÍNTEGRA e, logo abaixo, fornecer a sua resposta detalhada em PORTUGUÊS.
|
| 30 |
+
|
| 31 |
+
PERGUNTAS:
|
| 32 |
+
{chr(10).join(questions)}"""
|
| 33 |
+
|
| 34 |
+
# Tenta os modelos da lista até um funcionar
|
| 35 |
+
for model in model_chain:
|
| 36 |
+
try:
|
| 37 |
+
response = requests.post(
|
| 38 |
+
url="https://openrouter.ai/api/v1/chat/completions",
|
| 39 |
+
headers={"Authorization": f"Bearer {api_key}"},
|
| 40 |
+
json={
|
| 41 |
+
"model": model,
|
| 42 |
+
"messages": [{"role": "user", "content": prompt_completo}],
|
| 43 |
+
"temperature": 0.1,
|
| 44 |
+
"max_tokens": 5000 # Aumentado para não cortar o final
|
| 45 |
+
},
|
| 46 |
+
timeout=180
|
| 47 |
+
)
|
| 48 |
+
if response.status_code == 200:
|
| 49 |
+
resultado = response.json()['choices'][0]['message']['content']
|
| 50 |
+
return resultado, model
|
| 51 |
+
except:
|
| 52 |
+
continue
|
| 53 |
+
|
| 54 |
+
return "Falha em todos os modelos da cadeia.", "Erro"
|