IBM VEST Workshops
45 min
Last updated 07/15/2024

Lab 105: Building AI apps with Langchain and watsonx.ai

About

Langchain is a framework which helps in developing more complex AI powered apps. It works with many language models, and provides a set of tools to make intricate logic more manageable, such as sending system and human instructions, dynamic prompts using templates, chaining, output parsing, and more.

Today, we will be using Langchain with watsonx.ai, and the IBM Watson Machine Learning SDK, specifically the SDK's Langchain extension. Langchain already provides support for over 70 LLMs, but in case you want to support a new one, there is also custom LLM functionality. Read more here

The first part starts with a gentle introduction to some langchain capabilities, including how to initialize a model, change inference parameters, use templates, chains, and load documents. We will then move on to summarization of large amounts of text, which includes more moving parts.

There are 3 ways you can run these Langchain labs:

  1. Locally on your laptop (requires more technical expertise)
  2. In our JupyterHub server environment (easiest)
  3. In the watsonx UI (Work with data and models in Python or R notebooks)

1: Run the labs locally on your laptop:

Prerequisites

  1. Make sure Python >= 3.9 is installed. (python --version / python3 --version)

    • If you are running Windows, make sure Microsoft C++ Build tools are installed.
  2. Download langchain.zip, decompress it, and change to the directory, (cd langchain).

  3. In your terminal, create a Virtual Environment: python3 -m venv env.

  4. Activate the environment:

    • MacOS + Linux: source env/bin/activate
    • Windows:

      # If using Windows cmd.exe: (command prompt)
      env\Scripts\activate.bat
      # If using Windows PowerShell:
      env\Scripts\Activate.ps1

    • You can deactivate it later with deactivate when done with the labs.
  5. Install the requirements: python3 -m pip install -r requirements.txt

  6. Start your JupyterHub server with jupyter notebook and then run the "env-test.ipynb" to make sure there aren't any issues.

  7. Using the .env.example file, create an .env file in your folder.

  8. Fill out the values in the .env file:

    NOTE: If you are attending a workshop, ask your instructors how to acquire these values. Additional instructions on how to find these values are below.

    • API_KEY can be found at cloud.ibm.com/iam/apikeys after logging in. (If you don't have access to create an API key, contact your IBM cloud account Admin.)
    • IBM_CLOUD_URL should be your regional IBM cloud URL like in .env.example. You can find this value by clicking the "View code" button next to the model dropdown in the Prompt Lab. You should use the URL without any paths (aka https://us-south.ml.cloud.ibm.com).
Instance urlInstance url
  • PROJECT_ID can be found under watsonx projects under the project manage tab. The id is also part of the URL: https://dataplatform.cloud.ibm.com/projects/<project-id>/manage/general?context=wx

After finishing the prerequisites, complete the labs with the jupyter notebooks below. If you run into any issues, consult the Troubleshooting page.

2: Run the labs in the JupyterHub server

For the workshop your instructor will provide the URL for JupyterHub, and your email will be allow-listed. You will just need to sign up to set your password, and then you will be able to log in. After that follow the instructions in the intro and summarization notebooks. They are the same notebooks as listed below. If needed, follow step 8 above to get the values for environment variables.

Notebooks

Part 1

Complete the lab by following the instructions in the notebook: langchain-intro.ipynb

Part 2

Complete the lab by following the instructions in the notebook: langchain-summarization.ipynb

3: Run the labs in Watsonx Cloud

Use the following notebooks if you are running in the watsonx cloud UI:

Part 1

Complete the lab by following the instructions in the notebook: langchain-intro-cloud.ipynb

Part 2

Complete the lab by following the instructions in the notebook: langchain-summarization-cloud.ipynb