mfraz commited on
Commit
8d46f3f
·
verified ·
1 Parent(s): 6b471e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -4,26 +4,30 @@ import requests
4
  # Groq API Key (Replace with your actual key)
5
  GROQ_API_KEY = "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"
6
 
7
- # Function to interact with Groq's LLaMA 3 model
 
 
 
8
  def chat_with_groq(prompt, history):
9
  headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
10
 
11
  # System role for chatbot identity
12
- messages = [{"role": "system", "content": "You are Faraz-Chatbot, a helpful assistant."}]
13
 
14
- # Convert history to Groq format
15
  for entry in history:
16
- if isinstance(entry, list) and len(entry) == 2: # Ensure correct format
17
  user_msg, bot_msg = entry
18
  messages.append({"role": "user", "content": user_msg})
19
  messages.append({"role": "assistant", "content": bot_msg})
20
-
21
- messages.append({"role": "user", "content": prompt}) # Add latest user query
 
22
 
23
  payload = {"model": "llama3-8b-8192", "messages": messages} # Groq's LLaMA 3 model
24
 
25
  try:
26
- response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
27
  response_data = response.json()
28
 
29
  if response.status_code == 200:
@@ -34,14 +38,14 @@ def chat_with_groq(prompt, history):
34
  except Exception as e:
35
  return f"Exception Error: {str(e)}"
36
 
37
- # Function to handle user queries
38
  def chatbot(query, history):
39
  if not query.strip():
40
  return history # Return previous history if query is empty
41
 
42
  response = chat_with_groq(query, history)
43
 
44
- # Append user query and bot response as a **list of lists**
45
  history = history + [[query, response]]
46
 
47
  return history # Return updated history
 
4
  # Groq API Key (Replace with your actual key)
5
  GROQ_API_KEY = "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"
6
 
7
+ # Groq API URL for chat completion
8
+ GROQ_API_URL = "https://console.groq.com/keys"
9
+
10
+ # Function to interact with Groq API
11
  def chat_with_groq(prompt, history):
12
  headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
13
 
14
  # System role for chatbot identity
15
+ messages = [{"role": "system", "content": "You are Faraz-Chatbot, an AI assistant providing helpful responses."}]
16
 
17
+ # Ensure history is in the correct format
18
  for entry in history:
19
+ if isinstance(entry, list) and len(entry) == 2:
20
  user_msg, bot_msg = entry
21
  messages.append({"role": "user", "content": user_msg})
22
  messages.append({"role": "assistant", "content": bot_msg})
23
+
24
+ # Add the latest user query
25
+ messages.append({"role": "user", "content": prompt})
26
 
27
  payload = {"model": "llama3-8b-8192", "messages": messages} # Groq's LLaMA 3 model
28
 
29
  try:
30
+ response = requests.post(GROQ_API_URL, headers=headers, json=payload)
31
  response_data = response.json()
32
 
33
  if response.status_code == 200:
 
38
  except Exception as e:
39
  return f"Exception Error: {str(e)}"
40
 
41
+ # Function to handle chatbot responses
42
  def chatbot(query, history):
43
  if not query.strip():
44
  return history # Return previous history if query is empty
45
 
46
  response = chat_with_groq(query, history)
47
 
48
+ # Append user query and bot response as a list of lists
49
  history = history + [[query, response]]
50
 
51
  return history # Return updated history