sales-assistant-py-new/app/chat_qa_query/service/query_qa_service.py

35 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
from typing import List
from langchain_huggingface import HuggingFaceEndpointEmbeddings
from app.chat_qa_query.query_context import QueryQAContext
from app.chat_qa_query.query_graph import qa_graph
from app.chat_qa_query.query_qa_state import QueryQAState
from app.core.log import logger
from app.repository.archive_messages_repository import ArchiveMessagesRepository
from app.repository.milvus.message_qa_repository import QARepository
class QueryQAService:
def __init__(self, archive_mysql_repository: ArchiveMessagesRepository,
qa_milvus_repository : QARepository):
self.archive_mysql_repository = archive_mysql_repository
self.qa_milvus_repository = qa_milvus_repository
async def query(self, query:str, history:List[str]=[])-> str:
state: QueryQAState = QueryQAState(original_query=query, history=history)
context = QueryQAContext(archive_mysql_repository=self.archive_mysql_repository,qa_milvus_repository=self.qa_milvus_repository)
try:
res= await qa_graph.ainvoke(input=state, context=context, stream_mode="custom")
logger.info(f"QA服务结果{res}")
if isinstance(res, list) and len(res) == 1 and isinstance(res[0], list):
res = res[0]
return res
except Exception as e:
logger.info(f"QA服务错误{e}")
return ""