Skip to article frontmatterSkip to article content

Setup

City University of Hong Kong

Tutorial materials

Tutorial materials are written in the form of Jupyter Notebooks. Jupyter Notebooks provide an interactive literate programming experience, which is a significant benefit for learners:

  • Jupyter Notebooks allow programs to be mixed with other contents, including figures, videos, and formatted textual explanations.
  • The notebooks can also run in a Jupyter server, which enables users to write and run their programs while adding formatted notes to it.
  • The Jupyter server can access local and remote large language models, which enables an AI pair programming experience that speed up tedious coding tasks.

How to access the programming environment?

The course’s programming environment is conveniently accessible via a JupyterHub server, allowing remote access without the need to install any special software, thanks to Project Jupyter. This means you can easily write and run programs on your mobile devices using just a web browser.

You can access the JupyterHub server from the canvas course website or click this LTI login link. The LTI Authenticator provides a seamless login experience using your Canvas login credentials. After your initial LTI login, your account will be created in JupyterHub.

To access the Jupyter environment, simply log into the JupyterHub server and select the Default option from the list of available Jupyter servers. Click Start to begin your session. If the server is spawned successfully, the JupyterLab interface should appear. Each student will have their own Jupyter server, providing individual computer resources such as CPU and memory.

How to access the tutorial notebooks?

The recommended way is to access the tutorial notebooks is to follow the links on the course homepage. The notebooks actually reside in a GitHub repository. The first time you access the notebook link, the following steps occur:

  • Your Jupyter server is opened.
  • The repository is cloned to the server as a local repository.
  • The desired notebook is opened in the JupyterLab interface.

Afterwards, accessing a notebook link again will

  • git-pulls new or existing files in the repository and
  • merge them with the notebooks stored under the course folder cs5483_23b in your home directory without overwriting your changes.

The released notebooks are also compiled into an ebook https://www.cs.cityu.edu.hk/~ccha23/cs5483book, which allows you to preview the notebooks conveniently as webpages and download the notebooks in PDF or ipynb formats.

Lab Assignments

How to complete a lab assignment?

The notebooks can be edited in JupyterLab to include your notes and answers for submission. If this is your first time using JupyterLab, take a look at the official video tutorial:

Official video tutorial on Jupyter

The Help menu gives more detailed references. For instance:

  • Checkout the menu item Show Keyboard Shortcuts and try some of the shortcuts to see their effect. Jupyter Reference opens the user guide in a new tab.
  • Try the MyST Markdown syntax to format your notes.

In learning a new programming language, the first program to write is often the “Hello, World!” program, which says Hello to the world. As your first lab exercise, you will write such a program in python.

The following code cell is a solution cell. Since you are not expected to know python before, you can simply expand the hint above and copy the solution to the solution cell instead.

def say_hello():
    # YOUR CODE HERE
    raise NotImplementedError()
    
say_hello()

To test your program:

  • Run your program by selecting the solution cell and press Shift+Enter.
  • Then, run the following visible test to check whether your program prints the correct message.
# Run this test cell right after running your "Hello, World!" program.
import io
import sys

old_stdout, sys.stdout = sys.stdout, io.StringIO()
say_hello()
printed = sys.stdout.getvalue()
sys.stdout = old_stdout
assert printed == "Hello, World!\n"

The test returns an assertion error if your program does not print the correct message.

How to submit a notebook

Each lecture has a set of corresponding tutorial notebooks. They provide hands-on practice for the individual Project 1 and group Project 2. Although they don’t typically count towards the final grade, if both submission rates of the first two tutorials are over 50%, the tutorial scores may count up to 5% bonus points towards the 30% project 1 and 2 grades.

Nbgrader is the tool used for grading notebook assignments. It is well-integrated into the JupyterLab interface, allowing students to submit their notebooks directly through the JupyterHub server. Submitted notebooks can be both auto-graded with pre-defined test cases and manually graded with custom feedback. After grading is complete, you can check your scores and access the feedback using the same interface.

