How to Set Up AutoGen Studio with Docker

5/5 - (1 vote)

Hi and welcome to this tutorial series on AutoGen Studio. My name is Dirk van Meerveld, and I’ll be your host and guide for this three part tutorial series where we’ll take a look at AutoGen Studio.

πŸ‘‰ Go Back to the Full Course: Next Level Prompt Engineering with AutoGen Studio

AutoGen Studio is an open-source interface that runs on top of AutoGen. This combination will allow us to work with LLMs and give them skills (like tools/functions) that they can use and also allows us to use agents and even define multi-agent workflows. Think of it as a multi-agent framework. One of the most amazing features is that it can write and execute code on the fly to solve problems!

On top of that AutoGen Studio provides us with a sleek and easy-to-use interface to define all the above and chat with our agents, much of it without even having to write code! This truly is next-level prompt engineering and over the coming lessons, we will harness the power of digital agents and see them collaborate with each other!

In this first part, we’ll focus on the setup and take some time to properly get started. If you’re a more seasoned developer and intimately familiar with a particular topic like Docker, feel free to skip ahead a bit here and there, but we’ll cover all the bases so that everybody can follow along nicely. This is especially so as much of AutoGen Studio can be used via the interface which makes it more accessible to non-coders. We will leave no man or woman behind!

AutoGen safety measures

In order to answer more complex requests AutoGen will actually write Python code for you and execute this Python code on your local computer.

For example, there is a demo question where you ask the following question: "Plot a chart of NVDA and TESLA stock price for 2023. Save the result to a file named nvda_tesla.png". AutoGen will solve this question by writing Python code to get the stock prices, install needed libraries to execute the code, write code for creating the graph in MathPlotLib, etc… It will then execute this code on your local computer and return the result to you.

The problem occurs where operating systems of course have some kind of built-in security. Generally, the Execution Policy on your OS will prevent AutoGen (and others) from just running random code and scripts on your computer. And this is a good thing! One solution would be to loosen up the Execution Policy on your computer, but this is not really the best idea. Even if you don’t have an Execution Policy problem on your system, do you really want to have any and all AI-generated code running on your machine without any checks or balances? It’s a bit dangerous, to say the least.

Why we won’t use Anaconda

Many tutorials try to take the 100% no-code approach, using Anaconda for virtual environments and not using an IDE like VS Code altogether in an attempt to claim that ‘You don’t need to know any code at all’. Then you will:

  • Still have to install Docker as well as Anaconda, in order to allow safe code execution inside of a Docker container.
  • Set your API key over and over every time you reload.
  • Type the same Anaconda commands over and over.
  • You might be stuck with a buggy experience trying to get it to use Docker for safe code execution, causing you a lot of frustration.

While there is absolutely nothing wrong with Anaconda, it doesn’t remove the need for Docker for safe code execution as it doesn’t provide any isolation from your local system. So why don’t we just use Docker instead of Anaconda AND Docker? This biting the bullet will make this part 1 of the tutorial a bit harder, but after that, it will be smooth sailing all the way! β›΅

Docker to the rescue

Using this approach we can skip Anaconda altogether as Docker will also be separate from our system-wide Python environment, and we have only 1 piece of software to worry about for both the separate environment and the safe code execution. We will use some bare basic code in the form of a ‘docker file’ and basic commands, but you can just copy mine, so no worries! Again, if you’re already a Docker expert, feel free to just skip ahead to the Dockerfile and get started.

Docker uses operating system-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files. All containers are run by a single operating system kernel and are thus more lightweight than virtual machines. Crudely stated this means that docker will let us have a separate container with Linux, Python, and all the libraries we need to run AutoGen Studio in it, kind of like running a different computer system within a virtual machine.

We will use this ‘virtual machine’, or Docker container, to run AutoGen Studio inside of it, which automatically also means that any code it generates is executed within the container, solving our code execution policy problems and safety concerns in one go. Caveat: Theoretically even Docker is not 100% secure and isolated from the rest of your system, but it’s a lot better than just running code directly on your local machine and good enough for non-enterprise use.

Getting Docker installed and running – WSL2

I’m going to assume you’re on a Windows machine. If you’re on a Mac or Linux machine, you’re in luck and can probably just run the installer for ‘Docker Desktop’ and be done with it. Just head over here and scroll down to select the Docker download for your platform. https://docs.docker.com/desktop/ (or Google for the updated link).

Continuing for the Windows users now, as they have a couple of extra steps to take. Most tutorials will just tell you to install Docker and give no details which might leave you will a hard and long process of figuring out why it won’t work on your system. So let’s cover the pitfalls and make sure we get it right the first time!

