-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from TogetherCrew/feat/slash-commands
Feat: Integrate with discord slash commands
- Loading branch information
Showing
22 changed files
with
355 additions
and
83 deletions.
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
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
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 |
---|---|---|
|
@@ -160,4 +160,5 @@ cython_debug/ | |
#.idea/ | ||
|
||
hivemind-bot-env/* | ||
main.ipynb | ||
main.ipynb | ||
.DS_Store |
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
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,2 +1,3 @@ | ||
# hivemind-bot | ||
|
||
This repository is made for TogetherCrew's LLM bot. |
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
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
This file was deleted.
Oops, something went wrong.
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,29 +1,90 @@ | ||
from celery_app.job_send import job_send | ||
from celery_app.server import app | ||
from utils.credentials import load_rabbitmq_credentials | ||
import json | ||
import logging | ||
from typing import Any | ||
|
||
# TODO: Write tasks that match our requirements | ||
from celery_app.server import app | ||
from celery_app.utils.fire_event import job_send | ||
from subquery import query_multiple_source | ||
from tc_messageBroker.rabbit_mq.event import Event | ||
from tc_messageBroker.rabbit_mq.payload.discord_bot.base_types.interaction_callback_data import ( | ||
InteractionCallbackData, | ||
) | ||
from tc_messageBroker.rabbit_mq.payload.discord_bot.chat_input_interaction import ( | ||
ChatInputCommandInteraction, | ||
) | ||
from tc_messageBroker.rabbit_mq.payload.discord_bot.interaction_response import ( | ||
InteractionResponse, | ||
) | ||
from tc_messageBroker.rabbit_mq.payload.payload import Payload | ||
from tc_messageBroker.rabbit_mq.queue import Queue | ||
|
||
|
||
@app.task | ||
def add(x, y): | ||
rabbit_creds = load_rabbitmq_credentials() | ||
username = rabbit_creds["user"] | ||
password = rabbit_creds["password"] | ||
broker_url = rabbit_creds["host"] | ||
port = rabbit_creds["port"] | ||
def ask_question_auto_search( | ||
question: str, | ||
community_id: str, | ||
bot_given_info: ChatInputCommandInteraction, | ||
) -> None: | ||
""" | ||
this task is for the case that the user asks a question | ||
it would first retrieve the search metadata from summaries | ||
then perform a query on the filetred raw data to find answer | ||
res = x + y | ||
job_send(broker_url, port, username, password, res) | ||
Parameters | ||
------------ | ||
question : str | ||
the user question | ||
community_id : str | ||
the community that the question was asked in | ||
bot_given_info : tc_messageBroker.rabbit_mq.payload.discord_bot.chat_input_interaction.ChatInputCommandInteraction | ||
the information data that needed to be sent back to the bot again. | ||
This would be the `ChatInputCommandInteraction`. | ||
""" | ||
prefix = f"COMMUNITY_ID: {community_id} | " | ||
logging.info(f"{prefix}Processing question!") | ||
create_interaction_content = Payload.DISCORD_BOT.INTERACTION_RESPONSE.Create( | ||
interaction=bot_given_info.to_dict(), | ||
data=InteractionResponse( | ||
type=4, | ||
data=InteractionCallbackData( | ||
content="Processing your question ...", flags=64 | ||
), | ||
), | ||
).to_dict() | ||
|
||
return res | ||
logging.info(f"{prefix}Sending process question to discord-bot!") | ||
job_send( | ||
event=Event.DISCORD_BOT.INTERACTION_RESPONSE.CREATE, | ||
queue_name=Queue.DISCORD_BOT, | ||
content=create_interaction_content, | ||
) | ||
logging.info(f"{prefix}Querying the data sources!") | ||
# for now we have just the discord platform | ||
response, source_nodes = query_multiple_source( | ||
query=question, | ||
community_id=community_id, | ||
discord=True, | ||
) | ||
|
||
source_nodes_dict: list[dict[str, Any]] = [] | ||
for node in source_nodes: | ||
node_dict = dict(node) | ||
node_dict.pop("relationships", None) | ||
source_nodes_dict.append(node_dict) | ||
|
||
@app.task | ||
def mul(x, y): | ||
return x * y | ||
results = { | ||
"response": response, | ||
"source_nodes": source_nodes_dict, | ||
} | ||
|
||
response_payload = Payload.DISCORD_BOT.INTERACTION_RESPONSE.Edit( | ||
interaction=bot_given_info.to_dict(), | ||
data=InteractionCallbackData(content=json.dumps(results)), | ||
).to_dict() | ||
|
||
@app.task | ||
def xsum(numbers): | ||
return sum(numbers) | ||
logging.info(f"{prefix}Sending Edit response to discord-bot!") | ||
job_send( | ||
event=Event.DISCORD_BOT.INTERACTION_RESPONSE.EDIT, | ||
queue_name=Queue.DISCORD_BOT, | ||
content=response_payload, | ||
) |
Empty file.
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import Any | ||
|
||
from tc_messageBroker import RabbitMQ | ||
from utils.credentials import load_rabbitmq_credentials | ||
|
||
|
||
def job_send(event: str, queue_name: str, content: dict[str, Any]) -> None: | ||
""" | ||
fire the data to a specific event on a specific queue | ||
Parameters | ||
----------- | ||
event : str | ||
the event to fire message to | ||
queue_name : str | ||
the queue to fire message on | ||
content : dict[str, Any] | ||
the content to send messages to | ||
""" | ||
|
||
rabbit_creds = load_rabbitmq_credentials() | ||
username = rabbit_creds["user"] | ||
password = rabbit_creds["password"] | ||
broker_url = rabbit_creds["host"] | ||
port = rabbit_creds["port"] | ||
rabbit_mq = RabbitMQ( | ||
broker_url=broker_url, port=port, username=username, password=password | ||
) | ||
rabbit_mq.connect(queue_name) | ||
rabbit_mq.publish( | ||
queue_name=queue_name, | ||
event=event, | ||
content=content, | ||
) |
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
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,3 +1,3 @@ | ||
#!/usr/bin/env bash | ||
python3 -m coverage run --omit=tests/* -m pytest . | ||
python3 -m coverage lcov -o coverage/lcov.info | ||
python3 -m coverage lcov -o coverage/lcov.info |
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
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
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from datetime import datetime, timedelta | ||
from unittest import TestCase | ||
|
||
from bson import ObjectId | ||
from utils.fetch_community_id import fetch_community_id_by_guild_id | ||
from utils.mongo import MongoSingleton | ||
|
||
|
||
class TestFetchDiscordCommunityId(TestCase): | ||
def add_platform(self): | ||
client = MongoSingleton.get_instance().get_client() | ||
|
||
action = { | ||
"INT_THR": 1, | ||
"UW_DEG_THR": 1, | ||
"PAUSED_T_THR": 1, | ||
"CON_T_THR": 4, | ||
"CON_O_THR": 3, | ||
"EDGE_STR_THR": 5, | ||
"UW_THR_DEG_THR": 5, | ||
"VITAL_T_THR": 4, | ||
"VITAL_O_THR": 3, | ||
"STILL_T_THR": 2, | ||
"STILL_O_THR": 2, | ||
"DROP_H_THR": 2, | ||
"DROP_I_THR": 1, | ||
} | ||
|
||
client["Core"]["platforms"].insert_one( | ||
{ | ||
"_id": ObjectId(self.platform_id), | ||
"name": "discord", | ||
"metadata": { | ||
"id": self.guild_id, | ||
"icon": "111111111111111111111111", | ||
"name": "A guild", | ||
"selectedChannels": [ | ||
{"channelId": "1020707129214111827", "channelName": "general"} | ||
], | ||
"window": {"period_size": 7, "step_size": 1}, | ||
"action": action, | ||
"period": datetime.now() - timedelta(days=30), | ||
}, | ||
"community": ObjectId(self.community_id), | ||
"disconnectedAt": None, | ||
"connectedAt": (datetime.now() - timedelta(days=40)), | ||
"isInProgress": True, | ||
"createdAt": datetime(2023, 11, 1), | ||
"updatedAt": datetime(2023, 11, 1), | ||
} | ||
) | ||
|
||
def delete_platform(self): | ||
client = MongoSingleton.get_instance().get_client() | ||
client["Core"]["platforms"].delete_one({"_id": ObjectId(self.platform_id)}) | ||
|
||
def test_get_guild_id(self): | ||
self.platform_id = "515151515151515151515151" | ||
self.guild_id = "1234" | ||
self.community_id = "aabbccddeeff001122334455" | ||
self.delete_platform() | ||
self.add_platform() | ||
|
||
community_id = fetch_community_id_by_guild_id(guild_id=self.guild_id) | ||
|
||
self.assertEqual(community_id, self.community_id) | ||
|
||
def test_get_guild_id_no_data(self): | ||
self.platform_id = "515151515151515151515151" | ||
self.guild_id = "1234" | ||
self.community_id = "aabbccddeeff001122334455" | ||
|
||
self.delete_platform() | ||
self.add_platform() | ||
|
||
client = MongoSingleton.get_instance().get_client() | ||
client["Core"]["platforms"].delete_one({"_id": ObjectId(self.platform_id)}) | ||
|
||
with self.assertRaises(ValueError): | ||
_ = fetch_community_id_by_guild_id(guild_id=self.guild_id) |
Oops, something went wrong.