Skip to content

Twitch Bot for generating messages based on GPT models and VectorDB

Notifications You must be signed in to change notification settings

starstormtwitch/TwitchGPTVector

Repository files navigation

TwitchGPTVector

Twitch Bot for generating messages based on GPT models and VectorDB


Explanation

When the bot starts, it listens to chat messages in the channels listed in the settings.json file. It "learns" from any chat message that is talking directly to the bot and that is not a question. When someone requests a message to be generated, a GPT model using VectorDB will generate a sentence based on the learned data. Note that the bot is unaware of the meaning of any of its inputs and outputs. This means it can use bad language if it was taught to use bad language by people in chat. You can add a list of banned words it should never learn or say. Use at your own risk.

How it works

Sentence Parsing

The bot processes chat messages, extracts their keywords and nouns, using those as the subject for it's prompt. It creates a semantic vector from the message and stores it in the vectordb, so that previous messages about that subject can be retrieved.


Generation

When a message is generated with !generate or at the default automatic interval in the configuration, the GPT model and VectorDB work together to create a message based on the learned data. The generated message will resemble the structure and content of the chat messages the bot has learned from. It will use context from the last few minutes of chat, as well as some context from its long term database.

Commands

Chat members can generate chat-like messages using the following commands (Note that they are aliases):

!generate [words]
!g [words]

Example:

!g Curly

Result (for example):

Curly fries are the reason I don't go to the movies anymore

Streamer commands

All of these commands can be whispered to the bot account, or typed in chat. To disable the bot from generating messages, while still learning from regular chat messages:

!disable

After disabling the bot, it can be re-enabled using:

!enable

Changing the cooldown between generations is possible with one of the following two commands:

!setcooldown <seconds>
!setcd <seconds>

Example:

!setcd 30

Which sets the cooldown between generations to 30 seconds.


Moderator commands

Moderators (and the broadcaster) can modify the blacklist to prevent the bot learning words it shouldn't. To add word to the blacklist, a moderator can message the bot:

!blacklist <word>

Similarly, to remove word from the blacklist, a moderator can message the bot:

!whitelist <word>

And to check whether word is already on the blacklist or not, a moderator can message the bot:

!check <word>

Settings

This bot is controlled by a settings.json file, which has the following structure:

{
  "Channel": ["#starstorm"]
  "ClientID": "exampleexampleexampleexample",
  "ClientID": "exampleexampleexampleexample",
  "DeniedUsers": ["StreamElements", "Nightbot", "Moobot", "Marbiebot"],
  "AllowedUsers": [],
  "Cooldown": 20,
  "KeyLength": 2,
  "MaxSentenceWordAmount": 25,
  "MinSentenceWordAmount": -1,
  "HelpMessageTimer": 18000,
  "AutomaticGenerationTimer": -1,
  "WhisperCooldown": true,
  "EnableGenerateCommand": true,
  "SentenceSeparator": " - ",
  "AllowGenerateParams": true,
  "GenerateCommands": ["!generate", "!g"]
}
Parameter Meaning Example
Channels Channel that will be connected to. "#Starstorm"
ClientID The Client Id for the bot account. You must create your own bot and add it's client id here: https://dev.twitch.tv/console/apps/create Redirect url should be set to http://localhost:3000/callback "oauth:pivogip8ybletucqdz4pkhag6itbax"
ClientSecret The Client Secret for the bot account, same place as above. "oauth:pivogip8ybletucqdz4pkhag6itbax"
OpenAIKey This is the openai api key that will be used to generate the messages. You will have to sign up an account and then create an API key to put it here. Here is the link to openai: https://platform.openai.com/login?launch "oauth:pivogip8ybletucqdz4pkhag6itbax"
DeniedUsers The list of (bot) accounts whose messages should not be learned from. The bot itself it automatically added to this. ["StreamElements", "Nightbot", "Moobot", "Marbiebot"]
AllowedUsers A list of users with heightened permissions. Gives these users the same power as the channel owner, allowing them to bypass cooldowns, set cooldowns, disable or enable the bot, etc. ["loltyler1", "starstorm"]
Cooldown A cooldown in seconds between successful generations. If a generation fails (eg inputs it can't work with), then the cooldown is not reset and another generation can be done immediately. `20
AutomaticGenerationTimer The amount of seconds between automatically sending a generated message, as if someone wrote !g. -1 for no automatic generations. -1
WhisperCooldown Allows the bot to whisper a user the remaining cooldown after that user has attempted to generate a message. true
EnableGenerateCommand Globally enables/disables the generate command. `true
GenerateCommands The generation commands that the bot will listen for. Defaults to ["!generate", "!g"]. Useful if your chat is used to commands with ~, -, /, etc. ["!generate", "!g"]

Note that the example OAuth token is not an actual token, but merely a generated string to give an indication what it might look like.

I got my real OAuth token from https://twitchapps.com/tmi/.


Blacklist

You may add words to a blacklist by adding them on a separate line in blacklist.txt. Each word is case insensitive. By default, this file only contains <start> and <end>, which are required for the current implementation.

Words can also be added or removed from the blacklist via whispers, as is described in the Moderator Command section.


Requirements

Additionally, you'll need to have the GPT model files and access to the Vector DB for the bot to function correctly. whichever model you want from https://nlp.stanford.edu/projects/glove/ spacy en_core_web_sm https://github.com/spotify/annoy