Skip to content

Commit 1559d4d

Browse files
committed
fixed defective websearch
1 parent b37354b commit 1559d4d

File tree

2 files changed

+84
-56
lines changed

2 files changed

+84
-56
lines changed

klite.embd

Lines changed: 75 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Current version indicated by LITEVER below.
1212
-->
1313

1414
<script>
15-
const LITEVER = 199;
15+
const LITEVER = 200;
1616
const urlParams = new URLSearchParams(window.location.search);
1717
var localflag = true;
1818
const STORAGE_PREFIX = (localflag?"e_":"")+"kaihordewebui_";
@@ -2972,6 +2972,7 @@ Current version indicated by LITEVER below.
29722972
var comfyui_is_connected = false;
29732973
var pending_storyjson_autosave = null;
29742974
var mainmenu_is_untab = false;
2975+
var websearch_in_progress = false;
29752976

29762977
var localsettings = {
29772978
my_api_key: "0000000000", //put here so it can be saved and loaded in persistent mode
@@ -3899,9 +3900,7 @@ initializeInstructUIFunctionality();
38993900
return replace_all(localsettings.instruct_systag, "\\n", "\n");
39003901
}
39013902
}
3902-
function replace_placeholders(text) {
3903-
// Replace {{user}} and other placeholders
3904-
text = replace_placeholders(text);
3903+
function replace_search_placeholders(text) {
39053904

39063905
// Remove any instruct tags as needed to ensure a more accurate search
39073906
text = replace_all(text, get_instruct_starttag(false), "");
@@ -3912,8 +3911,56 @@ initializeInstructUIFunctionality();
39123911
text = replace_all(text, get_instruct_systag(false).trim(), "");
39133912
text = text.replace(/\{\{\[INPUT\]\}\}/g, "").replace(/\{\{\[OUTPUT\]\}\}/g, "");
39143913

3914+
// Replace {{user}} and other placeholders
3915+
text = replace_placeholders(text);
3916+
39153917
return text;
39163918
}
3919+
//we separate these 2 functions, as sometimes we only need to replace instruct
3920+
function replace_instruct_placeholders(inputtxt) //only for instruct placeholders first
3921+
{
3922+
inputtxt = replace_all(inputtxt,instructstartplaceholder,get_instruct_starttag(false));
3923+
inputtxt = replace_all(inputtxt,instructendplaceholder,get_instruct_endtag(false));
3924+
inputtxt = replace_all(inputtxt,instructsysplaceholder,get_instruct_systag(false));
3925+
//failsafe to handle removing newline tags
3926+
inputtxt = replace_all(inputtxt,instructstartplaceholder.trim(),get_instruct_starttag(false));
3927+
inputtxt = replace_all(inputtxt,instructendplaceholder.trim(),get_instruct_endtag(false));
3928+
inputtxt = replace_all(inputtxt,instructsysplaceholder.trim(),get_instruct_systag(false));
3929+
return inputtxt;
3930+
}
3931+
function replace_noninstruct_placeholders(inputtxt,escape=false)
3932+
{
3933+
if(escape)
3934+
{
3935+
inputtxt = replace_all(inputtxt,"{{user}}",escape_html(localsettings.chatname?localsettings.chatname:"User"),true);
3936+
inputtxt = replace_all(inputtxt,"{{char}}",escape_html(localsettings.chatopponent?localsettings.chatopponent:defaultchatopponent),true);
3937+
}
3938+
else
3939+
{
3940+
inputtxt = replace_all(inputtxt,"{{user}}",(localsettings.chatname?localsettings.chatname:"User"),true);
3941+
inputtxt = replace_all(inputtxt,"{{char}}",(localsettings.chatopponent?localsettings.chatopponent:defaultchatopponent),true);
3942+
}
3943+
3944+
for(let i=0;i<placeholder_tags_data.length;++i)
3945+
{
3946+
if(placeholder_tags_data[i].p && placeholder_tags_data[i].r)
3947+
{
3948+
inputtxt = replace_all(inputtxt,placeholder_tags_data[i].p,placeholder_tags_data[i].r);
3949+
}
3950+
}
3951+
return inputtxt;
3952+
}
3953+
//if alwaysreplace, then settings are not considered, otherwise checks settings
3954+
function replace_placeholders(inputtxt, escape=false, alwaysreplace=false)
3955+
{
3956+
//only do this for chat and instruct modes
3957+
if(alwaysreplace || localsettings.placeholder_tags)
3958+
{
3959+
inputtxt = replace_instruct_placeholders(inputtxt);
3960+
inputtxt = replace_noninstruct_placeholders(inputtxt,escape);
3961+
}
3962+
return inputtxt;
3963+
}
39173964

39183965
//saving and loading functionality
39193966
function indexeddb_save(objkey, objdatastr) //save to indexeddb, but fallback to localstorage
@@ -11328,52 +11375,7 @@ initializeInstructUIFunctionality();
1132811375
render_gametext(false);
1132911376
}
1133011377

