ReneeHWT commited on
Commit
eee2931
·
verified ·
1 Parent(s): 5be466d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -24
app.py CHANGED
@@ -21,33 +21,41 @@ client = Groq(api_key=api_key)
21
 
22
  # 定义聊天机器人的响应函数
23
  def chatbot_response(messages):
24
- completion = client.chat.completions.create(
25
- model="llama3-8b-8192",
26
- messages=[
27
- {
28
- "role": "system",
29
- "content": "You are a corporate secretary who is skilled at drafting business emails. The prompt will feed you addressee, main message, and final greetings."
30
- }
31
- ] + messages,
32
- temperature=1,
33
- max_tokens=1024,
34
- top_p=1,
35
- stream=True,
36
- stop=None,
37
- )
38
-
39
- response = ""
40
- for chunk in completion:
41
- response += chunk.choices[0].delta.content or ""
42
-
43
- return response
 
 
 
 
44
 
45
  # 使用 gradio 创建聊天机器人界面
46
  def respond(message, history):
47
- history.append({"role": "user", "content": message})
48
- bot_response = chatbot_response(history)
49
- history.append({"role": "assistant", "content": bot_response})
50
- return "", history
 
 
 
 
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: