|
|
|
@ -21,6 +21,7 @@ import jieba.analyse
|
|
|
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 提取用户输入的关键词
|
|
|
|
|
def extract_keywords(text, topK=3):
|
|
|
|
|
"""
|
|
|
|
@ -32,6 +33,7 @@ def extract_keywords(text, topK=3):
|
|
|
|
|
keywords = jieba.analyse.extract_tags(text, topK=topK)
|
|
|
|
|
return keywords
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 初始化 Word2Vec 模型
|
|
|
|
|
model_path = MS_MODEL_PATH
|
|
|
|
|
model = KeyedVectors.load_word2vec_format(model_path, binary=False, limit=MS_MODEL_LIMIT)
|
|
|
|
@ -44,6 +46,7 @@ milvus_pool = MilvusConnectionPool(host=MS_HOST, port=MS_PORT, max_connections=M
|
|
|
|
|
collection_name = MS_COLLECTION_NAME
|
|
|
|
|
collection_manager = MilvusCollectionManager(collection_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 将文本转换为嵌入向量
|
|
|
|
|
def text_to_embedding(text):
|
|
|
|
|
words = jieba.lcut(text) # 使用 jieba 分词
|
|
|
|
@ -58,6 +61,7 @@ def text_to_embedding(text):
|
|
|
|
|
logger.warning("未找到有效词,返回零向量")
|
|
|
|
|
return [0.0] * model.vector_size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 使用 Lifespan Events 处理应用启动和关闭逻辑
|
|
|
|
|
@asynccontextmanager
|
|
|
|
|
async def lifespan(app: FastAPI):
|
|
|
|
@ -69,6 +73,7 @@ async def lifespan(app: FastAPI):
|
|
|
|
|
milvus_pool.close()
|
|
|
|
|
logger.info("Milvus 连接池已关闭。")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 初始化 FastAPI 应用
|
|
|
|
|
app = FastAPI(lifespan=lifespan)
|
|
|
|
|
|
|
|
|
@ -78,6 +83,7 @@ client = AsyncOpenAI(
|
|
|
|
|
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/reply")
|
|
|
|
|
async def reply(session_id: str = Form(...), prompt: str = Form(...)):
|
|
|
|
|
"""
|
|
|
|
@ -130,7 +136,8 @@ async def reply(session_id: str = Form(...), prompt: str = Form(...)):
|
|
|
|
|
response = await client.chat.completions.create( # 使用异步调用
|
|
|
|
|
model=MODEL_NAME,
|
|
|
|
|
messages=[
|
|
|
|
|
{"role": "system", "content": "你是一个私人助理,负责回答用户的问题。请根据用户的历史对话和当前问题,提供准确且简洁的回答。不要提及你是通义千问或其他无关信息,也不可以回复与本次用户问题不相关的历史对话记录内容。"},
|
|
|
|
|
{"role": "system",
|
|
|
|
|
"content": "你是一个私人助理,负责回答用户的问题。请根据用户的历史对话和当前问题,提供准确且简洁的回答。不要提及你是通义千问或其他无关信息,也不可以回复与本次用户问题不相关的历史对话记录内容。"},
|
|
|
|
|
{"role": "user", "content": f"历史对话记录:{history_prompt},本次用户问题: {prompt}"} # 将历史交互和当前输入一起发送
|
|
|
|
|
],
|
|
|
|
|
max_tokens=500
|
|
|
|
@ -183,8 +190,9 @@ async def reply(session_id: str = Form(...), prompt: str = Form(...)):
|
|
|
|
|
# 释放连接
|
|
|
|
|
milvus_pool.release_connection(connection)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 运行 FastAPI 应用
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
import uvicorn
|
|
|
|
|
|
|
|
|
|
uvicorn.run(app, host="0.0.0.0", port=5600)
|
|
|
|
|
uvicorn.run(app, host="0.0.0.0", port=5600, workers=4)
|
|
|
|
|