youngtsai commited on
Commit
496c5e9
·
verified ·
1 Parent(s): 8eb8ed4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -11
app.py CHANGED
@@ -1,22 +1,45 @@
1
  import gradio as gr
2
- import random
 
 
 
 
 
3
 
4
  class SimpleChatBot:
5
  def __init__(self):
6
- self.responses = {
7
- "你好": "你好!有什麼我可以幫助你的嗎?",
8
- "再見": "再見!希望很快能再見到你。",
9
- "你叫什麼名字": "我是你的Gradio聊天機器人。",
10
- "今天天氣如何": "我不太清楚,但你可以查一下當地的天氣預報。"
11
- }
12
-
13
- def get_response(self, message):
14
- return self.responses.get(message, "抱歉,我不太明白你的意思。")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  chatbot = SimpleChatBot()
17
 
18
  def respond(message, chat_history):
19
- response = chatbot.get_response(message)
 
20
  chat_history.append({"role": "user", "content": message})
21
  chat_history.append({"role": "assistant", "content": response})
22
  return chat_history, ""
 
1
  import gradio as gr
2
+ import os
3
+ from groq import Groq
4
+
5
+ # Set up the Groq client with the secret key
6
+ groq_key = os.getenv('groq_key')
7
+ client = Groq(api_key=groq_key)
8
 
9
  class SimpleChatBot:
10
  def __init__(self):
11
+ self.initial_prompt = [
12
+ {
13
+ "role": "system",
14
+ "content": "你是一個老師"
15
+ }
16
+ ]
17
+
18
+ def get_response(self, message, chat_history):
19
+ messages = self.initial_prompt + chat_history
20
+ messages.append({"role": "user", "content": message})
21
+
22
+ completion = client.chat.completions.create(
23
+ model="llama3-8b-8192",
24
+ messages=messages,
25
+ temperature=1,
26
+ max_tokens=1024,
27
+ top_p=1,
28
+ stream=True,
29
+ stop=None,
30
+ )
31
+
32
+ response_content = ""
33
+ for chunk in completion:
34
+ response_content += chunk.choices[0].delta.content or ""
35
+
36
+ return response_content
37
 
38
  chatbot = SimpleChatBot()
39
 
40
  def respond(message, chat_history):
41
+ chat_history = [{"role": entry["role"], "content": entry["content"]} for entry in chat_history]
42
+ response = chatbot.get_response(message, chat_history)
43
  chat_history.append({"role": "user", "content": message})
44
  chat_history.append({"role": "assistant", "content": response})
45
  return chat_history, ""