SillyTavern / SillyTavern-Extras

Extensions API for SillyTavern.
GNU Affero General Public License v3.0
549 stars 124 forks source link

websearch prompt injection management? #201

Closed Technologicat closed 8 months ago

Technologicat commented 9 months ago

Hi,

Currently, websearch keeps injecting the most recent search results into the prompt until the user supplies a new query. Given the current capabilities of local LLM technology (i.e. highly promising but not quite ready for prime time), I'd like to be able to control this more explicitly.

I'd like an option to inject the results only with the initial message asking the question, or alternatively, a command to explicitly clear (forget) the web search injection until a new query is supplied.

The reason is that a mix of results from web search and vector storage sometimes confuses the LLM. But I still want to have both available, without constantly toggling the settings - a web search is better for an overview of recent news on a topic, whereas a vector store is better for answering questions about a given document.

Background/context:

As I mentioned in the other thread, I'm testing a SillyTavern-based LLM setup with retrieval, aiming to build an AI assistant for answering questions about particular scientific papers (among other things). "Retrieval" here is meant to include both vector storage (with PDF file input) and web search, as these cover complementary use cases.

For a concrete example, I fed in this paper into the vector store, and proceeded to ask the LLM (dolphin-2.2.1-mistral-7B running on an ooba backend) about the numerical method the authors of the paper had developed.

The model's initial output mentioned that the method belongs to the Runge-Kutta family. Then, I asked a follow-up question to get more detail. Here came the problem: websearch saw that I was asking a question, picked up the words "Runge-Kutta", and proceeded to inject random facts about unrelated Runge-Kutta methods. The LLM confidently composed its answer out of this mix of vector store extracts and web search results, mixing up what came from where, and incorrectly attributing everything to the paper being discussed. (The prompt template saying that these web results are relevant, probably added to the LLM's confusion.)

Disabling the web search plugin in the settings, and then regenerating the answer, worked fine. This time only the vector store extracts were injected into the prompt. The LLM answered correctly.

Arguably, in both runs, the LLM itself successfully extracted the key points of the text provided in the prompt - it was the contents of the prompt injection that were misleading. It actually surprises me how much a Mistral 7B can successfully dig up from text littered with broken math notation that was produced by automatically converting a PDF into plain text.

Of course, I'm not looking to get the actual equations from the LLM - the point is to summarize the key points (which can be described in plain English), so I'll know whether to look at the paper myself in more detail. Right now, in the testing phase, to be able to judge how well it works, I'm only feeding it papers I've already read. :)

Cohee1207 commented 9 months ago

You can escape questions using a period at the beginning of user messages so that they won't trigger a new search automatically, e.g. .What is beauty?

Technologicat commented 9 months ago

Yes, but won't it still inject the latest previous search result into the prompt?

Cohee1207 commented 9 months ago

Yes. I can propose a new syntax to discard the search, like !Now let's talk about... and everything above that won't trigger a search.

Technologicat commented 9 months ago

Sounds great!

Being able to explicitly discard the search would solve the exact issue I had.

Technologicat commented 8 months ago

Judging by SillyTavern/public/scripts/extensions/third-party/Extension-WebSearch/index.js, it seems you have included this in the meantime... :)

I have one remaining issue here. Using the smart search script you posted in SillyTavern#1421, this doesn't provide a way to clear the context injected by that.

Would it be possible to add a slash command, too, that would do something like storage.clear()?

By the way, I think the smart search script is ingenious and should be included in the standard quick replies. It's the primary way I use the websearch module now. The only customizations I did were /genraw instruct=off ... (as you suggested in that thread) and /inject position=chat depth=1 .... In my tests the prompt looks more logical (to a human eye at least) with depth=1 than the suggested depth=2.

Cohee1207 commented 8 months ago

To unset the inject, use /inject id=webSearchResults (without any implicit value).

WebSearch now has the following syntaxes (as seen in the WebSearch extension readme):

.Ignore just this user message
!Ignore user messages above that point
Search `a piece of text` encased in backticks

Backtick and trigger phrases can be toggled separately, depending on what you'd want to use more.

Technologicat commented 8 months ago

Thank you. I honestly have no idea why it didn't occur to me to re-check the README when I noticed WebSearch had been updated. :)

Aaaah... so that's how it works. I didn't realize it was /inject, not /websearch, that was remembering the search context in the smart search script. I have skimmed the STScript manual, but probably too fast to internalize everything.

I ended up packaging the unset command into a "Clear search context" quick reply:

/inject id=webSearchResults | /echo Smart search context cleared.

Now this works perfectly for me. Closing the ticket.

Technologicat commented 8 months ago

Ah, and I think backticks have superior usability over trigger phrases - no false positive when discussing things like "search for extraterrestrial intelligence" with your AI character. :P

For me, explicit smart search and an explicit context clear command are superior to even that.