DEV Community

Juan Carlos Feris Gómez
Juan Carlos Feris Gómez

Posted on

Open AI API with Docker

So we are gonna have some Python script to use the
Open AI library and make some prompts. On my first intent
I was having some issues regarding Python not finding the
libraries concerning the lessons I was taking.

So, since I have a lots of problems with Python versioning
and library installing on my m1 because I have a lot of
versions of Python installed in it and I'm too lazy
(have tons of work) to fix them, I created a docker file
to run everything without the sweat.

Setup

First we create a Dockerfile and a requirements.txt:

requirements.txt

openai
IPython
python-dotenv
Enter fullscreen mode Exit fullscreen mode

Dockerfile

FROM python:3.11.3

WORKDIR /app

COPY requirements.txt .

RUN pip install --upgrade pip setuptools

RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["python", "app.py"]
Enter fullscreen mode Exit fullscreen mode

Step by step:

  1. Get the Python version that you need (use at least 3.11)
  2. Tell Docker that it is gonna work on the app directory, this will create a directory of the same name in the container.
  3. Copy a requirements.txt file (it contains a list of libraries to install)
  4. Install pip and setuptools at it's latest vertions
  5. Install the required libraries (remember the file on 3)

Now that we have this two, let's build the image but first
let's create a docker-compose.yml file to also launch our container:

docker-compose.yml

version: "3"
services:
  openai:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      - OPENAI_API_KEY=YOUR_OPEN_AI_API_KEY
Enter fullscreen mode Exit fullscreen mode

Step by step:

  1. Declare the version
  2. Declare a services
  3. Create the openai service
  4. Declare a build pointing to the Dockerfile
  5. Declare the environment variables I'm declaring SCRIPT as an environment variable to make posible to change the script when launching the container.

Prompt script

Let's create an script to test the Open AI API:

app.py

# imports is self describing
import os
from io import StringIO
import openai
from IPython.display import display, HTML
from dotenv import load_dotenv

# get the OPENAI_API_KEY from the environment (docker-compose.yml)
openai.api_key = os.environ.get( 'OPENAI_API_KEY' )

# create a function to get propmt AI response
def get_completion(user_prompt, model="gpt-3.5-turbo"):
    messages = [{"role": "user", "content": user_prompt}]
    ai_response = openai.ChatCompletion.create(
            model=model,
            messages=messages,
            temperature=0,
            )
    return ai_response.choices[0].message["content"]

promp="Name five reasons to become a developer"
Enter fullscreen mode Exit fullscreen mode

Launch the container

docker-compose up --build
Enter fullscreen mode Exit fullscreen mode

Use --build only the first time launching

That's it now you can have a service to get response
from the AI, you can use it to prompt from a chatbot
you build.

Happy prompting!

Top comments (0)