File size: 2,047 Bytes
12ba16b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# src/utils/gemini_client.py
# A centralized client for interacting with the Google Gemini API.

import google.generativeai as genai
import config
from .logger import log

class GeminiClient:
    """Handles all communication with the Gemini API."""
    
    def __init__(self):
        log("GeminiClient", "Initializing Gemini client...")
        try:
            genai.configure(api_key=config.API_KEY)
            self.model = genai.GenerativeModel('gemini-1.5-pro')
            self.generation_config = config.GEMINI_MODEL_CONFIG
            log("GeminiClient", "Gemini client configured successfully.")
        except Exception as e:
            log("GeminiClient", f"FATAL: Failed to configure Gemini API. Check your API key. Error: {e}", level="ERROR")
            self.model = None

    def generate(self, specialized_prompt):
        """
        Sends a prompt to the Gemini API and returns the response.
        
        Args:
            specialized_prompt (str): A detailed, role-playing prompt crafted by an SCM.

        Returns:
            str: The text response from the Gemini API, or an error message.
        """
        if not self.model:
            return "Error: Gemini client is not initialized. Please check your API key."

        try:
            log("GeminiClient", "Sending request to Gemini API...")
            response = self.model.generate_content(
                specialized_prompt,
                generation_config=self.generation_config
            )
            log("GeminiClient", "Response received successfully.")
            return response.text
        except Exception as e:
            log("GeminiClient", f"API call failed. Error: {e}", level="ERROR")
            # Check for specific quota errors
            if "quota" in str(e).lower():
                return "API Error: You have exceeded your usage quota. Please check your plan and billing details."
            return f"API Error: {e}"

# Create a single, shared instance of the client to be used by all modules
gemini_client = GeminiClient()