Skip to content

Commit 5eb314a

Browse files
committed
websearch length limits and caching
1 parent 3fea116 commit 5eb314a

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

koboldcpp.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@
8383
multiplayer_turn_minor = 1
8484
multiplayer_dataformat = "" # used to tell what is the data payload in saved story. set by client
8585
multiplayer_lastactive = {} # timestamp of last activity for each unique player
86+
websearch_lastquery = ""
87+
websearch_lastresponse = []
8688
preloaded_story = None
8789
chatcompl_adapter = None
8890
embedded_kailite = None
@@ -1272,8 +1274,14 @@ def detokenize_ids(tokids):
12721274

12731275
# Performs a web search using DuckDuckGo and extracts text content from the top results.
12741276
def websearch(query):
1277+
global websearch_lastquery
1278+
global websearch_lastresponse
12751279
if not query or query=="":
12761280
return []
1281+
query = query[:300] # only search first 300 chars, due to search engine limits
1282+
if query==websearch_lastquery:
1283+
print("Returning cached websearch...")
1284+
return websearch_lastresponse
12771285
import urllib.parse
12781286
import urllib.request
12791287
import difflib
@@ -1282,7 +1290,7 @@ def websearch(query):
12821290
num_results = 3
12831291
searchresults = []
12841292
if args.debugmode != -1 and not args.quiet:
1285-
print("Performing websearch...")
1293+
print("Performing new websearch...")
12861294

12871295
def fetch_searched_webpage(url):
12881296
if args.debugmode:
@@ -1420,6 +1428,9 @@ def handle_data(self, data):
14201428
if args.debugmode != -1 and not args.quiet:
14211429
print(f"Error fetching URL {search_url}: {e}")
14221430
return ""
1431+
if len(searchresults) > 0:
1432+
websearch_lastquery = query
1433+
websearch_lastresponse = searchresults
14231434
return searchresults
14241435

14251436
#################################################################

0 commit comments

Comments
 (0)