10 changed files with 1149 additions and 272 deletions
@ -0,0 +1,177 @@ |
|||||||
|
# LLM Engineering - Master AI and LLMs |
||||||
|
|
||||||
|
## Setup instructions for Windows |
||||||
|
|
||||||
|
Welcome, PC people! |
||||||
|
|
||||||
|
I should confess up-front: setting up a powerful environment to work at the forefront of AI is not as simple as I'd like. For most people these instructions will go great; but in some cases, for whatever reason, you'll hit a problem. Please don't hesitate to reach out - I am here to get you up and running quickly. There's nothing worse than feeling _stuck_. Message me, email me or LinkedIn message me and I will unstick you quickly! |
||||||
|
|
||||||
|
Email: ed@edwarddonner.com |
||||||
|
LinkedIn: https://www.linkedin.com/in/eddonner/ |
||||||
|
|
||||||
|
I use a platform called Anaconda to set up your environment. It's a powerful tool that builds a complete science environment. Anaconda ensures that you're working with the right version of Python and all your packages are compatible with mine, even if our systems are completely different. It takes more time to set up, and it uses more hard drive space (5+ GB) but it's very reliable once its working. |
||||||
|
|
||||||
|
Having said that: if you have any problems with Anaconda, I've provided an alternative approach. It's faster and simpler and should have you running quickly, with less of a guarantee around compatibility. |
||||||
|
|
||||||
|
### Part 1: Clone the Repo |
||||||
|
|
||||||
|
This gets you a local copy of the code on your box. |
||||||
|
|
||||||
|
1. **Install Git** (if not already installed): |
||||||
|
|
||||||
|
- Download Git from https://git-scm.com/download/win |
||||||
|
- Run the installer and follow the prompts, using default options (press OK lots of times!) |
||||||
|
|
||||||
|
2. **Open Command Prompt:** |
||||||
|
|
||||||
|
- Press Win + R, type `cmd`, and press Enter |
||||||
|
|
||||||
|
3. **Navigate to your projects folder:** |
||||||
|
|
||||||
|
If you have a specific folder for projects, navigate to it using the cd command. For example: |
||||||
|
`cd C:\Users\YourUsername\Documents\Projects` |
||||||
|
Replacing YourUsername with your actual Windows user |
||||||
|
|
||||||
|
If you don't have a projects folder, you can create one: |
||||||
|
``` |
||||||
|
mkdir C:\Users\YourUsername\Documents\Projects |
||||||
|
cd C:\Users\YourUsername\Documents\Projects |
||||||
|
``` |
||||||
|
|
||||||
|
4. **Clone the repository:** |
||||||
|
|
||||||
|
Enter this in the command prompt in the Projects folder: |
||||||
|
|
||||||
|
`git clone https://github.com/ed-donner/llm_engineering.git` |
||||||
|
|
||||||
|
This creates a new directory `llm_engineering` within your Projects folder and downloads the code for the class. Do `cd llm_engineering` to go into it. This `llm_engineering` directory is known as the "project root directory". |
||||||
|
|
||||||
|
### Part 2: Install Anaconda environment |
||||||
|
|
||||||
|
There is an alternative to Part 2 if this gives you problems. |
||||||
|
|
||||||
|
1. **Install Anaconda:** |
||||||
|
|
||||||
|
- Download Anaconda from https://docs.anaconda.com/anaconda/install/windows/ |
||||||
|
- Run the installer and follow the prompts. Note that it takes up several GB and take a while to install, but it will be a powerful platform for you to use in the future. |
||||||
|
|
||||||
|
2. **Set up the environment:** |
||||||
|
|
||||||
|
- Open **Anaconda Prompt** (search for it in the Start menu) |
||||||
|
- Navigate to the "project root directory" by entering something like `cd C:\Users\YourUsername\Documents\Projects\llm_engineering` using the actual path to your llm_engineering project root directory. Do a `dir` and check you can see subdirectories for each week of the course. |
||||||
|
- Create the environment: `conda env create -f environment.yml` |
||||||
|
- Wait for a few minutes for all packages to be installed - in some cases, this can literally take 20-30 minutes if you've not used Anaconda before, and even longer depending on your internet connection. Important stuff is happening! If this runs for more than 1 hour 15 mins, or gives you other problems, please go to Part 2B instead. |
||||||
|
- You have now built an isolated, dedicated AI environment for engineering LLMs, running vector datastores, and so much more! You now need to **activate** it using this command: `conda activate llms` |
||||||
|
|
||||||
|
You should see `(llms)` in your prompt, which indicates you've activated your new environment. |
||||||
|
|
||||||
|
3. **Start Jupyter Lab:** |
||||||
|
|
||||||
|
- In the Anaconda Prompt, from within the `llm_engineering` folder, type: `jupyter lab` |
||||||
|
|
||||||
|
...and Jupyter Lab should open up in a browser. If you've not seen Jupyter Lab before, I'll explain it in a moment! Now close the jupyter lab browser tab, and close the Anaconda prompt, and move on to Part 3. |
||||||
|
|
||||||
|
### Part 2B - Alternative to Part 2 if Anaconda gives you trouble |
||||||
|
|
||||||
|
1. **Open Command Prompt** |
||||||
|
|
||||||
|
Press Win + R, type `cmd`, and press Enter |
||||||
|
|
||||||
|
Run `python --version` to find out which python you're on. Ideally you'd be using a version of Python 3.11, so we're completely in sync. |
||||||
|
If not, it's not a big deal, but we might need to come back to this later if you have compatibility issues. |
||||||
|
You can download python here: |
||||||
|
https://www.python.org/downloads/ |
||||||
|
|
||||||
|
2. Navigate to the "project root directory" by entering something like `cd C:\Users\YourUsername\Documents\Projects\llm_engineering` using the actual path to your llm_engineering project root directory. Do a `dir` and check you can see subdirectories for each week of the course. |
||||||
|
|
||||||
|
Then, create a new virtual environment with this command: |
||||||
|
`python -m venv llms` |
||||||
|
|
||||||
|
3. Activate the virtual environment with |
||||||
|
`llms\Scripts\activate` |
||||||
|
You should see (llms) in your command prompt, which is your sign that things are going well. |
||||||
|
|
||||||
|
4. Run `pip install -r requirements.txt` |
||||||
|
This may take a few minutes to install. |
||||||
|
|
||||||
|
5. **Start Jupyter Lab:** |
||||||
|
|
||||||
|
From within the `llm_engineering` folder, type: `jupyter lab` |
||||||
|
...and Jupyter Lab should open up, ready for you to get started. Open the `week1` folder and double click on `day1.ipynb`. Success! Now close down jupyter lab and move on to Part 3. |
||||||
|
|
||||||
|
If there are any problems, contact me! |
||||||
|
|
||||||
|
### Part 3 - OpenAI key (OPTIONAL but recommended) |
||||||
|
|
||||||
|
Particularly during weeks 1 and 2 of the course, you'll be writing code to call the APIs of Frontier models (models at the forefront of AI). |
||||||
|
|
||||||
|
For week 1, you'll only need OpenAI, and you can add the others if you wish later on. |
||||||
|
|
||||||
|
1. Create an OpenAI account if you don't have one by visiting: |
||||||
|
https://platform.openai.com/ |
||||||
|
|
||||||
|
2. OpenAI asks for a minimum credit to use the API. For me in the US, it's \$5. The API calls will spend against this \$5. On this course, we'll only use a small portion of this. I do recommend you make the investment as you'll be able to put it to excellent use. But if you'd prefer not to pay for the API, I give you an alternative in the course using Ollama. |
||||||
|
|
||||||
|
You can add your credit balance to OpenAI at Settings > Billing: |
||||||
|
https://platform.openai.com/settings/organization/billing/overview |
||||||
|
|
||||||
|
I recommend you disable the automatic recharge! |
||||||
|
|
||||||
|
3. Create your API key |
||||||
|
|
||||||
|
The webpage where you set up your OpenAI key is at https://platform.openai.com/api-keys - press the green 'Create new secret key' button and press 'Create secret key'. Keep a record of the API key somewhere private; you won't be able to retrieve it from the OpenAI screens in the future. It should start `sk-proj-`. |
||||||
|
|
||||||
|
In week 2 we will also set up keys for Anthropic and Google, which you can do here when we get there. |
||||||
|
- Claude API at https://console.anthropic.com/ from Anthropic |
||||||
|
- Gemini API at https://ai.google.dev/gemini-api from Google |
||||||
|
|
||||||
|
Later in the course you'll be using the fabulous HuggingFace platform; an account is available for free at https://huggingface.co - you can create an API token from the Avatar menu >> Settings >> Access Tokens. |
||||||
|
|
||||||
|
And in Week 6/7 you'll be using the terrific Weights & Biases at https://wandb.ai to watch over your training batches. Accounts are also free, and you can set up a token in a similar way. |
||||||
|
|
||||||
|
### PART 4 - .env file |
||||||
|
|
||||||
|
When you have these keys, please create a new file called `.env` in your project root directory. The filename needs to be exactly the four characters ".env" rather than "my-keys.env" or ".env.txt". Here's how to do it: |
||||||
|
|
||||||
|
1. Open the Notepad (Windows + R to open the Run box, enter `notepad`) |
||||||
|
|
||||||
|
2. In the Notepad, type this, replacing xxxx with your API key (starting `sk-proj-`). |
||||||
|
|
||||||
|
``` |
||||||
|
OPENAI_API_KEY=xxxx |
||||||
|
``` |
||||||
|
|
||||||
|
If you have other keys, you can add them too, or come back to this in future weeks: |
||||||
|
``` |
||||||
|
GOOGLE_API_KEY=xxxx |
||||||
|
ANTHROPIC_API_KEY=xxxx |
||||||
|
HF_TOKEN=xxxx |
||||||
|
``` |
||||||
|
|
||||||
|
Double check there are no spaces before or after the `=` sign, and no spaces at the end of the key. |
||||||
|
|
||||||
|
3. Go to File > Save As. In the "Save as type" dropdown, select All Files. In the "File name" field, type exactly **.env** as the filename. Choose to save this in the project root directory (the folder called `llm_engineering`) and click Save. |
||||||
|
|
||||||
|
4. Navigate to the foler where you saved the file in Explorer and ensure it was saved as ".env" not ".env.txt" - if necessary rename it to ".env" - you might need to ensure that "Show file extensions" is set to "On" so that you see the file extensions. Message or email me if that doesn't make sense! |
||||||
|
|
||||||
|
This file won't appear in Jupyter Lab because jupyter hides files starting with a dot. This file is listed in the `.gitignore` file, so it won't get checked in and your keys stay safe. |
||||||
|
|
||||||
|
### Part 5 - Showtime!! |
||||||
|
|
||||||
|
- Open **Anaconda Prompt** (search for it in the Start menu) |
||||||
|
|
||||||
|
- Navigate to the "project root directory" by entering something like `cd C:\Users\YourUsername\Documents\Projects\llm_engineering` using the actual path to your llm_engineering project root directory. Do a `dir` and check you can see subdirectories for each week of the course. |
||||||
|
|
||||||
|
- Activate your environment with `conda activate llms` (or `llms\Scripts\activate` if you used the alternative approach in Part 2B) |
||||||
|
|
||||||
|
- You should see (llms) in your prompt which is your sign that all is well. And now, type: `jupyter lab` and Jupyter Lab should open up, ready for you to get started. Open the `week1` folder and double click on `day1.ipynb`. |
||||||
|
|
||||||
|
And you're off to the races! |
||||||
|
|
||||||
|
Note that any time you start jupyter lab in the future, you'll need to follow these Part 5 instructions to start it from within the `llm_engineering` directory with the `llms` environment activated. |
||||||
|
|
||||||
|
For those new to Jupyter Lab / Jupyter Notebook, it's a delightful Data Science environment where you can simply hit shift+return in any cell to run it; start at the top and work your way down! There's a notebook in the week1 folder with a [Guide to Jupyter Lab](week1/Guide%20to%20Jupyter.ipynb), and an [Intermediate Python](week1/Intermediate%20Python.ipynb) tutorial, if that would be helpful. When we move to Google Colab in Week 3, you'll experience the same interface for Python runtimes in the cloud. |
||||||
|
|
||||||
|
If you have any problems, I've included a notebook in week1 called [troubleshooting.ipynb](week1/troubleshooting.ipynb) to figure it out. |
||||||
|
|
||||||
|
Please do message me or email me at ed@edwarddonner.com if this doesn't work or if I can help with anything. I can't wait to hear how you get on. |
Binary file not shown.
@ -0,0 +1,182 @@ |
|||||||
|
# LLM Engineering - Master AI and LLMs |
||||||
|
|
||||||
|
## Setup instructions for Mac |
||||||
|
|
||||||
|
Welcome, Mac people! |
||||||
|
|
||||||
|
I should confess up-front: setting up a powerful environment to work at the forefront of AI is not as simple as I'd like. For most people these instructions will go great; but in some cases, for whatever reason, you'll hit a problem. Please don't hesitate to reach out - I am here to get you up and running quickly. There's nothing worse than feeling _stuck_. Message me, email me or LinkedIn message me and I will unstick you quickly! |
||||||
|
|
||||||
|
Email: ed@edwarddonner.com |
||||||
|
LinkedIn: https://www.linkedin.com/in/eddonner/ |
||||||
|
|
||||||
|
I use a platform called Anaconda to set up your environment. It's a powerful tool that builds a complete science environment. Anaconda ensures that you're working with the right version of Python and all your packages are compatible with mine, even if our systems are completely different. It takes more time to set up, and it uses more hard drive space (5+ GB) but it's very reliable once its working. |
||||||
|
|
||||||
|
Having said that: if you have any problems with Anaconda, I've provided an alternative approach. It's faster and simpler and should have you running quickly, with less of a guarantee around compatibility. |
||||||
|
|
||||||
|
### Part 1: Clone the Repo |
||||||
|
|
||||||
|
This gets you a local copy of the code on your box. |
||||||
|
|
||||||
|
1. **Install Git** if not already installed (it will be in most cases) |
||||||
|
|
||||||
|
- Open Terminal (Applications > Utilities > Terminal) |
||||||
|
- Type `git --version` If not installed, you'll be prompted to install it |
||||||
|
|
||||||
|
2. **Navigate to your projects folder:** |
||||||
|
|
||||||
|
If you have a specific folder for projects, navigate to it using the cd command. For example: |
||||||
|
`cd ~/Documents/Projects` |
||||||
|
|
||||||
|
If you don't have a projects folder, you can create one: |
||||||
|
``` |
||||||
|
mkdir ~/Documents/Projects |
||||||
|
cd ~/Documents/Projects |
||||||
|
``` |
||||||
|
|
||||||
|
3. **Clone the repository:** |
||||||
|
|
||||||
|
Enter this in the terminal in the Projects folder: |
||||||
|
|
||||||
|
`git clone https://github.com/ed-donner/llm_engineering.git` |
||||||
|
|
||||||
|
This creates a new directory `llm_engineering` within your Projects folder and downloads the code for the class. Do `cd llm_engineering` to go into it. This `llm_engineering` directory is known as the "project root directory". |
||||||
|
|
||||||
|
### Part 2: Install Anaconda environment |
||||||
|
|
||||||
|
There is an alternative to Part 2 if this gives you problems. |
||||||
|
|
||||||
|
1. **Install Anaconda:** |
||||||
|
|
||||||
|
- Download Anaconda from https://docs.anaconda.com/anaconda/install/mac-os/ |
||||||
|
- Double-click the downloaded file and follow the installation prompts. Note that it takes up several GB and take a while to install, but it will be a powerful platform for you to use in the future. |
||||||
|
|
||||||
|
2. **Set up the environment:** |
||||||
|
|
||||||
|
- Open a new Terminal (Applications > Utilities > Terminal) |
||||||
|
- Navigate to the "project root directory" using `cd ~/Documents/Projects/llm_engineering` (replace this path as needed with the actual path to the llm_engineering directory, your locally cloned version of the repo). Do `ls` and check you can see subdirectories for each week of the course. |
||||||
|
- Create the environment: `conda env create -f environment.yml` |
||||||
|
- Wait for a few minutes for all packages to be installed - in some cases, this can literally take 20-30 minutes if you've not used Anaconda before, and even longer depending on your internet connection. Important stuff is happening! If this runs for more than 1 hour 15 mins, or gives you other problems, please go to Part 2B instead. |
||||||
|
- You have now built an isolated, dedicated AI environment for engineering LLMs, running vector datastores, and so much more! You now need to **activate** it using this command: `conda activate llms` |
||||||
|
|
||||||
|
You should see `(llms)` in your prompt, which indicates you've activated your new environment. |
||||||
|
|
||||||
|
3. **Start Jupyter Lab:** |
||||||
|
|
||||||
|
- In the Terminal window, from within the `llm_engineering` folder, type: `jupyter lab` |
||||||
|
|
||||||
|
...and Jupyter Lab should open up in a browser. If you've not seen Jupyter Lab before, I'll explain it in a moment! Now close the jupyter lab browser tab, and close the Terminal, and move on to Part 3. |
||||||
|
|
||||||
|
### Part 2B - Alternative to Part 2 if Anaconda gives you trouble |
||||||
|
|
||||||
|
1. **Open a new Terminal** (Applications > Utilities > Terminal) |
||||||
|
|
||||||
|
Run `python --version` to find out which python you're on. Ideally you'd be using a version of Python 3.11, so we're completely in sync. |
||||||
|
If not, it's not a big deal, but we might need to come back to this later if you have compatibility issues. |
||||||
|
You can download python here: |
||||||
|
https://www.python.org/downloads/ |
||||||
|
|
||||||
|
2. Navigate to the "project root directory" using `cd ~/Documents/Projects/llm_engineering` (replace this path with the actual path to the llm_engineering directory, your locally cloned version of the repo). Do `ls` and check you can see subdirectories for each week of the course. |
||||||
|
|
||||||
|
Then, create a new virtual environment with this command: |
||||||
|
`python -m venv llms` |
||||||
|
|
||||||
|
3. Activate the virtual environment with |
||||||
|
`source llms/bin/activate` |
||||||
|
You should see (llms) in your command prompt, which is your sign that things are going well. |
||||||
|
|
||||||
|
4. Run `pip install -r requirements.txt` |
||||||
|
This may take a few minutes to install. |
||||||
|
|
||||||
|
5. **Start Jupyter Lab:** |
||||||
|
|
||||||
|
From within the `llm_engineering` folder, type: `jupyter lab` |
||||||
|
...and Jupyter Lab should open up, ready for you to get started. Open the `week1` folder and double click on `day1.ipynb`. Success! Now close down jupyter lab and move on to Part 3. |
||||||
|
|
||||||
|
If there are any problems, contact me! |
||||||
|
|
||||||
|
### Part 3 - OpenAI key (OPTIONAL but recommended) |
||||||
|
|
||||||
|
Particularly during weeks 1 and 2 of the course, you'll be writing code to call the APIs of Frontier models (models at the forefront of AI). |
||||||
|
|
||||||
|
For week 1, you'll only need OpenAI, and you can add the others if you wish later on. |
||||||
|
|
||||||
|
1. Create an OpenAI account if you don't have one by visiting: |
||||||
|
https://platform.openai.com/ |
||||||
|
|
||||||
|
2. OpenAI asks for a minimum credit to use the API. For me in the US, it's \$5. The API calls will spend against this \$5. On this course, we'll only use a small portion of this. I do recommend you make the investment as you'll be able to put it to excellent use. But if you'd prefer not to pay for the API, I give you an alternative in the course using Ollama. |
||||||
|
|
||||||
|
You can add your credit balance to OpenAI at Settings > Billing: |
||||||
|
https://platform.openai.com/settings/organization/billing/overview |
||||||
|
|
||||||
|
I recommend you disable the automatic recharge! |
||||||
|
|
||||||
|
3. Create your API key |
||||||
|
|
||||||
|
The webpage where you set up your OpenAI key is at https://platform.openai.com/api-keys - press the green 'Create new secret key' button and press 'Create secret key'. Keep a record of the API key somewhere private; you won't be able to retrieve it from the OpenAI screens in the future. It should start `sk-proj-`. |
||||||
|
|
||||||
|
In week 2 we will also set up keys for Anthropic and Google, which you can do here when we get there. |
||||||
|
- Claude API at https://console.anthropic.com/ from Anthropic |
||||||
|
- Gemini API at https://ai.google.dev/gemini-api from Google |
||||||
|
|
||||||
|
Later in the course you'll be using the fabulous HuggingFace platform; an account is available for free at https://huggingface.co - you can create an API token from the Avatar menu >> Settings >> Access Tokens. |
||||||
|
|
||||||
|
And in Week 6/7 you'll be using the terrific Weights & Biases at https://wandb.ai to watch over your training batches. Accounts are also free, and you can set up a token in a similar way. |
||||||
|
|
||||||
|
### PART 4 - .env file |
||||||
|
|
||||||
|
When you have these keys, please create a new file called `.env` in your project root directory. The filename needs to be exactly the four characters ".env" rather than "my-keys.env" or ".env.txt". Here's how to do it: |
||||||
|
|
||||||
|
1. Open Terminal (Applications > Utilities > Terminal) |
||||||
|
|
||||||
|
2. Navigate to the "project root directory" using `cd ~/Documents/Projects/llm_engineering` (replace this path with the actual path to the llm_engineering directory, your locally cloned version of the repo). |
||||||
|
|
||||||
|
3. Create the .env file with |
||||||
|
|
||||||
|
nano .env |
||||||
|
|
||||||
|
4. Then type your API keys into nano, replacing xxxx with your API key (starting `sk-proj-`). |
||||||
|
|
||||||
|
``` |
||||||
|
OPENAI_API_KEY=xxxx |
||||||
|
``` |
||||||
|
|
||||||
|
If you have other keys, you can add them too, or come back to this in future weeks: |
||||||
|
``` |
||||||
|
GOOGLE_API_KEY=xxxx |
||||||
|
ANTHROPIC_API_KEY=xxxx |
||||||
|
HF_TOKEN=xxxx |
||||||
|
``` |
||||||
|
|
||||||
|
5. Save the file: |
||||||
|
|
||||||
|
Control + O |
||||||
|
Enter (to confirm save the file) |
||||||
|
Control + X to exit the editor |
||||||
|
|
||||||
|
6. Use this command to list files in your project root directory: |
||||||
|
|
||||||
|
`ls -a` |
||||||
|
|
||||||
|
And confirm that the `.env` file is there. |
||||||
|
|
||||||
|
This file won't appear in Jupyter Lab because jupyter hides files starting with a dot. This file is listed in the `.gitignore` file, so it won't get checked in and your keys stay safe. |
||||||
|
|
||||||
|
### Part 5 - Showtime!! |
||||||
|
|
||||||
|
- Open Terminal (Applications > Utilities > Terminal) |
||||||
|
|
||||||
|
- Navigate to the "project root directory" using `cd ~/Documents/Projects/llm_engineering` (replace this path with the actual path to the llm_engineering directory, your locally cloned version of the repo). Do `ls` and check you can see subdirectories for each week of the course. |
||||||
|
|
||||||
|
- Activate your environment with `conda activate llms` (or `source llms/bin/activate` if you used the alternative approach in Part 2B) |
||||||
|
|
||||||
|
- You should see (llms) in your prompt which is your sign that all is well. And now, type: `jupyter lab` and Jupyter Lab should open up, ready for you to get started. Open the `week1` folder and double click on `day1.ipynb`. |
||||||
|
|
||||||
|
And you're off to the races! |
||||||
|
|
||||||
|
Note that any time you start jupyter lab in the future, you'll need to follow these Part 5 instructions to start it from within the `llm_engineering` directory with the `llms` environment activated. |
||||||
|
|
||||||
|
For those new to Jupyter Lab / Jupyter Notebook, it's a delightful Data Science environment where you can simply hit shift+return in any cell to run it; start at the top and work your way down! I've included a notebook called 'Guide to Jupyter' that shows you more features. When we move to Google Colab in Week 3, you'll experience the same interface for Python runtimes in the cloud. |
||||||
|
|
||||||
|
If you have any problems, I've included a notebook in week1 called [troubleshooting.ipynb](week1/troubleshooting.ipynb) to figure it out. |
||||||
|
|
||||||
|
Please do message me or email me at ed@edwarddonner.com if this doesn't work or if I can help with anything. I can't wait to hear how you get on. |
Binary file not shown.
@ -0,0 +1,464 @@ |
|||||||
|
{ |
||||||
|
"cells": [ |
||||||
|
{ |
||||||
|
"cell_type": "markdown", |
||||||
|
"id": "5c291475-8c7c-461c-9b12-545a887b2432", |
||||||
|
"metadata": {}, |
||||||
|
"source": [ |
||||||
|
"# Intermediate Level Python\n", |
||||||
|
"\n", |
||||||
|
"## Getting you up to speed\n", |
||||||
|
"\n", |
||||||
|
"This course assumes that you're at an intermediate level of python. For example, you should have a decent idea what something like this might do:\n", |
||||||
|
"\n", |
||||||
|
"`yield from {book.get(\"author\") for book in books if book.get(\"author\")}`\n", |
||||||
|
"\n", |
||||||
|
"If not - then you've come to the right place! Welcome to the crash course in intermediate level python. The best way to learn is by doing!\n" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "markdown", |
||||||
|
"id": "542f0577-a826-4613-a5d7-4170e9666d04", |
||||||
|
"metadata": {}, |
||||||
|
"source": [ |
||||||
|
"## First: if you need a refresher on the foundations\n", |
||||||
|
"\n", |
||||||
|
"I'm going to defer to an AI friend for this, because these explanations are so well written with great examples. Copy and paste the code examples into a new cell to give them a try.\n", |
||||||
|
"\n", |
||||||
|
"**Python imports:** \n", |
||||||
|
"https://chatgpt.com/share/672f9f31-8114-8012-be09-29ef0d0140fb\n", |
||||||
|
"\n", |
||||||
|
"**Python functions** including default arguments: \n", |
||||||
|
"https://chatgpt.com/share/672f9f99-7060-8012-bfec-46d4cf77d672\n", |
||||||
|
"\n", |
||||||
|
"**Python strings**, including slicing, split/join, replace and literals: \n", |
||||||
|
"https://chatgpt.com/share/672fb526-0aa0-8012-9e00-ad1687c04518\n", |
||||||
|
"\n", |
||||||
|
"**Python f-strings** including number and date formatting: \n", |
||||||
|
"https://chatgpt.com/share/672fa125-0de0-8012-8e35-27918cbb481c\n", |
||||||
|
"\n", |
||||||
|
"**Python lists, dicts and sets**, including the `get()` method: \n", |
||||||
|
"https://chatgpt.com/share/672fa225-3f04-8012-91af-f9c95287da8d\n", |
||||||
|
"\n", |
||||||
|
"**Python classes:** \n", |
||||||
|
"https://chatgpt.com/share/672fa07a-1014-8012-b2ea-6dc679552715" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "5802e2f0-0ea0-4237-bbb7-f375a34260f0", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# Next let's create some things:\n", |
||||||
|
"\n", |
||||||
|
"fruits = [\"Apples\", \"Bananas\", \"Pears\"]\n", |
||||||
|
"\n", |
||||||
|
"book1 = {\"title\": \"Great Expectations\", \"author\": \"Charles Dickens\"}\n", |
||||||
|
"book2 = {\"title\": \"Bleak House\", \"author\": \"Charles Dickens\"}\n", |
||||||
|
"book3 = {\"title\": \"An Book By No Author\"}\n", |
||||||
|
"book4 = {\"title\": \"Moby Dick\", \"author\": \"Herman Melville\"}\n", |
||||||
|
"\n", |
||||||
|
"books = [book1, book2, book3, book4]" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "markdown", |
||||||
|
"id": "9b941e6a-3658-4144-a8d4-72f5e72f3707", |
||||||
|
"metadata": {}, |
||||||
|
"source": [ |
||||||
|
"# Part 1: List and dict comprehensions" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "61992bb8-735d-4dad-8747-8c10b63aec82", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# Simple enough to start\n", |
||||||
|
"\n", |
||||||
|
"for fruit in fruits:\n", |
||||||
|
" print(fruit)" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "c89c3842-9b74-47fa-8424-0fcb08e4177c", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# Let's make a new version of fruits\n", |
||||||
|
"\n", |
||||||
|
"fruits_shouted = []\n", |
||||||
|
"for fruit in fruits:\n", |
||||||
|
" fruits_shouted.append(fruit.upper())\n", |
||||||
|
"\n", |
||||||
|
"fruits_shouted" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "4ec13b3a-9545-44f1-874a-2910a0663560", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# You probably already know this\n", |
||||||
|
"# There's a nice Python construct called \"list comprehension\" that does this:\n", |
||||||
|
"\n", |
||||||
|
"fruits_shouted2 = [fruit.upper() for fruit in fruits]\n", |
||||||
|
"fruits_shouted2" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "ecc08c3c-181d-4b64-a3e1-b0ccffc6c0cd", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# But you may not know that you can do this to create dictionaries, too:\n", |
||||||
|
"\n", |
||||||
|
"fruit_mapping = {fruit:fruit.upper() for fruit in fruits}\n", |
||||||
|
"fruit_mapping" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "500c2406-00d2-4793-b57b-f49b612760c8", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# you can also use the if statement to filter the results\n", |
||||||
|
"\n", |
||||||
|
"fruits_with_longer_names_shouted = [fruit.upper() for fruit in fruits if len(fruit)>5]\n", |
||||||
|
"fruits_with_longer_names_shouted" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "38c11c34-d71e-45ba-945b-a3d37dc29793", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"fruit_mapping_unless_starts_with_a = {fruit:fruit.upper() for fruit in fruits if not fruit.startswith('A')}\n", |
||||||
|
"fruit_mapping_unless_starts_with_a" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "5c97d8e8-31de-4afa-973e-28d8e5cab749", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# Another comprehension\n", |
||||||
|
"\n", |
||||||
|
"[book['title'] for book in books]" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "50be0edc-a4cd-493f-a680-06080bb497b4", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# This code will fail with an error because one of our books doesn't have an author\n", |
||||||
|
"\n", |
||||||
|
"[book['author'] for book in books]" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "53794083-cc09-4edb-b448-2ffb7e8495c2", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# But this will work, because get() returns None\n", |
||||||
|
"\n", |
||||||
|
"[book.get('author') for book in books]" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "b8e4b859-24f8-4016-8d74-c2cef226d049", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# And this variation will filter out the None\n", |
||||||
|
"\n", |
||||||
|
"[book.get('author') for book in books if book.get('author')]" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "c44bb999-52b4-4dee-810b-8a400db8f25f", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# And this version will convert it into a set, removing duplicates\n", |
||||||
|
"\n", |
||||||
|
"set([book.get('author') for book in books if book.get('author')])" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "80a65156-6192-4bb4-b4e6-df3fdc933891", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# And finally, this version is even nicer\n", |
||||||
|
"# curly braces creates a set, so this is a set comprehension\n", |
||||||
|
"\n", |
||||||
|
"{book.get('author') for book in books if book.get('author')}" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "markdown", |
||||||
|
"id": "c100e5db-5438-4715-921c-3f7152f83f4a", |
||||||
|
"metadata": {}, |
||||||
|
"source": [ |
||||||
|
"# Part 2: Generators\n", |
||||||
|
"\n", |
||||||
|
"We use Generators in the course because AI models can stream back results.\n", |
||||||
|
"\n", |
||||||
|
"If you've not used Generators before, please start with this excellent intro from ChatGPT:\n", |
||||||
|
"\n", |
||||||
|
"https://chatgpt.com/share/672faa6e-7dd0-8012-aae5-44fc0d0ec218\n", |
||||||
|
"\n", |
||||||
|
"Try pasting some of its examples into a cell." |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "1efc26fa-9144-4352-9a17-dfec1d246aad", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# First define a generator; it looks like a function, but it has yield instead of return\n", |
||||||
|
"\n", |
||||||
|
"import time\n", |
||||||
|
"\n", |
||||||
|
"def come_up_with_fruit_names():\n", |
||||||
|
" for fruit in fruits:\n", |
||||||
|
" time.sleep(1) # thinking of a fruit\n", |
||||||
|
" yield fruit" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "eac338bb-285c-45c8-8a3e-dbfc41409ca3", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# Then use it\n", |
||||||
|
"\n", |
||||||
|
"for fruit in come_up_with_fruit_names():\n", |
||||||
|
" print(fruit)" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "f6880578-a3de-4502-952a-4572b95eb9ff", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# Here's another one\n", |
||||||
|
"\n", |
||||||
|
"def authors_generator():\n", |
||||||
|
" for book in books:\n", |
||||||
|
" if book.get(\"author\"):\n", |
||||||
|
" yield book.get(\"author\")" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "9e316f02-f87f-441d-a01f-024ade949607", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# Use it\n", |
||||||
|
"\n", |
||||||
|
"for author in authors_generator():\n", |
||||||
|
" print(author)" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "7535c9d0-410e-4e56-a86c-ae6c0e16053f", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# Here's the same thing written with list comprehension\n", |
||||||
|
"\n", |
||||||
|
"def authors_generator():\n", |
||||||
|
" for author in [book.get(\"author\") for book in books if book.get(\"author\")]:\n", |
||||||
|
" yield author" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "dad34494-0f6c-4edb-b03f-b8d49ee186f2", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# Use it\n", |
||||||
|
"\n", |
||||||
|
"for author in authors_generator():\n", |
||||||
|
" print(author)" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "abeb7e61-d8aa-4af0-b05a-ae17323e678c", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# Here's a nice shortcut\n", |
||||||
|
"# You can use \"yield from\" to yield each item of an iterable\n", |
||||||
|
"\n", |
||||||
|
"def authors_generator():\n", |
||||||
|
" yield from [book.get(\"author\") for book in books if book.get(\"author\")]" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "05b0cb43-aa83-4762-a797-d3beb0f22c44", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# Use it\n", |
||||||
|
"\n", |
||||||
|
"for author in authors_generator():\n", |
||||||
|
" print(author)" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "fdfea58e-d809-4dd4-b7b0-c26427f8be55", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# And finally - we can replace the list comprehension with a set comprehension\n", |
||||||
|
"\n", |
||||||
|
"def unique_authors_generator():\n", |
||||||
|
" yield from {book.get(\"author\") for book in books if book.get(\"author\")}" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "3e821d08-97be-4db9-9a5b-ce5dced3eff8", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# Use it\n", |
||||||
|
"\n", |
||||||
|
"for author in unique_authors_generator():\n", |
||||||
|
" print(author)" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "905ba603-15d8-4d01-9a79-60ec293d7ca1", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# And for some fun - press the stop button in the toolbar when bored!\n", |
||||||
|
"# It's like we've made our own Large Language Model... although not particularly large..\n", |
||||||
|
"# See if you understand why it prints a letter at a time, instead of a word at a time. If you're unsure, try removing the keyword \"from\" everywhere in the code.\n", |
||||||
|
"\n", |
||||||
|
"import random\n", |
||||||
|
"import time\n", |
||||||
|
"\n", |
||||||
|
"pronouns = [\"I\", \"You\", \"We\", \"They\"]\n", |
||||||
|
"verbs = [\"eat\", \"detest\", \"bathe in\", \"deny the existence of\", \"resent\", \"pontificate about\", \"juggle\", \"impersonate\", \"worship\", \"misplace\", \"conspire with\", \"philosophize about\", \"tap dance on\", \"dramatically renounce\", \"secretly collect\"]\n", |
||||||
|
"adjectives = [\"turqoise\", \"smelly\", \"arrogant\", \"festering\", \"pleasing\", \"whimsical\", \"disheveled\", \"pretentious\", \"wobbly\", \"melodramatic\", \"pompous\", \"fluorescent\", \"bewildered\", \"suspicious\", \"overripe\"]\n", |
||||||
|
"nouns = [\"turnips\", \"rodents\", \"eels\", \"walruses\", \"kumquats\", \"monocles\", \"spreadsheets\", \"bagpipes\", \"wombats\", \"accordions\", \"mustaches\", \"calculators\", \"jellyfish\", \"thermostats\"]\n", |
||||||
|
"\n", |
||||||
|
"def infinite_random_sentences():\n", |
||||||
|
" while True:\n", |
||||||
|
" yield from random.choice(pronouns)\n", |
||||||
|
" yield \" \"\n", |
||||||
|
" yield from random.choice(verbs)\n", |
||||||
|
" yield \" \"\n", |
||||||
|
" yield from random.choice(adjectives)\n", |
||||||
|
" yield \" \"\n", |
||||||
|
" yield from random.choice(nouns)\n", |
||||||
|
" yield \". \"\n", |
||||||
|
"\n", |
||||||
|
"for letter in infinite_random_sentences():\n", |
||||||
|
" print(letter, end=\"\", flush=True)\n", |
||||||
|
" time.sleep(0.02)" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "markdown", |
||||||
|
"id": "04832ea2-2447-4473-a449-104f80e24d85", |
||||||
|
"metadata": {}, |
||||||
|
"source": [ |
||||||
|
"# Exercise\n", |
||||||
|
"\n", |
||||||
|
"Write some python classes for the books example.\n", |
||||||
|
"\n", |
||||||
|
"Write a Book class with a title and author. Include a method has_author()\n", |
||||||
|
"\n", |
||||||
|
"Write a BookShelf class with a list of books. Include a generator method unique_authors()" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "markdown", |
||||||
|
"id": "35760406-fe6c-41f9-b0c0-3e8cf73aafd0", |
||||||
|
"metadata": {}, |
||||||
|
"source": [ |
||||||
|
"# Finally\n", |
||||||
|
"\n", |
||||||
|
"Here are some intermediate level details of Classes from our AI friend, including use of type hints, inheritance and class methods. This includes a Book example.\n", |
||||||
|
"\n", |
||||||
|
"https://chatgpt.com/share/67348aca-65fc-8012-a4a9-fd1b8f04ba59" |
||||||
|
] |
||||||
|
} |
||||||
|
], |
||||||
|
"metadata": { |
||||||
|
"kernelspec": { |
||||||
|
"display_name": "Python 3 (ipykernel)", |
||||||
|
"language": "python", |
||||||
|
"name": "python3" |
||||||
|
}, |
||||||
|
"language_info": { |
||||||
|
"codemirror_mode": { |
||||||
|
"name": "ipython", |
||||||
|
"version": 3 |
||||||
|
}, |
||||||
|
"file_extension": ".py", |
||||||
|
"mimetype": "text/x-python", |
||||||
|
"name": "python", |
||||||
|
"nbconvert_exporter": "python", |
||||||
|
"pygments_lexer": "ipython3", |
||||||
|
"version": "3.11.10" |
||||||
|
} |
||||||
|
}, |
||||||
|
"nbformat": 4, |
||||||
|
"nbformat_minor": 5 |
||||||
|
} |
Loading…
Reference in new issue