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