LangChain Building Agents

LangChain is a framework for building LLM applications, upgrading model calls into composable, controllable, and scalable application systems.

What LangChain solves is not how to call models, but:

  • How to organize multi-step reasoning
  • How to integrate external data
  • How tools can be safely called by models
  • How context is managed over the long term

Open Source Repository:https://github.com/langchain-ai/langchain。

LangChain Tutorial:https://www.example.com/langchain/langchain-tutorial.html

Imagine you are building an intelligent robot assistant. This assistant needs to understand your questions, search for information from various sources, perform logical reasoning, and finally answer you in natural language. Using a language model like GPT alone is like giving the robot only a smart brain, but it still lacks hands and eyes—it doesn't know how to obtain external data or execute specific tasks.

LangChainIt is exactly such a framework, playing the role of a connector and coordinator.

LangChainIt cleverly connects powerful language models (such as GPT-4, DeepSeek) with external data sources, computational tools, and memory systems to build powerful, practical AI applications.

In simple terms, the core value of LangChain lies in:Making language models useful, which addresses several key limitations of large language models (LLMs):

  1. Real-time knowledge: LLM training data has a cutoff date and cannot obtain the latest information.
  2. Domain specialization: General LLMs lack private knowledge specific to an industry or company.
  3. Actionability: LLMs themselves cannot perform actions such as calculations, database queries, API calls, etc.
  4. Conversation coherence: In multi-turn conversations, LLMs need to remember previous chat history.

LangChain organizes these capabilities through a series of standardized chains and components, allowing developers to quickly build complex AI applications like building blocks.

Background Top: Your AI Application Your AI Application Arrow: from top to middle layer Middle layer: LangChain framework (large box) LangChain Framework First row modules Models Model Interfaces Prompts Prompts Chains Chains/Flows Second row modules Memory Memory Retrieval Retrieval Agents & Tools Agents and Tools Arrow: from middle layer to bottom Bottom: External Resources LLM API Vector Database External Tools/APIs Arrow Legend

LangChain Module Overview:

  • LLMs / ChatModels: Model Interfaces
  • Prompt Templates: Prompt Structuring
  • Chains: Workflow Orchestration
  • Memory: Context Management
  • Retrievers / VectorStores: Knowledge Retrieval
  • Agents & Tools: Automatic Decision-Making and Execution

Environment Preparation

Install using a domestic mirror:

pip install langchain langchain-openai langchain-community python-dotenv -i https://mirrors.aliyun.com/pypi/simple/

If you use OpenAI, you can configure environment variables:

export OPENAI_API_KEY=你的key

Then test with the following code:

Example

from langchain_openai import ChatOpenAI


llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)


resp = llm.invoke("Explain what LangChain is in one sentence")
print(resp.content)

In China, we can use the DeepSeek large model for testing. If you don't have one yet, you need to first go tohttps://platform.deepseek.com/api_keysCreate an API key.

Reference for DeepSeek API documentation:https://api-docs.deepseek.com/zh-cn/。

If you need to centrally manage multiple third-party models, you can choose to install LiteLLM as a model gateway:

pip install -U litellm

However, the examples in this article directly useChatOpenAItogether withopenai_api_baseparameter to connect to DeepSeek, without needing to install LiteLLM separately.

Example

import os
from langchain_openai import ChatOpenAI

# It is recommended to put the API Key in an environment variable, or assign it directly here (do not hard-code it in production).
# os.environ["DEEPSEEK_API_KEY"] = "sk-your-DeepSeek-secret-key"

# Initialize the model
llm = ChatOpenAI(
    model="deepseek-v4-pro",             # DeepSeek V4 model name
    openai_api_key="sk-your-Key",        # Fill in your DeepSeek API Key
    openai_api_base="https://api.deepseek.com", # DeepSeek API endpoint
    temperature=0.7,
    max_tokens=1024
)

# Test it out
response = llm.invoke("Hello, DeepSeek! Please give a brief self-introduction.")
print(response.content)

Executing the above code will output:

你好!很高兴认识你!

我是DeepSeek,由深度求索公司创造的AI助手。我的特点包括:
。。。

Building LCEL Chains (LangChain Expression Language)

Simply calling the model is not powerful enough; we need to build a standard processing pipeline:

