| #!/usr/bin/env python3 | |
| """ | |
| پیکربندی Gemini API برای پروژه CA14 | |
| Gemini API Configuration for CA14 Project | |
| """ | |
| import os | |
| from typing import Optional, Dict, Any | |
| from dataclasses import dataclass | |
| from dotenv import load_dotenv | |
| # بارگذاری متغیرهای محیطی | |
| load_dotenv() | |
| class GeminiConfig: | |
| """کلاس پیکربندی Gemini API""" | |
| api_key: str | |
| model: str = "gemini-1.5-pro" | |
| temperature: float = 0.7 | |
| max_tokens: int = 2048 | |
| top_p: float = 0.8 | |
| top_k: int = 40 | |
| safety_settings: Optional[Dict[str, Any]] = None | |
| def __post_init__(self): | |
| """تنظیمات امنیتی پیشفرض""" | |
| if self.safety_settings is None: | |
| self.safety_settings = { | |
| "HARM_CATEGORY_HARASSMENT": "BLOCK_MEDIUM_AND_ABOVE", | |
| "HARM_CATEGORY_HATE_SPEECH": "BLOCK_MEDIUM_AND_ABOVE", | |
| "HARM_CATEGORY_SEXUALLY_EXPLICIT": "BLOCK_MEDIUM_AND_ABOVE", | |
| "HARM_CATEGORY_DANGEROUS_CONTENT": "BLOCK_MEDIUM_AND_ABOVE", | |
| } | |
| class GeminiManager: | |
| """مدیر Gemini API""" | |
| def __init__(self, config: Optional[GeminiConfig] = None): | |
| """مقداردهی اولیه""" | |
| self.config = config or self._create_default_config() | |
| self._validate_config() | |
| def _create_default_config(self) -> GeminiConfig: | |
| """ایجاد پیکربندی پیشفرض""" | |
| api_key = os.getenv("GEMINI_API_KEY") | |
| if not api_key: | |
| raise ValueError( | |
| "GEMINI_API_KEY not found in environment variables. " | |
| "Please set your Gemini API key in .env file or environment." | |
| ) | |
| return GeminiConfig(api_key=api_key) | |
| def _validate_config(self): | |
| """اعتبارسنجی پیکربندی""" | |
| if not self.config.api_key: | |
| raise ValueError("Gemini API key is required") | |
| if not self.config.api_key.startswith("AI"): | |
| raise ValueError("Invalid Gemini API key format") | |
| def get_model_config(self) -> Dict[str, Any]: | |
| """دریافت پیکربندی مدل""" | |
| return { | |
| "model": self.config.model, | |
| "temperature": self.config.temperature, | |
| "max_output_tokens": self.config.max_tokens, | |
| "top_p": self.config.top_p, | |
| "top_k": self.config.top_k, | |
| "safety_settings": self.config.safety_settings, | |
| } | |
| def update_config(self, **kwargs): | |
| """بهروزرسانی پیکربندی""" | |
| for key, value in kwargs.items(): | |
| if hasattr(self.config, key): | |
| setattr(self.config, key, value) | |
| else: | |
| raise ValueError(f"Unknown configuration parameter: {key}") | |
| # نمونه سراسری | |
| gemini_manager = GeminiManager() | |
| # توابع کمکی | |
| def get_gemini_config() -> GeminiConfig: | |
| """دریافت پیکربندی فعلی Gemini""" | |
| return gemini_manager.config | |
| def update_gemini_config(**kwargs): | |
| """بهروزرسانی پیکربندی Gemini""" | |
| gemini_manager.update_config(**kwargs) | |
| def get_model_config() -> Dict[str, Any]: | |
| """دریافت پیکربندی مدل""" | |
| return gemini_manager.get_model_config() | |
| # تنظیمات مدلهای مختلف | |
| MODEL_CONFIGS = { | |
| "gemini-1.5-pro": { | |
| "model": "gemini-1.5-pro", | |
| "temperature": 0.7, | |
| "max_tokens": 8192, | |
| "top_p": 0.8, | |
| "top_k": 40, | |
| }, | |
| "gemini-1.5-flash": { | |
| "model": "gemini-1.5-flash", | |
| "temperature": 0.7, | |
| "max_tokens": 8192, | |
| "top_p": 0.8, | |
| "top_k": 40, | |
| }, | |
| "gemini-pro": { | |
| "model": "gemini-pro", | |
| "temperature": 0.7, | |
| "max_tokens": 2048, | |
| "top_p": 0.8, | |
| "top_k": 40, | |
| }, | |
| } | |
| def get_model_config_by_name(model_name: str) -> Dict[str, Any]: | |
| """دریافت پیکربندی بر اساس نام مدل""" | |
| if model_name not in MODEL_CONFIGS: | |
| raise ValueError(f"Unknown model: {model_name}") | |
| config = MODEL_CONFIGS[model_name].copy() | |
| config["safety_settings"] = get_gemini_config().safety_settings | |
| return config | |
| if __name__ == "__main__": | |
| # تست پیکربندی | |
| try: | |
| config = get_gemini_config() | |
| print("✅ پیکربندی Gemini با موفقیت بارگذاری شد") | |
| print(f"مدل: {config.model}") | |
| print(f"دما: {config.temperature}") | |
| print(f"حداکثر توکن: {config.max_tokens}") | |
| except Exception as e: | |
| print(f"❌ خطا در بارگذاری پیکربندی: {e}") | |
| print("لطفاً GEMINI_API_KEY را در فایل .env تنظیم کنید") | |
Xet Storage Details
- Size:
- 4.71 kB
- Xet hash:
- 1e6964f8762a648996a3fa33caeec745eb918e98788a378816c76e7fb1fb9acf
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.