yangtb24 commited on
Commit
5261088
·
verified ·
1 Parent(s): d0a29d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -22
app.py CHANGED
@@ -323,31 +323,71 @@ def handsome_chat_completions():
323
 
324
  user_content = extract_user_content(data.get("messages", []))
325
 
326
- phrases_to_check = ["hello", "你好", "什么模型", "签到", "社工", "你是谁", "'content': 'hi'"]
327
  canned_response_content = "这是公益api,模型全部可用且保真,请不要对模型进行无意义的测试,请尽量不要使用高级模型解决没必要的问题。\n换个话题吧,请不要对模型进行无意义的测试,请尽量不要使用高级模型解决没必要的问题。"
328
-
329
- if any(phrase in user_content.lower() for phrase in phrases_to_check):
330
- logging.info("成功拦截一次!")
331
-
 
332
  if data.get("stream", False):
333
- def generate_canned_stream():
334
- message_data = {
335
- "choices": [
336
- {
337
- "delta": {
338
- "content": canned_response_content
339
- },
340
- "index": 0,
341
- "finish_reason": "stop"
342
- }
343
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
344
  }
345
- yield f"data: {json.dumps(message_data)}\n\n".encode("utf-8")
346
- yield f"data: [DONE]\n\n".encode("utf-8")
347
- return Response(
348
- stream_with_context(generate_canned_stream()),
349
- content_type="text/event-stream"
350
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
351
  else:
352
  canned_response = {
353
  "choices": [
 
323
 
324
  user_content = extract_user_content(data.get("messages", []))
325
 
326
+ phrases_to_check = ["hello", "你好", "什么模型", "签到", "社工", "你是谁"]
327
  canned_response_content = "这是公益api,模型全部可用且保真,请不要对模型进行无意义的测试,请尽量不要使用高级模型解决没必要的问题。\n换个话题吧,请不要对模型进行无意义的测试,请尽量不要使用高级模型解决没必要的问题。"
328
+
329
+ user_content_lower = user_content.lower()
330
+
331
+ if user_content_lower == "hi":
332
+ logging.info("成功拦截一次!(仅hi)")
333
  if data.get("stream", False):
334
+ def generate_canned_stream():
335
+ message_data = {
336
+ "choices": [
337
+ {
338
+ "delta": {
339
+ "content": canned_response_content
340
+ },
341
+ "index": 0,
342
+ "finish_reason": "stop"
343
+ }
344
+ ]
345
+ }
346
+ yield f"data: {json.dumps(message_data)}\n\n".encode("utf-8")
347
+ yield f"data: [DONE]\n\n".encode("utf-8")
348
+ return Response(
349
+ stream_with_context(generate_canned_stream()),
350
+ content_type="text/event-stream"
351
+ )
352
+ else:
353
+ canned_response = {
354
+ "choices": [
355
+ {
356
+ "message": {
357
+ "content": canned_response_content
358
+ },
359
+ "index": 0,
360
+ "finish_reason": "stop"
361
+ }
362
+ ],
363
+ "usage": {
364
+ "prompt_tokens": 0,
365
+ "completion_tokens": 0,
366
+ "total_tokens": 0
367
  }
368
+ }
369
+ return jsonify(canned_response)
370
+ elif any(phrase in user_content_lower for phrase in phrases_to_check):
371
+ logging.info("成功拦截一次!")
372
+ if data.get("stream", False):
373
+ def generate_canned_stream():
374
+ message_data = {
375
+ "choices": [
376
+ {
377
+ "delta": {
378
+ "content": canned_response_content
379
+ },
380
+ "index": 0,
381
+ "finish_reason": "stop"
382
+ }
383
+ ]
384
+ }
385
+ yield f"data: {json.dumps(message_data)}\n\n".encode("utf-8")
386
+ yield f"data: [DONE]\n\n".encode("utf-8")
387
+ return Response(
388
+ stream_with_context(generate_canned_stream()),
389
+ content_type="text/event-stream"
390
+ )
391
  else:
392
  canned_response = {
393
  "choices": [