13
13
TypedDict ,
14
14
)
15
15
from uuid import UUID , uuid4
16
-
17
16
import openai
18
17
from langchain .retrievers import ContextualCompressionRetriever
19
18
from langchain_cohere import CohereRerank
38
37
from quivr_core .rag .entities .models import (
39
38
ParsedRAGChunkResponse ,
40
39
QuivrKnowledge ,
40
+ RAGResponseMetadata ,
41
41
)
42
42
from quivr_core .rag .prompts import custom_prompts
43
43
from quivr_core .rag .utils import (
@@ -950,6 +950,8 @@ async def answer_astream(
950
950
version = "v1" ,
951
951
config = {"metadata" : metadata , "callbacks" : [langfuse_handler ]},
952
952
):
953
+ node_name = self ._extract_node_name (event )
954
+
953
955
if self ._is_final_node_with_docs (event ):
954
956
tasks = event ["data" ]["output" ]["tasks" ]
955
957
docs = tasks .docs if tasks else []
@@ -965,9 +967,17 @@ async def answer_astream(
965
967
966
968
if new_content :
967
969
chunk_metadata = get_chunk_metadata (rolling_message , docs )
970
+ if node_name :
971
+ chunk_metadata .workflow_step = node_name
968
972
yield ParsedRAGChunkResponse (
969
973
answer = new_content , metadata = chunk_metadata
970
974
)
975
+ else :
976
+ if node_name :
977
+ yield ParsedRAGChunkResponse (
978
+ answer = "" ,
979
+ metadata = RAGResponseMetadata (workflow_step = node_name ),
980
+ )
971
981
972
982
# Yield final metadata chunk
973
983
yield ParsedRAGChunkResponse (
@@ -991,6 +1001,17 @@ def _is_final_node_and_chat_model_stream(self, event: dict) -> bool:
991
1001
and event ["metadata" ]["langgraph_node" ] in self .final_nodes
992
1002
)
993
1003
1004
+ def _extract_node_name (self , event : dict ) -> str :
1005
+ if "metadata" in event and "langgraph_node" in event ["metadata" ]:
1006
+ name = event ["metadata" ]["langgraph_node" ]
1007
+ for node in self .retrieval_config .workflow_config .nodes :
1008
+ if node .name == name :
1009
+ if node .description :
1010
+ return node .description
1011
+ else :
1012
+ return node .name
1013
+ return ""
1014
+
994
1015
async def ainvoke_structured_output (
995
1016
self , prompt : str , output_class : Type [BaseModel ]
996
1017
) -> Any :
0 commit comments