20240813 / app.py
ReneeHWT's picture
Update app.py
fe33690 verified
import os
import gradio as gr
# 手动设置 API 金钥
os.environ["groq_key"] = "your_groq_api_key"
# 确保 groq 套件已安装
try:
from groq import Groq
except ImportError:
os.system('pip install groq')
from groq import Groq
# 从环境变量中获取 API 金钥
api_key = os.getenv("groq_key")
if api_key is None:
raise ValueError("API key is not set. Please set it in your environment variables with the name 'groq_key'.")
# 初始化 Groq 客户端
client = Groq(api_key=api_key)
# 定义聊天机器人的响应函数
def chatbot_response(messages):
completion = client.chat.completions.create(
model="llama3-8b-8192",
messages=[
{
"role": "system",
"content": "You are a corporate secretary who is skilled at drafting business emails. The prompt will feed you addressee, main message, and final greetings."
}
] + messages,
temperature=1,
max_tokens=1024,
top_p=1,
stream=True,
stop=None,
)
response = ""
for chunk in completion:
respons