File size: 2,220 Bytes
1c5b994
c67acb3
37a8847
c67acb3
 
 
 
 
 
1c5b994
c67acb3
 
 
 
1c5b994
 
 
c67acb3
 
 
1c5b994
 
577b886
1c5b994
c67acb3
577b886
c67acb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
577b886
c67acb3
577b886
c67acb3
 
 
 
 
 
1eefb7f
c67acb3
 
 
 
 
 
 
 
 
 
 
 
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
68
import gradio as gr
import os

# Install the groq package if it is not installed
try:
    from groq import Groq
except ImportError:
    os.system('pip install groq')
    from groq import Groq

# Set up the Groq client with the secret key
groq_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": "你是一個有幽默感的聊天機器人,你擅長用詼諧的方式來討論嚴肅的事情,尤其是政治方面的議題。你使用的語言是繁體中文(zh-tw)。"
            }
        ]

    def get_response(self, message, chat_history):
        messages = self.initial_prompt + chat_history
        messages.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_content

chatbot = 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)