@@ -284,7 +284,9 @@ def get_news(topic, max=10):
284
284
break
285
285
return articles
286
286
287
- def fetch_news (topic ):
287
+ def fetch_news (topic , retries = 3 ):
288
+ if retries == 0 :
289
+ return "Unable to fetch news" , "Unable to fetch news"
288
290
log ("Get News" )
289
291
context_str = get_news (topic , 25 )
290
292
log (f"News Raw Context = { context_str } " )
@@ -293,24 +295,32 @@ def fetch_news(topic):
293
295
# Replace IDs in answer with URLs
294
296
result = ""
295
297
text_only = ""
298
+ all_lines = []
296
299
for line in answer .split ("\n " ):
297
300
if "ID:" in line :
298
301
elements = line .split ("ID: " )
299
302
title = elements [0 ].strip ()
303
+ all_lines .append (title )
300
304
text_only += title + "\n "
301
305
if len (elements ) > 1 :
302
306
uuid = elements [1 ].strip ()
303
307
# Ensure we have a valid UUId that is a integer
304
308
if not uuid .isdigit ():
305
309
result += line
306
- continue
310
+ continue
307
311
url = news_cache .get (int (uuid ))
308
312
result += f"{ title } <a href=\" { url } \" >[Link]</a>"
309
313
else :
310
314
result += line
311
315
else :
312
316
result += line
313
317
result += "\n "
318
+ # Query the LLM to see if all_lines are duplicated
319
+ prompt = expand_prompt (prompts ["rag" ], {"context_str" : "\n " .join (all_lines ), "prompt" : "Are these news items all about the same thing?" })
320
+ answer = ask (prompt )
321
+ if "yes" in answer .lower ():
322
+ log ("News items are all about the same thing" )
323
+ return fetch_news (topic , retries - 1 )
314
324
return result , text_only
315
325
316
326
def handle_weather_command (p ):
0 commit comments