File tree Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Original file line number Diff line number Diff line change 22GenerateAnswerNode Module
33"""
44from typing import List , Optional
5+ from json .decoder import JSONDecodeError
56from langchain .prompts import PromptTemplate
67from langchain_core .output_parsers import JsonOutputParser
78from langchain_core .runnables import RunnableParallel
@@ -121,9 +122,21 @@ def execute(self, state: dict) -> dict:
121122 partial_variables = {"context" : doc , "format_instructions" : format_instructions }
122123 )
123124 chain = prompt | self .llm_model
125+ raw_response = str ((prompt | self .llm_model ).invoke ({"question" : user_prompt }))
126+
124127 if output_parser :
125- chain = chain | output_parser
126- answer = chain .invoke ({"question" : user_prompt })
128+ try :
129+ answer = output_parser .parse (raw_response )
130+ except JSONDecodeError :
131+ lines = raw_response .split ('\n ' )
132+ if lines [0 ].strip ().startswith ('```' ):
133+ lines = lines [1 :]
134+ if lines [- 1 ].strip ().endswith ('```' ):
135+ lines = lines [:- 1 ]
136+ cleaned_response = '\n ' .join (lines )
137+ answer = output_parser .parse (cleaned_response )
138+ else :
139+ answer = raw_response
127140
128141 state .update ({self .output [0 ]: answer })
129142 return state
You can’t perform that action at this time.
0 commit comments