Skip to main content
本指南将带您创建第一个具有规划能力、文件系统工具和子智能体功能的深度智能体。您将构建一个能够进行研究并撰写报告的研究智能体。

前置条件

开始之前,请确保您拥有模型提供商(例如 Anthropic、OpenAI)的 API 密钥。
深度智能体需要支持工具调用的模型。请参阅自定义配置了解如何配置模型。

第一步:安装依赖

pip install deepagents tavily-python
本指南以 Tavily 作为示例搜索提供商,您也可以替换为任何其他搜索 API(例如 DuckDuckGo、SerpAPI、Brave Search)。

第二步:设置 API 密钥

export ANTHROPIC_API_KEY="your-api-key"
export TAVILY_API_KEY="your-tavily-api-key"

第三步:创建搜索工具

import os
from typing import Literal
from tavily import TavilyClient
from deepagents import create_deep_agent

tavily_client = TavilyClient(api_key=os.environ["TAVILY_API_KEY"])

def internet_search(
    query: str,
    max_results: int = 5,
    topic: Literal["general", "news", "finance"] = "general",
    include_raw_content: bool = False,
):
    """Run a web search"""
    return tavily_client.search(
        query,
        max_results=max_results,
        include_raw_content=include_raw_content,
        topic=topic,
    )

第四步:创建深度智能体

# System prompt to steer the agent to be an expert researcher
research_instructions = """You are an expert researcher. Your job is to conduct thorough research and then write a polished report.

You have access to an internet search tool as your primary means of gathering information.

## `internet_search`

Use this to run an internet search for a given query. You can specify the max number of results to return, the topic, and whether raw content should be included.
"""

agent = create_deep_agent(
    tools=[internet_search],
    system_prompt=research_instructions
)

第五步:运行智能体

result = agent.invoke({"messages": [{"role": "user", "content": "What is langgraph?"}]})

# Print the agent's response
print(result["messages"][-1].content)

工作原理

您的深度智能体会自动:
  1. 规划方案 — 使用内置的 write_todos 工具分解研究任务。
  2. 执行研究 — 调用 internet_search 工具收集信息。
  3. 管理上下文 — 使用文件系统工具(write_fileread_file)将大量搜索结果卸载到文件中。
  4. 派生子智能体 — 按需将复杂子任务委托给专门的子智能体。
  5. 综合报告 — 将研究发现整理成连贯的回复。

示例

有关可使用深度智能体构建的智能体、模式和应用,请参阅 Examples

流式输出

深度智能体内置了 流式输出 功能,可通过 LangGraph 实时获取智能体执行的更新。 您可以逐步观察输出,并审查和调试智能体及子智能体的工作,例如工具调用、工具结果和 LLM 响应。

后续步骤

构建好第一个深度智能体后,您可以:
  • 自定义智能体:了解自定义配置选项,包括自定义系统提示、工具和子智能体。
  • 添加长期记忆:在对话之间启用持久记忆
  • 部署到生产环境:了解 LangGraph 应用的部署选项