websearch length limits and caching

This commit is contained in:
Concedo 2024-12-30 18:30:54 +08:00
parent 3fea11675d
commit 5eb314a04b

View file

@ -83,6 +83,8 @@ multiplayer_turn_major = 1 # to keep track of when a client needs to sync their
multiplayer_turn_minor = 1 multiplayer_turn_minor = 1
multiplayer_dataformat = "" # used to tell what is the data payload in saved story. set by client multiplayer_dataformat = "" # used to tell what is the data payload in saved story. set by client
multiplayer_lastactive = {} # timestamp of last activity for each unique player multiplayer_lastactive = {} # timestamp of last activity for each unique player
websearch_lastquery = ""
websearch_lastresponse = []
preloaded_story = None preloaded_story = None
chatcompl_adapter = None chatcompl_adapter = None
embedded_kailite = None embedded_kailite = None
@ -1272,8 +1274,14 @@ def detokenize_ids(tokids):
# Performs a web search using DuckDuckGo and extracts text content from the top results. # Performs a web search using DuckDuckGo and extracts text content from the top results.
def websearch(query): def websearch(query):
global websearch_lastquery
global websearch_lastresponse
if not query or query=="": if not query or query=="":
return [] return []
query = query[:300] # only search first 300 chars, due to search engine limits
if query==websearch_lastquery:
print("Returning cached websearch...")
return websearch_lastresponse
import urllib.parse import urllib.parse
import urllib.request import urllib.request
import difflib import difflib
@ -1282,7 +1290,7 @@ def websearch(query):
num_results = 3 num_results = 3
searchresults = [] searchresults = []
if args.debugmode != -1 and not args.quiet: if args.debugmode != -1 and not args.quiet:
print("Performing websearch...") print("Performing new websearch...")
def fetch_searched_webpage(url): def fetch_searched_webpage(url):
if args.debugmode: if args.debugmode:
@ -1420,6 +1428,9 @@ def websearch(query):
if args.debugmode != -1 and not args.quiet: if args.debugmode != -1 and not args.quiet:
print(f"Error fetching URL {search_url}: {e}") print(f"Error fetching URL {search_url}: {e}")
return "" return ""
if len(searchresults) > 0:
websearch_lastquery = query
websearch_lastresponse = searchresults
return searchresults return searchresults
################################################################# #################################################################