This repository contains two projects:
-
Scikit RAG + OpenAI in
/app
: A Flask-based Retrieval-Augmented Generation (RAG) chatbot using OpenAI's GPT model, scikit-learn, and Sentence Transformers for dynamic knowledge retrieval. -
Discord App for Defang in
/discord-bot
: A Discord bot designed for Defang Software Labs, providing helpful resources and interacting with users via slash commands.
This application demonstrates how to deploy a Flask-based Retrieval-Augmented Generation (RAG) chatbot using OpenAI's GPT model. The chatbot retrieves relevant documents from a knowledge base using scikit-learn and Sentence Transformers and then generates responses using OpenAI's GPT model.
- Download Defang CLI
- (Optional) If you are using Defang BYOC authenticated with your AWS account
- (Optional - for local development) Docker CLI
- Open the terminal and type
defang login
- Type
defang compose up
in the CLI. - Your app will be running within a few minutes.
-
Clone the repository.
-
Create a
.env
file in the root directory and set your OpenAI API key, or add theOPENAI_API_KEY
to your.zshrc
or.bashrc
file. -
Run the command:
docker compose -f compose.dev.yaml up --build
This spins up a Docker container for the RAG chatbot.
- The knowledge base is the all the markdown files in the Defang docs website. The logic for parsing can be found in
./app/get_knowledge_base.py
. - The file
get_knowledge_base.py
parses every webpage as specified into paragraphs and writes toknowledge_base.json
for the RAG retrieval. - To obtain your own knowledge base, please feel free to implement your own parsing scheme.
- for local development, please use the
compose.dev.yaml
file where as for production, please use thecompose.yaml
.
This is a Discord bot developed for Defang Software Labs. It provides helpful resources in a Discord server and interacts with users via slash commands. The bot is built using Discord's official template.
/ask
: A command to ask Defang-related questions to the bot. The bot accesses the Ask Defang (ask.defang.io) API endpoint for retrieving responses.
/test
: A basic command to test functionality using the Discord API, without relying on external APIs.
Below is a basic overview of the project structure:
├── .env. -> .env file (not shown)
├── app.js -> main entrypoint for app
├── commands.js -> slash command payloads + helpers
├── utils.js -> utility functions and enums
├── package.json
├── README.md
└── .gitignore
Before you start, you'll need to install NodeJS and create a Discord app with the proper permissions:
applications.commands
bot
(with Send Messages enabled) Configuring the app is covered in detail in the getting started guide.
cd discord-bot
npm install
Fetch the credentials from your app's settings and add them to a .env
file. You'll need your app ID (DISCORD_APP_ID
), bot token (DISCORD_TOKEN
), and public key (DISCORD_PUBLIC_KEY
).
You will also need an ASK_TOKEN
to authenticate API calls to the Ask Defang endpoint.
The commands for the example app are set up in commands.js
. All of the commands in the ALL_COMMANDS
array at the bottom of commands.js
will be installed when you run the register
command configured in package.json
:
cd discord-bot
npm run register
After your credentials are added, go ahead and run the app:
cd discord-bot
npm run start
The project needs a public endpoint where Discord can send requests. To develop and test locally, you can use something like ngrok
to tunnel HTTP traffic.
Install ngrok if you haven't already, then start listening on port 3000
in a separate terminal:
ngrok http 3000
You should see your connection open:
Tunnel Status online
Version 2.0/2.0
Web Interface http://127.0.0.1:4040
Forwarding https://1234-someurl.ngrok.io -> localhost:3000
Connections ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00
Copy the forwarding address that starts with https
, in this case https://1234-someurl.ngrok.io
, then go to your app's settings.
On the General Information tab, there will be an Interactions Endpoint URL. Paste your ngrok address there, and append /interactions
to it (https://1234-someurl.ngrok.io/interactions
in the example).
Click Save Changes, and your app should be ready to run 🚀