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

前提条件

在开始之前,请确保您已从模型提供商(例如,Anthropic、OpenAI)获取了API密钥。

第1步:安装依赖项

pip install deepagents tavily-python

步骤 2:设置您的API密钥

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

第3步:创建搜索工具

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,
    )

第4步:创建一个深度智能体

# 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
)

第5步:运行智能体

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应用的部署选项