Creating your AI bot with docker
In this article, we will learn how to create an AI bot with Docker. Docker is an application virtualization tool that allows you to easily package and distribute apps in containers. This is useful for AI bot development because it allows the bot code to be easily distributed and run on different environments consistently.
To start, we will create a new project in a directory of your choice. Next, we will create a Dockerfile, which is a simple text file containing instructions for Docker to create a container for your AI bot.
The first step is to define a base image for the container. This can be any operating system image, such as Ubuntu or Debian, that has all the libraries and dependencies needed to run your code. In this case, we will use a image with Python3:
FROM python:3
Next, we need to install the dependencies for your project. This can be done using the operating system’s package manager, such as apt-get on Ubuntu. In this case, we will install the openAI package:
RUN pip install openai
Now, we need to copy your project code into the container. This can be done using the ADD command, followed by the path to your code on the host and the path to where the code should be copied in the container.
ADD code.py /
Finally, we need to define a command to be run when the container is started. This can be done using the ENTRYPOINT command, followed by the command to be executed.
ENTRYPOINT [ "python", "./code.py"]
Now we have our complete Dockerfile which would be:
FROM python:3
RUN pip install openai
ADD code.py /
ENTRYPOINT [ "python", "./code.py"]
This would be an example of python code to access the OpenAI API (the file code.py):
import os
import sys
import openai
openai.api_key = os.getenv("API_KEY")
cmd = ''.join(sys.argv[1:])
response = openai.Completion.create(
model="text-davinci-003",
prompt=cmd,
temperature=0.4,
max_tokens=200,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)
print(response['choices'][0]['text'])
With the Dockerfile created, we can build the container using the “docker build” command. This will create an image of your AI bot that can be easily distributed and run on any environment with Docker installed.
root@pop-os:~/openai# docker build -t luis/openai .
Sending build context to Docker daemon 5.12kB
Step 1/4 : FROM python:3
3: Pulling from library/python
f2f58072e9ed: Already exists
5c8cfbf51e6e: Already exists
aa3a609d1579: Already exists
094e7d9bb04e: Already exists
2cbfd734f382: Already exists
aa86ac293d0f: Already exists
4cffc9f44941: Already exists
ae2c75627c86: Already exists
2d2b74d2f0f7: Already exists
Digest: sha256:11560799e4311fd5abcca7ace13585756d7222ce5471162cd78c78a4ecaf62bd
Status: Downloaded newer image for python:3
.
.
.
Step 4/4 : ENTRYPOINT [ "python", "./code.py"]
---> Running in 99c8f4005359
Removing intermediate container 99c8f4005359
---> d482a8d913b4
Successfully built d482a8d913b4
Successfully tagged luis/openai:latest
In this link you can create your API KEY which will be necessary to make the request to the API:
To run the container, just use the “docker run” command. This will start the container with your AI bot and it will answer your questions.
You must replace the API_KEY variable with your API KEY generated on the OpenAI website.
root@pop-os:~/openai# docker run -e "API_KEY=sk-...tq" luis/openai biggest country in the world?
The largest country in the world by land area is Russia, which covers an area of 17,098,242 square kilometers (6,601,665 square miles).
Another example:
root@pop-os:~/openai# docker run -e "API_KEY=sk-...tq" luis/openai 10 decimal places of the number pi
3.1415926535
Remember that you need to have Docker installed and an account at OpenAI to follow this tutorial. If you don’t have an account at OpenAI yet, just go to the website and follow the instructions to create one.
Photo by Kenny Eliason on Unsplash