The “Prompt Engineering for Generative AI” book code examples require Python 3.9.12, which is not available by default in Ubuntu 24.04. You could add a new repository like ppa:deadsnakes/ppa and install this specific version, but this could create a lot of problems in the future if you also use Python for other projects.

The best way that I found to solve this problem was to use asdf, that allows to install multiple versions of a package (a very common problem with Node.js). Below is a step-by-step guide of what I did.

Installing dependencies

During the process, I got a lot of errors and warnings due to missing dependencies. Here is the list that should solve asll asdf and Python dependencies.

sudo apt-get install make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl git llvm \
libncurses5-dev libncursesw5-dev xz-utils tk-dev libxml2-dev \
libxmlsec1-dev libffi-dev liblzma-dev
Bash

Installing asdf

Just follow the instructions in the official getting started guide, downloading the git repository and adding two lines in your .bashrc file.

Install Python with asdf

First, install the Python plugin for asdf.

asdf plugin-add python
Bash

Then, install the version used for the book examples.

asdf install python 3.9.12
Bash

To check if the installation worked, run ls ~/.asdf/installs/python/ and there should be a directory with the number of the version.

Setting up the environment

Clone the book examples with:

git clone https://github.com/BrightPool/prompt-engineering-for-generative-ai-examples.git
Bash

Enter the examples folder and then set the Python version for the current directory with:

asdf local python 3.9.12
Bash

To confirm if you are using the right version, run python --version.

Now create a Virtual Environment, and activate it.

python -m venv .venv

source venv/bin/activate
Bash

Finally, install the dependencies (this will take some time):

pip install -r requirements.txt
Bash

Setting API keys

Create a .env file with your keys. Below is an example of the file content.

OPENAI_API_KEY=your-key-here
Bash

In the code, make sure you are using the following two codes.

# Code to load the library
from dotenv import load_dotenv
load_dotenv()

# Code to load the key
api_key=os.environ['OPENAI_API_KEY']
Python

Running Jupyter Notebook

To run Jupyter Notebook I was more successful using the Jupyter VS Code Extension instead of using the browser version. This was my first time using it, and to run it, you just need to click on the play icon next to the code.

When you run it for the first time, it will ask to choose a Kernel, I picked Python Environments... and them for the Python Environment I chose venv (Python 3.9.12).



Comments

Leave a Reply

Your email address will not be published. Required fields are marked *