Prompt -> LLM -> OutputParser
Background Step 1: User Input User Input {"concept": "quantum entanglement"} Arrow 1 + Pipe | Step 2: Prompt Template Prompt Template Fill variables Arrow 2 + Pipe | Step 3: ChatModel ChatModel DeepSeek / GPT Arrow 3 + Pipe | Step 4: OutputParser OutputParser Parse output Arrow 4 Step 5: Output Result Output Result Plain text Bottom note LCEL uses the pipe operator | to connect components, and data flows from left to right. Arrow definitions

The complete code is as follows:

Example

from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI

# 1. Define the model (Model)
llm = ChatOpenAI(
    model="deepseek-v4-pro",
    openai_api_key="your_DEEPSEEK_API_KEY",
    openai_api_base="https://api.deepseek.com"
)

# 2. Define the prompt template (Prompt)
# system: Set the AI's role
# user: The user's specific input
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a senior technical expert who excels at explaining complex technology concepts in plain, easy-to-understand language. Your explanation should include a vivid analogy."),
    ("user", "{concept}")
])

# 3. Define the output parser (Output Parser)
# Directly convert the model's Message object to a plain string
parser = StrOutputParser()

# 4. Build the chain (Chain) - use the pipe operator | to connect
# Flow: input dictionary -> fill Prompt -> send to LLM -> parse output
chain = prompt | llm | parser

# 5. Call Chain
concept_to_explain = Quantum Entanglement
print(fExplaining concept: {concept_to_explain}...\n")

result = chain.invoke({"concept": concept_to_explain})

print(--- DeepSeek Answer ---)
print(result)

Streaming Output

In real-world applications (such as chatbots), we need to output text character by character like ChatGPT, rather than displaying it all at once after generation is complete.

LangChain supports this very simply, using.stream()instead of.invoke():

Example

from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI

# 1. Define the Model
llm = ChatOpenAI(
    model="deepseek-v4-pro",
    openai_api_key=your_DEEPSEEK_API_KEY,
    openai_api_base="https://api.deepseek.com"
)

# 2. Define the Prompt Template
# system: Set the AI's role
# user: The user's specific input
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a senior technical expert, adept at explaining complex tech concepts in simple, easy-to-understand 'everyday language'. Your explanation should include a vivid metaphor."),
    ("user", "{concept}")
])

# 3. Define the Output Parser
# Directly convert the model's Message object into a plain string
parser = StrOutputParser()

# 4. Build the Chain - using the pipe | to connect
# Flow: input dictionary -> fill Prompt -> send to LLM -> parse output
chain = prompt | llm | parser

