Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,33 +21,41 @@ client = Groq(api_key=api_key)
|
|
| 21 |
|
| 22 |
# 定义聊天机器人的响应函数
|
| 23 |
def chatbot_response(messages):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# 使用 gradio 创建聊天机器人界面
|
| 46 |
def respond(message, history):
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# 初始化 Gradio 应用
|
| 53 |
with gr.Blocks() as demo:
|
|
|
|
| 21 |
|
| 22 |
# 定义聊天机器人的响应函数
|
| 23 |
def chatbot_response(messages):
|
| 24 |
+
try:
|
| 25 |
+
completion = client.chat.completions.create(
|
| 26 |
+
model="llama3-8b-8192",
|
| 27 |
+
messages=[
|
| 28 |
+
{
|
| 29 |
+
"role": "system",
|
| 30 |
+
"content": "You are a corporate secretary who is skilled at drafting business emails. The prompt will feed you addressee, main message, and final greetings."
|
| 31 |
+
}
|
| 32 |
+
] + messages,
|
| 33 |
+
temperature=1,
|
| 34 |
+
max_tokens=1024,
|
| 35 |
+
top_p=1,
|
| 36 |
+
stream=True,
|
| 37 |
+
stop=None,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
response = ""
|
| 41 |
+
for chunk in completion:
|
| 42 |
+
response += chunk.choices[0].delta.content or ""
|
| 43 |
+
|
| 44 |
+
return response
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"Error in chatbot_response: {e}")
|
| 47 |
+
return "Error: Unable to get response from the API."
|
| 48 |
|
| 49 |
# 使用 gradio 创建聊天机器人界面
|
| 50 |
def respond(message, history):
|
| 51 |
+
try:
|
| 52 |
+
history.append({"role": "user", "content": message})
|
| 53 |
+
bot_response = chatbot_response(history)
|
| 54 |
+
history.append({"role": "assistant", "content": bot_response})
|
| 55 |
+
return "", history
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"Error in respond function: {e}")
|
| 58 |
+
return "", history
|
| 59 |
|
| 60 |
# 初始化 Gradio 应用
|
| 61 |
with gr.Blocks() as demo:
|