import os import gradio as gr os.system("pip install groq") from groq import Groq # 取得 Groq API key groq_key = os.getenv("groq_key") client = Groq(api_key=groq_key) # 設定系統提示詞 system_prompt = "你是一個樂於助人的聊天機器人,能回答各種問題。" def generate_response(user_input): completion = client.chat.completions.create( model="llama-3.1-70b-versatile", messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": user_input} ], # 其他參數可根據需要調整 temperature=0.7, max_tokens=1024, top_p=1, stream=False, stop=None, ) return completion.choices[0].message.content # 建立 Gradio 聊天介面 demo = gr.Interface( fn=generate_response, inputs="text", outputs="text", title="我的 Groq Chatbot", description="與 AI 聊天吧!" ) demo.launch()