First, we need to install WSL2 (Windows Subsystem for Linux 2) on our system. This is a Windows feature that allows us to run Linux on our Windows machine. This is needed because Docker Desktop for Windows requires WSL2 to run. (You can also use Hyper-Vinstead, but WSL2 is the recommended way).

(If you already use WSL but need to check if you have version 2, you can check your version by entering the command: wsl -l -v in PowerShell or Windows Command Prompt.)

You can find the details for installing WSL2 here, but basically you do this:

  • Open a PowerShell or Windows Command Prompt in administrator mode by right-clicking and selecting “Run as administrator”
  • Run the following command in the terminal: wsl --install
  • Restart your computer when prompted

This will enable WSL on your Windows machine and install a Linux distribution (Ubuntu) on your system. You may need to create a Username and Password for your Linux distribution and save them somewhere. More details can be found here or google "Setting up your Linux username and password."

Some things to check before we install Docker Desktop

A quick caveat: If anywhere along the way you run into problems I haven’t covered here, you’ll need to do some googling to find out what it means and what needs to be done, as I cannot prepare you for every possible problem you might run into.

The software development world sometimes requires messing around and googling for an hour or even two before you get something to work, it’s part of the game. That being said I will try my best to cover all bases so you’ll hopefully have a very smooth experience πŸ˜€.

We have a couple more things to check before we can install Docker Desktop. First, we need to make sure the Virtual Machine Platform option is enabled. This is fairly easy! Just press the Windows key or click in the Windows search bar and search for “Turn Windows features on or off”.

Open this and make sure the “Virtual Machine Platform” option is checked. If it’s not, check it and click OK. You may need to restart your computer after this.

(You don’t have to match the other checkboxes with the settings in the image!)

Next, we need to make sure that Virtualization is enabled in our BIOS. The easiest way to check if this is enabled is to open the Windows Task Manager by pressing Ctrl+Alt+Delete and selecting Task Manager. Then click on the Performance tab and click on CPU on the left-hand side. If Virtualization is enabled, you will see “Virtualization: Enabled” in the bottom right information block like this:

If Virtualization is not enabled, you will have to go into your BIOS settings and enable Virtualization. This is where you will have to do some googling and research on your own, as every system has slightly different keys to get into the BIOS setup menu, and the settings may be located in different parts of the BIOS menu for different manufacturers. I’ll leave you with two links to get you started, the first one describes the general process of getting this setting enabled in your BIOS:

When you’ve got that ready and set up to go, let’s continue on.

Installing Docker Desktop πŸš€

Finally, it’s time! Head over to the Docker Desktop download page here and download the appropriate Docker version for your OS. I’m running Docker 4.26.1, but just download the latest version and you should be fine. When the download finishes start the installer. The installer will give you the following options:

Just accept both of these options and click OK, unless you chose not to install WSL 2 and use Hyper-V instead. Whether or not you want a shortcut on your desktop is entirely up to you of course πŸ˜‰.

Now just let the installer do its magic:

Done!

Then go ahead and run the Docker Desktop application, where you’ll have to accept the service agreement:

And then just choose “Use recommended settings” and click Finish:

Now Docker Desktop will start and you will be prompted to either sign up or sign in. Docker is free for personal and even small-business use, so press the button to sign up and create an account. You can even use your Google or GitHub account to create one really fast. (You can also continue without signing in if you want to, and it should still work fine). I’m just going to go ahead and sign in with my Google account.

If everything was successful, you should be greeted by the following screen:

Congratulations! You’ve now installed Docker Desktop and are ready to go! If you still have problems, first try the below, and if that doesn’t work, google will have a solution. Never despair!

(Only for those who still have problems πŸ™ˆπŸ™‚)

- Hypervisor enabled at Windows startup -
If you have completed the steps described above and are still experiencing Docker Desktop startup issues, this could be because the Hypervisor is installed, but not launched during Windows startup. Some tools (such as older versions of Virtual Box) and video game installers turn off hypervisor on boot. To turn it back on:

    - Open an administrative console prompt.
    - Run bcdedit /set hypervisorlaunchtype auto.
    - Restart Windows.

Creating a Dockerfile

Ok, now that we all have Docker Desktop installed and running, let’s move on to the next step! I’ll be using VS Code for this, just because it feels convenient to me. You can also use any other code editor or literally just copy the text into Notepad and use a separate terminal window, it makes no difference.

Create a base project directory and open it in VS Code. I’ll simply call my directory AUTOGEN:

πŸ“AUTOGEN (root project folder)

Now inside the AUTOGEN folder create a new file called Dockerfile.base:

    πŸ“AUTOGEN (root project folder)
        πŸ“„Dockerfile.base

