File size: 1,079 Bytes
971b0ff
 
 
 
 
cf6b61d
971b0ff
cf6b61d
971b0ff
cf6b61d
 
 
 
 
971b0ff
cf6b61d
 
971b0ff
 
 
 
 
 
 
 
cf6b61d
971b0ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# groq_client.py
# Filepath: ai-email-assistant/groq_client.py
# Interfaces with the Groq API for AI-powered responses

import os
import json
from groq import Groq
from dotenv import load_dotenv

def load_config():
    load_dotenv()
    return {
        'groq_api_key': os.getenv('GROQ_API_KEY')
    }

config = load_config()
api_key = config.get('groq_api_key')
client = Groq(api_key=api_key) if api_key else None

def set_api_key(api_key):
    global client
    client = Groq(api_key=api_key)

def get_ai_response(message, system_prompt, context=""):
    if not client:
        raise ValueError("Groq API key not set. Please check your config.json file.")

    messages = [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": message}
    ]
    
    if context:
        messages.append({"role": "system", "content": context})

    response = client.chat.completions.create(
        model="mixtral-8x7b-32768",
        messages=messages,
        temperature=0.7,
        max_tokens=1000
    )

    return response.choices[0].message.content