ftjc2021 commited on
Commit
f60fbb3
·
verified ·
1 Parent(s): f14a415

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +2 -2
  2. app.py +2 -2
  3. dify_api_client.py +67 -16
  4. requirements.txt +3 -1
Dockerfile CHANGED
@@ -23,5 +23,5 @@ USER user
23
  ENV HOME=/home/user \
24
  PATH=/home/user/.local/bin:$PATH
25
 
26
- # Command to run the application
27
- CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
23
  ENV HOME=/home/user \
24
  PATH=/home/user/.local/bin:$PATH
25
 
26
+ # Command to run the application with multiple workers
27
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "4"]
app.py CHANGED
@@ -98,8 +98,8 @@ async def dify_api(
98
  )
99
  return {"answer": response_text}
100
  else:
101
- # 阻塞模式
102
- response_data = client.send_request_blocking(
103
  content=query_request.query,
104
  conversation_id=query_request.conversation_id,
105
  user=query_request.user
 
98
  )
99
  return {"answer": response_text}
100
  else:
101
+ # 阻塞模式 - 使用异步方法
102
+ response_data = await client.send_request_blocking_async(
103
  content=query_request.query,
104
  conversation_id=query_request.conversation_id,
105
  user=query_request.user
dify_api_client.py CHANGED
@@ -12,6 +12,7 @@ import sys
12
  import json
13
  import argparse
14
  import requests
 
15
  import sseclient
16
  import logging
17
  import configparser
@@ -167,34 +168,84 @@ class DifyAPIClient:
167
  logging.error(f"HTTP错误: {e}")
168
  if hasattr(response, 'text'):
169
  logging.error(f"API响应内容: {response.text}")
170
- print(f"HTTP错误: {e}")
171
- if hasattr(response, 'text') and response.text:
172
- print(f"API响应: {response.text}")
173
- sys.exit(1)
174
  except requests.exceptions.ConnectionError:
175
  logging.error("连接错误: 无法连接到API服务器")
176
- print("连接错误: 无法连接到API服务器")
177
- sys.exit(1)
178
  except requests.exceptions.Timeout:
179
  logging.error("超时错误: API请求超时")
180
- print("超时错误: API请求超时")
181
- sys.exit(1)
182
  except requests.exceptions.RequestException as e:
183
  logging.error(f"请求错误: {str(e)}", exc_info=True)
184
- print(f"请求错误: {e}")
185
- sys.exit(1)
186
  except json.JSONDecodeError:
187
  logging.error("解析错误: API返回的响应不是有效的JSON格式")
188
  if hasattr(response, 'text'):
189
  logging.error(f"API响应内容: {response.text}")
190
- print("解析错误: API返回的响应不是有效的JSON格式")
191
- if hasattr(response, 'text'):
192
- print(f"API响应: {response.text}")
193
- sys.exit(1)
194
  except Exception as e:
195
  logging.error(f"未知错误: {str(e)}", exc_info=True)
196
- print(f"发送请求时发生未知错误: {str(e)}")
197
- sys.exit(1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
 
199
  def send_request_streaming(self, content: str, conversation_id: str = None, user: str = "user") -> str:
200
  """
 
12
  import json
13
  import argparse
14
  import requests
15
+ import httpx
16
  import sseclient
17
  import logging
18
  import configparser
 
168
  logging.error(f"HTTP错误: {e}")
169
  if hasattr(response, 'text'):
170
  logging.error(f"API响应内容: {response.text}")
171
+ raise Exception(f"HTTP错误: {e}")
 
 
 
172
  except requests.exceptions.ConnectionError:
173
  logging.error("连接错误: 无法连接到API服务器")
174
+ raise Exception("连接错误: 无法连接到API服务器")
 
175
  except requests.exceptions.Timeout:
176
  logging.error("超时错误: API请求超时")
177
+ raise Exception("超时错误: API请求超时")
 
178
  except requests.exceptions.RequestException as e:
179
  logging.error(f"请求错误: {str(e)}", exc_info=True)
180
+ raise Exception(f"请求错误: {e}")
 
181
  except json.JSONDecodeError:
182
  logging.error("解析错误: API返回的响应不是有效的JSON格式")
183
  if hasattr(response, 'text'):
184
  logging.error(f"API响应内容: {response.text}")
185
+ raise Exception("解析错误: API返回的响应不是有效的JSON格式")
 
 
 
186
  except Exception as e:
187
  logging.error(f"未知错误: {str(e)}", exc_info=True)
188
+ raise Exception(f"发送请求时发生未知错误: {str(e)}")
189
+
190
+ async def send_request_blocking_async(self, content: str, conversation_id: str = None, user: str = "user") -> Dict[str, Any]:
191
+ """
192
+ 异步发送阻塞模式请求
193
+
194
+ Args:
195
+ content: 请求内容
196
+ conversation_id: 会话ID
197
+ user: 用户标识
198
+
199
+ Returns:
200
+ API响应
201
+ """
202
+ url = f"{self.base_url}/chat-messages"
203
+
204
+ payload = {
205
+ "query": content,
206
+ "inputs": {},
207
+ "response_mode": "blocking",
208
+ "user": user
209
+ }
210
+
211
+ if conversation_id:
212
+ payload["conversation_id"] = conversation_id
213
+
214
+ logging.info(f"异步发送阻塞模式请求,用户: {user}")
215
+ if conversation_id:
216
+ logging.info(f"会话ID: {conversation_id}")
217
+
218
+ logging.debug(f"请求URL: {url}")
219
+ logging.debug(f"请求负载: {json.dumps(payload, ensure_ascii=False)}")
220
+
221
+ try:
222
+ async with httpx.AsyncClient() as client:
223
+ response = await client.post(url, headers=self.headers, json=payload)
224
+ response.raise_for_status()
225
+ response_data = response.json()
226
+ logging.info("API请求成功")
227
+ logging.debug(f"API响应: {json.dumps(response_data, ensure_ascii=False)}")
228
+ return response_data
229
+ except httpx.HTTPStatusError as e:
230
+ logging.error(f"HTTP错误: {e}")
231
+ if hasattr(e.response, 'text'):
232
+ logging.error(f"API响应内容: {e.response.text}")
233
+ raise Exception(f"HTTP错误: {e}")
234
+ except httpx.ConnectError:
235
+ logging.error("连接错误: 无法连接到API服务器")
236
+ raise Exception("连接错误: 无法连接到API服务器")
237
+ except httpx.TimeoutException:
238
+ logging.error("超时错误: API请求超时")
239
+ raise Exception("超时错误: API请求超时")
240
+ except httpx.RequestError as e:
241
+ logging.error(f"请求错误: {str(e)}", exc_info=True)
242
+ raise Exception(f"请求错误: {e}")
243
+ except json.JSONDecodeError:
244
+ logging.error("解析错误: API返回的响应不是有效的JSON格式")
245
+ raise Exception("解析错误: API返回的响应不是有效的JSON格式")
246
+ except Exception as e:
247
+ logging.error(f"未知错误: {str(e)}", exc_info=True)
248
+ raise Exception(f"发送请求时发生未知错误: {str(e)}")
249
 
250
  def send_request_streaming(self, content: str, conversation_id: str = None, user: str = "user") -> str:
251
  """
requirements.txt CHANGED
@@ -3,4 +3,6 @@ sseclient-py>=1.7.2
3
  fastapi>=0.95.0
4
  uvicorn>=0.21.0
5
  python-multipart>=0.0.6
6
- pydantic>=1.10.7
 
 
 
3
  fastapi>=0.95.0
4
  uvicorn>=0.21.0
5
  python-multipart>=0.0.6
6
+ pydantic>=1.10.7
7
+ httpx>=0.24.0
8
+ aiofiles>=0.8.0