Spaces:
Sleeping
Sleeping
| import os | |
| from dotenv import load_dotenv | |
| from agents import AsyncOpenAI, OpenAIChatCompletionsModel, set_tracing_disabled | |
| set_tracing_disabled(True) | |
| load_dotenv() | |
| # Get Gemini API key from environment variables | |
| gemini_api_key = os.getenv("GEMINI_API_KEY") | |
| # Remove quotes if present (sometimes .env files have quotes) | |
| if gemini_api_key: | |
| gemini_api_key = gemini_api_key.strip().strip('"').strip("'") | |
| # Check if Gemini API key is set | |
| if not gemini_api_key: | |
| raise ValueError( | |
| "GEMINI_API_KEY is not set. Please add it to your .env file: GEMINI_API_KEY=your_key_here" | |
| ) | |
| # Validate API key format (Gemini keys typically start with AIza) | |
| if not gemini_api_key.startswith("AIza"): | |
| print(f"Warning: GEMINI_API_KEY format may be incorrect. Keys usually start with 'AIza'. Got: {gemini_api_key[:10]}...") | |
| # Configure Gemini using AsyncOpenAI with Gemini's OpenAI-compatible endpoint | |
| client_provider = AsyncOpenAI( | |
| api_key=gemini_api_key, | |
| base_url="https://generativelanguage.googleapis.com/v1beta/openai/", | |
| ) | |
| # Use Gemini model - gemini-1.5-pro or gemini-1.5-flash are good options | |
| # gemini-1.5-flash is faster, gemini-1.5-pro is more capable | |
| model = OpenAIChatCompletionsModel( | |
| model="gemini-1.5-flash", # Using Gemini 1.5 Flash for fast responses | |
| openai_client=client_provider | |
| ) | |
| print("Setup complete! Model ready with Google Gemini 1.5 Flash") | |
| print(f"API Key status: {'Loaded' if gemini_api_key else 'Missing'} (length: {len(gemini_api_key) if gemini_api_key else 0})") |