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

Discord endpoint #133

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
43 changes: 41 additions & 2 deletions discord_bot/discord_bot/bot.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Sets up discord bot."""

import json
import os
from collections import defaultdict
from typing import Self

import discord
import requests
from discord.ext import commands

from discord_bot import config
Expand Down Expand Up @@ -40,8 +42,32 @@ async def bad_button(
)


@bot.command()
@commands.has_any_role("Admins", "Moderators")
async def sync(ctx) -> None:
"""Call to Discord API to update slash commands."""
await ctx.send("Synchronizing commands...")
await bot.tree.sync()


def query_llm(prompt, stop_signs):
"""Returns llm's response."""
url = "http://llm:9000/v1/completions"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should query our api when will be ready ;)

headers = {"Content-Type": "application/json"}
data = {"prompt": prompt, "stop": stop_signs}

response = requests.post(url, headers=headers, data=json.dumps(data), timeout=5)

respose_status_code = 200

if response.status_code == respose_status_code:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: use HTTPStatus as you did in views.py

return response.json()

return response.text


async def get_chats_history():
"""Taking chat conversation from all chanells."""
"""Taking chat conversation from all channels."""
chats_history = defaultdict(list)
for guild in bot.guilds:
readable_channels = filter(
Expand All @@ -58,7 +84,7 @@ async def get_chats_history():

@bot.command()
async def show(ctx: commands.Context, limit: int = 100):
"""Shows what get_chats_history gets."""
"""Shows the results of get_chats_history."""
last_messages = await get_chats_history()
channel_id = ctx.channel.id
if last_messages[channel_id]:
Expand All @@ -68,6 +94,19 @@ async def show(ctx: commands.Context, limit: int = 100):
await ctx.send("Brak ostatnich wiadomości.")


@bot.tree.command(name="chatknml", description="Porozmawiaj z chatbotem")
async def chatknml(interaction: discord.Interaction, *, prompt: str):
"""Passes the prompt to the llm and returns the answer."""
await interaction.response.defer()

query = "\n\n### Instructions:\n" + prompt + "\n\n### Response:\n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: f-strings are significantly faster

stop_signs = ["\n", "###"]

result = query_llm(query, stop_signs)

await interaction.followup.send(result["choices"][0]["text"])


def main():
"""Entrypoint."""
bot.run(os.environ["TOKEN"])
Expand Down
Loading
Loading