Implementing the filterText function for APIs in SillyTavern.

Do not do this until you are well aware of what you are doing. I, Flamanon, do not take any responsibility if you break your SillyTavern while making the changes prescribed below, proceed of your own volition.


Table of Contents


Please use the filterText.js file provided with the FilterConfigurationModule and not your old file from Zlaude

  • Quick intro


    After I made Zlaude, people requested me to make a filter for API keys as well, at first i was a bit apprehensive of making changes in the SillyTavern files to implement this functionality, primarily because people absolutely do not want to read and figure things out for themselves, which leads to my DMs being cluttered with how-do-i-do-this type of messages. But because of popular demand, I have decided to create this tutorial to allow you to implement the filterText function for APIs.

Copy the original server.js file from the Sillytavern folder and paste it somewhere else, in case you mess things up or do not want to use the filterText function, you can paste the original file back into the SillyTavern folder, hence replacing the modified file.

  • For Anthropic API

    (OOC: To all you lucky peeps whose applications went through, congratulations Jealous slow claps. This one is going to be pretty straightforward, or not Muahahaha)

    1. Install a text editor, preferably Sublime Text.
    2. Navigate to the path where you would like to clone the Filter Configuration Module and type cmd.exe in the address bar. Once the command prompt opens up, type git clone https://github.com/Flamanon/FilterConfigurationModule and hit enter. This will clone the repository in the desired path. Now, click on newconfig.bat and follow the steps in the README.md file in the Flamanon/FilterConfigurationModule repository to configure your filter.
    3. Once you have configured the filter, copy the filterText.js file and paste it in the SillyTavern folder.
    4. This is where the fun begins:
    • Open the server.js file in the SillyTavern Folder using your text editor, and navigate to line number 2900 (OOC: Yeah, Big number) and above the async function sendClaudeRequest(request, response) { line write const filterText = require('./filterText');. Now, Navigate to console.log('Claude response:', responseText); using Ctrl+F (Shortcut for Find tool in Sublime text).
      This is what it should look like
    • Now below this paste the following bit of code:

      1
      2
      3
      // Apply the filterText function to the responseText
      const filteredResponseText = filterText(responseText);
      console.log('Filtered Claude response:', filteredResponseText);
      

      Under this you will find //Wrap it back up to OAI format, in this section, replace responseText with filteredResponseText. So it should look something like this: This is what it should finally look like
      And we're done for this section. (OOC: Yup, drink some water, wipe the sweat on your forehead big guy. You live to see another day!)

  • For OpenAI API

    (OOC: This one might look a tad-bit scarier, and honestly while testing it i did face some bumps here and there, but in the end it just began working like expected, weird.)

    1. The first two steps for this are identical to the changes we made for Anthropic's API. And also, remember to paste const filterText = require('./filterText'); in the right place in the server.js file.(see pt. no. 4 in Anthropic API)
    2. Navigate to line 3022 or something like that (OOC: but it might not be 3022 anymore if you also just altered the Anthropic API section or even pasted the const filterText = require('./filterText'); line as instructed, as it was located just above the OAI section, so the line we're looking for is prolly numbered anything >3022, dont worry tho, use the Find tool with the text in the picture below to navigate to it)
      This is what it looks like
    3. So replace the code below
      1
      2
      3
      4
      5
      } else {
           response_generate_openai.send(response.data);
           console.log(response.data);
           console.log(response.data?.choices[0]?.message);
      }
      
      with this:
        } else {
          const filteredResponseData = {
              ...response.data,
              choices: response.data.choices.map(choice => ({
                  ...choice,
                  message: {
                      ...choice.message,
                      content: filterText(choice.message.content)
                  }
              }))
          };
      
          response_generate_openai.send(filteredResponseData);
          console.log(filteredResponseData);
          console.log(filteredResponseData?.choices[0]?.message);
      }
      
      After you've done that, it should look something like this:
      This is what it should finally look like
      (OOC: oooohhh boy! That one looked a bit messy didn't it? Yeah...but you did it tho(hopefully), good job!)
  • Poe API

    (OOC: This is probably the easiest one out of all the modification suggested in this document. However, I have not tested this one as much as i have tested the others, so just keep that in mind.)

    1. Open the SillyTavern folder and then open the src subfolder. Now paste your filterText.js file here. You will have to modify the filterText.js file a little bit for this one. Copy the function filterText(text) { line and replace it with:
      function filterText(text) { // Will check if input is a valid string if (typeof text !== 'string') { return ''; }

      so before: Before and after: After

    2. Now open the poe-client.js file, and right under the starting comment (probably line 20) write const filterText = require('./filterText');
      Should look like: This.
    3. In the same file find async on_message, this section looks like: This
      • Replace const message = message_data["payload"]["data"]["messageAdded"] with:
        1
        2
        3
        4
        const message = message_data["payload"]["data"]["messageAdded"]
        if (message) {
             message["text"] = filterText(message["text"])
        }
        
        so it will finally look like: Ligma Balls

    (OOC: And we're done, you survived!!(hopefully). Make sure to keep a backup of server.js and poe-client.js files.)


Edit

Pub: 01 Jul 2023 14:58 UTC

Edit: 06 Jul 2023 07:01 UTC

Views: 409