Location: /gen-ai-app-dev-template/LangChain/working-with-llms.ipnyb
from langchain_community.llms.fake import FakeListLLM
from langchain_community.llms.fake import FakeStreamingListLLM
# 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)
# Quick test
fake_list_llm.invoke("give whatever")
# use await to invoke the async function
response = await fake_list_llm.ainvoke("give whatever")
# print response
print(response)
# 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="")
# 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="")
# 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)