跳到主要内容
本指南将引导您创建一个具有规划、文件系统工具和子智能体功能的深度智能体。您将构建一个能够进行研究和撰写报告的研究智能体。

先决条件

在开始之前,请确保您拥有模型提供商(例如 Anthropic、OpenAI)的 API 密钥。

第一步:安装依赖项

pip install deepagents tavily-python

第二步:设置您的 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. 综合了报告:将研究结果汇编成连贯的响应

后续步骤

现在您已经构建了您的第一个深度智能体
  • 自定义您的智能体:了解自定义选项,包括自定义系统提示、工具和子智能体。
  • 了解中间件:深入了解驱动深度智能体的中间件架构
  • 添加长期记忆:启用跨对话的持久记忆
  • 部署到生产环境:了解 LangGraph 应用程序的部署选项

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.