-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnose_api.py
More file actions
executable file
·103 lines (85 loc) · 3.61 KB
/
Copy pathdiagnose_api.py
File metadata and controls
executable file
·103 lines (85 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
"""
Script para diagnosticar problemas com a API do Gemini
"""
import os
import sys
from google import genai
from logger_config import setup_logger, get_logger
logger = setup_logger("diagnose", level="INFO", detailed=True)
def test_api_connection():
"""Testa a conexão com a API do Gemini"""
logger.info("=" * 60)
logger.info("Diagnóstico da API do Gemini")
logger.info("=" * 60)
# Verificar variáveis de ambiente
logger.info("\n1. Verificando variáveis de ambiente...")
api_key = os.environ.get("GEMINI_API_KEY")
use_vertexai = os.environ.get("USE_VERTEXAI", "false").lower() in ("true", "1")
if api_key:
logger.info(f"✅ GEMINI_API_KEY encontrada (primeiros 10 chars: {api_key[:10]}...)")
else:
logger.error("❌ GEMINI_API_KEY não encontrada!")
return False
logger.info(f"USE_VERTEXAI: {use_vertexai}")
if use_vertexai:
project = os.environ.get("VERTEXAI_PROJECT")
location = os.environ.get("VERTEXAI_LOCATION")
logger.info(f"VERTEXAI_PROJECT: {project}")
logger.info(f"VERTEXAI_LOCATION: {location}")
# Criar cliente
logger.info("\n2. Criando cliente Gemini...")
try:
if use_vertexai:
client = genai.Client(
vertexai=True,
project=os.environ.get("VERTEXAI_PROJECT"),
location=os.environ.get("VERTEXAI_LOCATION"),
)
logger.info("✅ Cliente Vertex AI criado")
else:
client = genai.Client(api_key=api_key)
logger.info("✅ Cliente API Key criado")
except Exception as e:
logger.error(f"❌ Erro ao criar cliente: {e}")
return False
# Testar chamada simples
logger.info("\n3. Testando chamada à API...")
try:
model_name = "gemini-2.5-computer-use-preview-10-2025"
logger.info(f"Modelo: {model_name}")
response = client.models.generate_content(
model=model_name,
contents=[{"role": "user", "parts": [{"text": "Hello"}]}],
)
logger.info("✅ Resposta recebida da API")
if response.candidates:
logger.info(f"✅ Número de candidatos: {len(response.candidates)}")
candidate = response.candidates[0]
logger.info(f"✅ Finish reason: {candidate.finish_reason}")
if candidate.content and candidate.content.parts:
text = candidate.content.parts[0].text if candidate.content.parts[0].text else "N/A"
logger.info(f"✅ Texto da resposta: {text[:100]}...")
else:
logger.error("❌ Resposta sem candidatos!")
if hasattr(response, 'prompt_feedback'):
logger.error(f"Prompt feedback: {response.prompt_feedback}")
return False
# Verificar metadata
if hasattr(response, 'usage_metadata') and response.usage_metadata:
logger.info(f"✅ Usage metadata: {response.usage_metadata}")
logger.info("\n" + "=" * 60)
logger.info("✅ DIAGNÓSTICO: API funcionando corretamente!")
logger.info("=" * 60)
return True
except Exception as e:
logger.error(f"\n❌ Erro ao chamar API: {type(e).__name__}: {e}")
import traceback
logger.error(f"Traceback:\n{traceback.format_exc()}")
logger.info("\n" + "=" * 60)
logger.error("❌ DIAGNÓSTICO: Problema detectado com a API")
logger.info("=" * 60)
return False
if __name__ == "__main__":
success = test_api_connection()
sys.exit(0 if success else 1)