nanoppa commited on
Commit
ce0ae73
·
verified ·
1 Parent(s): c74550c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -5
app.py CHANGED
@@ -64,13 +64,54 @@ def chat():
64
  }
65
  data = request.get_json()
66
  stream_flag = data.get('stream', False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  try:
68
- response = requests.post(API_BASE_URL+"/chat/completions",headers=headers,json=data,stream=stream_flag)
69
- return Response(stream_with_context(response.iter_content(chunk_size=1024)),
70
- status=response.status_code,
71
- content_type=response.headers.get('content-type'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  except Exception as e:
73
- logging("get models error. %s" ,e)
 
 
 
 
 
 
74
 
75
 
76
  if __name__ == '__main__':
 
64
  }
65
  data = request.get_json()
66
  stream_flag = data.get('stream', False)
67
+
68
+ def generate():
69
+ try:
70
+ with requests.post(API_BASE_URL+"/chat/completions", headers=headers, json=data, stream=stream_flag) as response:
71
+ response.raise_for_status() # 检查上游请求是否成功
72
+ for chunk in response.iter_content(chunk_size=1024):
73
+ yield chunk
74
+ except requests.exceptions.RequestException as e:
75
+ logging.error("Request to upstream API failed: %s", e)
76
+ # 在这里处理上游请求失败,例如可以 yield 一个错误消息或者抛出异常
77
+ # 但请注意,一旦开始 yield 数据,就不能改变 HTTP 状态码和头部了
78
+ yield b'{"error": "Upstream API request failed"}' # 作为 JSON 错误返回
79
+ except Exception as e:
80
+ logging.error("Unexpected error during streaming: %s", e)
81
+ yield b'{"error": "Internal server error during streaming"}'
82
+
83
+
84
  try:
85
+ # 如果不是流式请求,可以考虑不使用生成器,或者根据 stream_flag 来判断
86
+ if not stream_flag:
87
+ # 对于非流式请求,直接返回完整响应
88
+ response = requests.post(API_BASE_URL+"/chat/completions", headers=headers, json=data, stream=False)
89
+ response.raise_for_status()
90
+ return Response(response.content,
91
+ status=response.status_code,
92
+ content_type=response.headers.get('content-type'))
93
+ else:
94
+ # 对于流式请求,使用生成器
95
+ # 注意:在生成器中处理异常时,如果已经开始发送数据,状态码和头部就不能更改了。
96
+ # 所以最好是在生成器开始之前捕获requests.post的异常。
97
+ initial_response = requests.post(API_BASE_URL+"/chat/completions", headers=headers, json=data, stream=True)
98
+ initial_response.raise_for_status() # 检查初始请求是否成功
99
+
100
+ return Response(generate_from_response(initial_response),
101
+ status=initial_response.status_code,
102
+ content_type=initial_response.headers.get('content-type'))
103
+
104
+ except requests.exceptions.RequestException as e:
105
+ logging.error("Initial upstream API request failed: %s", e)
106
+ return jsonify({"success": False, "message": f"Upstream API request failed: {e}"}), 500
107
  except Exception as e:
108
+ logging.error("Error setting up chat completion: %s", e)
109
+ return jsonify({"success": False, "message": str(e)}), 500
110
+
111
+ def generate_from_response(upstream_response):
112
+ # 这是一个辅助函数,用于将上游响应的迭代器包装成一个生成器
113
+ for chunk in upstream_response.iter_content(chunk_size=1024):
114
+ yield chunk
115
 
116
 
117
  if __name__ == '__main__':