🤖 LangCrew 入门教程
01.AI 开源的多 Agent 协作框架,快速上手

📅 2026-06-27 | 🏷️ 学习专区,AI工具,LangCrew,智能体,教程

📖 一、LangCrew 是什么

LangCrew01.AI(零一万物,李开复创办) 推出的多 Agent 开发框架,基于 LangGraph 构建。用 Python 代码来编排多个 AI 智能体协同工作。

一句话概括

LangCrew = 用 Python 代码让多个 AI 智能体像团队一样分工合作。
比如:一个搜索资料 → 一个分析数据 → 一个写报告 → 一个审核质量。

📦 二、安装

# 极简安装
pip install langcrew

我们已在 186 服务器上安装好了,激活环境即可使用:

source /home/a1234/langcrew-env/bin/activate

🎯 三、核心概念

LangCrew 只有 4 个核心概念,非常好理解:

概念比喻说明
Agent👤 团队成员一个 AI 角色,有角色、目标、背景故事
Task📋 工作任务分配给某个 Agent 的具体任务
Crew🏢 项目组一组 Agent + 一组 Task 的组合
Process🔄 工作流程Agent 之间的协作方式(顺序/层级)

🚀 四、快速上手:第一个 LangCrew 程序

示例 1:最简单的 2-Agent 协作

from langcrew import Crew, Agent, Task

# 1. 创建两个 Agent
analyst = Agent(
    role="分析师",                    # 角色名称
    goal="分析用户的问题并提供见解",    # 目标
    backstory="你是一个资深分析师"     # 背景故事
)

writer = Agent(
    role="写手",
    goal="撰写清晰易懂的文章",
    backstory="你是一个专业的科技写手"
)

# 2. 创建两个 Task
research = Task(
    description="分析人工智能的发展趋势",
    expected_output="一份简要的分析报告",
    agent=analyst                     # 分配给分析师
)

report = Task(
    description="基于分析报告写一篇科普文章",
    expected_output="一篇通俗易懂的文章",
    agent=writer                      # 分配给写手
)

# 3. 组建 Crew 并执行
crew = Crew(
    agents=[analyst, writer],         # 团队成员
    tasks=[research, report],         # 任务列表
    verbose=True                      # 显示执行过程
)

result = crew.kickoff()               # 启动!
print(result)

示例 2:带工具的 Agent

from langcrew import Crew, Agent, Task

# Agent 可以配置工具(需要先配置 LLM)
researcher = Agent(
    role="研究员",
    goal="搜索并整理最新信息",
    backstory="你擅长从各种来源获取信息",
    tools=[],                         # 可以传入搜索等工具
    allow_delegation=True              # 允许委托任务给其他 Agent
)

# 配置 LLM(以 DeepSeek 为例)
from langcrew import LLMConfig
llm = LLMConfig(
    provider="openai",
    api_key="sk-xxx",
    base_url="https://api.deepseek.com/v1",
    model="deepseek-chat"
)

crew = Crew(
    agents=[researcher],
    tasks=[Task(description="搜索量子计算最新进展", expected_output="摘要")],
    llm=llm
)

💼 五、实际应用场景

📝 自动写文章

研究员搜索资料 → 分析师整理 → 写手撰写 → 编辑审核

💻 自动编程

架构师设计 → 程序员编码 → 测试员测试 → 审查代码

📊 数据分析

数据采集 → 清洗 → 分析 → 可视化 → 报告

🎯 招聘筛选

简历筛选 → 技能评估 → 面试问题生成 → 评分

⚡ 六、高级功能

人机协同(HITL)

LangCrew 的特色功能——Agent 执行到关键步骤时,可以暂停等待人工审批:

from langcrew import Crew, Agent, Task, Process

task = Task(
    description="生成一份投资报告",
    expected_output="完整的投资分析报告",
    human_input=True,        # ← 需要人工确认
    agent=analyst
)

crew = Crew(
    agents=[analyst, writer],
    tasks=[task, report_task],
    process=Process.sequential  # 顺序执行
)

记忆管理

Agent 可以记住之前的对话和任务结果:

from langcrew import Crew, Agent, Task
from langcrew.memory import ShortTermMemory, LongTermMemory

agent = Agent(
    role="客服",
    goal="解决用户问题",
    backstory="你是一个专业的客服",
    memory=True,                        # 开启记忆
    short_term_memory=ShortTermMemory(), # 短期记忆
    long_term_memory=LongTermMemory()    # 长期记忆
)

多流程模式

模式说明适合
sequential任务按顺序执行,前一个输出给后一个流水线作业
hierarchical有管理 Agent 分配任务给下属团队管理
consensus多个 Agent 讨论达成共识决策讨论

🔧 七、配置 LLM

LangCrew 需要连一个大模型才能工作。支持多种模型:

from langcrew import LLMConfig

# DeepSeek(推荐,便宜)
llm = LLMConfig(
    provider="openai",
    api_key="sk-xxx",
    base_url="https://api.deepseek.com/v1",
    model="deepseek-chat"
)

# 通义千问
llm = LLMConfig(
    provider="openai",
    api_key="sk-xxx",
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    model="qwen-plus"
)

# 本地 Ollama
llm = LLMConfig(
    provider="ollama",
    base_url="http://localhost:11434",
    model="qwen2.5:7b"
)

# 环境变量方式(推荐)
import os
os.environ["OPENAI_API_KEY"] = "sk-xxx"
os.environ["OPENAI_BASE_URL"] = "https://api.deepseek.com/v1"

crew = Crew(
    agents=[...],
    tasks=[...],
    # 不传 llm 参数,自动读取环境变量
)

🎬 八、完整示例:自动写投资分析报告

from langcrew import Crew, Agent, Task, Process
from langcrew import LLMConfig

# 配置模型
llm = LLMConfig(
    provider="openai",
    api_key=os.getenv("DEEPSEEK_API_KEY"),
    base_url="https://api.deepseek.com/v1",
    model="deepseek-chat"
)

# 创建 3 个 Agent
researcher = Agent(
    role="研究员",
    goal="收集并整理最新市场信息",
    backstory="你擅长从各种渠道获取信息",
    llm=llm
)

analyst = Agent(
    role="分析师",
    goal="分析数据并给出投资建议",
    backstory="你是一个资深金融分析师",
    llm=llm
)

writer = Agent(
    role="报告撰写人",
    goal="撰写专业的投资分析报告",
    backstory="你擅长写清晰的投资报告",
    llm=llm
)

# 创建任务
task1 = Task(
    description="收集 NVIDIA 公司的最新财务数据和市场动态",
    expected_output="一份数据清单",
    agent=researcher
)

task2 = Task(
    description="分析数据,评估 NVIDIA 的投资价值",
    expected_output="分析结论和建议",
    agent=analyst
)

task3 = Task(
    description="撰写一份完整的投资分析报告",
    expected_output="PDF 格式的投资报告",
    agent=writer
)

# 组建 Crew
crew = Crew(
    agents=[researcher, analyst, writer],
    tasks=[task1, task2, task3],
    process=Process.sequential,  # 顺序执行
    verbose=True
)

# 执行
result = crew.kickoff()
print(f"最终输出: {result}")

⚠️ 九、注意事项

  • 需要 API Key:LangCrew 本身不包含模型,需要配置 LLM(DeepSeek / 通义千问 / Ollama 等)
  • 186 服务器上已安装:激活命令 source /home/a1234/langcrew-env/bin/activate
  • 轻量:纯 Python 包,不占多少资源
  • 可集成:可以直接集成到 Ascent Creator 或其他 Python 项目中

📎 官方仓库:github.com/01-ai/langcrew