Files
inversionitasbot/app/iacorrector.py
Your Name b4b2d899aa testing
2025-02-09 21:56:13 +01:00

86 lines
3.1 KiB
Python

import requests
import json
import os
import logging
# Configuración del logging
LOG_FILE = "app.log"
logging.basicConfig(
filename=LOG_FILE, # Archivo de logs
level=logging.INFO, # Nivel de logging (DEBUG, INFO, WARNING, ERROR, CRITICAL)
format="%(asctime)s - %(levelname)s - %(message)s", # Formato de los logs
)
# Obtener variables de entorno
OLLAMA_URL = os.environ.get("OLLAMA_URL", "http://host.docker.internal:11434/api/generate")
OLLAMA_MODEL = os.environ.get("OLLAMA_MODEL", "llama3")
def is_security_related(prompt):
data = {
"model": OLLAMA_MODEL,
"prompt": f"Does the following topic relate to national defense, armed forces, police, espionage, or intelligence? Answer only with 'true' or 'false'. Topic: {prompt}",
}
try:
response = requests.post(OLLAMA_URL, json=data)
response.raise_for_status() # Lanza una excepción si la solicitud falla
for line in response.text.strip().split("\n"):
json_data = json.loads(line)
if "response" in json_data and json_data["response"].strip():
result = json_data["response"].strip().lower() == "true"
return result
except requests.RequestException as e:
logging.error(f"Request error: {e}")
except json.JSONDecodeError as e:
logging.error(f"JSON Decode Error: {e}")
return False
def is_critico(prompt):
data = {
"model": OLLAMA_MODEL,
"prompt": f"Does the following text criticizes the armed forces, security forces as Guardia Civil or Police, intelligence agencies such as CNI? Answer only with 'true' or 'false'. Topic: {prompt}",
}
try:
response = requests.post(OLLAMA_URL, json=data)
response.raise_for_status()
for line in response.text.strip().split("\n"):
json_data = json.loads(line)
if "response" in json_data and json_data["response"].strip():
result = json_data["response"].strip().lower() == "true"
return result
except requests.RequestException as e:
logging.error(f"Request error: {e}")
except json.JSONDecodeError as e:
logging.error(f"JSON Decode Error: {e}")
return False
def is_favorable(prompt):
data = {
"model": OLLAMA_MODEL,
"prompt": f"Does the following text favor the armed forces, security forces as Guardia Civil or Police, intelligence agencies such as CNI? Answer only with 'true' or 'false'. Topic: {prompt}",
}
try:
response = requests.post(OLLAMA_URL, json=data)
response.raise_for_status()
for line in response.text.strip().split("\n"):
json_data = json.loads(line)
if "response" in json_data and json_data["response"].strip():
result = json_data["response"].strip().lower() == "true"
return result
except requests.RequestException as e:
logging.error(f"Request error: {e}")
except json.JSONDecodeError as e:
logging.error(f"JSON Decode Error: {e}")
return False