Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AI #26

Draft
wants to merge 28 commits into
base: main
Choose a base branch
from
Draft

AI #26

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
36679e4
Bert AI
codeman1o1 Jan 8, 2024
31ae169
Remove unused requirement
codeman1o1 Jan 9, 2024
c442e24
Use async invoke
codeman1o1 Jan 9, 2024
3fa8ee5
Add image support
codeman1o1 Jan 9, 2024
b7866a0
Ollama URL is not an env var
codeman1o1 Jan 10, 2024
3b74f73
Provide default AI url
codeman1o1 Jan 10, 2024
d713d84
Switch to official Ollama package instead of Langchain
codeman1o1 Feb 1, 2024
220f73c
Merge remote-tracking branch 'origin/main' into experimental-ai
codeman1o1 Feb 1, 2024
6ca48c4
some fixes
codeman1o1 Feb 1, 2024
a9a1157
some fixes
codeman1o1 Feb 1, 2024
d548c63
fix imaging
codeman1o1 Feb 1, 2024
7b55b13
remove unused import
codeman1o1 Feb 1, 2024
55c0cb2
iomprovenemenmt
codeman1o1 Feb 3, 2024
7f6aff5
Merge branch 'main' into experimental-ai
codeman1o1 May 6, 2024
2cb00f7
Update ollama
codeman1o1 May 6, 2024
a405be1
reorder imports to make isort happy
codeman1o1 May 6, 2024
135a40a
rename docker volume
codeman1o1 Jun 20, 2024
c4440f6
Merge branch 'main' into experimental-ai
codeman1o1 Jul 8, 2024
76ba23a
Automatically download models
codeman1o1 Jul 8, 2024
df1c4ee
yay i sorted the imports
codeman1o1 Jul 8, 2024
32bbc12
Merge branch 'main' into experimental-ai
codeman1o1 Sep 28, 2024
d477549
Merge branch 'main' into experimental-ai
codeman1o1 Oct 6, 2024
800016f
add chat history of 10 min
codeman1o1 Oct 6, 2024
3f4264d
bump ollama
codeman1o1 Oct 6, 2024
9e6531e
lint
codeman1o1 Oct 6, 2024
05ed4f5
add /ai command
codeman1o1 Oct 6, 2024
cbe5ffb
allow model read in `bert model` command
codeman1o1 Oct 6, 2024
2af30f0
Merge branch 'main' into experimental-ai
codeman1o1 Nov 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
BOT_TOKEN=...
PB_EMAIL=...
PB_PASSWORD=...
LAVALINK_URL=...
GOOGLE_API_KEY=...
LAVALINK_PASSWORD=...
OLLAMA_URL=...
TZ=...
154 changes: 153 additions & 1 deletion bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from asyncio import sleep
from datetime import UTC, datetime, time, timedelta
from random import choice, randint
from typing import List
from zoneinfo import ZoneInfo

import coloredlogs
Expand All @@ -20,6 +21,7 @@
from discord.commands import option
from discord.ext import commands, tasks
from dotenv import load_dotenv
from ollama import AsyncClient
from pocketbase import PocketBaseError # type: ignore
from pb import PB, pb_login
from ui.message import StoreMessage
Expand All @@ -35,6 +37,8 @@

TZ = ZoneInfo(os.getenv("TZ") or "Europe/Amsterdam")

ollama = AsyncClient(host=os.getenv("OLLAMA_URL") or "http://ai:11434")


class LogFilter(logging.Filter):
def filter(self, record: logging.LogRecord):
Expand Down Expand Up @@ -99,13 +103,31 @@ def filter(self, record: logging.LogRecord):
logger.info("Found %s upcoming holidays", len(holidays))


async def download_ai_models(models: List[str]):
"""Download the AI models from the Ollama server."""
downloaded_models = await ollama.list()
for model in models.copy():
if any(
m["name"].replace(":latest", "") == model
for m in downloaded_models["models"]
):
models.remove(model)
if not models:
return
logger.debug("Downloading %s AI models (%s)", len(models), ", ".join(models))
for model in models:
logger.debug("Downloading %s...", model)
await ollama.pull(model=model)


async def connect_nodes():
"""Connect to our Lavalink nodes."""
await bert.wait_until_ready()

nodes = [
wavelink.Node(
uri="http://lavalink:2333", password=os.getenv("LAVALINK_PASSWORD")
uri=os.getenv("LAVALINK_URL") or "http://lavalink:2333",
password=os.getenv("LAVALINK_PASSWORD"),
)
]
await wavelink.Pool.connect(nodes=nodes, client=bert)
Expand Down Expand Up @@ -209,6 +231,102 @@ async def on_message(message: discord.Message):
await message.delete()
return

if message.channel.name == "bert-ai":
available_models = [
model["name"].split(":")[0] for model in (await ollama.list())["models"]
]
model = "llama2-uncensored"

