peterchuang commited on
Commit
a2d9258
1 Parent(s): a8512ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -58
app.py CHANGED
@@ -1,67 +1,63 @@
1
- import gradio as gr
2
  import os
 
 
3
 
4
- # Install the groq package if it is not installed
5
- try:
6
- from groq import Groq
7
- except ImportError:
8
- os.system('pip install groq')
9
- from groq import Groq
10
 
11
- # Set up the Groq client with the secret key
12
- groq_key = os.getenv('groq_key')
13
- if not groq_key:
14
- raise ValueError("groq_key environment variable is not set")
15
 
16
- client = Groq(api_key=groq_key)
 
 
 
 
17
 
18
- class SimpleChatBot:
19
- def __init__(self):
20
- self.initial_prompt = [
 
 
 
 
 
 
 
21
  {
22
- "role": "system",
23
- "content": "你是一個英文老師,會教我背單字,並用蘇格拉底問答引導我,引導時請用繁體中文 zhTW"
24
  }
25
- ]
26
-
27
- def get_response(self, message, chat_history):
28
- messages = self.initial_prompt + chat_history
29
- messages.append({"role": "user", "content": message})
30
-
31
- completion = client.chat.completions.create(
32
- model="llama-3.1-70b-versatile",
33
- messages=messages,
34
- temperature=1,
35
- max_tokens=1024,
36
- top_p=1,
37
- stream=True,
38
- stop=None,
39
- )
40
-
41
- response_content = ""
42
- for chunk in completion:
43
- response_content += chunk.choices[0].delta.content or ""
44
-
45
- return response_content
46
-
47
- chatbot = SimpleChatBot()
48
-
49
- def respond(message, chat_history):
50
- chat_history = [{"role": entry["role"], "content": entry["content"]} for entry in chat_history]
51
- response = chatbot.get_response(message, chat_history)
52
- chat_history.append({"role": "user", "content": message})
53
- chat_history.append({"role": "assistant", "content": response})
54
- return chat_history, ""
55
-
56
- with gr.Blocks(title="簡單的Gradio聊天機器人") as demo:
57
- gr.Markdown("# 簡單的Gradio聊天機器人")
58
-
59
- chatbot_interface = gr.Chatbot(type="messages")
60
-
61
- with gr.Row():
62
- user_input = gr.Textbox(placeholder="輸入訊息...", label="你的訊息")
63
- send_button = gr.Button("發送")
64
 
65
- send_button.click(respond, inputs=[user_input, chatbot_interface], outputs=[chatbot_interface, user_input])
 
66
 
67
- demo.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ import gradio as gr
3
+ from groq import Groq
4
 
5
+ # 從環境變量中獲取 API 金鑰
6
+ api_key = os.getenv("groq_key")
 
 
 
 
7
 
8
+ # 初始化 groq 客戶端
9
+ client = Groq(api_key=api_key)
 
 
10
 
11
+ # 系統消息的內容
12
+ system_message = {
13
+ "role": "system",
14
+ "content": "你是一位樂觀、鼓舞人心的學習嚮導,通過解釋想法並問學生問題來幫助他理解概念。\n\n1. 開始時向學生介紹自己,告訴他你是他的 AI 學習嚮導(名字:瑞伊 RAI),樂意幫助他解答任何問題。每次只問一個問題,不要在學生回答之前轉移話題。\n2. 首先,問他想學什麼。等待回應,不要替學生回答。然後問他的學習層次:你是高中生、大學生還是專業人士?等待他們的回應。\n3. 然後問他對所選主題的了解程度。你可以問他已經知道什麼,或者可以即興提問,以了解學生對主題的了解程度。等待回應。\n4. 根據這些信息,幫助學生通過提供解釋、例子和類比來理解主題。這些解釋應該根據學生的學習層次和先前知識或已知主題的知識來量身定制。\n\n引導風格:\n\n1. 通過思考每個可能的例子或類比並考慮:這能否說明概念?這個例子或類比突出了概念的哪些元素?根據需要修改這些例子和類比,使它們對學生有用並突出概念或想法的不同方面。\n2. 你應該以開放式的方式引導學生。不要立即提供答案或問題的解決方案,而是通過引導性問題幫助學生生成自己的答案。要求學生解釋他們的思維過程。如果學生遇到困難或答錯了,試著給予額外的支持或給予提示。如果學生進步了,則以成長型語言稱讚他們並表示興奮。如果學生遇到困難,則給予鼓勵並給予一些想法供他們思考。\n3. 在向學生索取信息時,嘗試以問題結尾,這樣學生就必須不斷生成想法。\n4. 一旦學生根據他們的學習水平表現出一定程度的理解,要求他執行以下一項或多項任務:用自己的話解釋概念;用引導性短語提問,推動他們表達概念的基本原則,如“為什麼……?”、“怎麼樣……?”、“如果……?”、“有哪些證據支持……?”;要求他們舉例或給予一個新的問題或情境,然後要求他應用概念。\n5. 當學生展示出對概念的理解時,你可以結束對話並告訴他如果有進一步的問題,你會在這裡幫助他們。規則:問學生是否理解或是否跟上進度不是一個好策略(他們可能不知道自己是否理解)。相反,專注於通過要求他們解釋、舉例、將例子與概念聯繫起來、比較和對比例子或應用他的知識來探索他們的理解。"
15
+ }
16
 
17
+ # 聊天機器人的回調函數
18
+ def respond(message, history):
19
+ # 將新的消息添加到歷史記錄
20
+ history.append(("User", message))
21
+
22
+ # 使用 groq API 獲取回應
23
+ completion = client.chat.completions.create(
24
+ model="llama-3.1-70b-versatile",
25
+ messages=[
26
+ system_message,
27
  {
28
+ "role": "user",
29
+ "content": message
30
  }
31
+ ],
32
+ temperature=1,
33
+ max_tokens=1024,
34
+ top_p=1,
35
+ stream=True,
36
+ stop=None,
37
+ )
38
+
39
+ # 讀取 groq 的回應
40
+ response_text = ""
41
+ for chunk in completion:
42
+ response_text += chunk.choices[0].delta.content or ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
+ # 將回應消息添加到歷史記錄
45
+ history.append(("Bot", response_text))
46
 
47
+ # 返回新的歷史記錄
48
+ return history
49
+
50
+ # 創建 Gradio 界面
51
+ chatbot = gr.Chatbot()
52
+
53
+ # 設置 Gradio 界面
54
+ demo = gr.Interface(
55
+ fn=respond,
56
+ inputs=[gr.inputs.Textbox(lines=2, placeholder="Type here..."), "state"],
57
+ outputs="state",
58
+ live=True
59
+ )
60
+
61
+ # 啟動 Gradio 應用
62
+ if __name__ == "__main__":
63
+ demo.launch()