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_23bin your home directory without overwriting your changes.
The released notebooks are also compiled into an ebook https://
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 Shortcutsand try some of the shortcuts to see their effect.Jupyter Referenceopens 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()It’s important to follow certain guidelines when writing your answers or code in the notebooks:
- Only provide your answers in the cells that are designated for this purpose, which are usually labeled with
YOUR CODE HEREorYOUR ANSWER HERE. - For coding exercises, be sure to remove the line
raise NotImplementedError()from the cell before submitting your work. - Do not clone or duplicate the answer cells, as this can cause issues with version control and grading.
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.
- You can repeatedly modify your solution and run the test cell until your solution passes the test. There is no mark penalty in failing the test before submission.
- You should try to understand the error messages from a failed test.
- To assess your solution thoroughly, we will run new tests hidden from you after you have submitted your notebook. Therefore, you should ensure your solution works in general rather than just the visible tests.
- If you open the same notebook multiple times in different browser windows, be careful in making changes in different windows as you may overwrite your own changes.
- If your notebook fails to run any code, the kernel might have died. You can restart the kernel with
KernelRestart. If restarting fails, check your code cells to see if there is anything that breaks the kernel.
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.
- The tutorial notebooks for each lecture are due before the next lecture.
- Late submissions will not be graded without valid justifications.
- You can submit your assignment repeatedly before the deadline without penalty, so it is recommended to submit your assignment in advance.
- It is also important to make a record or backup of your submission that includes the submission timestamp. Double check to ensure that you have submitted the correct lab assignment, since multiple lab assignments may be open for submission at the same time.
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:
- 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.
- Save the notebook: Unsaved changes are not exported, even though they are in the memory.
- Restart the kernel:
KernelRestart Kernel...to have a clean state before running visible tests. - run all cells:
RunRun All Cellsto double check the results of the visible tests are as expected.
To submit your notebook:
- Select the menu item
NbgraderAssignment List. - Expand the Lab folder and click the
validatebutton next to the notebook(s) to check if all the visible tests pass. - Click
Submitto submit your notebook.
Your submission may not be graded under the following circumstances:
- If the notebook files have been renamed, for example,
Setup.ipynbbeing changed or copied toSetup (Copy).ipynb. - If an HTML file exists with the same name as a notebook, for example,
Setup.html. - If the file size exceeds
100MB. - If the code takes too long to run or requires an excessive amount of memory.
It is essential to ensure that your submission meets these guidelines to avoid any issues with grading.
Troubleshooting
If you believe your notebooks are corrupted,
- download/backup your existing notebooks,
- remove them from the
Lab*/folder, - click the git-pull links from the course homepage to re-pull the notebooks, and
- copy your solutions to the new notebooks.
You may also run the course notebooks outside the jupyterhub server and locally on your computer. For more details, see the course homepage.
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 listWhile 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:
- Click the chat icon on the left menu bar. A chat panel will open.
- Click the gear icon on the chat panel to set up the provider.
- Select the first model
AI :: mistraland click theSave Changesbutton. - Click the back arrow at the top to go back to the chat window.
- Enter
/ask how are you today?to see a response.
Tip
You can also use the vscode interace with the ai-genie extension:
- Open VSCode.
- Click the genie icon of the left menu bar to open the chat panel.
- Click the gear button to setup the provider.
- Set
genieai.openai.modeltogpt-3.5-turbo, which will trigger a pop-up box asking for API key. - Enter any non-empty string as the key.
- Change
genieai.openai.apiBaseUrlfromhttps://api.openai.comtohttps://ai.local-aiwithout trailing slash. - Reload the vscode window (
Shift-Cmd/Ctrl-Pand 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:
- Access https://
www .cs .cityu .edu .hk / ~ccha23 /cs5483 _23b /apim and click Active Directory - Loginto login with your CityU active directory credentials. - From the top menu, navigate to the product page and click the link to cs5483-2024-openai
- 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.
- Navigate to the apis page and click the link to cs5483-2024.
- On the left panel, click the available API to show the corresponding base url.
- On the right panel, you may test access the chosen API chosen on the left panel:
- Select the
echoAPI on the left and the protocolHTTPon the right. - Click
Sendto send the request. - You should receive HTTP response
200 okayif the connection is successful.
- Select the
- For APIs other than echo:
- Click
+ Add parameterand enter the parameter nameapi-versionand value2023-08-01-preview. - In the textbox under
Body, enter the message body in a format expected by the API. See some examples here.
- Click
Run the following cell to enter your API Key securely:
from ipywidgets import Password
api_key_widget = Password(description='API Key:')
api_key_widgetCreate 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)