# 5. Call Chain
concept_to_explain = Quantum Entanglement
print(fExplaining concept: {concept_to_explain} (streaming output)...\n")

# Here, chunk is the snippet generated each time
for chunk in chain.stream({"concept": Recursive Neural Network}):
    print(chunk, end="", flush=True)

Code Structure

Prompt

  • ChatPromptTemplateClearly distinguishsystem / user
  • Prompt isstructured input function, not a string

LLM

  • ChatOpenAIjust a unified interface
  • LangChain does not enhance model capabilities, only enhancescontrollability

OutputParser

  • Model output is alwaysMessage
  • StrOutputParserYesexplicit type conversion
  • Without a Parser, the project is uncontrollable.

Core Components Explained

1. Models

LangChain provides a unified abstraction interface. Currently, it is mainly recommended to useChat Models, because current models (such as DeepSeek, GPT-4) are mostly optimized for conversation.

  • Chat Models: Input and output are structured messages.
  • SystemMessage: Set the AI's role.
  • HumanMessage: Messages sent by the user.
  • AIMessage: The message returned by AI.

2. Prompts

In the new version, it is recommended to useChatPromptTemplateto build conversational prompts, which better aligns with the calling conventions of current mainstream LLMs.

from langchain_core.prompts import ChatPromptTemplate

# 使用 from_messages 构建结构化模板
prompt_template = ChatPromptTemplate.from_messages([
    ("system", "你是一位专业的{role}。"),
    ("user", "{content}")
])

# 动态填充变量
prompt = prompt_template.invoke({"role": "美食评论家", "content": "评价一下这碗炸酱面"})
# 输出:包含 SystemMessage 和 HumanMessage 的列表

3. Chains & LCEL

Important updates: LLMChainhas been deprecated. Now useLCEL (pipe operator|)syntax. This approach is more flexible and supports asynchronous and streaming output.

# 现代写法:Prompt | Model | OutputParser
from langchain_core.output_parsers import StrOutputParser

# 这里的 llm 是 ChatOpenAI(model="deepseek-v4-pro") 的实例
chain = prompt_template | llm | StrOutputParser()

# 调用链
result = chain.invoke({"role": "导游", "content": "介绍一下故宫"})
print(result) # 直接输出字符串

4. Indexes & Retrieval

This isRAG (Retrieval-Augmented Generation)at the core. The new version emphasizes the process of transforming documents into "retrievers".

  • Process: DocumentLoaders(Loading) ->TextSplitter(Chunking) ->Embeddings(Vectorization) ->VectorStore(Storage).
  • Retriever: It is no longer just a database search; it is a component that can be integrated into a chain.
# 将向量库转为检索器
retriever = vectorstore.as_retriever()
# 在新版链中引用
# chain = {"context": retriever, "question": RunnablePassthrough()} | prompt | llm

Complete RAG practical example

Below we will implement a complete RAG application. First, we need to install additional dependencies:

pip install langchain-community faiss-cpu sentence-transformers -i https://mirrors.aliyun.com/pypi/simple/

Example

import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_community.vectorstores import FAISS
from langchain.text_splitter import RecursiveCharacterTextSplitter

# Load environment variables
load_dotenv()

# Initialize model and Embedding
llm = ChatOpenAI(
    model=os.getenv('DEEPSEEK_MODEL', 'deepseek-v4-pro'),
    openai_api_key=os.getenv('DEEPSEEK_API_KEY'),
    openai_api_base=os.getenv('DEEPSEEK_BASE_URL', 'https://api.deepseek.com'),
)

# Use a free local Embedding model (automatically downloaded on first run)
embeddings = HuggingFaceEmbeddings(
    model_name="sentence-transformers/all-MiniLM-L6-v2"
)

# --- 1. Prepare knowledge documents ---
# In real projects, these documents can be loaded from files, web pages, databases, etc.
documents = [
    "LangChain is an open-source framework for building large language model applications, created by Harrison Chase in 2022.",
    "LangChain's core components include: model interfaces, prompt templates, chains, memory, retrieval, and agents.",
    "LCEL (LangChain Expression Language) is LangChain's new generation chain-building syntax, using the pipe operator | to connect components.",
    "RAG (Retrieval-Augmented Generation) retrieves relevant documents before generation, allowing LLMs to answer questions beyond training data.",
    "LangGraph is a new framework released by the LangChain team, specifically for building complex multi-step AI agent workflows.",
    "LangSmith is LangChain's observability platform for debugging, testing, and monitoring LLM applications.",
]

# --- 2. Text chunking ---
text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=100,
    chunk_overlap=20
)
# The documents here are already short; in real scenarios, long documents are split into multiple segments
texts = text_splitter.create_documents(documents)

# --- 3. Vectorize and store into FAISS ---
vectorstore = FAISS.from_documents(texts, embeddings)

# --- 4. Create a retriever ---
retriever = vectorstore.as_retriever(search_kwargs={"k": 2})

# --- 5. Build RAG Chain ---
rag_prompt = ChatPromptTemplate.from_messages([
    ("system", You are a knowledge assistant. Answer the question based on the retrieved context below. If the answer is not in the context, say you don't know.\n\nContext: {context}),
    ("human", "{question}")
])

# Helper function: concatenate retrieved documents into a string
def format_docs(docs):
    return "\n".join(doc.page_content for doc in docs)

rag_chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | rag_prompt
    | llm
    | StrOutputParser()
)

# --- 6. Test RAG ---
question = What are the core components of LangChain?
print(fQuestion: {question})
print(fAnswer: {rag_chain.invoke(question)})
RAG Technical Key Points Explanation:
  • FAISSIt is Facebook's open-source vector similarity search library, suitable for local development and testing, with data stored in memory.
  • Production environmentIt is recommended to use professional vector databases such as Pinecone, Milvus, Chroma, and Weaviate, which support persistence and distributed deployment.
  • Embedding model: : This example uses HuggingFace'sall-MiniLM-L6-v2Model: will be automatically downloaded on first run (approximately 80MB).
  • If you have an OpenAI API Key, you can also use it.OpenAIEmbeddingsGet better Chinese results.
  • Recommended for domestic Chinese scenarios.BAAI/bge-small-zh-v1.5etc. Chinese embedding models
  • chunk_sizeandchunk_overlapNeed to tune according to the actual document characteristics; too large may lose precision, too small may lose context.

