| import os |
| import subprocess |
|
|
| |
| try: |
| from dotenv import load_dotenv |
| except ImportError: |
| print("dotenv module not found. Installing...") |
| subprocess.check_call(["pip", "install", "python-dotenv"]) |
| from dotenv import load_dotenv |
|
|
| |
| try: |
| from groq import Groq |
| except ImportError: |
| print("groq module not found. Installing...") |
| subprocess.check_call(["pip", "install", "groq"]) |
| from groq import Groq |
|
|
| import gradio as gr |
|
|
| |
| load_dotenv() |
|
|
| |
| GROQ_API_KEY = os.getenv("GROQ_API_KEY") |
| if not GROQ_API_KEY: |
| raise ValueError("❌ 找不到 API Key,請確認已設定環境變數 'GROQ_API_KEY'") |
|
|
| |
| client = Groq(api_key=GROQ_API_KEY) |
|
|
| |
| SYSTEM_PROMPT = "你是一位喜歡熊的動物學家,你喜歡真實的熊也喜歡卡通的熊,使用的語言為繁體中文(zh-tw)。你的興趣是觀察熊的習性及動作,熱愛講冷笑話。無論學生問你什麼問題,你都會把話題引導到熊的相關的討論上。" |
|
|
| |
| def chatbot_response(message, history): |
| messages = [{"role": "system", "content": SYSTEM_PROMPT}] |
| |
| |
| for user_msg, bot_msg in history: |
| messages.append({"role": "user", "content": user_msg}) |
| messages.append({"role": "assistant", "content": bot_msg}) |
| |
| |
| messages.append({"role": "user", "content": message}) |
|
|
| |
| response = client.chat.completions.create( |
| model="llama-3.1-8b-instant", |
| messages=messages, |
| temperature=1, |
| max_completion_tokens=1024, |
| top_p=1, |
| stream=False |
| ) |
|
|
| |
| ai_reply = response.choices[0].message.content |
| return ai_reply |
|
|
| |
| with gr.Blocks() as demo: |
| chatbot = gr.Chatbot() |
| msg = gr.Textbox(label="輸入你的問題") |
| clear = gr.Button("清除對話") |
|
|
| def user(message, history): |
| return "", history + [[message, chatbot_response(message, history)]] |
|
|
| msg.submit(user, [msg, chatbot], [msg, chatbot]) |
| clear.click(lambda: None, None, chatbot, queue=False) |
|
|
| |
| demo.launch() |
|
|