#!/usr/bin/env python3
"""
测试 Gemini 2.5 Flash 模型
"""

import os
import requests
import json

def test_gemini_2_5_flash():
    """测试 Gemini 2.5 Flash 模型"""
    
    # 获取环境变量
    base_url = os.getenv('GOOGLE_BASE_URL')
    api_key = os.getenv('GEMINI_API_KEY')
    
    if not base_url or not api_key:
        print("❌ 环境变量未设置")
        print(f"GOOGLE_BASE_URL: {base_url}")
        print(f"GEMINI_API_KEY: {api_key}")
        return False
    
    print("✅ 环境变量已设置")
    print(f"Base URL: {base_url}")
    print(f"API Key: {api_key[:10]}...{api_key[-4:]}")
    
    # 构建请求URL
    model_name = "gemini-2.5-flash"
    url = f"{base_url}/v1beta/models/{model_name}:generateContent"
    
    # 请求头
    headers = {
        "Content-Type": "application/json",
        "x-goog-api-key": api_key
    }
    
    # 请求体
    payload = {
        "contents": [{
            "parts": [{
                "text": "你好，请简单介绍一下你自己。"
            }]
        }]
    }
    
    try:
        print(f"\n🚀 测试 {model_name} 模型...")
        print(f"请求URL: {url}")
        
        response = requests.post(url, headers=headers, json=payload, timeout=30)
        
        print(f"响应状态码: {response.status_code}")
        
        if response.status_code == 200:
            result = response.json()
            if 'candidates' in result and len(result['candidates']) > 0:
                content = result['candidates'][0]['content']['parts'][0]['text']
                print(f"✅ {model_name} 响应成功:")
                print(f"📝 回复: {content[:200]}...")
                return True
            else:
                print(f"❌ 响应格式异常: {result}")
                return False
        else:
            print(f"❌ 请求失败: {response.status_code}")
            print(f"错误信息: {response.text}")
            return False
            
    except Exception as e:
        print(f"❌ 请求异常: {str(e)}")
        return False

if __name__ == "__main__":
    print("🧪 Gemini 2.5 Flash 模型测试")
    print("=" * 50)
    
    success = test_gemini_2_5_flash()
    
    if success:
        print("\n🎉 测试成功！Gemini 2.5 Flash 模型工作正常")
    else:
        print("\n💥 测试失败！请检查配置")