bwilkie commited on
Commit
0b5120b
·
verified ·
1 Parent(s): 3c62566

Create groq_api.py

Browse files
Files changed (1) hide show
  1. groq_api.py +218 -0
groq_api.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ from typing import List, Dict, Any
4
+
5
+ def grok_get_llm_response(
6
+ system_prompt: str,
7
+ user_input: str,
8
+ tools: List[Dict[str, Any]] = None,
9
+ tool_choice: str = "auto",
10
+ reasoning_effort: str = "default", #"default",json, text, verbose_json
11
+ response_format: Dict[str, Any] = None,
12
+ temperature: float = 0.3,
13
+ max_completion_tokens: int = 2000,
14
+ include_reasoning: str = False # True
15
+ ) -> str:
16
+ """
17
+ Make a request to the Grok API and return the response content, supporting tool usage and agentic features.
18
+
19
+ Args:
20
+ system_prompt (str): The system prompt to set the context.
21
+ user_input (str): The user input to process.
22
+ tools (List[Dict[str, Any]], optional): List of tool definitions for tool-calling.
23
+ tool_choice (str, optional): Controls tool usage ("none", "auto", "required"). Defaults to "auto".
24
+ reasoning_effort (str, optional): Reasoning mode for Qwen3 models ("none", "default"). Defaults to "default".
25
+ response_format (Dict[str, Any], optional): Format for structured outputs (e.g., JSON schema).
26
+ temperature (float, optional): Sampling temperature (0 to 2). Defaults to 0.7 for determinism.
27
+ max_completion_tokens (int, optional): Max tokens in response. Defaults to 1000.
28
+
29
+ Returns:
30
+ str: The content of the assistant's response or tool call results, or empty string on error.
31
+ """
32
+ # Retrieve API key from environment
33
+ api_key = os.getenv("GROQ_API_KEY")
34
+ if not api_key:
35
+ print("Grok API error: GROQ_API_KEY environment variable not set")
36
+ return ""
37
+
38
+ # API endpoint
39
+ api_url = "https://api.groq.com/openai/v1/chat/completions"
40
+
41
+ # Prepare messages in Grok API format
42
+ messages = [
43
+ {"role": "system", "content": system_prompt},
44
+ {"role": "user", "content": user_input}
45
+ ]
46
+
47
+ # Prepare payload
48
+ payload = {
49
+ "model": "qwen/qwen3-32b",
50
+ "messages": messages,
51
+ "temperature": max(0, min(temperature, 2)), # Clamp to valid range [0, 2]
52
+ "include_reasoning": include_reasoning,
53
+ "max_completion_tokens": max_completion_tokens
54
+ }
55
+
56
+ # Add tools and tool_choice if provided
57
+ if tools:
58
+ payload["tools"] = tools
59
+ if tool_choice in ["none", "auto", "required"]:
60
+ payload["tool_choice"] = tool_choice
61
+ else:
62
+ print(f"Grok API warning: Invalid tool_choice '{tool_choice}', defaulting to 'auto'")
63
+ payload["tool_choice"] = "auto"
64
+
65
+ # Add reasoning_effort for Qwen3 models
66
+ if reasoning_effort in ["none", "default"]:
67
+ payload["reasoning_effort"] = reasoning_effort
68
+ else:
69
+ print(f"Grok API warning: Invalid reasoning_effort '{reasoning_effort}', defaulting to 'default'")
70
+ payload["reasoning_effort"] = "default"
71
+
72
+ # Add response_format if provided
73
+ if response_format:
74
+ payload["response_format"] = response_format
75
+
76
+ # Set headers
77
+ headers = {
78
+ "Content-Type": "application/json",
79
+ "Authorization": f"Bearer {api_key}"
80
+ }
81
+
82
+ try:
83
+ # Make API request
84
+ response = requests.post(api_url, headers=headers, json=payload, timeout=60)
85
+ response.raise_for_status()
86
+
87
+ # Parse response
88
+ result = response.json()
89
+ choice = result.get("choices", [{}])[0]
90
+ message = choice.get("message", {})
91
+
92
+ # Handle tool calls if present
93
+ if "tool_calls" in message:
94
+ tool_calls = message["tool_calls"]
95
+ tool_results = []
96
+ for tool_call in tool_calls:
97
+ tool_name = tool_call.get("function", {}).get("name", "")
98
+ tool_args = tool_call.get("function", {}).get("arguments", "{}")
99
+ tool_results.append(f"Tool Call: {tool_name} with args {tool_args}")
100
+ return "; ".join(tool_results) # Combine tool call results into a single string
101
+
102
+ # Return assistant content if no tool calls
103
+ content = message.get("content", "")
104
+ return content.strip()
105
+
106
+ except requests.exceptions.HTTPError as e:
107
+ print(f"Grok API error: HTTP {e.response.status_code} - {e.response.text}")
108
+ return ""
109
+ except requests.exceptions.RequestException as e:
110
+ print(f"Grok API error: Network error - {e}")
111
+ return ""
112
+ except (KeyError, ValueError) as e:
113
+ print(f"Grok API error: Unexpected response format - {e}")
114
+ return ""
115
+ except Exception as e:
116
+ print(f"Grok API error: Unexpected error - {e}")
117
+ return ""
118
+
119
+
120
+
121
+ def open_oss_get_llm_response(
122
+ system_prompt: str,
123
+ user_input: str,
124
+ tools: List[Dict[str, Any]] = None,
125
+ tool_choice: str = "auto",
126
+ temperature: float = 0.1,
127
+ max_completion_tokens: int = 3000
128
+ ) -> str:
129
+ """
130
+ Make a request to the Grok API and return the response content, supporting tool usage and agentic features.
131
+
132
+ Args:
133
+ system_prompt (str): The system prompt to set the context.
134
+ user_input (str): The user input to process.
135
+ tools (List[Dict[str, Any]], optional): List of tool definitions for tool-calling.
136
+ tool_choice (str, optional): Controls tool usage ("none", "auto", "required"). Defaults to "auto".
137
+ temperature (float, optional): Sampling temperature (0 to 2). Defaults to 0.7 for determinism.
138
+ max_completion_tokens (int, optional): Max tokens in response. Defaults to 1000.
139
+
140
+ Returns:
141
+ str: The content of the assistant's response or tool call results, or empty string on error.
142
+ """
143
+ # Retrieve API key from environment
144
+ api_key = os.getenv("GROQ_API_KEY")
145
+ if not api_key:
146
+ print("Grok API error: GROQ_API_KEY environment variable not set")
147
+ return ""
148
+
149
+ # API endpoint
150
+ api_url = "https://api.groq.com/openai/v1/chat/completions"
151
+
152
+ # Prepare messages in Grok API format
153
+ messages = [
154
+ {"role": "system", "content": system_prompt},
155
+ {"role": "user", "content": user_input}
156
+ ]
157
+
158
+ # Prepare payload
159
+ payload = {
160
+ "model": "openai/gpt-oss-20b",
161
+ "messages": messages,
162
+ "temperature": max(0, min(temperature, 2)), # Clamp to valid range [0, 2]
163
+ "max_completion_tokens": max_completion_tokens,
164
+ "reasoning_effort": "medium"
165
+ }
166
+
167
+ # Add tools and tool_choice if provided
168
+ if tools:
169
+ payload["tools"] = tools
170
+ if tool_choice in ["none", "auto", "required"]:
171
+ payload["tool_choice"] = tool_choice
172
+ else:
173
+ print(f"Grok API warning: Invalid tool_choice '{tool_choice}', defaulting to 'auto'")
174
+ payload["tool_choice"] = "auto"
175
+
176
+
177
+ # Set headers
178
+ headers = {
179
+ "Content-Type": "application/json",
180
+ "Authorization": f"Bearer {api_key}"
181
+ }
182
+
183
+ try:
184
+ # Make API request
185
+ response = requests.post(api_url, headers=headers, json=payload, timeout=60)
186
+ response.raise_for_status()
187
+
188
+ # Parse response
189
+ result = response.json()
190
+ choice = result.get("choices", [{}])[0]
191
+ message = choice.get("message", {})
192
+
193
+ # Handle tool calls if present
194
+ if "tool_calls" in message:
195
+ tool_calls = message["tool_calls"]
196
+ tool_results = []
197
+ for tool_call in tool_calls:
198
+ tool_name = tool_call.get("function", {}).get("name", "")
199
+ tool_args = tool_call.get("function", {}).get("arguments", "{}")
200
+ tool_results.append(f"Tool Call: {tool_name} with args {tool_args}")
201
+ return "; ".join(tool_results) # Combine tool call results into a single string
202
+
203
+ # Return assistant content if no tool calls
204
+ content = message.get("content", "")
205
+ return content.strip()
206
+
207
+ except requests.exceptions.HTTPError as e:
208
+ print(f"Grok API error: HTTP {e.response.status_code} - {e.response.text}")
209
+ return ""
210
+ except requests.exceptions.RequestException as e:
211
+ print(f"Grok API error: Network error - {e}")
212
+ return ""
213
+ except (KeyError, ValueError) as e:
214
+ print(f"Grok API error: Unexpected response format - {e}")
215
+ return ""
216
+ except Exception as e:
217
+ print(f"Grok API error: Unexpected error - {e}")
218
+ return ""