LangChain 核心组件详解
学习 LangChain 的基本构建模块,包括 LLM、提示词、链和智能体,以及它们如何交互
LangChain 核心组件详解 是 CoddyKit 上的免费 AI Agents with LangChain & Autonomous Workflows 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Agents with LangChain & Autonomous Workflows 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Agents with LangChain & Autonomous Workflows 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Welcome to LangChain Core!
Now meet LangChain, a framework for building agents. It gives you modular components you snap together like LEGO bricks.
The Brain: Large Language Models (LLMs)
The agent's brain is the LLM — a model trained on vast text to understand and generate language. LangChain makes it easy to plug into GPT or open-source models.
LLM in Action: A Simple Call
Here's a basic LLM call in LangChain, using a fake model so it runs without any API key.
from langchain_core.llms import FakeListLLM
# Our 'brain' that gives predefined answers
llm = FakeListLLM(responses=[
"The capital of France is Paris.",
"Hello! How can I help you today?"
])
# Ask the LLM a question
response = llm.invoke("What is the capital of France?")
print(response)Guiding the Brain: Prompts
An LLM needs instructions, and those are your prompts — the input text you feed it. Good prompts are crucial for getting the output you want.
Dynamic Prompts: Prompt Templates
For input that changes, use a prompt template: a reusable blueprint with placeholders you fill in at runtime.
from langchain_core.prompts import PromptTemplate
# Define a template with a placeholder '{topic}'
qa_template = """
Answer the following question about {topic}:
Question: {question}
"""
# Create a PromptTemplate object
prompt = PromptTemplate.from_template(qa_template)
# Format the prompt with specific values
formatted_prompt = prompt.format(
topic="Python programming",
question="What is a variable?"
)
print(formatted_prompt)Connecting Steps: Chains
Most agent tasks take several steps. A chain is a predefined sequence that wires LLMs, prompts, and utilities together, passing each output into the next step.
Building a Simple Chain
Here's a simple chain: a prompt template piped into an LLM using LangChain's Runnable interface. The prompt's output feeds straight into the model.
from langchain_core.llms import FakeListLLM
from langchain_core.prompts import PromptTemplate
# 1. Our 'brain'
llm = FakeListLLM(responses=["A variable is a named storage location."])
# 2. Our prompt template
qa_template = """
Explain {concept} in simple terms.
"""
prompt = PromptTemplate.from_template(qa_template)
# 3. Combine them into a chain using '|' operator
# The prompt output goes into the LLM as input
chain = prompt | llm
# Invoke the chain with the input for the template
response = chain.invoke({"concept": "variable"})
print(response)The Orchestrator: Agents
Chains run fixed steps; agents are dynamic. An agent decides which action to take, runs it, observes the result, and repeats until the goal is met.
How Agents Use Components
Agents tie it all together: LLMs for reasoning, prompts to guide thinking, chains for multi-step work, and tools to act on the real world.
Putting it All Together
Picture a smart assistant: a prompt sets the task, the LLM reasons, a chain processes input, and the agent picks a weather tool — then the LLM formats the answer.
Quick Check: Core Components
Which LangChain component is primarily responsible for dynamic decision-making and tool selection in an AI application?
Recap & Next Steps
Recap: the four LangChain building blocks — LLMs (the brain), prompts (instructions), chains (sequences), and agents (dynamic deciders). Next: build one.
常见问题解答
「LangChain 核心组件详解」课时是免费的吗?
是的 — 「LangChain 核心组件详解」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Agents with LangChain & Autonomous Workflows 课程的其余内容,请升级到 CoddyKit PRO。 AI Agents with LangChain & Autonomous Workflows 课程共包含 4 节课。
「LangChain 核心组件详解」这节课中我会学到什么?
学习 LangChain 的基本构建模块,包括 LLM、提示词、链和智能体,以及它们如何交互 你通过在浏览器中直接运行的动手代码来练习 AI Agents with LangChain & Autonomous Workflows,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 AI Agents with LangChain & Autonomous Workflows 需要有经验吗?
无需任何先前经验。CoddyKit 上的 AI Agents with LangChain & Autonomous Workflows 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「LangChain 核心组件详解」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 AI Agents with LangChain & Autonomous Workflows 课中编写并运行代码吗?
能。每节 AI Agents with LangChain & Autonomous Workflows 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 理解人工智能代理与 LLM
- LangChain 核心组件详解
- 构建您的第一个简单智能体
- 为代理添加记忆与对话状态