Skip to content

Commit

Permalink
add "--log-all-the-things" debug parameter, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisrude committed May 4, 2023
1 parent 410bd44 commit 8131eae
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 7 deletions.
147 changes: 147 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,150 @@
A Discord bot which talks to a Large Language Models AI (like LLaMA, llama.cpp, GPT-J, Pythia, OPT, and GALACTICA), running on [oobabooga's text-generation-webui](https://github.com/oobabooga/text-generation-webui).

[![python lint and test with poetry](https://github.com/chrisrude/oobabot/actions/workflows/python-package.yml/badge.svg)](https://github.com/chrisrude/oobabot/actions/workflows/python-package.yml)

![oobabot in action!](./docs/oobabot.png "discord action shot")

# Motivation
Text-generative UIs are cool to run at home, and Discord is fun to mess with your friends. Why not combine the two and have something awesome!

Real motivation: I wanted a chatbot in my discord that would act like my cat. A "catbot" if you will.

# Features
- user-supplied persona! -- you supply the persona on how would like the bot to behave
- multiple converations at once -- can track multiple conversational threads, and reply to each in a contextually appropriate way
- watchwords -- can monitor all channels in a server for one or more wakewords or @-mentions
- private conversations -- can chat with you 1:1 in a DM
- good discord hygiene -- splits messages into independent sentences, pings the author in the first one
- low-latency -- streams the reply live, sentence by sentence. Provides lower latency, especially on longer responses.
- stats -- track token generation speed, latency, failures and usage
- easy networking -- connects to discord from your machine using websockets, so no need to expose a server to the internet

# Getting Started with Oobabot

### See the [Installation Guide](./docs/INSTALL.md) for step-by-step instructions.

## Installation tl;dr:
1. Install [oobabooga's text-generation-webui](https://github.com/oobabooga/text-generation-webui) and enable its API module
1. Create [a Discord bot account](https://discordpy.readthedocs.io/en/stable/discord.html), invite it to your server, and note its authentication token.
1. [Install oobabot (see INSTALL.md)](./docs/INSTALL.md)
```
~: pip install oobabot
~: export DISCORD_TOKEN = __your_bots_discord_token__
~: oobabot --base-url ws://oobabooga-hostname:5005/ --ai-name YourBotsName --persona "You are a cat named YourBotsName"
```

You should now be able to run oobabot from whenever pip installed it.

```
usage: oobabot [-h] [--base-url BASE_URL] [--ai-name AI_NAME] [--wakewords [WAKEWORDS ...]]
[--persona PERSONA] [--local-repl] [--log-all-the-things LOG_ALL_THE_THINGS]
Discord bot for oobabooga's text-generation-webui
options:
-h, --help show this help message and exit
--base-url BASE_URL Base URL for the oobabooga instance. This should be ws://hostname[:port] for
plain websocket connections, or wss://hostname[:port] for websocket
connections over TLS.
--ai-name AI_NAME Name of the AI to use for requests. This can be whatever you want, but might
make sense to be the name of the bot in Discord.
--wakewords [WAKEWORDS ...]
One or more words that the bot will listen for. The bot will listen in all
discord channels can access for one of these words to be mentioned, then
reply to any messages it sees with a matching word. The bot will always reply
to @-mentions and direct messages, even if no wakewords are supplied.
--persona PERSONA This prefix will be added in front of every user-supplied request. This is
useful for setting up a 'character' for the bot to play. Alternatively, this
can be set with the OOBABOT_PERSONA environment variable.
--local-repl start a local REPL, instead of connecting to Discord
--log-all-the-things LOG_ALL_THE_THINGS
prints all oobabooga requests and responses in their entirety to STDOUT
Also, to authenticate to Discord, you must set the environment variable: DISCORD_TOKEN = <your bot's
discord token>
```

---
## Required settings:
- **`DISCORD_TOKEN`** environment variable

Set your shell environment's **`DISCORD_TOKEN`** to token Discord provided when you set up the bot account. It should be something like a 72-character-long random-looking string.

#### Bash shell
export DISCORD_TOKEN=___YOUR_TOKEN___

#### Fish shell
set -Ux DISCORD_TOKEN ___YOUR_TOKEN___

- **`--base-url`**

The base URL of oobabooga's streaming web API. This is
required if the oobabooga machine is different than where you're running oobabot.

By default, this will be port 5005 (even though the HTML UI runs on a different port). The protocol should typically be ws://.

All together, this should look something like:

`--base-url ws://localhost:5005`

This is also the default value, but any other setting should follow the same form.

## Optional settings:
- **`--ai-name`**

the name the AI will be instructed to call itself. Note that this technically doesn't need to be the same as the bot in your discord, but it would likely make sense to your users if they are at least similar.

- **`--wakewords`**

one or more words that the bot will look for. It will reply to any message which contains one of these words, in any channel.

- **`--local-repl`**

instead of connecting to discord, just start up a local REPL and send these prompts directly to the oobabooga server. Useful if you want to test if that part is working in isolation. Note that in this mode you will be sending the oobabooga server raw input, and so the persona or AI name settings will be ignored.

## Persona: the fun setting!

- **`--persona`**

is a short few sentences describing the role your bot should act as. For instance, this is the setting I'm using for my cat-bot, whose name is "Rosie".

```
Here is some background information about Rosie:
- You are Rosie
- You are a cat
- Rosie is a female cat
- Rosie is owned by Chris, whose nickname is xxxxxxx
- Rosie loves Chris more than anything
- You are 9 years old
- You enjoy laying on laps and murder
- Your personality is both witty and profane
- The people in this chat room are your friends
```
Persona may be set from the command line with the **`--persona`** argument.

Alternatively, it can be set through the environment variable **`OOBABOT_PERSONA`**.

## Then, run it!

You should see something like this if everything worked:

![oobabot running!](./docs/oobabot-cli.png "textually interesting image")

---

# Interacting with oobabot

By default, oobabot will listen for three types of messages in the servers it's connected to:

1. any message in which oobabot's account is @-mentioned
2. any direct message
3. any message containing a provided wakeword (see Optional Settings)


# Known Issues
- ooba's text generation can errs with OOM when more than one request comes in at once.
- sometimes the bot wants to continue conversations on behalf of other members of the chatroom. I have some hacks in place to notice and truncate this behavior, but it can lead to terse responses on occasion.
- found one not listed here? [Create an issue](https://github.com/chrisrude/oobabot/issues) on github!

72 changes: 72 additions & 0 deletions docs/INSTALL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

# Getting Started with Oobabot

To get this working, you'll need three things up and talking to each other:

## Fancy-Pants Architecture Diagram

Here's what you'll be setting up.

[ text-generation-webui ] <-- [ oobabot ] --> [ discord bot account ]

(--> means: connects to via websockets)


## 1. Install `text-generation-webui`

- get the text-generation-webui running on your box. I used a few guides to do this:
- https://www.reddit.com/r/LocalLLaMA/comments/11o6o3f/how_to_install_llama_8bit_and_4bit/
- the instructions on [oobabooga's text-generation-webui github](https://github.com/oobabooga/text-generation-webui)
- download a model to run. I suspect the most interesting model will change frequenty, but as of May 3 20213 I am currently using https://huggingface.co/chavinlo/gpt4-x-alpaca. This runs quite well a GPU with 10GB of RAM.
- enable the "API" plugin. You can do this with the '--api' command-line option, or by enabling the "api" plugin on the "interface mode" tab of the web UI.
- make note of the URL to the web UI, you'll need this in a later step (either http or https should work, your choice)

## 2. Create a Discord bot account for oobabot

You can follow the steps in [discord.py's documentation](https://discordpy.readthedocs.io/en/stable/discord.html), [Discord's own documentation](https://discord.com/developers/docs/getting-started) or follow any number of online guides.
It boils down to first, creating a bot account:
- log into the discord web interface (the native apps don't expose these settings)
- go to the [application page](https://discord.com/developers/applications)
- create a new application.
- Choose a name that matches what you want the bot to be called. This will be visible to users.
- Public bot / private bot doesn't matter
- generate a token for the bot. MAKE NOTE OF THIS FOR LATER.
- enable the bot's intents as follows:
- PRESENCE INTENT: OFF
- SERVER MEMBERS INTENT: ON
- MESSAGE CONTENT INTENT: ON

## 3. Invite oobabot to Discord servers

- go to the [application page](https://discord.com/developers/applications)
- click on your bot’s page, then the "OAuth2" tab
- under "Scopes" choose only "bot"
- Enable the following under "bot permissions":
- **General Permissions**
- ✅ read messages / view channels
- *disable everything else*
- **Text Permissions**
- ✅ send messages
- ✅ send messages in threads
- ✅ read mention history
- *disable everything else*
- **Voice Permissions**
- *disable everything*
- generate the URL
- give the URL to an admin on the Discord servers of interest, and have them accept the various warnings that will show up

## 4. Install oobabot itself

You can install oobabot on any machine that can reach the oobabooga's text-generation-webui URL you noted above. By default it will assume it's the same machine, but you can also run it anywhere else if that's more convenient.

Using python3.8, 3.9 or 3.10, install the `oobabot` package from PiPy, using your favorite package manager. E.g.
```
pip install oobabot
```

## 5. Configure oobabot amd have fun!

Please refer to the [main README.md](../README.md) for setup instructions.

Oobabot can be a lot of fun for a discord to talk to, especially with a creative personality. Be creative and enjoy it!

Binary file added docs/oobabot-cli.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/oobabot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 14 additions & 6 deletions src/oobabot/discord_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,19 @@ async def generate_prompt(
class DiscordBot(discord.Client):
HIST_SIZE = 10

def __init__(self, ooba_client: OobaClient, ai_name: str,
ai_persona: str, wakewords: list[str]):
def __init__(self,
ooba_client: OobaClient,
ai_name: str,
ai_persona: str,
wakewords: list[str],
log_all_the_things: bool):
self.ooba_client = ooba_client

self.ai_name = ai_name
self.ai_persona = ai_persona
self.ai_user_id = -1
self.wakewords = wakewords
self.log_all_the_things = log_all_the_things

self.average_stats = AggregateResponseStats(ooba_client)
self.prompt_generator = PromptGenerator(ai_name, ai_persona)
Expand Down Expand Up @@ -235,14 +240,17 @@ async def send_response(self, raw_message: discord.Message) -> None:
self.ai_user_id, recent_messages)

response_stats = self.average_stats.log_request_arrived(prompt)
print('Prompt:\n----------\n')
print(prompt)
print('Response:\n----------\n')
if self.log_all_the_things:
print('Prompt:\n----------\n')
print(prompt)
print('Response:\n----------\n')

try:
async for sentence in self.ooba_client.request_by_sentence(
prompt
):
print(sentence)
if self.log_all_the_things:
print(sentence)

# if the AI gives itself a second line, just ignore
# the line instruction and continue
Expand Down
3 changes: 2 additions & 1 deletion src/oobabot/oobabot.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def sigint_handler(_signum, _frame):
ooba_client,
ai_name=settings.ai_name,
ai_persona=settings.persona,
wakewords=settings.wakewords)
wakewords=settings.wakewords,
log_all_the_things=settings.log_all_the_things)
coroutine = bot.start(settings.DISCORD_TOKEN)

asyncio.run(coroutine)
6 changes: 6 additions & 0 deletions src/oobabot/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ def __init__(self):
help='start a local REPL, instead of connecting to Discord',
action='store_true'
)
self.add_argument(
'--log-all-the-things',
default=False,
help='prints all oobabooga requests and responses in their ' +
'entirety to STDOUT',
)

def settings(self) -> dict[str, str]:
if self._settings is None:
Expand Down

0 comments on commit 8131eae

Please sign in to comment.