This repository demonstrates how to do semantic search on Discord, relying on Pyserini for retrieval.
- Python ≥ 3.10
- NVIDIA GPU with CUDA support
- Conda (recommended)
- Discord Account
- Ownership of Discord Server
-
Install Pyserini according to https://github.com/castorini/pyserini/blob/master/docs/installation.md#pypi-installation-walkthrough using Conda. (Preferably Linux)
conda create -n pyserini python=3.11 -y conda activate pyserini # Inside the new environment... conda install -c conda-forge openjdk=21 maven -y # from https://pytorch.org/get-started/locally/ (it is important to have CUDA enabled) pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126 conda install -c pytorch faiss-cpu -y # Good idea to always explicitly specify the latest version, found here: https://pypi.org/project/pyserini/ pip install pyserini==1.3.0
-
Install the required packages:
pip install -r requirements.txt
eval "$(/home/<username>/miniforge3/bin/conda shell.bash hook)"
conda activate pyserini
-
Set the following environment variables in the
.envfile:CHANNEL_ID: The channel in a given Discord server that the searcher searches throughTOKEN: Auth token for your Discord botCACHE: True means that data is cached. False means that the data is updated each time the searcher is started up. (Default: True for the sake of demonstration)TOP_K: Show the topTOP_Kresults (Default: 5 for the sake of demonstration)
-
Create a Discord server if you already do not own a Discord Server: https://support.discord.com/hc/en-us/articles/204849977-How-do-I-create-a-server
-
How To Find The Channel ID
- Navigate to the channel you want to copy the ID for.
- Right-click on the channel, and then tap on Copy Channel ID
- Now you can paste the channel ID into the
.envfile
- Make sure you’re logged on to the Discord website: https://discord.com
- Navigate to https://discord.com/developers/applications
- Create a New Application
- Within your new application, on the left side, navigate to Settings > Bot
- Click on Reset Token under Token. This is important to grab this token for the
TOKENin the.envfile - Click on Message Content Intent to enable it
- Click on Reset Token under Token. This is important to grab this token for the
- Navigate to Settings > OAuth2
-
Click on the
botcheckbox under the OAuth2 URL Generator > Scopes -
Then under OAuth2 URL Generator > Bot Permissions > Text Permissions, click on
- Send Messages
- Read Message History
- Embed Links
-
Copy the URL under Generated URL
-
Paste the URL into a new window/tab so as to open the URL
-
Add bot to the server set up in Configuration section above.
-
To run:
python main.py
Depending on if you have allowed the cache to be reset or if this is the first time running the searcher, it may take a while before the searcher is ready; terminal should say when the searcher is ready:
Searcher is ready!
Once the searcher is running, the !search <query_text> is all you need
By default (as described in .env), the bot will return the top 5 results by semantics.
Discord-Semantic-Search/
├── data/ # Local document corpus
├── main.py # Entry point
├── utils/
│ ├── config.py # Configuration management
│ ├── bot.py # Discord bot business logic
└── requirements.txt
The implementation can be broken down into two parts:
- Retrieval
The retrieval first grabs the chat history of a given channel and stores it locally.
The business logic then creates indexes and embeddings of said chat history in a format
that friendly is friendly to FAISS (Facebook AI Similarity Search). Upon a given query,
it scores the chats based on semantic relevance to the query and returns the top
krelevant chat messages. - Discord Using the Discord SDK, the bot is created. It is the entry point for accessing and reading the message history of a given Discord channel. It is also the interactive bot that accepts query strings and prints out the results of the FAISS retrieval to the Discord server.
The search has a NDCG@10 score of about 35.19% and precision of about 27.95%. In other words, this search does slightly worse than BM25 and other term matching methodologies. Nonetheless, heuristically, the search has helped me
find results that I would otherwise not be able to do so via the built-in discord search. So practically speaking,
semantic search is helpful as an additonal or supplementary approach to searching for chat rather than just being
constrained to term matching search.
Things to do if this was not just a demo, and I wanted to productionized it
- Deploy the bot in a cloud environment
- Setup a cloud vectorized database
- Enable it for more than just a singular channel search
- Use a hybrid model in an attempt to improve performance
- Rich Discord user interface
- Replace pyserini with something more performant
- Replace
listdirwithscandir





