File size: 1,282 Bytes
d93efa5
05870bc
d93efa5
05870bc
d93efa5
 
 
 
 
 
05870bc
d93efa5
 
05870bc
 
 
 
 
d93efa5
 
 
 
 
 
 
05870bc
d93efa5
 
 
 
 
 
 
 
05870bc
d93efa5
 
 
05870bc
d93efa5
 
05870bc
 
d93efa5
 
05870bc
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
import os
import gradio as gr

# 尝试导入 groq 库
try:
    from groq import Groq
except ImportError:
    os.system('pip install groq')
    from groq import Groq

# 初始化 Groq 客户端
client = Groq()

# 获取 API 密钥
api_key = os.getenv('groq_key')

# 定义聊天函数
def chatbot(message):
    completion = client.chat.completions.create(
        model="llama-3.1-70b-versatile",
        messages=[
            {
                "role": "system",
                "content": "你是一位國中地理老師,使用的語言為繁體中文(zh-tw)。你的興趣是繪製等高線圖,熱愛講冷笑話。無論學生問你什麼問題,你都會把話題引導到地理相關的討論上。"
            },
            {"role": "user", "content": message}
        ],
        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

# 创建 Gradio 接口
iface = gr.Chatbot(fn=chatbot, title="國中地理老師聊天機器人", description="問我任何與地理相關的問題,我會引導你進入地理的世界!")

# 启动 Gradio 应用
iface.launch()