@@ -205,12 +205,14 @@ async def _determine_language_node(self, state: dict, config: Optional[RunnableC
205205 return {"language" : question_language }
206206
207207 async def _rephrase_node (self , state : dict , config : Optional [RunnableConfig ] = None ) -> dict :
208+ if not state .get ("history" ):
209+ return {"rephrased_question" : state ["question" ]}
208210 rephrased_question = await self ._rephrasing_chain .ainvoke (chain_input = state , config = config )
209211 # Ensure rephrased_question is a string
210- if hasattr (rephrased_question , "content" ):
211- rephrased_question = rephrased_question . content
212- elif not isinstance ( rephrased_question , str ) :
213- rephrased_question = str ( rephrased_question )
212+ rephrased_question = getattr (rephrased_question , "content" , rephrased_question )
213+ rephrased_question = rephrased_question . strip () if isinstance ( rephrased_question , str ) else str ( rephrased_question ). strip ()
214+ if not rephrased_question :
215+ rephrased_question = state [ "question" ]
214216 return {"rephrased_question" : rephrased_question }
215217
216218 async def _generate_node (self , state : dict , config : Optional [RunnableConfig ] = None ) -> dict :
@@ -228,7 +230,8 @@ async def _generate_node(self, state: dict, config: Optional[RunnableConfig] = N
228230
229231 async def _retrieve_node (self , state : dict ) -> dict :
230232 try :
231- retrieved_documents = await self ._composite_retriever .ainvoke (retriever_input = state ["rephrased_question" ])
233+ question = state .get ("rephrased_question" ) or state ["question" ]
234+ retrieved_documents = await self ._composite_retriever .ainvoke (retriever_input = question )
232235 except NoOrEmptyCollectionError :
233236 logger .warning ("No or empty collection encountered." )
234237 return {
@@ -280,11 +283,9 @@ def _add_nodes(self):
280283 self ._state_graph .add_node (GraphNodeNames .ERROR_NODE , self ._error_node )
281284
282285 def _wire_graph (self ):
283- self ._state_graph .add_edge (START , GraphNodeNames .REPHRASE )
284286 self ._state_graph .add_edge (START , GraphNodeNames .DETERMINE_LANGUAGE )
285- self ._state_graph .add_edge (
286- [GraphNodeNames .REPHRASE , GraphNodeNames .DETERMINE_LANGUAGE ], GraphNodeNames .RETRIEVE
287- )
287+ self ._state_graph .add_edge (GraphNodeNames .DETERMINE_LANGUAGE , GraphNodeNames .REPHRASE )
288+ self ._state_graph .add_edge (GraphNodeNames .REPHRASE , GraphNodeNames .RETRIEVE )
288289 self ._state_graph .add_conditional_edges (
289290 GraphNodeNames .RETRIEVE , self ._docs_retrieved_edge , [GraphNodeNames .GENERATE , GraphNodeNames .ERROR_NODE ]
290291 )
0 commit comments