Chad commited on
Commit
672c6f3
·
1 Parent(s): 96b881a

nwfbwfbwdwddwwdwddwdwdwd

Browse files
Files changed (1) hide show
  1. app.py +21 -19
app.py CHANGED
@@ -1,50 +1,53 @@
1
- import gradio as gr
2
  import os
 
3
  from groq import Groq
4
 
 
5
  api_key = os.getenv("GROQ_API_KEY")
6
 
7
  if not api_key:
8
  raise ValueError("GROQ_API_KEY is not set. Please set it as an environment variable.")
9
 
 
10
  client = Groq(api_key=api_key)
11
 
12
- def respond(message, history):
13
- if history is None:
14
- history = []
15
 
16
- messages = [{"role": "system", "content": (
17
  "Determine the language of the user's question and respond in the same language. "
18
  "Users may switch between English and French, so maintain consistency. "
19
  "Only answer history-related questions. If asked about another topic, reply with: "
20
  "'I am a history chatbot and can only answer history-related questions.' "
21
  "(or the equivalent in French, depending on the input language). Then stop responding."
22
  )}]
23
-
24
- for val in history:
25
- if val[0]:
26
- messages.append({"role": "user", "content": val[0]})
27
- if val[1]:
28
- messages.append({"role": "assistant", "content": val[1]})
29
 
30
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
31
 
32
  try:
33
  response = client.chat.completions.create(
34
  model="llama3-70b-8192",
35
- messages=messages,
36
  temperature=0.7,
37
  max_completion_tokens=512,
38
  top_p=0.95,
39
- stream=True,
40
  stop=None,
41
  )
42
  reply = response.choices[0].message.content or "..."
43
  except Exception as error:
44
  reply = f"Error: {error}"
45
 
46
- history.append((message, reply))
47
- return history, ""
 
48
 
49
  with gr.Blocks() as interface:
50
  gr.Markdown("## History Chatbot")
@@ -69,9 +72,9 @@ with gr.Blocks() as interface:
69
  ]
70
 
71
  send_button.click(
72
- fn=respond,
73
  inputs=[user_input, state],
74
- outputs=[chatbot, user_input],
75
  )
76
 
77
  reset_button = gr.Button("New Conversation")
@@ -79,4 +82,3 @@ with gr.Blocks() as interface:
79
 
80
  if __name__ == "__main__":
81
  interface.launch(share=True)
82
-
 
 
1
  import os
2
+ import gradio as gr
3
  from groq import Groq
4
 
5
+ # Set your API key using an environment variable
6
  api_key = os.getenv("GROQ_API_KEY")
7
 
8
  if not api_key:
9
  raise ValueError("GROQ_API_KEY is not set. Please set it as an environment variable.")
10
 
11
+ # Initialize Groq client
12
  client = Groq(api_key=api_key)
13
 
14
+ def generate_reply(user_input, chat_history):
15
+ if chat_history is None:
16
+ chat_history = []
17
 
18
+ conversation = [{"role": "system", "content": (
19
  "Determine the language of the user's question and respond in the same language. "
20
  "Users may switch between English and French, so maintain consistency. "
21
  "Only answer history-related questions. If asked about another topic, reply with: "
22
  "'I am a history chatbot and can only answer history-related questions.' "
23
  "(or the equivalent in French, depending on the input language). Then stop responding."
24
  )}]
 
 
 
 
 
 
25
 
26
+ for entry in chat_history:
27
+ if entry[0]:
28
+ conversation.append({"role": "user", "content": entry[0]})
29
+ if entry[1]:
30
+ conversation.append({"role": "assistant", "content": entry[1]})
31
+
32
+ conversation.append({"role": "user", "content": user_input})
33
 
34
  try:
35
  response = client.chat.completions.create(
36
  model="llama3-70b-8192",
37
+ messages=conversation,
38
  temperature=0.7,
39
  max_completion_tokens=512,
40
  top_p=0.95,
41
+ stream=False,
42
  stop=None,
43
  )
44
  reply = response.choices[0].message.content or "..."
45
  except Exception as error:
46
  reply = f"Error: {error}"
47
 
48
+ chat_history.append({"role": "user", "content": user_input})
49
+ chat_history.append({"role": "assistant", "content": reply})
50
+ return chat_history, ""
51
 
52
  with gr.Blocks() as interface:
53
  gr.Markdown("## History Chatbot")
 
72
  ]
73
 
74
  send_button.click(
75
+ fn=generate_reply,
76
  inputs=[user_input, state],
77
+ outputs=[state, user_input],
78
  )
79
 
80
  reset_button = gr.Button("New Conversation")
 
82
 
83
  if __name__ == "__main__":
84
  interface.launch(share=True)