mjolnir1122 commited on
Commit
ec68136
·
verified ·
1 Parent(s): 6abf4f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -32
app.py CHANGED
@@ -143,8 +143,11 @@ def calculate_cost(method, water_cost, water_purification_cost, energy_source, e
143
  def call_groq_api(user_inputs, production_data, cost_data):
144
  """Call Groq API with Llama 3 to analyze production parameters and provide recommendations"""
145
 
146
- if not GROQ_API_KEY:
147
- return {"error": "Groq API key not found. Please set the GROQ_API_KEY environment variable."}
 
 
 
148
 
149
  # Format inputs for the API
150
  prompt = f"""
@@ -179,7 +182,7 @@ def call_groq_api(user_inputs, production_data, cost_data):
179
  3. Three specific recommendations to reduce costs
180
  4. An ideal parameter configuration based on the provided inputs
181
 
182
- Format your response as a structured JSON with the following fields:
183
  {
184
  "efficiency_assessment": "text analysis",
185
  "efficiency_recommendations": ["recommendation1", "recommendation2", "recommendation3"],
@@ -199,38 +202,29 @@ def call_groq_api(user_inputs, production_data, cost_data):
199
  """
200
 
201
  try:
202
- # API call to Groq
203
- headers = {
204
- "Authorization": f"Bearer {GROQ_API_KEY}",
205
- "Content-Type": "application/json"
206
- }
207
-
208
- payload = {
209
- "messages": [
210
- {"role": "user", "content": prompt}
211
  ],
212
- "model": "llama3-70b-8192",
213
- "temperature": 0.5,
214
- "max_tokens": 1024,
215
- "response_format": {"type": "json_object"}
216
- }
217
-
218
- response = requests.post(
219
- "https://api.groq.com/openai/v1/chat/completions",
220
- headers=headers,
221
- json=payload
222
  )
223
 
224
- if response.status_code == 200:
225
- response_data = response.json()
226
- # Extract JSON from the response
227
- try:
228
- recommendations_json = json.loads(response_data["choices"][0]["message"]["content"])
229
- return recommendations_json
230
- except json.JSONDecodeError:
231
- return {"error": "Failed to parse API response as JSON"}
232
- else:
233
- return {"error": f"API call failed with status code {response.status_code}: {response.text}"}
234
 
235
  except Exception as e:
236
  return {"error": f"Error calling Groq API: {str(e)}"}
 
143
  def call_groq_api(user_inputs, production_data, cost_data):
144
  """Call Groq API with Llama 3 to analyze production parameters and provide recommendations"""
145
 
146
+ # Initialize Groq client
147
+ try:
148
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
149
+ except Exception as e:
150
+ return {"error": f"Failed to initialize Groq client: {str(e)}"}
151
 
152
  # Format inputs for the API
153
  prompt = f"""
 
182
  3. Three specific recommendations to reduce costs
183
  4. An ideal parameter configuration based on the provided inputs
184
 
185
+ Format your response as a structured JSON with these fields:
186
  {
187
  "efficiency_assessment": "text analysis",
188
  "efficiency_recommendations": ["recommendation1", "recommendation2", "recommendation3"],
 
202
  """
203
 
204
  try:
205
+ # Call Groq API using the client
206
+ chat_completion = client.chat.completions.create(
207
+ messages=[
208
+ {
209
+ "role": "user",
210
+ "content": prompt,
211
+ }
 
 
212
  ],
213
+ model="llama-3.3-70b-versatile",
214
+ temperature=0.5,
215
+ max_tokens=1024,
216
+ response_format={"type": "json_object"}
 
 
 
 
 
 
217
  )
218
 
219
+ # Extract the response content
220
+ response_content = chat_completion.choices[0].message.content
221
+
222
+ # Parse the JSON response
223
+ try:
224
+ recommendations_json = json.loads(response_content)
225
+ return recommendations_json
226
+ except json.JSONDecodeError:
227
+ return {"error": "Failed to parse API response as JSON"}
 
228
 
229
  except Exception as e:
230
  return {"error": f"Error calling Groq API: {str(e)}"}