sanbo commited on
Commit
5191811
1 Parent(s): 77c6321

update sth. at 2024-11-21 18:18:17

Browse files
Files changed (1) hide show
  1. app.py +26 -7
app.py CHANGED
@@ -272,15 +272,15 @@ def generate_response(user_prompt, system_instructions=sysx, model="microsoft/Ph
272
  max_new_tokens=100,
273
  do_sample=True,
274
  )
275
-
276
  formatted_prompt = f"[SYSTEM] {system_instructions}[QUESTION]{user_prompt}[ANSWER]"
277
- stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True,
278
- return_full_text=False)
279
  output = ""
 
280
  for response in stream:
281
  output += response.token.text
282
  if "[QUESTION]" in output:
283
  break
 
284
  # 移除 [QUESTION] 及其之后的内容
285
  if "[QUESTION]" in output:
286
  output = output[:output.index("[QUESTION]")].strip()
@@ -294,6 +294,10 @@ def home():
294
  return "hello, work~~~"
295
 
296
  # /completions 路径
 
 
 
 
297
  @app.route("/completions", methods=["POST"])
298
  def completions():
299
  data = request.json
@@ -306,11 +310,26 @@ def completions():
306
 
307
  # 生成响应
308
  response_text = generate_response(user_message, system_instructions, model)
309
- response_data = {"choices": [{"message": {"role": "assistant", "content": response_text}}]}
 
 
 
 
 
 
 
 
 
 
310
  print(response_data)
311
- # 返回 JSON 响应
312
- # return jsonify(response_data)
313
- return response_data
 
 
 
 
 
314
 
315
  if __name__ == "__main__":
316
  # 启动 Flask 应用
 
272
  max_new_tokens=100,
273
  do_sample=True,
274
  )
 
275
  formatted_prompt = f"[SYSTEM] {system_instructions}[QUESTION]{user_prompt}[ANSWER]"
276
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
 
277
  output = ""
278
+
279
  for response in stream:
280
  output += response.token.text
281
  if "[QUESTION]" in output:
282
  break
283
+ print("output:",output)
284
  # 移除 [QUESTION] 及其之后的内容
285
  if "[QUESTION]" in output:
286
  output = output[:output.index("[QUESTION]")].strip()
 
294
  return "hello, work~~~"
295
 
296
  # /completions 路径
297
+ @app.route("/api/v1/chat/completions", methods=["POST"])
298
+ @app.route("/hf/v1/chat/completions", methods=["POST"])
299
+ @app.route("/chat/completions", methods=["POST"])
300
+ @app.route("/chat", methods=["POST"])
301
  @app.route("/completions", methods=["POST"])
302
  def completions():
303
  data = request.json
 
310
 
311
  # 生成响应
312
  response_text = generate_response(user_message, system_instructions, model)
313
+ # 构建响应数据
314
+ response_data = {
315
+ "choices": [
316
+ {
317
+ "message": {
318
+ "role": "assistant",
319
+ "content": response_text
320
+ }
321
+ }
322
+ ]
323
+ }
324
  print(response_data)
325
+ # 将数据转换为JSON字符串,确保非ASCII字符正确显示
326
+ response_json = json.dumps(response_data, ensure_ascii=False)
327
+
328
+ # 打印调试信息
329
+ print(response_json)
330
+
331
+ # 返回 JSON 响应,确保内容类型为 application/json
332
+ return app.response_class(response=response_json, mimetype='application/json')
333
 
334
  if __name__ == "__main__":
335
  # 启动 Flask 应用