File size: 2,981 Bytes
ec4aa90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Centralized Gemini API configuration.
Allows users to configure model settings from .env file.
"""

import os
from typing import Optional
from dotenv import load_dotenv

# Load environment variables
load_dotenv()


class GeminiConfig:
    """Centralized configuration for Gemini API."""
    
    # Default model - can be overridden in .env
    DEFAULT_MODEL = "gemini-2.5-flash"
    
    # Model configuration from environment
    MODEL_NAME: str = os.getenv("GEMINI_MODEL", DEFAULT_MODEL)
    API_KEY: str = os.getenv("GEMINI_API_KEY", "")
    
    # Temperature settings for different use cases
    TEMPERATURE_PRECISE = 0.0  # For JSON schema responses
    TEMPERATURE_LOW = 0.1      # For code generation
    TEMPERATURE_MEDIUM = 0.2   # For transformations
    TEMPERATURE_HIGH = 0.7     # For creative tasks
    
    # Token limits
    MAX_OUTPUT_TOKENS_SMALL = 8192
    MAX_OUTPUT_TOKENS_MEDIUM = 16384
    MAX_OUTPUT_TOKENS_LARGE = 32768
    
    # Retry settings
    MAX_RETRIES = 3
    RETRY_DELAY = 1.0  # seconds
    
    @classmethod
    def validate(cls) -> bool:
        """Validate that required configuration is present."""
        if not cls.API_KEY:
            raise ValueError(
                "GEMINI_API_KEY not found in environment variables. "
                "Please set it in your .env file."
            )
        return True
    
    @classmethod
    def get_model_name(cls) -> str:
        """Get the configured model name."""
        return cls.MODEL_NAME
    
    @classmethod
    def get_api_key(cls) -> str:
        """Get the API key."""
        cls.validate()
        return cls.API_KEY
    
    @classmethod
    def get_base_config(cls, temperature: float = TEMPERATURE_LOW, 
                       max_tokens: int = MAX_OUTPUT_TOKENS_MEDIUM) -> dict:
        """
        Get base configuration for Gemini API calls.
        
        Args:
            temperature: Temperature setting (0.0-1.0)
            max_tokens: Maximum output tokens
            
        Returns:
            Configuration dictionary
        """
        return {
            "temperature": temperature,
            "max_output_tokens": max_tokens,
            "top_p": 0.95,
        }
    
    @classmethod
    def get_json_config(cls, schema: dict, 
                       temperature: float = TEMPERATURE_PRECISE,
                       max_tokens: int = MAX_OUTPUT_TOKENS_MEDIUM) -> dict:
        """
        Get configuration for JSON schema-enforced responses.
        
        Args:
            schema: JSON schema dictionary
            temperature: Temperature setting (default: 0.0 for precision)
            max_tokens: Maximum output tokens
            
        Returns:
            Configuration dictionary with schema enforcement
        """
        config = cls.get_base_config(temperature, max_tokens)
        config.update({
            "response_mime_type": "application/json",
            "response_schema": schema
        })
        return config