5. Memory

Important update:traditionalConversationBufferMemoryIt is relatively difficult to integrate in complex LCEL chains. The latest recommendation is to useChatMessageHistorycooperationRunnableWithMessageHistory。

  • Core logicMemory is no longer a property of the chain, but an independent message repository, throughsession_idDistinguish between different users.
  • persistenceSupports storing to Redis, PostgreSQL, or memory.

6. Agents

Agent is one of LangChain's most powerful features. Unlike Chain, agents can act based on user input.Autonomous decision-makingWhich tools to call, and in what order to execute, is the true "agent."

Agent's core loop: receive task -> think (choose tool) -> execute tool -> observe result -> continue thinking or give final answer.

Background Left: User Input User input Arrow: user input to thinking. Loop region background Agent decision loop Step 1: Think/Reason Think/Reason LLM analyzes the task Step 2: Select tool Select tool Decide which one to call Step 3: Execute tool Execute tool Call and get result Step 4: Observe result Observe result Analyze the tool's return Loop arrow Annotation: Need more information Need more Information Right side: Final answer Final answer Task complete Arrow: from observe result to final answer Task complete Arrow definition

Using@toolThe decorator can turn any Python function into a tool callable by an LLM. LangChain automatically passes the function name and docstring to the model, letting the model know when and how to use this tool.

Below is a complete Agent practical example, containing three tools: weather query, math calculation, and knowledge search:

Example

import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.tools import tool
from langchain.agents import AgentExecutor, create_tool_calling_agent

# Load environment variables
load_dotenv()

# Initialize DeepSeek model
llm = ChatOpenAI(
    model=os.getenv('DEEPSEEK_MODEL', 'deepseek-v4-pro'),
    openai_api_key=os.getenv('DEEPSEEK_API_KEY'),
    openai_api_base=os.getenv('DEEPSEEK_BASE_URL', 'https://api.deepseek.com'),
)

# --- Define tools ---
# Using the @tool decorator, LangChain automatically informs the model of the function name and docstring

@tool
def get_current_weather(city: str) -> str:
    """Get the current weather information for a specified city. Use this tool when the user asks about the weather."""
    # In real applications, a real weather API would be called here.
    weather_data = {
        "Beijing": "Sunny, temperature 28°C, humidity 45%",
        "Shanghai": "Cloudy, temperature 25°C, humidity 70%",
        "Shenzhen": "Thundershowers, temperature 30°C, humidity 85%",
    }
    return weather_data.get(city, f"Sorry, no weather data available for {city}.")

@tool
def calculate(expression: str) -> str:
    """Calculate mathematical expressions. Use this tool when the user needs to perform mathematical calculations.
Supports basic four arithmetic operations, e.g., '2 + 3 * 4'."""

    import ast
    import operator
   
    # Safe operator mapping
    ops = {
        ast.Add: operator.add,
        ast.Sub: operator.sub,
        ast.Mult: operator.mul,
        ast.Div: operator.truediv,
        ast.Pow: operator.pow,
        ast.USub: operator.neg,
    }
   
    def safe_eval(node):
        if isinstance(node, ast.Expression):
            return safe_eval(node.body)
        elif isinstance(node, ast.Constant):
            return node.value
        elif isinstance(node, ast.BinOp):
            left = safe_eval(node.left)
            right = safe_eval(node.right)
            return ops[type(node.op)](left, right)
        elif isinstance(node, ast.UnaryOp):
            operand = safe_eval(node.operand)
            return ops[type(node.op)](operand)
        else:
            raise ValueError(f"Unsupported expression type: {type(node)}")
   
    try:
        tree = ast.parse(expression, mode='eval')
        result = safe_eval(tree)
        return f"Calculation result: {expression} = {result}"
    except Exception as e:
        return f"Calculation error: {e}"

