leonsimon23 commited on
Commit
dc1a05f
·
verified ·
1 Parent(s): b0a6cf5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -6
app.py CHANGED
@@ -5,6 +5,8 @@ import requests
5
  API_BASE_URL = "http://key.aistory.uk/v1/chat/completions" # 替换为你自己的API URL
6
  API_KEY = "sk-HfD4NYIN6bq2DkSfIiUcciRvo9MkgMdFCsahP9NWEOUPHe8H" # 替换为你自己的 API 密钥
7
 
 
 
8
  # 定义 AI 响应函数,调用第三方 API
9
  def ai_response(message, chat_history):
10
  # 定义系统提示词
@@ -13,7 +15,7 @@ def ai_response(message, chat_history):
13
  # 组合历史聊天记录和用户输入的信息
14
  conversation = [{"role": "system", "content": system_prompt}]
15
  for msg in chat_history:
16
- conversation.append({"role": msg['role'], "content": msg['message']})
17
  conversation.append({"role": "user", "content": message})
18
 
19
  # 构建请求体
@@ -37,14 +39,14 @@ def ai_response(message, chat_history):
37
  response_data = response.json()
38
  assistant_message = response_data['choices'][0]['message']['content']
39
 
40
- # 返回新的聊天记录
41
- chat_history.append({"role": "user", "message": message})
42
- chat_history.append({"role": "assistant", "message": assistant_message})
43
 
44
- return assistant_message, chat_history
45
  else:
46
  # 如果请求失败,返回错误消息
47
- return "Sorry, something went wrong with the API.", chat_history
48
 
49
  # 创建 Gradio 应用
50
  def create_interface():
 
5
  API_BASE_URL = "http://key.aistory.uk/v1/chat/completions" # 替换为你自己的API URL
6
  API_KEY = "sk-HfD4NYIN6bq2DkSfIiUcciRvo9MkgMdFCsahP9NWEOUPHe8H" # 替换为你自己的 API 密钥
7
 
8
+
9
+
10
  # 定义 AI 响应函数,调用第三方 API
11
  def ai_response(message, chat_history):
12
  # 定义系统提示词
 
15
  # 组合历史聊天记录和用户输入的信息
16
  conversation = [{"role": "system", "content": system_prompt}]
17
  for msg in chat_history:
18
+ conversation.append({"role": msg[0], "content": msg[1]})
19
  conversation.append({"role": "user", "content": message})
20
 
21
  # 构建请求体
 
39
  response_data = response.json()
40
  assistant_message = response_data['choices'][0]['message']['content']
41
 
42
+ # 返回新的聊天记录,转换为符合 gr.Chatbot 期望的元组格式
43
+ chat_history.append(("user", message))
44
+ chat_history.append(("assistant", assistant_message))
45
 
46
+ return chat_history
47
  else:
48
  # 如果请求失败,返回错误消息
49
+ return chat_history + [("assistant", "Sorry, something went wrong with the API.")]
50
 
51
  # 创建 Gradio 应用
52
  def create_interface():