MCPcopy Create free account
hub / github.com/nlweb-ai/NLWeb / synthesizeAnswer

Method synthesizeAnswer

AskAgent/python/methods/generate_answer.py:156–236  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

154 raise
155
156 async def synthesizeAnswer(self):
157 if not self.connection_alive_event.is_set():
158 logger.warning("Connection lost, skipping answer synthesis")
159 return
160
161 try:
162 logger.info("Starting answer synthesis")
163
164 # Check if we have any ranked answers to work with
165 if not self.final_ranked_answers:
166 logger.warning("No ranked answers found, sending empty response")
167 message = {
168 "message_type": "nlws",
169 "@type": "GeneratedAnswer",
170 "answer": "I couldn't find relevant information to answer your question.",
171 "items": []
172 }
173 await self.send_message(message)
174 return
175
176 response = await PromptRunner(self).run_prompt(self.SYNTHESIZE_PROMPT_NAME, timeout=100, verbose=True)
177 logger.debug("Synthesis response received")
178
179 json_results = []
180 description_tasks = []
181 answer = response["answer"]
182
183
184 # Process each URL mentioned in the response
185 if response.get("urls"):
186 for url in response["urls"]:
187 # Find the matching item in our items list
188 matching_items = [item for item in self.items if item[0] == url]
189 if not matching_items:
190 logger.warning(f"URL {url} referenced in response not found in items")
191 continue
192
193 item = matching_items[0]
194 (url, json_str, name, site) = item
195 logger.debug(f"Creating description task for item: {name}")
196 t = asyncio.create_task(self.getDescription(url, json_str, self.decontextualized_query, answer, name, site))
197 description_tasks.append(t)
198
199 if description_tasks:
200 logger.info(f"Waiting for {len(description_tasks)} description tasks to complete")
201 desc_answers = await asyncio.gather(*description_tasks, return_exceptions=True)
202
203 for result in desc_answers:
204 if isinstance(result, Exception):
205 logger.error(f"Error getting description: {result}")
206 continue
207
208 url, name, site, description, json_str = result
209 logger.debug(f"Adding result for {name} to final message")
210 json_results.append({
211 "@type": "Item",
212 "url": url,
213 "name": name,

Callers 1

get_ranked_answersMethod · 0.95

Calls 10

getDescriptionMethod · 0.95
PromptRunnerClass · 0.90
run_promptMethod · 0.80
getMethod · 0.80
warningMethod · 0.45
infoMethod · 0.45
send_messageMethod · 0.45
debugMethod · 0.45
errorMethod · 0.45
exceptionMethod · 0.45

Tested by

no test coverage detected