Fix SSE route dependency and align architecture docs
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
"""Define workflow state for rag workflow."""
|
||||
|
||||
from typing import TypedDict, List
|
||||
from langgraph.graph import StateGraph, END
|
||||
# Keep workflow state definitions compact so transitions stay easy to audit.
|
||||
|
||||
|
||||
|
||||
class RagState(TypedDict):
|
||||
"""Track workflow state for rag state."""
|
||||
query: str
|
||||
query_embedding: List[float]
|
||||
retrieved_docs: List[dict]
|
||||
@@ -12,14 +17,14 @@ class RagState(TypedDict):
|
||||
|
||||
|
||||
def embed_query(state: RagState) -> dict:
|
||||
"""将查询转为向量"""
|
||||
"""Embed query."""
|
||||
from app.services import embedding_service
|
||||
embedding = embedding_service.embed_single(state["query"])
|
||||
return {"query_embedding": embedding}
|
||||
|
||||
|
||||
def retrieve_docs(state: RagState) -> dict:
|
||||
"""向量检索"""
|
||||
"""Handle retrieve docs."""
|
||||
from app.services import milvus_service
|
||||
from app.core.config import settings
|
||||
docs = milvus_service.search(
|
||||
@@ -30,7 +35,7 @@ def retrieve_docs(state: RagState) -> dict:
|
||||
|
||||
|
||||
def build_context(state: RagState) -> dict:
|
||||
"""构建上下文"""
|
||||
"""Build context."""
|
||||
context_parts = []
|
||||
sources = []
|
||||
|
||||
@@ -46,7 +51,7 @@ def build_context(state: RagState) -> dict:
|
||||
|
||||
|
||||
def generate_answer(state: RagState) -> dict:
|
||||
"""生成答案"""
|
||||
"""Handle generate answer."""
|
||||
from app.services import llm_service
|
||||
prompt = f"""请根据以下法规内容回答用户问题,并在回答中标注引用的法规条款。
|
||||
|
||||
@@ -64,7 +69,7 @@ def generate_answer(state: RagState) -> dict:
|
||||
return {"answer": answer}
|
||||
|
||||
|
||||
# 构建工作流图
|
||||
# Keep workflow state definitions compact so transitions stay easy to audit.
|
||||
rag_graph = StateGraph(RagState)
|
||||
|
||||
rag_graph.add_node("embed", embed_query)
|
||||
@@ -82,23 +87,23 @@ rag_workflow = rag_graph.compile()
|
||||
|
||||
|
||||
async def run_rag_workflow(query: str) -> RagState:
|
||||
"""运行RAG工作流"""
|
||||
"""Handle run rag workflow."""
|
||||
initial_state: RagState = {"query": query}
|
||||
result = rag_workflow.invoke(initial_state)
|
||||
return result
|
||||
|
||||
|
||||
def stream_rag_workflow(query: str):
|
||||
"""流式运行RAG工作流"""
|
||||
"""Stream rag workflow."""
|
||||
from app.services import llm_service
|
||||
|
||||
# 先完成检索阶段
|
||||
# Keep workflow state definitions compact so transitions stay easy to audit.
|
||||
state: RagState = {"query": query}
|
||||
state.update(embed_query(state))
|
||||
state.update(retrieve_docs(state))
|
||||
state.update(build_context(state))
|
||||
|
||||
# 流式生成阶段
|
||||
# Keep workflow state definitions compact so transitions stay easy to audit.
|
||||
prompt = f"""请根据以下法规内容回答用户问题,并在回答中标注引用的法规条款。
|
||||
|
||||
法规内容:
|
||||
|
||||
Reference in New Issue
Block a user