File size: 962 Bytes
8a9e538
3e85d55
8a9e538
8e798b6
8a9e538
b6ddc87
3e85d55
b6ddc87
 
 
3e85d55
b6ddc87
 
3e85d55
b6ddc87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e85d55
b6ddc87
5ac78a3
b6ddc87
5ac78a3
 
b6ddc87
 
3e85d55
 
 
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
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()