diff --git a/app/utils/milvus_utils.py b/app/utils/milvus_utils.py
index 4217def..dfc2184 100644
--- a/app/utils/milvus_utils.py
+++ b/app/utils/milvus_utils.py
@@ -1,13 +1,15 @@
import asyncio
from typing import List, Optional
+from langchain_core.output_parsers import StrOutputParser
+from langchain_core.prompts import ChatPromptTemplate
from llama_index.core import Document, VectorStoreIndex
from llama_index.core.vector_stores import MetadataFilters, MetadataFilter, FilterOperator
from llama_index.vector_stores.milvus import MilvusVectorStore
from pydantic import BaseModel, Field
from app.config.env import env
-from app.utils.llm_utils import create_embeddings
+from app.utils.llm_utils import create_embeddings, create_llm
from app.utils.nltk_utils import load_nltk
@@ -47,7 +49,7 @@ class MilvusService:
# 检查Milvus连接是否正常
async def check_milvus_connection(self):
- await self.async_search("hello")
+ await self.async_search(KnowledgeQueryParam(question="hello", kb_code=""))
print("✅ Milvus connection successful:", f"Milvus://{env.milvus_username}:{env.milvus_password}@{env.milvus_uri}/{env.llama_index_database}/{env.llama_index_collection}")
async def async_create_index_from_documents(self, documents: List[Document]) -> VectorStoreIndex:
@@ -86,11 +88,36 @@ class MilvusService:
result_nodes = await retriever.aretrieve(param.question)
- # 异步执行查询
- # response = await query_engine.aquery(query)
+ chain = ChatPromptTemplate.from_template("""
+ 你将收到一个用户问题和一组来自LlamaIndex的检索结果。你的任务是:
+ 1. 分析检索结果内容
+ 2. 提取与用户问题直接相关的信息片段
+ 3. 基于这些相关信息生成准确、简洁且有帮助的回答
+
+ 用户问题如下标签中的内容所示:
+
+ {question}
+
+
+ 检索结果如下标签中的内容所示:
+
+ {context}
+
+
+ 处理要求:
+ - 仅关注检索结果中与用户问题直接相关的内容
+ - 忽略任何无关或关联性较弱的信息
+ - 如果检索结果中没有相关信息,请明确回答:"在提供的资料中未找到相关信息"
+ - 不要添加检索结果之外的额外知识
+ - 回答时优先使用检索结果中的原文表述
+ """) | create_llm() | StrOutputParser()
+
+ chain_input = {"context": "\n".join([node.node.text for node in result_nodes]), "question": param.question}
+ print("chain_input", chain_input)
+ relative_content = await chain.ainvoke(chain_input)
return {
- # "answer": str(response),
+ "answer": relative_content,
"sources": [
{
"text": node.node.text,