11331-
//we separate these 2 functions, as sometimes we only need to replace instruct
11332-
function replace_instruct_placeholders(inputtxt) //only for instruct placeholders first
11333-
{
11334-
inputtxt = replace_all(inputtxt,instructstartplaceholder,get_instruct_starttag(false));
11335-
inputtxt = replace_all(inputtxt,instructendplaceholder,get_instruct_endtag(false));
11336-
inputtxt = replace_all(inputtxt,instructsysplaceholder,get_instruct_systag(false));
11337-
//failsafe to handle removing newline tags
11338-
inputtxt = replace_all(inputtxt,instructstartplaceholder.trim(),get_instruct_starttag(false));
11339-
inputtxt = replace_all(inputtxt,instructendplaceholder.trim(),get_instruct_endtag(false));
11340-
inputtxt = replace_all(inputtxt,instructsysplaceholder.trim(),get_instruct_systag(false));
11341-
return inputtxt;
11342-
}
11343-
function replace_noninstruct_placeholders(inputtxt,escape=false)
11344-
{
11345-
if(escape)
11346-
{
11347-
inputtxt = replace_all(inputtxt,"{{user}}",escape_html(localsettings.chatname?localsettings.chatname:"User"),true);
11348-
inputtxt = replace_all(inputtxt,"{{char}}",escape_html(localsettings.chatopponent?localsettings.chatopponent:defaultchatopponent),true);
11349-
}
11350-
else
11351-
{
11352-
inputtxt = replace_all(inputtxt,"{{user}}",(localsettings.chatname?localsettings.chatname:"User"),true);
11353-
inputtxt = replace_all(inputtxt,"{{char}}",(localsettings.chatopponent?localsettings.chatopponent:defaultchatopponent),true);
11354-
}
11355-
11356-
for(let i=0;i<placeholder_tags_data.length;++i)
11357-
{
11358-
if(placeholder_tags_data[i].p && placeholder_tags_data[i].r)
11359-
{
11360-
inputtxt = replace_all(inputtxt,placeholder_tags_data[i].p,placeholder_tags_data[i].r);
11361-
}
11362-
}
11363-
return inputtxt;
11364-
}
1136511378

11366-
//if alwaysreplace, then settings are not considered, otherwise checks settings
11367-
function replace_placeholders(inputtxt, escape=false, alwaysreplace=false)
11368-
{
11369-
//only do this for chat and instruct modes
11370-
if(alwaysreplace || localsettings.placeholder_tags)
11371-
{
11372-
inputtxt = replace_instruct_placeholders(inputtxt);
11373-
inputtxt = replace_noninstruct_placeholders(inputtxt,escape);
11374-
}
11375-
return inputtxt;
11376-
}
1137711379

1137811380
function apply_display_only_regex(inputtxt)
1137911381
{
@@ -11530,6 +11532,7 @@ initializeInstructUIFunctionality();
1153011532
document.getElementById("abortgen").classList.remove("hidden");
1153111533
document.getElementById("chat_msg_send_btn_abort").classList.remove("hidden");
1153211534
document.getElementById("corpo_chat_send_btn_abort").classList.remove("hidden");
11535+
websearch_in_progress = false;
1153311536
}
1153411537
}
1153511538

@@ -16569,6 +16572,14 @@ initializeInstructUIFunctionality();
1656916572

1657016573
idle_timer = 0;
1657116574
document.getElementById("token-budget").innerText = last_token_budget;
16575+
if(websearch_in_progress && websearch_enabled && is_using_kcpp_with_websearch())
16576+
{
16577+
document.getElementById("searchingtxt").classList.remove("hidden");
16578+
}
16579+
else
16580+
{
16581+
document.getElementById("searchingtxt").classList.add("hidden");
16582+
}
1657216583
}
1657316584

