Dall-E Slack Slash Command
Fri 30 December 2022I've been looking into OpenAI's API platform, which includes text completion, code completion, text processing, and more. This post will use the Images API to generate images from a text prompt, using the wildly popular Dall-E model (try it here), and, as an added bonus, we will deploy it in Slack as a slash command.
The Images API is not free, although you can sign up for a 30-day trial. It is reasonably priced with careful consideration for how it will be used. In our case, we will allow individual requests of images sized 256 x 256 pixels, so that will be $0.016 per request (as of writing).
You can sign up for OpenAI's APIs here.
Within your account, create an API key and copy it for later. You can also set a monthly billing limit, just for added peace of mind.
We will use a Slack slash command as the front end for this app. The basic steps are:
- Create a slash command through the Slack UI (instructions). Because the app uses modals, you must enable interactivity once you've created the slash command. You can enter any value for the slash command and interactivity request URLs (we will create them later). Install the slash command to your Slack workspace, and make note of the
Bot User OAuth Token
which will be used in the environment variables below. - Create the backend app. I used the template repo described here to create the backend (repo), which consists of an AWS API Gateway and serverless Lambda Functions.
- Include some fanciness to install the
openai
andnumpy
dependencies in theworker
lambda function as Lambda Layers. - Modify the
worker
lambda to include a request to the OpenAI Images API, and return the image to Slack (code). - Deploy. The repo uses CDK as the deployment mechanism, so once you've modified your environment variables and anything else to your liking, simply run
cdk deploy
to deploy to AWS. Once deployed, you should be provided the API URL, which you can use as the slash command and interactivity request URLs above.
My backend app repo is here.
The final app looks like so:
That's all. Have fun using Dall-E (or anything else) in Slack through slash commands! Get in touch with me on Twitter or Mastodon if you want to talk more about it!