update
This commit is contained in:
parent
35d93bd787
commit
7ecafd2fbf
@ -7,11 +7,13 @@ from watchfiles import awatch
|
|||||||
from app.chat_qa_query.api.qa_dependencies import get_qa_query_service
|
from app.chat_qa_query.api.qa_dependencies import get_qa_query_service
|
||||||
from app.chat_qa_query.api.schema.query_qa_schema import QueryQASchema
|
from app.chat_qa_query.api.schema.query_qa_schema import QueryQASchema
|
||||||
from app.chat_qa_query.service.query_qa_service import QueryQAService
|
from app.chat_qa_query.service.query_qa_service import QueryQAService
|
||||||
|
from app.core.log import logger
|
||||||
|
|
||||||
query_qa_router = APIRouter()
|
query_qa_router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
@query_qa_router.post("/api/qa/query")
|
@query_qa_router.post("/api/qa/query")
|
||||||
async def query_handler(query: QueryQASchema, query_service: Annotated[QueryQAService, Depends(get_qa_query_service)]):
|
async def query_handler(query: QueryQASchema, query_service: Annotated[QueryQAService, Depends(get_qa_query_service)]):
|
||||||
|
logger.info(f"请求参数:{query.external_str},历史top15:{query.messages_top15}")
|
||||||
return await query_service.query(query.external_str, query.messages_top15)
|
return await query_service.query(query.external_str, query.messages_top15)
|
||||||
|
|
||||||
|
|||||||
@ -45,7 +45,7 @@ if __name__ == '__main__':
|
|||||||
archive_messages_mysql_repository = ArchiveMessagesRepository(db_session)
|
archive_messages_mysql_repository = ArchiveMessagesRepository(db_session)
|
||||||
qa_milvus_repository = QARepository()
|
qa_milvus_repository = QARepository()
|
||||||
|
|
||||||
state: QueryQAState = QueryQAState(original_query="培训的费用?")
|
state: QueryQAState = QueryQAState(original_query="暂时有点忙", history=['2026-05-26T13:14:59 EXTERNAL 暂时有点忙\n', '2026-05-26T12:48:09 INTERNAL 下午好呀 你这边怎么计划的呀 考虑跟咱们6月1号的班级学习嘛\n'])
|
||||||
context = QueryQAContext(meta_mysql_repository=archive_messages_mysql_repository,
|
context = QueryQAContext(meta_mysql_repository=archive_messages_mysql_repository,
|
||||||
qa_milvus_repository=qa_milvus_repository
|
qa_milvus_repository=qa_milvus_repository
|
||||||
)
|
)
|
||||||
|
|||||||
@ -28,7 +28,6 @@ problem_rewriting_prompt = """
|
|||||||
|
|
||||||
4. **输出格式**:
|
4. **输出格式**:
|
||||||
- 仅输出JSON,不要任何解释性文字
|
- 仅输出JSON,不要任何解释性文字
|
||||||
- rewritten_query 必须是一个完整的疑问句(以"是什么"、"有哪些"、"怎么样"、"如何"等结尾,或以问号结尾)
|
|
||||||
|
|
||||||
## 改写示例
|
## 改写示例
|
||||||
|
|
||||||
@ -76,6 +75,9 @@ problem_rewriting_prompt = """
|
|||||||
历史会话:
|
历史会话:
|
||||||
{history_text}
|
{history_text}
|
||||||
|
|
||||||
|
历史会话字段说明:
|
||||||
|
EXTERNAL:客户
|
||||||
|
INTERNAL:销售
|
||||||
当前问题:{query}
|
当前问题:{query}
|
||||||
|
|
||||||
请直接输出JSON:
|
请直接输出JSON:
|
||||||
@ -90,6 +92,7 @@ async def rewrite_query(state:QueryQAState):
|
|||||||
"""
|
"""
|
||||||
history_text = state.get("history", "")
|
history_text = state.get("history", "")
|
||||||
query = state.get("original_query", "")
|
query = state.get("original_query", "")
|
||||||
|
logger.info(f"问题改写:{query},历史会话:{history_text}")
|
||||||
prompt = PromptTemplate(template=problem_rewriting_prompt, input_variables=["history_text", "query"])
|
prompt = PromptTemplate(template=problem_rewriting_prompt, input_variables=["history_text", "query"])
|
||||||
output = JsonOutputParser()
|
output = JsonOutputParser()
|
||||||
chain = prompt | llm | output
|
chain = prompt | llm | output
|
||||||
|
|||||||
@ -15,7 +15,7 @@ async def search_embed(state:QueryQAState) :
|
|||||||
batch_embeddings = await embedding_client.aembed_query(rewritten_query)
|
batch_embeddings = await embedding_client.aembed_query(rewritten_query)
|
||||||
qa_repository = QARepository()
|
qa_repository = QARepository()
|
||||||
|
|
||||||
result = await qa_repository.search_qa(query_text=batch_embeddings, top_k=3)
|
result = await qa_repository.search_qa(query_vector=batch_embeddings, top_k=3)
|
||||||
logger.info(f"原始问题:{original_query},改写后的问题:{rewritten_query},匹配结果:{result}")
|
logger.info(f"原始问题:{original_query},改写后的问题:{rewritten_query},匹配结果:{result}")
|
||||||
if result:
|
if result:
|
||||||
question = result[0]["question"]
|
question = result[0]["question"]
|
||||||
|
|||||||
@ -100,12 +100,12 @@ class QARepository:
|
|||||||
logger.error(f"QA数据入库失败: {str(e)}", exc_info=True)
|
logger.error(f"QA数据入库失败: {str(e)}", exc_info=True)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
async def search_qa(self, query_text: List[float], top_k: int = 5) -> List[Dict[str, Any]]:
|
async def search_qa(self, query_vector: List[float], top_k: int = 5) -> List[Dict[str, Any]]:
|
||||||
"""
|
"""
|
||||||
根据查询文本搜索QA数据
|
根据查询文本搜索QA数据
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
query_text: 查询文本
|
query_vector: 查询向量
|
||||||
top_k: 返回结果数量
|
top_k: 返回结果数量
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@ -114,13 +114,16 @@ class QARepository:
|
|||||||
res = milvus_client.client.search(
|
res = milvus_client.client.search(
|
||||||
collection_name=self.collection_name,
|
collection_name=self.collection_name,
|
||||||
anns_field="question_dense_vector",
|
anns_field="question_dense_vector",
|
||||||
data=[query_text],
|
data=[query_vector],
|
||||||
limit=top_k,
|
limit=top_k,
|
||||||
search_params={"metric_type": "COSINE"},
|
search_params={
|
||||||
|
"metric_type": "COSINE",
|
||||||
|
"efSearch": 300 # 你配置 efSearch=200:检索时最多遍历 200 个候选节点,再从中选出相似度最高的结果。
|
||||||
|
},
|
||||||
output_fields=["question", "answer", "created_at"]
|
output_fields=["question", "answer", "created_at"]
|
||||||
)
|
)
|
||||||
|
|
||||||
score_threshold = 0.6
|
score_threshold = 0.60
|
||||||
filter_results = []
|
filter_results = []
|
||||||
|
|
||||||
# res 是二维结构:[ [单条结果列表] ]
|
# res 是二维结构:[ [单条结果列表] ]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user