-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroq_helper.py
More file actions
44 lines (33 loc) · 1.24 KB
/
groq_helper.py
File metadata and controls
44 lines (33 loc) · 1.24 KB
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
from groq import Groq
def chat_with_groq(api_key, system_prompt, user_prompt):
"""
Interact with the Groq LLM API
Args:
api_key (str): The API key for Groq
system_prompt (str): The system prompt for the LLM
user_prompt (str): The user prompt for the LLM
Returns:
str: The LLM response content
"""
client = Groq(api_key=api_key)
messages = []
wants_json = False
if system_prompt:
wants_json = "JSON" in system_prompt.upper()
messages.append({"role": "system", "content": system_prompt})
if "json" not in user_prompt.lower() and not wants_json:
user_prompt = f"{user_prompt}\n\nProvide your response in JSON format."
messages.append({"role": "user", "content": user_prompt})
# Setup parameters for the API call
params = {
"model": "llama3-70b-8192",
"messages": messages,
"temperature": 0.2,
"max_tokens": 1000
}
params["response_format"] = {"type": "json_object"}
try:
response = client.chat.completions.create(**params)
return response.choices[0].message.content
except Exception as e:
return f'{{"error": "API Error: {str(e)}"}}'