@tool
def search_knowledge(query: str) -> str:
    """Search the knowledge base to obtain relevant information. Use this tool when the user asks factual questions."""
    # In real applications, this would connect to a vector database or search engine.
    knowledge = {
        "LangChain": LangChain is an open-source framework for building LLM applications, supporting chained calls, memory management, and tool integration.,
        "Python": Python is a high-level programming language known for its simplicity and readability, widely used in AI, data science, and other fields.,
    }
    for key, value in knowledge.items():
        if key.lower() in query.lower():
            return value
    return f"No information related to '{query}' was found"

# --- Assemble tool list ---
tools = [get_current_weather, calculate, search_knowledge]

# --- Create Agent prompt ---
prompt = ChatPromptTemplate.from_messages([
    ("system", You are a powerful AI assistant that can use tools to help users. Based on the user's question, determine whether a tool is needed and choose the most appropriate tool to obtain information.),
    ("human", "{input}"),
    MessagesPlaceholder(variable_name="agent_scratchpad"),
])

# --- Create Agent ---
agent = create_tool_calling_agent(llm, tools, prompt)

# --- Create AgentExecutor (agent executor) ---
# verbose=True lets you see the Agent's full thinking process
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

# --- Test Agent ---
# Test 1: Weather query
print("=" * 50)
response = agent_executor.invoke({"input": What's the weather like in Beijing today?})
print(f"\nFinal answer: {response['output']}")

# Test 2: Math calculation
print("\n" + "=" * 50)
response = agent_executor.invoke({"input": Help me calculate what (15 * 37 + 228) / 3 equals})
print(f"\nFinal answer: {response['output']}")

# Test 3: Comprehensive question (Agent needs to decide which tool to use)
print("\n" + "=" * 50)
response = agent_executor.invoke({"input": What is LangChain? Also help me calculate 2 to the 10th power.})
print(f"\nFinal answer: {response['output']}")

Run the above code and you will see(Chain-of-thought output when verbose=True):

==================================================

> Entering new AgentExecutor chain...
Invoking: `get_current_weather` with `{'city': '北京'}`

晴天,气温 28°C,湿度 45%

根据查询结果,北京今天的天气是晴天,气温 28°C,湿度 45%。

> Finished chain.

Final answer: 北京今天天气晴朗,气温 28°C,湿度 45%,适合外出活动。

==================================================

> Entering new AgentExecutor chain...
Invoking: `calculate` with `{'expression': '(15 * 37 + 228) / 3'}`

计算结果:(15 * 37 + 228) / 3 = 261.0

> Finished chain.

Final answer: (15 * 37 + 228) / 3 的计算结果是 261.0

==================================================

> Entering new AgentExecutor chain...
Invoking: `search_knowledge` with `{'query': 'LangChain'}`
Invoking: `calculate` with `{'expression': '2 ** 10'}`

LangChain 是一个用于构建 LLM 应用的开源框架...
计算结果:2 ** 10 = 1024

> Finished chain.

Final answer: LangChain 是一个用于构建 LLM 应用的开源框架,支持链式调用、记忆管理和工具集成。另外,2 的 10 次方等于 1024。
Security tip:This example uses safe AST parsing instead of eval(). In real projects, never use eval() directly on user input, as this poses a serious remote code execution risk.
Understanding the Agent's "thinking process":
  • verbose=TrueThis lets us see the Agent's complete decision chain, making it easier to debug and understand its behavior.
  • The Agent automatically determines which tools to call, without requiring manual specification of the call order.
  • If the question doesn't require tools (e.g., simple small talk), the Agent will directly answer using its own knowledge.
  • For complex problems, the Agent may call tools multiple times, or even call multiple tools in parallel.

Quick Start: Build Your First LangChain Application

Let's experience the convenience of LangChain with a simple example.

In actual development, we can save API-related information in the project root directory's.envIn the file, for convenient use.

# .env 文件内容
DEEPSEEK_API_KEY=sk-xxx
DEEPSEEK_BASE_URL=https://api.deepseek.com
DEEPSEEK_MODEL=deepseek-v4-pro
When using LangChain's ChatOpenAI, it is recommended to configure openai_api_base as https://api.deepseek.com, no need to manually add /v1, LangChain will automatically concatenate the path.

Example 1: Basic Q&A Chain

Example

import os
from dotenv import load_dotenv
# Correct import path
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI

# Load environment variables
load_dotenv()

llm = ChatOpenAI(
    model=os.getenv('DEEPSEEK_MODEL'),
    openai_api_key=os.getenv('DEEPSEEK_API_KEY'),
    openai_api_base=os.getenv('DEEPSEEK_BASE_URL'),
)

# Create prompt template
template = """
You are a friendly assistant.
Please answer the following question:

Question: {question}
Answer:
"""

prompt = PromptTemplate.from_template(template)

# Create chain using LCEL syntax (recommended approach)
# Structure: Prompt -> LLM -> parse to string
chain = prompt | llm | StrOutputParser()

# Run the chain
question = "What is LangChain? What are its main uses?"

response = chain.invoke({"question": question})

print("Question:", question)
print("-" * 20)
print("Answer:", response)

Example 2: Conversation Chain with Memory

Let's create a simple chatbot that can remember conversation history.

Example

import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_community.chat_message_histories import ChatMessageHistory
from langchain_core.chat_history import BaseChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory

# 1. Load configuration
load_dotenv()

# 2. Initialize DeepSeek model
llm = ChatOpenAI(
    model=os.getenv('DEEPSEEK_MODEL', 'deepseek-v4-pro'),
    openai_api_key=os.getenv('DEEPSEEK_API_KEY'),
    openai_api_base=os.getenv('DEEPSEEK_BASE_URL', 'https://api.deepseek.com'),
)

# 3. Define prompt template
# MessagesPlaceholder will be filled with conversation history at runtime
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful AI assistant."),
    MessagesPlaceholder(variable_name="history"),
    ("human", "{question}"),
])

# 4. Build the chain
chain = prompt | llm

# 5. Manage memory: create a dictionary to store history records for different users
store = {}

def get_session_history(session_id: str) -> BaseChatMessageHistory:
    if session_id not in store:
        store[session_id] = ChatMessageHistory()
    return store[session_id]

# 6. Wrap our chain with RunnableWithMessageHistory
# This way LangChain will automatically handle reading and updating history records
with_message_history = RunnableWithMessageHistory(
    chain,
    get_session_history,
    input_messages_key="question",
    history_messages_key="history",
)

# 7. Enter the conversation loop
print("--- Entered DeepSeek chat mode (type 'exit' to quit) ---")
session_config = {"configurable": {"session_id": "user_001"}} # Distinguish IDs of different sessions

while True:
    user_input = input("You: ")
    if user_input.lower() in ["exit", "quit", "exit"]:
        break
       
    # Call the chain with memory
    response = with_message_history.invoke(
        {"question": user_input},
        config=session_config
    )
   
    print(f"AI: {response.content}\n")

Run the above code, you will see:

--- 已进入 DeepSeek 聊天模式 (输入 'exit' 退出) ---
You: 你好
AI: 你好!很高兴见到你! 有什么我可以帮助你的吗?无论是回答问题、聊天,还是协助解决问题,我都很乐意为你提供帮助!

You: 我叫小明
AI: 你好,小明!很高兴认识你! 
如果你有任何问题、想法,或者需要帮助的地方,随时告诉我哦~

You: 我叫什么记得吗?
AI: 当然记得!你刚刚告诉我你叫**小明**~  
我会认真记住我们的对话内容,在之后的交流中尽量保持上下文连贯。不过如果对话过长或间隔太久,我可能需要你提醒一下哦~  
有什么想聊的或需要帮助的吗? 

Summary

This article introduces the core concepts and practical applications of the LangChain framework. LangChain enables developers to quickly build powerful AI applications through standardized components and chained calls.

The following table summarizes the core knowledge points covered in this article:

Concept Description Key Code
LCEL Chain Use pipe operator | to connect Prompt, Model, Parser chain = prompt | llm | parser
Prompt template Structured Prompt, distinguishing system/user roles ChatPromptTemplate.from_messages()
Streaming output Word-by-word output to improve user experience chain.stream()
Conversation memory Manage multi-turn conversation history via session_id RunnableWithMessageHistory
RAG retrieval-augmented generation External knowledge integration to break through LLM knowledge boundaries retriever | format_docs
Agent Autonomous decision-making to call tools, the most core advanced feature create_tool_calling_agent()

Recommended Learning Path

Background Step 1 LangChain basics Arrow 1 Step 2 LCEL chain Arrow 2 Step 3 RAG application Arrow 3 Step 4 Agent Arrow 4 Step 5 LangGraph workflow Arrow 5 Step 6 LangSmith monitoring Arrow definitions
Other extensions