Spaces:
Build error
Build error
| # 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 |