{ "cells": [ { "cell_type": "markdown", "id": "bb9dc285-0799-408e-90bc-693f5e70d0ce", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "# Installing software\n", "\n", "We’ll use [LangChain](https://www.langchain.com/), an open-source\n", "library for making applications with LLMs. We’ll use models from\n", "[HuggingFace](https://huggingface.co/), a website that has tools and\n", "models for machine learning.\n", "\n", "```{admonition} Exercise: Create a new notebook\n", "Create a new Jupyter Notebook called `installing` by clicking the\n", "File-menu in JupyterLab, and then New and Notebook. If you are asked to\n", "select a kernel, choose “Python 3”. Give the new notebook a name by\n", "clicking the File-menu in JupyterLab and then clicking Rename Notebook.\n", "Use the name `installing`.\n", "```\n", "\n", "Warning\n", "\n", "If you usually work with virtual environments, you should setup and\n", "activate a virtual environment before you continue, see [Virtual\n", "Environments](#virtual-environments). If you haven’t heard of virtual\n", "environments, you can continue without using virtual environments. In\n", "that case, jump to [Python packages](#python-packages).\n", "\n", "## Virtual Environments\n", "\n", "Venv is a module in Python that supports creating independent [virtual\n", "environments](https://docs.python.org/3/library/venv.html) (venv). The\n", "venv has its base in the existing python installation.\n", "\n", "## Creating a virtual environment\n", "\n", "The command below will create a venv on the project directory. We are\n", "doing this from Jupyter lab:" ] }, { "cell_type": "code", "execution_count": null, "id": "50432061-2251-4d51-b04c-52565a180f39", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "!python -m venv .venv" ] }, { "cell_type": "markdown", "id": "a4c16507-dcef-41ed-8855-00161c547791", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Activating the environment from the commandline " ] }, { "cell_type": "code", "execution_count": null, "id": "0d94cbcd-c759-4651-b82a-ea7e837d821b", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "## Terminal from jupyter lab:\n", "source .venv/bin/activate" ] }, { "cell_type": "markdown", "id": "043794c6-a255-44a5-94af-57c9ddf0c6ed", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## JupyterLab kernel for the environment" ] }, { "cell_type": "code", "execution_count": null, "id": "4d41ddff-9416-41b1-8847-00a9dcfcb0a9", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "! .venv/bin/python -m ipykernel install --user --name LLM --display-name \"Python (LLM)\"" ] }, { "cell_type": "markdown", "id": "975bc4aa-64d9-4b91-a9c4-b9ee431cc786", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Python packages\n", "\n", "We will use the python package manager `pip` for installing software.\n", "`pip` installs software from the [Python Package\n", "Index](https://pypi.org/). First, we update pip to the newest version:" ] }, { "cell_type": "code", "execution_count": null, "id": "899fb019-2a54-4a8f-9797-8f12174cc3e6", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "pip install --upgrade pip" ] }, { "cell_type": "markdown", "id": "392c5dd4-4d31-4634-b478-21552a6f4d24", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Requirements file \n", "\n", "In December 2025, while doing preparations for a course, we had some issues due to the fact that some of our most important libraries are deprecated. This led us to implement a requirements file. At this stage, we need the exact dependencies from the file LLM-requirements.txt. As you can read from the file, the technical issues are solved through naming a library like this: \"langchain==1.2.1\", rather than just \"langchain\". Run the code below:" ] }, { "cell_type": "code", "execution_count": null, "id": "04a98778-b6dd-4e1d-b53a-1fb68e77df77", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "pip install -r /fp/projects01/ec443/LLM-requirements.txt" ] }, { "cell_type": "markdown", "id": "f6c7139f", "metadata": {}, "source": [ "## Model location\n", "We want to tell the HuggingFace library where to store its data. The path below is for the Educloud/Fox project ec443.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "794509b2-0c1a-4afb-a8ec-c1aac8859226", "metadata": {}, "outputs": [], "source": [ "import os\n", "os.environ['HF_HOME'] = '/fp/projects01/ec443/huggingface/cache/'" ] }, { "cell_type": "markdown", "id": "cea5cf36-4f68-466c-ab6a-0db3f8886a70", "metadata": {}, "source": [ "Optional::\n", "\n", "If you run on your own, or want to download a model that is not\n", "previously used by the group, you will need to make an account at\n", "Huggingface, and store the token you get. Your token can be found under\n", "your profile, in the right side of the top menu: \"access tokens\". For\n", "the gated models, you need to apply, before you can download. Your token\n", "should be stored in a safe place, and not shared with anyone.\n", "\n", "Cell for entering your HF token:" ] }, { "cell_type": "code", "execution_count": null, "id": "f588e026-a444-49aa-b482-bf2811e6f191", "metadata": {}, "outputs": [], "source": [ "from huggingface_hub import login\n", "login()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.18" } }, "nbformat": 4, "nbformat_minor": 5 }