What is a Dockerfile? A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image and ultimately run a Docker container. Think of this dockerfile as a recipe that Docker will follow to build a custom image that can be used to create new containers.

In a Dockerfile, you can specify the base image to use, define the working directory, copy files from your local system to the container, run commands to install packages, expose ports for the application, and specify the command that should be run when a container is launched from the image.

So open up your Dockerfile.base, and let’s type out our Docker recipe! This will be mostly based on the example recommendations from AutoGen itself, with some minor tweaks. First, let’s specify the base image:

FROM python:3.11-slim-bookworm

This will use the official Python image from Docker Hub, which is based on Debian Linux. We’ll use the slim-bookworm version, which is a lightweight version of Debian Linux. We’ll also use Python 3.11, which is in the range of current recommended versions for AutoGen at the time of writing.

Then continue in your Dockerfile.base with the following:

RUN apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        software-properties-common sudo\
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

I’m going to explain what all the Docker commands do as these topics are interesting and very helpful as a developer, but if you’re not interested in Dockerfiles and Linux commands and don’t want to do any coding stuff, you have my blessings to skip the explanations and just scroll down to the finished Dockerfile at the end of this section and copy it into your own Dockerfile.base πŸ˜‰.

  • RUN is a Dockerfile command that executes a command in the container. In this case, we’re running the apt-get update command, which refreshes the local package index with the latest versions. && is a command separator, which lets us chain commands.
  • DEBIAN_FRONTEND=noninteractive is an environment variable that is set to noninteractive to prevent the apt-get install command from asking any questions during the installation and just apply default settings.
  • apt-get install is the command to install packages. The -y flag is used to automatically answer yes to any questions that may come up during the installation process. The --no-install-recommends flag is used to prevent the installation of any recommended packages that are not strictly required for the package to function, keeping it light.
  • software-properties-common is a package that provides utilities for managing software repositories. and sudo is a utility that allows us to run commands as a superuser.
  • apt-get clean is a command that cleans up the local repository of retrieved package files, and rm -rf /var/lib/apt/lists/* is a command that removes the package lists that were downloaded during the apt-get update command. This is done to save disk space.

Next in our Dockerfile, we’ll set up a root user with superuser access:

RUN adduser --disabled-password --gecos '' autogen
RUN adduser autogen sudo
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
USER autogen
WORKDIR /home/autogen

The first line creates a new user named autogen without a password and without prompting for additional information, whereas --gecos '' simply sets the user info to an empty string. The next line adds the user autogen to the sudo group, granting it administrative privileges.

The third line configures the sudo group to allow members to execute commands as root without requiring a password. After that, we switch to the autogen user and set the working directory to /home/autogen.

Next up:

ENV PATH="/home/autogen/.local/bin:$PATH"
ENV OPENAI_API_KEY=paste_your_api_key_here

First, we set the PATH environment variable (using Docker’s ENV command) to include the .local/bin directory in the autogen user’s home directory.

Then we set the OPENAI_API_KEY environment variable to the API key that we got from OpenAI. This is needed so that AutoGen Studio can access the OpenAI API.

Make sure you paste your own ChatGPT API key in there instead of paste_your_api_key_here, making sure not to add "" double quotes or anything. You create new or extra keys by going to the OpenAI API keys page:

Ok now to continue in our Dockerfile:

RUN pip install pyautogen==0.2.8 autogenstudio==0.0.34a0 numpy pandas matplotlib seaborn scikit-learn requests urllib3 nltk pillow pytest beautifulsoup4

We just pre-install some of the popular packages and of course pyautogen and autogenstudio themselves. I have specified two specific versions for pyautogen and autogenstudio, as these are the versions I’m using when writing this tutorial. I advise you to use the same ones to make sure you have exactly the same experience as me. You can always upgrade the packages after you finish the tutorial series. (Don’t worry, at the end of the last part I’ll show you how and you won’t lose any of your work!)

Now next up in our Dockerfile:

# Expose port
EXPOSE 8081

# Start Command for AutoGen Studio
CMD ["autogenstudio", "ui", "--host", "0.0.0.0", "--port", "8081"]

The EXPOSE command exposes port 8081, which is the port that AutoGen Studio will run on. The CMD command specifies the command that will be run when the container is launched from the image. In this case, we’re running the autogenstudio command with the ui option, which will start the AutoGen Studio interface. The --host and --port options specify the host and port that the interface will be available on. This means we’ll only have to start the container and AutoGen Studio will be available on port 8081 automatically!

Note that we used the address 0.0.0.0 instead of the usual localhost 127.0.0.1. If we bind to the 127.. version it’s only accessible from the same machine – in this case, the same Docker container. To make our application accessible from outside the Docker container, you need to bind it to 0.0.0.0 instead. This will make it accessible from any IP address, including from your host machine.

To finish off our Dockerfile, we’ll add a comment at the bottom for our own future reference:

# command to build the image:
    # docker build -t autogenstudio -f Dockerfile.base .
# command to run the container:
    # docker run -it --rm -p 8081:8081 --name autogenstudio autogenstudio
# Access AutoGen Studio at http://localhost:8081 make sure you don't click the 0.0.0.0:8081 link in the terminal, it won't work!

This is just a comment that you can use for future reference so you don’t have to search the tutorial to find the command to build and run the container, we’ll use and explain these commands in a second, just copy them for your future reference.

Building the Docker image and running the container

Your whole Dockerfile.base should now look like this:

FROM python:3.11-slim-bookworm

RUN apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        software-properties-common sudo\
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Setup a non-root user 'autogen' with sudo access
RUN adduser --disabled-password --gecos '' autogen
RUN adduser autogen sudo
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
USER autogen
WORKDIR /home/autogen

# Set environment variable
ENV PATH="/home/autogen/.local/bin:$PATH"
# Follow the = with your OpenAI API key (no quotes or anything, just OPENAI_API_KEY=sk-lotsOfLettersAndNumbers)
ENV OPENAI_API_KEY=

# Pre-load popular packages
RUN pip install pyautogen==0.2.8 autogenstudio==0.0.34a0 numpy pandas matplotlib seaborn scikit-learn requests urllib3 nltk pillow pytest beautifulsoup4

# Expose port
EXPOSE 8081

# Start Command for AutoGen Studio
CMD ["autogenstudio", "ui", "--host", "0.0.0.0", "--port", "8081"]

# command to build the image:
    # docker build -t autogenstudio -f Dockerfile.base .
# command to run the container:
    # docker run -it --rm -p 8081:8081 --name autogenstudio autogenstudio
# Access AutoGen Studio at http://localhost:8081 make sure you don't click the 0.0.0.0:8081 link in the terminal, it won't work!

Ok, so with that out of the way, first make sure that the Docker Desktop application is running, as we’ll need the Docker Engine process to be up and running. Then open up a terminal window and make sure you’re in the root project folder, in my case AUTOGEN:

admin@DirkMasterPC /c/Coding_Vault/AUTOGEN

Then run the following command to build the image:

docker build -t autogenstudio -f Dockerfile.base .

This will build the image and tag (-t) it with the name autogenstudio. The -f flag specifies the name of the Dockerfile to use, in this case Dockerfile.base. The . at the end specifies the build context, which is the current directory, as this is where our Dockerfile.base is located.

The Docker Image will be built and you will see the progress running through each of the steps in the Dockerfile. When it is done we can run the following command to start the container which will automatically run AutoGen Studio:

docker run -it --rm -p 8081:8081 --name autogenstudio autogenstudio

This works by running the docker run command, which creates a new container from the autogenstudio image that we just built. The -it flag specifies that we want to run the container in interactive mode, which means we can interact with the container via the terminal.

The --rm flag specifies that we want the container to be removed when it is stopped. The -p flag specifies that we want to map port 8081 from the container to port 8081 on our local machine. The --name flag specifies the name of the container, in this case, autogenstudio.

Now bring up your Docker Desktop application and open up the Containers tab (topmost icon in the left menu bar):

We can see that our container by the name autogenstudio, based on the image autogenstudio is running on port 8081! Hooray! πŸŽ‰

Victory!

You can either click on the blue 8081:8081 link to open up AutoGen Studio in your browser, or just open up your browser and go to http://localhost:8081. There is also a link in your terminal window that says that Uvicorn is running on http://0.0.0.0:8081. Do not click this link as the 0.0.0.0:8081 address is only accessible from within the Docker container and you are outside of it, so it will not work.

Give yourself a pat on the back for a job well done! You’ve set up AutoGen Studio properly and inside a Docker container! One quick sidenote, when we wrote the Dockerfile.base we hardcoded the ChatGPT API key into the Dockerfile to keep things as simple as possible. We can of course avoid this but this would complicate the tutorial further and I want to keep this one as low-code as possible.

Just make sure you do not share the Dockerfile.base or remove your API key from it first as anyone with your API key can obviously use OpenAI’s API on your credit. Your Docker Image also has the API key hardcoded into it. There is no reason you’d ever really want to share the image though, just don’t leave copies behind on a public computer or something.

That’s it for part 1! I hope it wasn’t too difficult. From now on the fun part begins. I’ll see you in part 2 where we’ll dive straight into AutoGen Studio. 😎

πŸ‘‰ Go Back to the Full Course: Next Level Prompt Engineering with AutoGen Studio