iisadia commited on
Commit
2998262
·
verified ·
1 Parent(s): 97ed4c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -18
app.py CHANGED
@@ -233,31 +233,50 @@ def ask_llama(conversation_history, category, is_final_guess=False):
233
  return "Could not generate question"
234
 
235
  # New function for the help AI assistant using the Hugging Face InferenceClient
236
-
237
  def ask_help_agent(query):
238
  try:
239
- API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.1"
240
- headers = {"Authorization": "Bearer hf_wm5eLl09b9I9cOxR3E9n5rrRr1CRQQjn"} # ⚠️ Replace with your token
241
-
242
- prompt = f"""<s>[INST] <<SYS>>
243
- You are a helpful assistant. Keep answers short.
244
- <</SYS>>
245
- {query} [/INST]"""
 
246
 
247
- response = requests.post(
248
- API_URL,
249
- headers=headers,
250
- json={"inputs": prompt},
251
- timeout=10
252
- )
 
 
253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
  if response.status_code == 200:
255
- return response.json()[0]['generated_text'].split("[/INST]")[-1].strip()
 
256
  else:
257
- return f"Error: {response.text}"
258
-
259
  except Exception as e:
260
- return f"Assistant busy. Try later. ({str(e)})"
 
261
 
262
  # Main game logic with enhanced UI
263
  def main():
 
233
  return "Could not generate question"
234
 
235
  # New function for the help AI assistant using the Hugging Face InferenceClient
236
+ MISTRAL_API_KEY = "wm5eLl09b9I9cOxR3E9n5rrRr1CRQQjn"
237
  def ask_help_agent(query):
238
  try:
239
+ # Prepare Mistral API request
240
+ url = "https://api.mistral.ai/v1/chat/completions"
241
+ headers = {
242
+ "Authorization": f"Bearer {MISTRAL_API_KEY}",
243
+ "Content-Type": "application/json"
244
+ }
245
+
246
+ system_message = "You are a friendly Chatbot."
247
 
248
+ # Build message history
249
+ messages = [{"role": "system", "content": system_message}]
250
+ if "help_conversation" in st.session_state:
251
+ for msg in st.session_state.help_conversation:
252
+ if msg.get("query"):
253
+ messages.append({"role": "user", "content": msg["query"]})
254
+ if msg.get("response"):
255
+ messages.append({"role": "assistant", "content": msg["response"]})
256
 
257
+ # Add current user query
258
+ messages.append({"role": "user", "content": query})
259
+
260
+ # API payload
261
+ payload = {
262
+ "model": "mistral-tiny",
263
+ "messages": messages,
264
+ "temperature": 0.7,
265
+ "top_p": 0.95
266
+ }
267
+
268
+ # Send POST request
269
+ response = requests.post(url, headers=headers, json=payload)
270
+
271
  if response.status_code == 200:
272
+ result = response.json()
273
+ return result["choices"][0]["message"]["content"]
274
  else:
275
+ return f"API Error {response.status_code}: {response.text}"
276
+
277
  except Exception as e:
278
+ return f"Error in help agent: {str(e)}"
279
+
280
 
281
  # Main game logic with enhanced UI
282
  def main():