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 2
2
GenerateAnswerNode Module
3
3
"""
4
4
from typing import List , Optional
5
+ from json .decoder import JSONDecodeError
5
6
from langchain .prompts import PromptTemplate
6
7
from langchain_core .output_parsers import JsonOutputParser
7
8
from langchain_core .runnables import RunnableParallel
@@ -121,9 +122,21 @@ def execute(self, state: dict) -> dict:
121
122
partial_variables = {"context" : doc , "format_instructions" : format_instructions }
122
123
)
123
124
chain = prompt | self .llm_model
125
+ raw_response = str ((prompt | self .llm_model ).invoke ({"question" : user_prompt }))
126
+
124
127
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
127
140
128
141
state .update ({self .output [0 ]: answer })
129
142
return state
You can’t perform that action at this time.
0 commit comments