if message.content == "bert clear":
await message.channel.send("Understood. ||bert-ignore||")
return

if message.content.startswith("bert model"):
if len(message.content.split(" ")) == 2:
history = await message.channel.history(
limit=100,
before=message.created_at,
after=message.created_at - timedelta(minutes=10),
).flatten()
history.reverse()
for msg in history:
if "bert-ignore" not in msg.content and not msg.author.bot:
if msg.content == "bert clear":
break
if (
msg.content.startswith("bert model ")
and msg.content.split(" ")[2] in available_models
):
model = msg.content.split(" ")[2]
break
await message.channel.send(f"Current model is {model}. ||bert-ignore||")
else:
if (model := message.content.split(" ")[2]) in available_models:
await message.channel.send(f"Model set to {model}. ||bert-ignore||")
else:
await message.channel.send(
f"Model {model} is not available. ||bert-ignore||"
)
return

async with message.channel.typing():
images = []
for sticker in message.stickers:
if sticker.format.name in ("png", "apng"):
images.append(await sticker.read())
for attachment in message.attachments:
if attachment.content_type.startswith("image"):
images.append(await attachment.read())

messages = []
history = await message.channel.history(
limit=100,
before=message.created_at,
after=message.created_at - timedelta(minutes=10),
oldest_first=True,
).flatten()
for msg in history:
if "bert-ignore" not in msg.content:
if msg.author.bot:
if msg.author == bert.user:
messages.append(
{"role": "assistant", "content": msg.content}
)
elif msg.content == "bert clear":
messages.clear()
elif msg.content.startswith("bert model "):
if msg.content.split(" ")[2] in available_models:
model = msg.content.split(" ")[2]
else:
images = []
for sticker in message.stickers:
if sticker.format.name in ("png", "apng"):
images.append(await sticker.read())
for attachment in message.attachments:
if attachment.content_type.startswith("image"):
images.append(await attachment.read())

messages.append(
{"role": "user", "content": msg.content, "images": images}
)
messages.append(
{"role": "user", "content": message.content, "images": images}
)

ai_reply = await ollama.chat(
"llava" if images else model, messages=messages
)

if response := ai_reply["message"]["content"]:
if len(response) > 2000:
await message.channel.send(
"_The response is too long to send in one message_"
)
else:
await message.channel.send(response)
else:
await message.channel.send("_No response from AI_")


@bert.event
async def on_member_join(member: discord.Member):
Expand Down Expand Up @@ -450,6 +568,39 @@ async def delete(interaction: discord.Interaction, key: str):
)


async def autocomplete_models(ctx: discord.AutocompleteContext):
"""Autocomplete the AI models from the Ollama server."""
models = (await ollama.list())["models"]
return [
discord.OptionChoice(model["name"].split(":")[0])
for model in models
if ctx.value in model["name"].split(":")[0]
]


@bert.slash_command(
integration_types={
discord.IntegrationType.guild_install,
discord.IntegrationType.user_install,
}
)
@option("prompt", description="The prompt to give to the AI")
@option("model", description="The model to use", autocomplete=autocomplete_models)
async def ai(interaction: discord.Interaction, prompt: str, model: str = "llama3.2"):
"""Bert AI Technologies Ltd."""
await interaction.response.defer()
ai_response = await ollama.generate(model, prompt)
if response := ai_response["response"]:
if len(response) > 2000:
await interaction.followup.send(
"_The response is too long to send in one message_"
)
else:
await interaction.followup.send(response)
else:
await interaction.followup.send("_No response from AI_")


@bert.slash_command(integration_types={discord.IntegrationType.user_install})
async def everythingisawesome(interaction: discord.Interaction):
"""Everything is AWESOME"""
Expand Down Expand Up @@ -988,6 +1139,7 @@ async def main():
except PocketBaseError:
logger.critical("Failed to login to Pocketbase")
sys.exit(111) # Exit code 111: Connection refused
await download_ai_models(["llama2-uncensored", "llava"])
async with bert:
await bert.start(os.getenv("BOT_TOKEN"))

Expand Down
1 change: 1 addition & 0 deletions bot/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pocketbase-async==0.11.0
coloredlogs==15.0.1
feedparser==6.0.11
PyNaCl==1.5.0
ollama==0.3.3
17 changes: 17 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ services:
condition: service_healthy
lavalink:
condition: service_healthy
ai:
condition: service_started
restart: unless-stopped

website:
Expand Down Expand Up @@ -67,5 +69,20 @@ services:
- ./bot/sounds:/opt/Lavalink/sounds
restart: unless-stopped

ai:
container_name: bert-ai
image: ollama/ollama
volumes:
- ai:/root/.ollama
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: 1
capabilities: [gpu]
restart: unless-stopped

volumes:
pocketbase:
ai: