Skip to content

Commit

Permalink
The Beginning
Browse files Browse the repository at this point in the history
  • Loading branch information
haesleinhuepf committed Nov 26, 2023
1 parent 0d8af91 commit 89b8962
Show file tree
Hide file tree
Showing 26 changed files with 1,663 additions and 2 deletions.
2 changes: 0 additions & 2 deletions .gitattributes

This file was deleted.

38 changes: 38 additions & 0 deletions .github/workflows/book.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: deploy-book

# Only run this when the master branch changes
on:
push:
branches:
- master
- main

# This job installs dependencies, build the book, and pushes it to `gh-pages`
jobs:
deploy-book:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

# Install dependencies
- name: Set up Python 3.7
uses: actions/setup-python@v1
with:
python-version: 3.7

- name: Install dependencies
run: |
pip install -r requirements.txt
# Build the book
- name: Build the book
run: |
jupyter-book build .
# Push the book's HTML to github-pages
- name: GitHub Pages action
uses: peaceiris/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./_build/html
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
_build
.ipynb_checkpoints
.idea
Binary file added 00_preparation/hello_world.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 00_preparation/install_jupyter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 00_preparation/install_mambaforge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 00_preparation/install_mambaforge2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions 00_preparation/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Setting up your computer

This chapter provides instructions for setting up your computer before the tutorial.

# Setting up Python and Conda environments
When working with Python, we will make use of many plugins and software libraries which need to be organized.
One way of doing this, is by managing *Conda* environments.
A conda environment can be seen as a virtual desktop, or virtual computer, accessible via the terminal.
If you install some software into one Conda environment, it may not be accessible from another environment.
If a Conda environment breaks, e.g. incompatible software was installed, you can just make a new one and start over.

See also
* [Getting started with Mambaforge and Python](https://biapol.github.io/blog/mara_lampert/getting_started_with_mambaforge_and_python/readme.html)
* [Managing Scientific Python environments using Conda, Mamba and friends](https://focalplane.biologists.com/2022/12/08/managing-scientific-python-environments-using-conda-mamba-and-friends/)
* [Scientific Data Analysis with Python](https://youtu.be/MOEPe9TGBK0)

## Step 1: Install Miniforge
Download and install mamba/conda. We recommend the distribution [Miniforge](https://github.com/conda-forge/miniforge#miniforge3).

For ease-of-use, it is recommended to install it for your use only and to add Conda to the PATH variable during installation.

![img.png](install_mambaforge.png)

![img.png](install_mambaforge2.png)

## Step 2: Install Python packages

Use this command from the terminal:

```
mamba create --name prompt-env python=3.9 jupyterlab openai scikit-image pandas matplotlib -c conda-forge
```

```
mamba activate prompt-env
```

```
pip install bia-bob
```

**Tip**: It is recommended to create one environment for every project you are executing.
In that way installed software libraries and tools cannot harm each other.

## Step 3: Configure OpenAI API Key

Create an OpenAI API Key and add it to your environment variables as explained on [this page](https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety).

## Step 4: Testing the installation

Afterwards, you can enter the environment to work with it.
Whenever you want to work on the same project again, you should start a command line and enter this:

```
mamba activate prompt-env
```

Start [Jupyter lab](https://jupyter.org/) from the terminal like this

```
jupyter lab
```

A browser will open and show you the following web page. In the section `Notebook` click on "Python 3 (ipykernel)" to create a new notebook:

![img.png](start_jupyter_lab.png)

In the new notebook, click in the first code cell, enter `print("Hello world")` and hit SHIFT+ENTER on your keyboard.
If everything is installed properly, it should look like this:

![img.png](hello_world.png)


Binary file added 00_preparation/slides.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 00_preparation/start_jupyter_lab.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 00_preparation/test_opencl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
175 changes: 175 additions & 0 deletions 01_prompts/01_prompting.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "9ee67a8d-c234-4427-91c9-a0dc7033657e",
"metadata": {},
"source": [
"# Prompting chatGPT\n",
"In this notebook we will send basic prompts to chatGPT and receive answers. We will write a small `prompt` helper function that makes it more accessible."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "3b7ff58a-4c07-431b-b123-543023f75b75",
"metadata": {},
"outputs": [],
"source": [
"from openai import OpenAI\n",
"\n",
"from IPython.core.magic import register_line_cell_magic\n",
"from IPython.display import display, Markdown"
]
},
{
"cell_type": "markdown",
"id": "33f1bf93-26e8-4d10-bd57-6ade5afdcd7f",
"metadata": {},
"source": [
"A basic prompt requires to define the model we will be using as well as the role we have and the content/message we would like to be responded to. To make it more convenient we write a little helper function that allows us to retrieve the response to a message directly."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f9406734-ba7a-4deb-8bb1-90b33c460d87",
"metadata": {},
"outputs": [],
"source": [
"@register_line_cell_magic\n",
"def prompt(line:str, cell:str, model=\"gpt-3.5-turbo\"):\n",
" \"\"\"A prompt helper function that sends a message to openAI\n",
" and prints out the text response.\n",
" \"\"\"\n",
" message = line + \"\\n\" + cell\n",
" client = OpenAI()\n",
" response = client.chat.completions.create(\n",
" model=model,\n",
" messages=[{\"role\": \"user\", \"content\": message}]\n",
" )\n",
" text = response.choices[0].message.content\n",
" display(Markdown(text))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "43c6f1dc-fb3c-463f-a144-90b017031600",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"The countries in Central Europe are:\n",
"\n",
"1. Austria\n",
"2. Czech Republic\n",
"3. Germany (some parts are considered part of Central Europe)\n",
"4. Hungary\n",
"5. Poland\n",
"6. Slovakia\n",
"7. Slovenia (sometimes considered part of Central Europe)\n",
"8. Switzerland (some parts are considered part of Central Europe)"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%prompt\n",
"Which countries are in central Europe?"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ed9c0e13-2f2b-421b-8230-425507372969",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"Switzerland is located in central Europe, so there are no parts of Switzerland that are not part of central Europe."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%prompt\n",
"Which parts of Switzerland are not central Europe?"
]
},
{
"cell_type": "markdown",
"id": "a256a055-c5ef-4846-a370-006c8a80a119",
"metadata": {},
"source": [
"## A comment on reproducibility\n",
"Note that prompt responses may not be very reproducible. You can execute the same prompt twice and receive different responses."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "8269c2a0-0a68-452d-9c20-1cabdaf66e1d",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"The parts of Switzerland that are not considered central Europe are its overseas territories or colonies, as Switzerland does not have any. However, geographically speaking, Switzerland entirely lies within Central Europe."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%prompt\n",
"Which parts of Switzerland are not central Europe?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "47f85f74-7742-4660-a60e-83e7c310b8f9",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
202 changes: 202 additions & 0 deletions 01_prompts/02_generating_images.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit 89b8962

Please sign in to comment.