1657416585
function render_corpo_welcome()
@@ -18634,8 +18645,14 @@ initializeInstructUIFunctionality();
1863418645
if(webSearchQuery=="")
1863518646
{
1863618647
webSearchQuery = (gametext_arr.length > 0 ? gametext_arr.slice(-1)[0] : "");
18648+
webSearchQuery = replace_search_placeholders(webSearchQuery);
18649+
webSearchQuery = webSearchQuery.trim();
18650+
if(webSearchQuery=="")
18651+
{
18652+
webSearchQuery = (gametext_arr.length > 1 ? gametext_arr.slice(-2,-1)[0] : "");
18653+
}
1863718654
}
18638-
webSearchQuery = replace_placeholders(webSearchQuery);
18655+
webSearchQuery = replace_search_placeholders(webSearchQuery);
1863918656
webSearchQuery = webSearchQuery.trim();
1864018657
webSearchQuery = webSearchQuery.replace(/(?:\r\n|\r|\n)/g, '. ');
1864118658
if(webSearchQuery==lastSearchQuery || webSearchQuery=="")
@@ -18647,6 +18664,7 @@ initializeInstructUIFunctionality();
1864718664
if(pending_response_id=="")
1864818665
{
1864918666
pending_response_id = "-1";
18667+
websearch_in_progress = true;
1865018668
render_gametext(false);
1865118669
}
1865218670
let murl = `${custom_kobold_endpoint}${koboldcpp_websearch_endpoint}`;
@@ -18664,6 +18682,7 @@ initializeInstructUIFunctionality();
1866418682
{
1866518683
pending_response_id = "";
1866618684
}
18685+
websearch_in_progress = false;
1866718686
onDone();
1866818687
})
1866918688
.catch(error => {
@@ -18674,6 +18693,7 @@ initializeInstructUIFunctionality();
1867418693
{
1867518694
pending_response_id = "";
1867618695
}
18696+
websearch_in_progress = false;
1867718697
onDone();
1867818698
});
1867918699
}
@@ -18682,6 +18702,7 @@ initializeInstructUIFunctionality();
1868218702
{
1868318703
lastSearchResults = [];
1868418704
lastSearchQuery = "";
18705+
websearch_in_progress = false;
1868518706
onDone();
1868618707
}
1868718708
}
@@ -18770,9 +18791,9 @@ initializeInstructUIFunctionality();
1877018791
allText = replace_all(allText,recentTextStr,"");
1877118792

1877218793
// Ensure placeholders are replaced to allow searching for user / character
18773-
allText = replace_placeholders(allText)
18774-
searchStr = replace_placeholders(searchStr)
18775-
recentTextStr = replace_placeholders(recentTextStr)
18794+
allText = replace_search_placeholders(allText)
18795+
searchStr = replace_search_placeholders(searchStr)
18796+
recentTextStr = replace_search_placeholders(recentTextStr)
1877618797

1877718798
let i = 0, startLoc = 0;
1877818799
while (startLoc < allText.length && i < Number.MAX_SAFE_INTEGER) {
@@ -18993,6 +19014,7 @@ initializeInstructUIFunctionality();
1899319014
<button type="button" class="btn wait mainnav" id="btnsend" disabled
1899419015
onclick="submit_generation_button(false)" onmousedown="ptt_start()" onmouseup="ptt_end()">Loading</button>
1899519016
<a href="#" id="abortgen" class="hidden bg_black mainnav" style="text-align: center;color: #ffaaaa;" onclick="abort_generation()"><b style="display: block;">[ABORT]</b></a>
19017+
<span id="searchingtxt" class="hidden bg_black mainnav" style="text-align: center;color: #49a8e4; font-size: 9px;"><b style="display: block;">WebSearch...</b></span>
1899619018
</div>
1899719019
</div>
1899819020
</div>

koboldcpp.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,9 @@ def detokenize_ids(tokids):
12811281
def websearch(query):
12821282
global websearch_lastquery
12831283
global websearch_lastresponse
1284+
# sanitize query
1285+
query = re.sub(r'[+\-\"\\/*^|<>~`]', '', query) # Remove blacklisted characters
1286+
query = re.sub(r'\s+', ' ', query).strip() # Replace multiple spaces with a single space
12841287
if not query or query=="":
12851288
return []
12861289
query = query[:300] # only search first 300 chars, due to search engine limits
@@ -1406,9 +1409,12 @@ def handle_data(self, data):
14061409
titles = parser.titles[:num_results]
14071410
searchurls = parser.urls[:num_results]
14081411
descs = parser.descs[:num_results]
1409-
fetchedcontent = fetch_webpages_parallel(searchurls)
1410-
if len(descs)==0:
1412+
1413+
if len(descs)==0 or len(titles)==0 or len(descs)==0:
14111414
utfprint("No results found! Maybe something went wrong...",1)
1415+
return []
1416+
1417+
fetchedcontent = fetch_webpages_parallel(searchurls)
14121418
for i in range(len(descs)):
14131419
# dive into the results to try and get even more details
14141420
title = titles[i]
@@ -1439,7 +1445,7 @@ def handle_data(self, data):
14391445

14401446
except Exception as e:
14411447
utfprint(f"Error fetching URL {search_url}: {e}",1)
1442-
return ""
1448+
return []
14431449
if len(searchresults) > 0:
14441450
websearch_lastquery = query
14451451
websearch_lastresponse = searchresults

0 commit comments

Comments
 (0)