graph TB
A[LangChain 核心组件] --> B[Model I/O]
A --> C[Retrieval]
A --> D[Agents]
A --> E[Chains / LCEL]
B --> B1[Prompt Templates]
B --> B2[LLM / Chat Models]
B --> B3[Output Parsers]
C --> C1[Document Loaders]
C --> C2[Text Splitters]
C --> C3[Vector Stores]
C --> C4[Retrievers]
D --> D1[Agent Executor]
D --> D2[Tools]
D --> D3[Agent Types]
E --> E1[RunnableSequence]
E --> E2[RunnableParallel]
E --> E3[RunnableBranch]
style A fill:#1c3d5a,color:#fff
style B fill:#3498db,color:#fff
style C fill:#2ecc71,color:#fff
style D fill:#e74c3c,color:#fff
style E fill:#f39c12,color:#000
from langchain_openai import ChatOpenAI from langchain_core.prompts import ChatPromptTemplate from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnableParallel, RunnablePassthrough
# Run multiple chains in parallel analysis_chain = RunnableParallel( summary=prompt_summary | model | output_parser, keywords=prompt_keywords | model | output_parser, sentiment=prompt_sentiment | model | output_parser, )
result = analysis_chain.invoke({"text": "LangChain是一个优秀的LLM应用框架..."}) # result = {"summary": "...", "keywords": "...", "sentiment": "..."}
RunnableBranch — 条件路由
1 2 3 4 5 6 7 8 9 10
from langchain_core.runnables import RunnableBranch
# Route to different chains based on input router = RunnableBranch( (lambda x: "代码"in x["question"], code_chain), (lambda x: "数学"in x["question"], math_chain), general_chain, # Default fallback )
result = router.invoke({"question": "写一段Python代码实现快速排序"})
review_chain = review_prompt.partial( format_instructions=parser.get_format_instructions() ) | model | parser
result = review_chain.invoke({ "code": "def add(a,b): return a+b" }) # result is a dict matching CodeReview schema
使用 with_structured_output
1 2 3 4 5 6 7 8 9 10
# More robust: use model's native function calling classExtractedInfo(BaseModel): """Extracted information from the text.""" name: str = Field(description="人名") age: int = Field(description="年龄") occupation: str = Field(description="职业")
structured_llm = model.with_structured_output(ExtractedInfo) result = structured_llm.invoke("张三今年30岁,是一名软件工程师。") # result = ExtractedInfo(name='张三', age=30, occupation='软件工程师')
Memory Systems
graph LR
A[Memory Types] --> B[ConversationBufferMemory<br/>完整历史]
A --> C[ConversationBufferWindowMemory<br/>窗口历史]
A --> D[ConversationSummaryMemory<br/>摘要历史]
A --> E[ConversationTokenBufferMemory<br/>Token限制]
A --> F[VectorStoreMemory<br/>向量存储]
style B fill:#3498db,color:#fff
style D fill:#e74c3c,color:#fff
style F fill:#2ecc71,color:#fff
from langchain_core.prompts import MessagesPlaceholder from langchain_core.chat_history import InMemoryChatMessageHistory from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_community.document_loaders import WebBaseLoader from langchain_community.vectorstores import FAISS from langchain_openai import OpenAIEmbeddings from langchain.chains import create_retrieval_chain from langchain.chains.combine_documents import create_stuff_documents_chain
# Load and index documents loader = WebBaseLoader("https://docs.example.com/guide") docs = loader.load()