Performance: What is LangChain
LangChain affects the speed and responsiveness of applications that use language models by managing how data flows and how calls to models are made.
Jump into concepts and practice - no test required
from langchain.chains import SimpleSequentialChain from langchain.llms import OpenAI from langchain.cache import InMemoryCache llm = OpenAI() llm.cache = InMemoryCache() chain = SimpleSequentialChain(chains=[llm, llm]) response = chain.run('Hello')
from langchain.chains import SimpleSequentialChain from langchain.llms import OpenAI llm = OpenAI() chain = SimpleSequentialChain(chains=[llm, llm]) response = chain.run('Hello')
| Pattern | Network Calls | Latency Impact | Caching Use | Verdict |
|---|---|---|---|---|
| Sequential calls without caching | Multiple calls | High latency per call | No | [X] Bad |
| Sequential calls with caching | Multiple calls | Reduced latency on repeats | Yes | [!] OK |
| Batching calls or async calls | Fewer calls | Low latency | Yes | [OK] Good |
from langchain import PromptTemplate, LLMChain, OpenAI
prompt = PromptTemplate(template="Translate '{text}' to French.", input_variables=["text"])
llm = OpenAI(temperature=0)
chain = LLMChain(llm=llm, prompt=prompt)
result = chain.run(text="Hello")
print(result)from langchain import PromptTemplate, LLMChain
prompt = PromptTemplate(template="Say hello to {name}.", input_variables=["name"])
chain = LLMChain(prompt=prompt)
result = chain.run(name="Alice")
print(result)