-
Notifications
You must be signed in to change notification settings - Fork 1
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
sand194
wants to merge
6
commits into
main
Choose a base branch
from
dc_endpoint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Discord endpoint #133
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fbbee11
dc endpoint and slash commands support
sand194 d4f05d7
formatting fixes
sand194 92c31a5
formatting fixes 2
sand194 c1ecabe
create django dc endpoint
sand194 2f946e5
fill in bot/tests.py and change discord_bot/bot.py to query our api
sand194 489ae20
formatting fixes
sand194 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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" | ||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
@@ -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]: | ||
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"]) | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ;)