45 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			45 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Python
		
	
	
	
| #!/usr/bin/env python3
 | ||
| # -*- coding: utf-8 -*-
 | ||
| """
 | ||
| 直接测试Google Gemini API连接
 | ||
| """
 | ||
| 
 | ||
| import os
 | ||
| import google.generativeai as genai
 | ||
| 
 | ||
| def test_gemini_direct():
 | ||
|     """直接测试Gemini API"""
 | ||
|     print("🔍 测试Gemini API直连...")
 | ||
|     
 | ||
|     # 检查API密钥
 | ||
|     api_key = os.getenv('GOOGLE_API_KEY')
 | ||
|     if not api_key:
 | ||
|         print("❌ 未找到 GOOGLE_API_KEY")
 | ||
|         return False
 | ||
|     
 | ||
|     print(f"✅ API密钥已配置 (长度: {len(api_key)})")
 | ||
|     
 | ||
|     try:
 | ||
|         # 配置API
 | ||
|         genai.configure(api_key=api_key)
 | ||
|         
 | ||
|         # 创建模型
 | ||
|         print("📝 创建Gemini模型...")
 | ||
|         model = genai.GenerativeModel('gemini-2.0-flash-exp')
 | ||
|         
 | ||
|         # 发送测试消息
 | ||
|         print("💬 发送测试消息...")
 | ||
|         response = model.generate_content("请简单说'你好,我是Gemini'")
 | ||
|         
 | ||
|         print(f"✅ 测试成功!回复: {response.text}")
 | ||
|         return True
 | ||
|         
 | ||
|     except Exception as e:
 | ||
|         print(f"❌ 测试失败: {e}")
 | ||
|         import traceback
 | ||
|         traceback.print_exc()
 | ||
|         return False
 | ||
| 
 | ||
| if __name__ == "__main__":
 | ||
|     print("🚀 Gemini直连测试")
 | ||
|     test_gemini_direct() |