Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
"messages": [
|
| 210 |
-
{"role": "user", "content": prompt}
|
| 211 |
],
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
}
|
| 217 |
-
|
| 218 |
-
response = requests.post(
|
| 219 |
-
"https://api.groq.com/openai/v1/chat/completions",
|
| 220 |
-
headers=headers,
|
| 221 |
-
json=payload
|
| 222 |
)
|
| 223 |
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 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)}"}
|