Add Ollama connection check and better error messages
This commit is contained in:
17
bot_logic.py
17
bot_logic.py
@@ -257,8 +257,23 @@ async def llm_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|||||||
|
|
||||||
if not args:
|
if not args:
|
||||||
# Sem argumentos: mostra status atual
|
# Sem argumentos: mostra status atual
|
||||||
|
ollama_status = ""
|
||||||
|
if current == "OLLAMA":
|
||||||
|
try:
|
||||||
|
from llm_providers import check_ollama_connection
|
||||||
|
status = check_ollama_connection()
|
||||||
|
if status.get("status") == "ok":
|
||||||
|
models = status.get("models", [])
|
||||||
|
ollama_status = f"\n\n🔷 Ollama: ✅ Online\n Modelos: {', '.join(models[:3]) if models else 'Nenhum'}"
|
||||||
|
elif status.get("status") == "timeout":
|
||||||
|
ollama_status = "\n\n🔷 Ollama: ⏱️ Timeout - não respondeu"
|
||||||
|
else:
|
||||||
|
ollama_status = f"\n\n🔷 Ollama: ❌ {status.get('status', 'Erro desconhecido')}"
|
||||||
|
except Exception as e:
|
||||||
|
ollama_status = f"\n\n🔷 Ollama: ❌ Erro ao verificar"
|
||||||
|
|
||||||
await update.message.reply_text(
|
await update.message.reply_text(
|
||||||
f"🤖 LLM Atual: *{current}*\n\n"
|
f"🤖 LLM Atual: *{current}*{ollama_status}\n\n"
|
||||||
f"Para mudar: /llm gemini ou /llm ollama"
|
f"Para mudar: /llm gemini ou /llm ollama"
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -314,6 +314,22 @@ def _call_anthropic(model: str, prompt: str, system_prompt: str = None) -> str:
|
|||||||
# ----------------------------------------
|
# ----------------------------------------
|
||||||
# OLLAMA (LOCAL)
|
# OLLAMA (LOCAL)
|
||||||
# ----------------------------------------
|
# ----------------------------------------
|
||||||
|
def check_ollama_connection() -> dict:
|
||||||
|
"""Verifica se Ollama está acessível."""
|
||||||
|
endpoint = LLM_PROVIDERS["ollama"]["endpoint"]
|
||||||
|
try:
|
||||||
|
res = requests.get(f"{endpoint}/api/tags", timeout=10)
|
||||||
|
if res.status_code == 200:
|
||||||
|
models = [m.get("name") for m in res.json().get("models", [])]
|
||||||
|
return {"status": "ok", "models": models, "endpoint": endpoint}
|
||||||
|
return {"status": "error", "code": res.status_code, "endpoint": endpoint}
|
||||||
|
except requests.exceptions.Timeout:
|
||||||
|
return {"status": "timeout", "endpoint": endpoint}
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
|
return {"status": "unreachable", "endpoint": endpoint}
|
||||||
|
except Exception as e:
|
||||||
|
return {"status": "error", "message": str(e), "endpoint": endpoint}
|
||||||
|
|
||||||
def _call_ollama(model: str, prompt: str, system_prompt: str = None) -> str:
|
def _call_ollama(model: str, prompt: str, system_prompt: str = None) -> str:
|
||||||
"""Chama Ollama local."""
|
"""Chama Ollama local."""
|
||||||
endpoint = LLM_PROVIDERS["ollama"]["endpoint"]
|
endpoint = LLM_PROVIDERS["ollama"]["endpoint"]
|
||||||
@@ -333,6 +349,10 @@ def _call_ollama(model: str, prompt: str, system_prompt: str = None) -> str:
|
|||||||
if res.status_code == 200:
|
if res.status_code == 200:
|
||||||
return res.json().get("response", "")
|
return res.json().get("response", "")
|
||||||
return f"Erro Ollama: {res.status_code} - {res.text}"
|
return f"Erro Ollama: {res.status_code} - {res.text}"
|
||||||
|
except requests.exceptions.Timeout:
|
||||||
|
return f"[TIMEOUT] Ollama não respondeu em 120s. Verifique se o serviço está rodando em {endpoint}"
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
|
return f"[CONNECTION ERROR] Não conseguiu conectar ao Ollama em {endpoint}. Verifique se o container Ollama está na mesma rede Docker."
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return f"Erro Ollama: {str(e)}"
|
return f"Erro Ollama: {str(e)}"
|
||||||
|
|
||||||
|
|||||||
7
main.py
7
main.py
@@ -276,6 +276,13 @@ async def get_llm_configuration(is_auth: bool = Depends(verify_password)):
|
|||||||
"""Retorna configuração atual de LLMs."""
|
"""Retorna configuração atual de LLMs."""
|
||||||
return JSONResponse(content=get_llm_config())
|
return JSONResponse(content=get_llm_config())
|
||||||
|
|
||||||
|
@app.get("/api/ollama-status")
|
||||||
|
async def get_ollama_status(is_auth: bool = Depends(verify_password)):
|
||||||
|
"""Verifica status do Ollama."""
|
||||||
|
from llm_providers import check_ollama_connection
|
||||||
|
result = check_ollama_connection()
|
||||||
|
return JSONResponse(content=result)
|
||||||
|
|
||||||
@app.post("/api/llm-config")
|
@app.post("/api/llm-config")
|
||||||
async def update_llm_configuration(config_data: dict, is_auth: bool = Depends(verify_password)):
|
async def update_llm_configuration(config_data: dict, is_auth: bool = Depends(verify_password)):
|
||||||
"""Atualiza configuração de LLMs."""
|
"""Atualiza configuração de LLMs."""
|
||||||
|
|||||||
Reference in New Issue
Block a user