LangChain_LLMs.ipynb Open in SWAN Download

LangChain LLM Examples

LangChain is a framework that provides, among others, a set of Python APIs to ease the use of Large Language Models.
This notebook shows a few basic examples to get started, see https://python.langchain.com/en/latest/

OpenAI example

OpenAI provides a state of the art LLM, via an API. The model inference is perfomed on OpenAI platform. You need to register to get an API key. There is a limited amount of compute that can be done for free, see also pricing: https://openai.com/pricing

In [1]:
from langchain.llms import OpenAI
import os

from getpass import getpass
OPENAI_API_KEY = getpass()

# Get an OpenAI API Key from https://platform.openai.com/
os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY

# This will use OpenAI models, the specified model, see also https://openai.com/pricing
model = "gpt-3.5-turbo-instruct"
llm=OpenAI(model=model)
········
In [ ]:
llm.json()
In [3]:
from langchain import PromptTemplate, LLMChain

template = """Question: {question}

Answer: Let's think step by step."""
prompt = PromptTemplate(template=template, input_variables=["question"])
llm_chain = LLMChain(prompt=prompt, llm=llm)

question = "How to cook rice?"

print(llm_chain.run(question))
 

Step 1: Measure the rice
- The standard ratio for cooking rice is 1:2, which means for every 1 cup of rice, you will need 2 cups of water.
- Measure the desired amount of rice and place it in a bowl.

Step 2: Rinse the rice
- Rinse the rice in a fine-mesh strainer under cold running water.
- This will help remove any excess starch and impurities from the rice.

Step 3: Add water
- In a separate pot, add the measured amount of water.
- Bring the water to a boil over high heat.

Step 4: Add the rice
- Once the water is boiling, add the rinsed rice to the pot.
- Stir the rice briefly to ensure it is evenly distributed in the pot.

Step 5: Reduce heat and cover
- Reduce the heat to low and cover the pot with a lid.
- Let the rice simmer for about 18-20 minutes.

Step 6: Check the rice
- After 18-20 minutes, check the rice to see if it is done.
- The rice should be tender and all the water should be absorbed.

Step 7: Fluff the rice
- Once the rice is

Open access models, download and run on local GPU

There is a variety of models available for open access. However they currently do not perfom as well as the commercial ones and require machine with powerful GPUs to run the more advanced model.
Here we just provide some basic example of small models that can fit on a T4.
Note download the models from the web and uploading the weights into the GPU can take a few minutes.

In [5]:
import torch
from transformers import pipeline
from langchain.llms import HuggingFacePipeline
from langchain import PromptTemplate, LLMChain

# model = "EleutherAI/pythia-2.8b" 
model = "databricks/dolly-v2-3b" # Or try another model

pipe = pipeline(
    model=model, 
    torch_dtype=torch.bfloat16, 
    trust_remote_code=True, 
    device=0, 
    max_length=200,
)

llm = HuggingFacePipeline(pipeline=pipe)
2024-05-16 15:10:50.119570: I tensorflow/core/util/port.cc:110] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-05-16 15:10:50.199324: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
In [6]:
template = """Question: {question}

Answer: Let's think step by step."""

prompt = PromptTemplate(template=template, input_variables=["question"])

llm_chain = LLMChain(prompt=prompt, llm=llm, verbose=True, memory=None)

question = "How to cook rice?"
print(llm_chain.run(question))
Both `max_new_tokens` (=256) and `max_length`(=200) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)

> Entering new LLMChain chain...
Prompt after formatting:
Question: How to cook rice?

Answer: Let's think step by step.

> Finished chain.
ell.
3. Bring the water to a boil.
4. Once the water boils, reduce heat to low.
5. Simmer for 10-15 minutes.
6. Open the rice valve and let the rice steam.
7. When the rice is cooked, fluff the rice with a fork.
In [ ]: