This practice project was created to gain insights and foundational knowledge of the OpenAI API while fulfilling a long-standing desire to create a bot. The bot can be used with a slash command or by mentioning the bot in a message. This setup is not hosted, so you will need to expose your local server using a tool like ngrok.
- You'll need to create your own slack app/bot and replace
askchatgpt
with<your-slack-app-name>
- Follow this post here to get you started with slack
- Slash command integration:
/askchatgpt
- App mention integration:
@askchatgpt
- Maintains conversation history within Slack threads
- Customizable conversation model configurations
- Clone the repository to your local machine.
- Run
npm install
to install the required dependencies. - Set up your environment variables in a
.env
file:
SLACK_BOT_TOKEN=<your-slack-bot-token>
SLACK_SIGNING_SECRET=<your-slack-signing-secret>
OPENAI_API_KEY=<your-openai-api-key>
- Start the server by running
node index.js
ornpm start
.
- Download and install ngrok.
- Run
ngrok http 3000
in a new terminal window to expose your local server on port 3000. - Note the HTTPS forwarding URL provided by ngrok (e.g.,
https://<your-random-subdomain>.ngrok.io
).
- Go to the Slack API website and navigate to your app's management dashboard.
- Under "Features", click on "Slash Commands".
- Click on the
/askchatgpt
command and update the "Request URL" with your ngrok HTTPS forwarding URL followed by/slack/events
. Example:
https://<your-random-subdomain>.ngrok.io/slack/events
- Save your changes.
- Under "Features", click on "Event Subscriptions".
- Update the "Request URL" with your ngrok HTTPS forwarding URL followed by
/slack/events
. Same as above example - Save your changes.
- In Slack, use the slash command
/askchatgpt <question>
to ask the bot a question. Example:
/askchatgpt What is the capital of France?
- Mention the bot in a message to initiate a conversation. Example:
@ChatGPT How many planets are there in the solar system?
- The bot will maintain the conversation history within a thread if you reply to its messages.
You can customize the ChatGPT model configuration by editing the constants in index.js
. Available options include:
MAX_TOKENS
: Maximum number of tokens in a responseTEMPERATURE
: Controls randomness of the generated responses (higher values = more random)INITIAL_MESSAGES
: Default initial messages for chat modelCOMPLETION_MODEL_CONFIG
: Configuration for the completion model (used in the slash command)CHAT_COMPLETION_MODEL_CONFIG
: Configuration for the chat model (used in the app mention)