Before you submit, make sure everything runs as expected:

  1. Git-pull the notebooks (optional): Follow any one of the link on the course homepage to a notebook, which will git-pull any updates/corrections to (all) your notebooks.
  2. Save the notebook: Unsaved changes are not exported, even though they are in the memory.
  3. Restart the kernel: Kernel\to Restart Kernel... to have a clean state before running visible tests.
  4. run all cells: Run\to Run All Cells to double check the results of the visible tests are as expected.

To submit your notebook:

  1. Select the menu item Nbgrader\toAssignment List.
  2. Expand the Lab folder and click the validate button next to the notebook(s) to check if all the visible tests pass.
  3. Click Submit to submit your notebook.

AI Tutor

How to play with LLMs in notebooks?

Large language models (LLMs) have been integrated into the Jupyter server using jupyter-ai.

# load the cell magic to interact with a LLM in a cell
%reload_ext jupyter_ai
# list available providers/models
%ai list

While most of the providers require subscriptions, you can try a local model diveai-chat:mistral deployed locally.

%%ai diveai-chat:mistral
Write a python hello world program.
%%ai diveai-chat:mistral
Give a brief introduction to AI pair programming.

There is also a Jupyternaut chatbot:

  1. Click the chat icon on the left menu bar. A chat panel will open.
  2. Click the gear icon on the chat panel to set up the provider.
  3. Select the first model AI :: mistral and click the Save Changes button.
  4. Click the back arrow at the top to go back to the chat window.
  5. Enter /ask how are you today? to see a response.
Tip

You can also use the vscode interace with the ai-genie extension:

  1. Open VSCode.
  2. Click the genie icon of the left menu bar to open the chat panel.
  3. Click the gear button to setup the provider.
  4. Set genieai.openai.model to gpt-3.5-turbo, which will trigger a pop-up box asking for API key.
  5. Enter any non-empty string as the key.
  6. Change genieai.openai.apiBaseUrl from https://api.openai.com to https://ai.local-ai without trailing slash.
  7. Reload the vscode window (Shift-Cmd/Ctrl-P and type reload).

How to subscribe to Azure OpenAI?

To play with other models such as gpt-4, registered students can subscribe to Azure OpenAI as follows:

  1. Access https://www.cs.cityu.edu.hk/~ccha23/cs5483_23b/apim and click Active Directory - Login to login with your CityU active directory credentials.
  2. From the top menu, navigate to the product page and click the link to cs5483-2024-openai
  3. Enter any name such as cs5483 in the textbox and click subscribe. You will be brought to the profile page where you can show/regenerate your primary/secondary api keys.
  4. Navigate to the apis page and click the link to cs5483-2024.
  5. On the left panel, click the available API to show the corresponding base url.
  6. On the right panel, you may test access the chosen API chosen on the left panel:
    • Select the echo API on the left and the protocol HTTP on the right.
    • Click Send to send the request.
    • You should receive HTTP response 200 okay if the connection is successful.
  7. For APIs other than echo:
    • Click + Add parameter and enter the parameter name api-version and value 2023-08-01-preview.
    • In the textbox under Body, enter the message body in a format expected by the API. See some examples here.

Run the following cell to enter your API Key securely:

from ipywidgets import Password

api_key_widget = Password(description='API Key:')
api_key_widget

Create a client with the subscription information:

from openai import AzureOpenAI

# For Jupyternaut:
# Select Azure OpenAI::* provider
# to enter the following information
deployment_name = "cs-gpt-35-turbo-0301"
base_api_url = "https://apim-aoai-eas-dev02.azure-api.net/cs5483-2024"
api_version = "2023-08-01-preview"
api_key = api_key_widget.value # taken from the above widget

client = AzureOpenAI(
  azure_endpoint = base_api_url,
  api_version = api_version,
  api_key = api_key,
)

Create a chat completion:

stream = client.chat.completions.create(
  model = deployment_name,
  messages = [
    {"role": "user", "content": "What is data mining?"},
  ],
  temperature = 0,
  max_tokens = 75,
  stream = False
)

print(stream.choices[0].message.content)