File size: 945 Bytes
0e4080b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import google.generativeai as genai
from typing import List, Dict
import os

class GeminiChat:
    def __init__(self):
        api_key = os.getenv("GOOGLE_AI_STUDIO_KEY")
        if not api_key:
            raise ValueError("GOOGLE_AI_STUDIO_KEY environment variable not set")
        
        genai.configure(api_key=api_key)
        self.model = genai.GenerativeModel('gemini-pro')
        
    async def chat(self, 
                  messages: List[Dict[str, str]], 
                  temperature: float = 0.7) -> str:
        """Generate a chat response using Gemini API."""
        try:
            chat = self.model.start_chat(history=messages)
            response = chat.send_message(
                messages[-1]["content"],
                generation_config={"temperature": temperature}
            )
            return response.text
        except Exception as e:
            print(f"Error in Gemini chat: {e}")
            return ""