Spaces:
Sleeping
Sleeping
File size: 1,620 Bytes
3246582 97d0683 3246582 97d0683 3246582 85239c1 3246582 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import os
import subprocess
import sys
# 確保 groq 套件已安裝
def install(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
try:
from groq import Groq
except ImportError:
install("groq")
from groq import Groq
import gradio as gr
# 從環境變量中獲取 API 密鑰
api_key = os.getenv('groq_key')
# 設置 Groq 客戶端
client = Groq(api_key=api_key)
def groq_chatbot(messages):
completion = client.chat.completions.create(
model="llama-3.1-70b-versatile",
messages=[
{
"role": "system",
"content": "有點搞笑,但又有點悲觀的人\n請用繁體中文(zh-tw) 回覆"
},
*messages
],
temperature=1,
max_tokens=1024,
top_p=1,
stream=True,
stop=None,
)
response = ""
for chunk in completion:
response += chunk.choices[0].delta.content or ""
return response
def respond(message, history):
history.append(("user", message))
bot_response = groq_chatbot([{"role": role, "content": content} for role, content in history])
history.append(("assistant", bot_response))
return history, history
# 創建 Gradio 界面
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox(label="輸入訊息")
clear = gr.Button("清除對話")
def clear_history():
return [], []
msg.submit(respond, [msg, chatbot], [chatbot, chatbot])
clear.click(clear_history, [], [chatbot])
# 運行 Gradio 應用
if __name__ == "__main__":
demo.launch()
|