优化Dify工具逻辑,调整知识提取和重排序流程,增强API调用的重试机制,更新意图识别API以支持更好的错误处理和日志记录,改进多线程检索功能
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
from gevent import monkey
|
||||
monkey.patch_all()
|
||||
|
||||
from flask import Flask, request, Response
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
import json
|
||||
import time
|
||||
import threading
|
||||
from gevent.lock import RLock
|
||||
|
||||
|
||||
import datetime
|
||||
import logging
|
||||
# 加载环境变量
|
||||
@@ -29,12 +34,12 @@ logger = logging.getLogger(__name__)
|
||||
app = Flask(__name__)
|
||||
|
||||
# 创建线程锁,用于保护共享资源
|
||||
recognizer_lock = threading.Lock()
|
||||
recognizer_lock = RLock()
|
||||
|
||||
# 使用单例模式创建意图识别器
|
||||
class RecognizerSingleton:
|
||||
_instance = None
|
||||
_lock = threading.Lock()
|
||||
_lock = RLock()
|
||||
|
||||
@classmethod
|
||||
def get_instance(cls):
|
||||
@@ -105,14 +110,16 @@ def intent_recognize():
|
||||
"is_complete": slot_filling.get("is_complete", False),
|
||||
"missing_slots": slot_filling.get("missing_slots", {}),
|
||||
"filled_data": slot_filling.get("filled_data", {})
|
||||
}
|
||||
},
|
||||
"query_expand": result["query_expand"]
|
||||
}
|
||||
return Response(json.dumps(response_result, ensure_ascii=False), content_type='application/json; charset=utf-8')
|
||||
except Exception as e:
|
||||
print(f"意图识别出错: {str(e)}")
|
||||
logger.error(f"意图识别出错: {str(e)}",exc_info=True)
|
||||
return Response(json.dumps({"error": str(e)}, ensure_ascii=False), content_type='application/json; charset=utf-8', status=500)
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 开发环境使用Flask内置服务器
|
||||
# 生产环境使用gunicorn支持高并发 poetry run gunicorn -w 10 -k gevent -b 0.0.0.0:8001 rag2_0.dify.intent_recognition_api:app
|
||||
# 生产环境使用gunicorn支持高并发 uv run gunicorn -w 10 -k gevent -b 0.0.0.0:8001 rag2_0.dify.intent_recognition_api:app
|
||||
# uv run gunicorn -w 10 -k gevent --preload -b 0.0.0.0:8001 rag2_0.dify.intent_recognition_api:app
|
||||
app.run(host="0.0.0.0", port=8001, threaded=True)
|
||||
Reference in New Issue
Block a user