Exercise#1 LLM & Fakes

Objective

  • Learn to use the LangChain LLM class
  • Familiarize with the Fake LLM classes

Steps

1. Start by creating a new notebook

Location: /gen-ai-app-dev-template/LangChain/working-with-llms.ipnyb

Import the Fake LLM packages

from langchain_community.llms.fake import FakeListLLM
from langchain_community.llms.fake import FakeStreamingListLLM

2. Setup the Fake List LLM

# Random responses sent by the LLM
fake_responses = [
    "this is a fake response 1",
    "this is a fake response 2",
]

# Create the faker
fake_list_llm = FakeListLLM(responses=fake_responses)

Try synchronous : invoke()

# Quick test 
fake_list_llm.invoke("give whatever")

Try asynchronous : ainvoke()

  • Uses the Python asynchio library
  • Introduce a sleep time of 5 seconds to simulate a slow LLM
# use await to invoke the async function
response =  await fake_list_llm.ainvoke("give whatever")

# print response
print(response)

3. Streaming

  • Synchronous streaming
  • 0.5 second delay between characters
# Create a fake streaming LLM with an artificial delay of 0.5 seconds between chunks
fake_streaming_llm = FakeStreamingListLLM(responses=fake_responses, sleep=0.5)

# Sysnchronous streaming
for chunk in fake_streaming_llm.stream("does not matter"):
    print(chunk,end="")
  • Asynchronous streaming
# Initiate the LLM streaming as async
streaming_object = fake_streaming_llm.astream("does not matter")

# Do other things, while LLM is processing in parallel
print("\nDo something while, while LLM is generating a response i.e., thread is not blocked ...")

# Gather some of the streamed 
async for chunk in streaming_object:
    print(chunk,end="")

3. Batches

# Fake responses
fake_responses = [
    '{"person": "tom", "org": "apple"}',
    '{"person": "nick", "org": "amazon"}',
    '{"person": "anil", "org": "meta"}',
]

fake_list_llm = FakeListLLM(responses=fake_responses)

# Test requests sent in a batch in parallel
request_inputs = [
    "extract named entitities :  ......",
    "extract named entitities :  ......",
    "extract named entitities :  ......",
    "extract named entitities :  ......",
]

# Invoke LLM in parallel for all 3 
fake_list_llm.batch(request_inputs)

Solution

Local Jupyter Lab setup

  • Notebook requires API Token for Hugging Face
  • Open the notebook locally in your local Jupyter lab setup

llm-interactions-and-fake

Google colab

  • Open the notebook in Google colab
  • Make sure to run the required cells !!! otherwise you will get errors
Open In Colab