Spaces:
Runtime error
Runtime error
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() | |