22GenerateAnswerNode Module
33"""
44
5- import time
65import json
6+ import time
77from typing import List , Optional
88
99from langchain .prompts import PromptTemplate
@@ -105,10 +105,7 @@ def process(self, state: dict) -> dict:
105105 raise ValueError ("No user prompt found in state" )
106106
107107 # Create the chain input with both content and question keys
108- chain_input = {
109- "content" : content ,
110- "question" : user_prompt
111- }
108+ chain_input = {"content" : content , "question" : user_prompt }
112109
113110 try :
114111 response = self .invoke_with_timeout (self .chain , chain_input , self .timeout )
@@ -167,25 +164,13 @@ def execute(self, state: dict) -> dict:
167164 and not self .script_creator
168165 or self .is_md_scraper
169166 ):
170- template_no_chunks_prompt = (
171- TEMPLATE_NO_CHUNKS_MD + "\n \n IMPORTANT: " + format_instructions
172- )
173- template_chunks_prompt = (
174- TEMPLATE_CHUNKS_MD + "\n \n IMPORTANT: " + format_instructions
175- )
176- template_merge_prompt = (
177- TEMPLATE_MERGE_MD + "\n \n IMPORTANT: " + format_instructions
178- )
167+ template_no_chunks_prompt = TEMPLATE_NO_CHUNKS_MD
168+ template_chunks_prompt = TEMPLATE_CHUNKS_MD
169+ template_merge_prompt = TEMPLATE_MERGE_MD
179170 else :
180- template_no_chunks_prompt = (
181- TEMPLATE_NO_CHUNKS + "\n \n IMPORTANT: " + format_instructions
182- )
183- template_chunks_prompt = (
184- TEMPLATE_CHUNKS + "\n \n IMPORTANT: " + format_instructions
185- )
186- template_merge_prompt = (
187- TEMPLATE_MERGE + "\n \n IMPORTANT: " + format_instructions
188- )
171+ template_no_chunks_prompt = TEMPLATE_NO_CHUNKS
172+ template_chunks_prompt = TEMPLATE_CHUNKS
173+ template_merge_prompt = TEMPLATE_MERGE
189174
190175 if self .additional_info is not None :
191176 template_no_chunks_prompt = self .additional_info + template_no_chunks_prompt
@@ -210,8 +195,14 @@ def execute(self, state: dict) -> dict:
210195 chain , {"question" : user_prompt }, self .timeout
211196 )
212197 except (Timeout , json .JSONDecodeError ) as e :
213- error_msg = "Response timeout exceeded" if isinstance (e , Timeout ) else "Invalid JSON response format"
214- state .update ({self .output [0 ]: {"error" : error_msg , "raw_response" : str (e )}})
198+ error_msg = (
199+ "Response timeout exceeded"
200+ if isinstance (e , Timeout )
201+ else "Invalid JSON response format"
202+ )
203+ state .update (
204+ {self .output [0 ]: {"error" : error_msg , "raw_response" : str (e )}}
205+ )
215206 return state
216207
217208 state .update ({self .output [0 ]: answer })
@@ -241,7 +232,11 @@ def execute(self, state: dict) -> dict:
241232 async_runner , {"question" : user_prompt }, self .timeout
242233 )
243234 except (Timeout , json .JSONDecodeError ) as e :
244- error_msg = "Response timeout exceeded during chunk processing" if isinstance (e , Timeout ) else "Invalid JSON response format in chunk processing"
235+ error_msg = (
236+ "Response timeout exceeded during chunk processing"
237+ if isinstance (e , Timeout )
238+ else "Invalid JSON response format in chunk processing"
239+ )
245240 state .update ({self .output [0 ]: {"error" : error_msg , "raw_response" : str (e )}})
246241 return state
247242
@@ -261,7 +256,11 @@ def execute(self, state: dict) -> dict:
261256 self .timeout ,
262257 )
263258 except (Timeout , json .JSONDecodeError ) as e :
264- error_msg = "Response timeout exceeded during merge" if isinstance (e , Timeout ) else "Invalid JSON response format during merge"
259+ error_msg = (
260+ "Response timeout exceeded during merge"
261+ if isinstance (e , Timeout )
262+ else "Invalid JSON response format during merge"
263+ )
265264 state .update ({self .output [0 ]: {"error" : error_msg , "raw_response" : str (e )}})
266265 return state
267266
0 commit comments