LangChain 总览
LangChain 是最容易的方式去开发创建llm增强的agents和应用。仅仅只需要十几行代码就可以连接OpenAI, OpenAI, Anthropic, Google, and more. LangChain 提供了一个预构建(pre-built)agent架构和模型集成,帮助你快速开始,并无缝地将大型语言模型(LLMs)整合到你的agents和应用程序中。
如果想要快速创建agent和自动化应用,推荐使用LangChain。当你有更高级的需求,需要结合确定性工作流和代理工作流、大量的定制以及精确控制延迟时,可以使用 LangGraph,这是我们低级别的代理编排框架和运行时( runtime)。
LangChain agents 构建在LangGraph 之上,提供了持久执行、流streaming,人机交互 human-in-the-loop, 持久化persistence, 等等更多。 基础LangChain agent 的使用的话,可以不需要了解LangGraph 。
安装
pip install -U langchain
# Requires Python 3.10+
创建一个agent
# pip install -qU "langchain[anthropic]" to call the model
from langchain.agents import create_agent
def get_weather(city: str) -> str:
"""Get weather for a given city."""
return f"It's always sunny in {city}!"
agent = create_agent(
model="claude-sonnet-4-5-20250929",
tools=[get_weather],
system_prompt="You are a helpful assistant",
)
# Run the agent
agent.invoke(
{"messages": [{"role": "user", "content": "what is the weather in sf"}]}
)
阿里百炼大模型集成
from pprint import pprint
# pip install -qU "langchain[anthropic]" to call the model
from langchain.agents import create_agent
# 百炼 LLM
from langchain_community.chat_models.tongyi import ChatTongyi
# 1. 选模型(百炼目前支持 qwen-turbo、qwen-plus、qwen-max 等)
chatLLM = ChatTongyi(
api_key="sk-3**********************a",
model="qwen-plus", # 此处以qwen-plus为例,您可按需更换模型名称。模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
streaming=True,
# other params...
)
def get_weather(city: str) -> str:
"""Get weather for a given city."""
return f"It's always sunny in {city}!"
agent = create_agent(
model=chatLLM,
tools=[get_weather],
system_prompt="You are a helpful assistant",
)
# Run the agent
resp = agent.invoke(
{"messages": [{"role": "user", "content": "what is the weather in sf"}]}
)
pprint(resp)
阿里通义千问支持两种Chat Model的创建方式。
OpenAI 和 DashScope
pip install langchain_openai
from langchain_openai import ChatOpenAI
import os
chatLLM = ChatOpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
model="qwen-plus", # 此处以qwen-plus为例,您可按需更换模型名称。模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
# other params...
)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "你是谁?"}]
response = chatLLM.invoke(messages)
print(response.json())
pip install langchain-community
pip install dashscope
from langchain_community.chat_models.tongyi import ChatTongyi
from langchain_core.messages import HumanMessage
chatLLM = ChatTongyi(
model="qwen-plus", # 此处以qwen-plus为例,您可按需更换模型名称。模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
streaming=True,
# other params...
)
res = chatLLM.stream([HumanMessage(content="hi")], streaming=True)
for r in res:
print("chat resp:", r.content)
核心优势
标准模型接口
不同的供应商有自己的一套大模型交互APIs,包括回复的格式。LangChain标准化了你如何和模型交互,这样你就可以丝滑切换供应商接口。
使用简单,高度扩展的agent
LangChain的agent抽象设计使得你很容易开发一个简单agent,仅需要10行代码。但是它也会提供足够的灵活性做你想要的有上下文的工程。
构建在LangGraph之上
LangChain的代理构建在LangGraph之上,让我们可以利用LangGraph优势:持续执行,人机交互,持久化等。
用LangSmith来Debug
使用可视化工具来跟踪执行路径、捕获状态转换,提供详细运行时度量metrics,可视化工具可以更深入观察复杂agent的行为。
以编程的方式使用文档
The LangChain docs MCP server is available at:https://docs.langchain.com/mcp
使用VSCode 的MCP -doc文档
{
"mcpServers": {
"docs-langchain": {
"url": "https://docs.langchain.com/mcp"
}
}
}
更多推荐


所有评论(0)