Skip to content

Repository files navigation

Learning LangChain

Learning LangChain from a variety of sources

Setting up your virtual environment

I have used uv to manage the Python environment for this project. Here are the steps you should follow to recreate the same Python environment at your end, after you pull this GitHub repo to your local folder.

Before re-creating the Python environment, ensure you have uv installed on your local machine. To install uv for your operating system, follow the instructions here

Initializing a new managed environment

This step is applicable when you are creating a new Python environment that will be managed by uv. Skip this step if you have cloned this repo from GitHub. In that case, skip this step and jump to the next section. We'll assume that your project is located in a folder ~/code/my_project and its subfolders. Let's refer to this folder as the Project Folder.

  1. Change directory to the Project Folder
    $> cd ~/code/my_project
  2. Run the following command inside the Project Folder - this creates a local Python environment in the Project Folder/.venv, but does not install any Python packages!
    $> uv venv --python 3.12
    Here we have chosen to use Python 3.12 (with the --python 3.12 option). You can choose any other version of Python that you want to use for your project (replace 3.12 with 3.10 to use Python 3.10, for example).
  3. Initialize the new managed environment
    $> uv init
    This will create a pyproject.toml file in the Project Folder. This file will be used by uv to manage the Python environment and help your replicate the same environment across various machines.
  4. Activate the local environment you just created
    $> source .venv/Scripts/activate  ## on a Mac/Linux
    
    $> .venv\Scripts\activate ## on Windows
  5. Add packages to the local environment
    $> uv add <package_name>
    For example:
    $> uv add langchain
    You can add multiple packages at once as follows:
    $> uv add langchain openai python-dotenv ....
    # each package should be separated by a space
    As a best practice, you should add the packages to a requirements.txt file and then add all packages from that file using the following command:
    $> uv add -r requirements.txt
    requirements.txt is just a regular text file with one package listed per line (as was the case used with pip)

Recreating the Python environment

These are the steps to setup the Python environment (NOTE: in the code listings below $> refers to the command prompt and should not be entered by you!)

  1. cd to folder where you pulled the Github repo - let's refer to this folder as $PROJECT_HOME henceforth

    $> cd folder/to/learning_langchain
  2. Run the following command inside $PROJECT_HOME - - this creates a local Python environment in the $PROJECT_HOME/.venv, but does not install any Python packages! It also creates a pyproject.toml file, which uv will update.

    $> uv venv --python 3.12 .venv
  3. Activate the Python environment you just created

    $> source .venv/bin/activate  ## on a Mac/Linux
    
    # For Windows users (PowerShell):
    # .venv\Scripts\Activate.ps1
    # For Windows users (Command Prompt):
    # .venv\Scripts\activate.bat
  4. Recreate the environmant - will install the packages from pyproject.toml file

    $> uv sync
  5. Install some global packages - these will be installed globally without polluting your local Python environment. The include packages such as ipykernel (to run Notebook files) and black (to format Python code)

    $> uv tool install black 
    $> uv tool install ipykernel

Running the sample programs

The sample programs are a mix of Notebook files (*.ipynb) and Python modules (*.py) files.

To run the notebooks files (*.ipynb)

  1. Open the notebook in your code editor, such as IPython Notebook or VS Code or Cursor or PyCharm (professional only!)
  2. Select the correct Python kernel to run the cells in your notebook - it should be .venv\Scripts\python.exe
  3. Run the notebook as you normally do.

To run Python modules (*.py) files:

From within your IDE -

  1. Assign the correct Python interpreter to your project - it should be .venv\Scripts\python.exe
  2. Run the Python code as you'd normally do from your IDE.

From the command line -

  1. cd to $PROJECT_HOME
    $> cd folder/to/learning_langchain
  2. Use uv to run your Python code as follows:
    $> uv run full/path/to/python/code/file
    For example:
    $> uv run src/langchain_tutorial/01_chat_models_and_prompts.py

About

Learning LangChain from a variety of sources

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages