Core Framework Structure
LangChain consists of three primary packages:
- LangChain Core: Contains fundamental data structures and the LangChain Expression Language (LCEL)
- LangChain Community: Open-source integrations and community-contributed components
- LangChain Applications: High-level implementation logic for building applications
Key Terminology
LLM (Large Language Model)
Base models focused on text completion and generation tasks.
RAG (Retrieval Augmented Generation)
Combines retrieval models with generative models to enhance output relevance and accuracy. This approach reduces hallucinations and improves integration with external knowledge sources.
LCEL (LangChain Expression Language)
A domain-specific language using pipe operators (|) to connect Runnable components into processing chains:
processing_chain = prompt_template | language_model | output_parser
Core Components
Model Wrappers
Abstracts API differences between various LLM providers, simplifying integration.
Text Completion Models (LLM):
from langchain.llms import OpenAI
text_model = OpenAI(model="text-davinci-003")
response = text_model("Describe the highest mountain in China")
print(response)
Chat Models:
from langchain.chat_models import ChatOpenAI
from langchain.prompts import (
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
ChatPromptTemplate
)
chat_model = ChatOpenAI()
system_template = SystemMessagePromptTemplate.from_template(
"You are an AI assistant"
)
user_template = HumanMessagePromptTemplate.from_template("{query}")
chat_prompt = ChatPromptTemplate.from_messages(
[system_template, user_template]
)
response = chat_model(chat_prompt.format_prompt(query="Tell me about Mount Everest").to_messages())
print(response.content)
Prompt Templates
Structured templates for generating consistant model inputs:
# Similar to Python's string formatting
from langchain.prompts import PromptTemplate
template = PromptTemplate.from_template(
"Tell me about {subject} in {language} language"
)
formatted = template.format(subject="quantum physics", language="simple")