@@ -24,10 +24,35 @@ def compose(
2424 """
2525 log .info (f"Composing answer for question: { question } " )
2626 final_choice = graph_result .get ("final_choice" )
27+ all_candidates = graph_result .get ("all_candidates" , [])
2728
2829 # Xây dựng context từ Graph
2930 graph_context_str = ""
30- if final_choice :
31+
32+ # Nếu có all_candidates, dùng TẤT CẢ content thay vì chỉ final_choice
33+ if all_candidates :
34+ graph_context_str = "THÔNG TIN TỪ CƠ SỞ DỮ LIỆU LUẬT (Tin cậy cao):\n \n "
35+
36+ # Group by source_type
37+ direct_texts = [c for c in all_candidates if c .get ('source_type' ) == 'direct_text' ]
38+ cypher_texts = [c for c in all_candidates if c .get ('source_type' ) in ['graph_context' , 'fallback_text' ]]
39+
40+ # Add direct text content
41+ if direct_texts :
42+ graph_context_str += "== NỘI DUNG TRỰC TIẾP TỪ VĂN BẢN ==\n "
43+ for idx , c in enumerate (direct_texts , 1 ):
44+ score = c .get ('relevance_score' , 0 )
45+ graph_context_str += f"\n [{ idx } ] (Score: { score } ) { c ['text' ]} \n "
46+
47+ # Add Cypher-expanded content
48+ if cypher_texts :
49+ graph_context_str += "\n == NỘI DUNG MỞ RỘNG (GRAPH CONTEXT) ==\n "
50+ for idx , c in enumerate (cypher_texts , len (direct_texts ) + 1 ):
51+ score = c .get ('relevance_score' , 0 )
52+ graph_context_str += f"\n [{ idx } ] (Score: { score } ) { c ['text' ]} \n "
53+
54+ # Fallback: dùng final_choice nếu không có all_candidates
55+ elif final_choice :
3156 # Build citation string
3257 doc_title = final_choice .get ('document_title' , '' )
3358 article_num = final_choice .get ('article_number' , '' )
@@ -44,12 +69,12 @@ def compose(
4469 if point_letter :
4570 citation_parts .append (f"Điểm { point_letter } " )
4671
47- citation_info = ", " .join (citation_parts ) if citation_parts else f"Node ID: { final_choice [ 'source_node_id' ] } "
72+ citation_info = ", " .join (citation_parts ) if citation_parts else f"Node ID: { final_choice . get ( 'source_node_id' , 'unknown' ) } "
4873
4974 graph_context_str = (
5075 f"THÔNG TIN TỪ CƠ SỞ DỮ LIỆU LUẬT (Tin cậy cao):\n "
5176 f"- Nguồn: { citation_info } \n "
52- f"- Nội dung:\n { final_choice [ 'chosen_snippet' ] } \n "
77+ f"- Nội dung:\n { final_choice . get ( 'chosen_snippet' , '' ) } \n "
5378 )
5479 else :
5580 graph_context_str = "Không tìm thấy thông tin trong cơ sở dữ liệu luật nội bộ."
@@ -133,11 +158,30 @@ def compose_stream(
133158 Tổng hợp câu trả lời cuối cùng (Streaming).
134159 """
135160 final_choice = graph_result .get ("final_choice" )
161+ all_candidates = graph_result .get ("all_candidates" , [])
136162
137- # Xây dựng context từ Graph
163+ # Xây dựng context từ Graph (same logic as compose)
138164 graph_context_str = ""
139- if final_choice :
140- # Build citation string
165+
166+ if all_candidates :
167+ graph_context_str = "THÔNG TIN TỪ CƠ SỞ DỮ LIỆU LUẬT (Tin cậy cao):\n \n "
168+
169+ direct_texts = [c for c in all_candidates if c .get ('source_type' ) == 'direct_text' ]
170+ cypher_texts = [c for c in all_candidates if c .get ('source_type' ) in ['graph_context' , 'fallback_text' ]]
171+
172+ if direct_texts :
173+ graph_context_str += "== NỘI DUNG TRỰC TIẾP TỪ VĂN BẢN ==\n "
174+ for idx , c in enumerate (direct_texts , 1 ):
175+ score = c .get ('relevance_score' , 0 )
176+ graph_context_str += f"\n [{ idx } ] (Score: { score } ) { c ['text' ]} \n "
177+
178+ if cypher_texts :
179+ graph_context_str += "\n == NỘI DUNG MỞ RỘNG (GRAPH CONTEXT) ==\n "
180+ for idx , c in enumerate (cypher_texts , len (direct_texts ) + 1 ):
181+ score = c .get ('relevance_score' , 0 )
182+ graph_context_str += f"\n [{ idx } ] (Score: { score } ) { c ['text' ]} \n "
183+
184+ elif final_choice :
141185 doc_title = final_choice .get ('document_title' , '' )
142186 article_num = final_choice .get ('article_number' , '' )
143187 clause_num = final_choice .get ('clause_number' , '' )
@@ -153,12 +197,12 @@ def compose_stream(
153197 if point_letter :
154198 citation_parts .append (f"Điểm { point_letter } " )
155199
156- citation_info = ", " .join (citation_parts ) if citation_parts else f"Node ID: { final_choice [ 'source_node_id' ] } "
200+ citation_info = ", " .join (citation_parts ) if citation_parts else f"Node ID: { final_choice . get ( 'source_node_id' , 'unknown' ) } "
157201
158202 graph_context_str = (
159203 f"THÔNG TIN TỪ CƠ SỞ DỮ LIỆU LUẬT (Tin cậy cao):\n "
160204 f"- Nguồn: { citation_info } \n "
161- f"- Nội dung:\n { final_choice [ 'chosen_snippet' ] } \n "
205+ f"- Nội dung:\n { final_choice . get ( 'chosen_snippet' , '' ) } \n "
162206 )
163207 else :
164208 graph_context_str = "Không tìm thấy thông tin trong cơ sở dữ liệu luật nội bộ."
0 commit comments