Quickstart¶
快速構建您的第一個 deep agents。
本指南將引導您使用規劃、檔案系統工具和子智能體功能來建立您的第一個 deep agents。您將建立一個能夠執行研究並撰寫報告的研究型智能體。
Prerequisites¶
在開始之前,請確保您已從模型提供者(例如 Anthropic, OpenAI)處取得 API 金鑰。
Step 1: Install dependencies¶
Step 2: Set up your API keys¶
Step 3: Create a search tool¶
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,
)
Step 4: Create a deep agent¶
# 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
)
Step 5: Run the agent¶
result = agent.invoke({"messages": [{"role": "user", "content": "What is langgraph?"}]})
# Print the agent's response
print(result["messages"][-1].content)
What happened?¶
您的深度代理會自動執行以下操作:
- Planned its approach: 使用內建的
write_todos工具將研究任務分解成多個部分。 - Conducted research: 呼叫互聯網搜尋工具來收集信息
- Managed context: 使用檔案系統工具(
write_file,read_file)卸載大量搜尋結果 - Spawned subagents (if needed): 將複雜的子任務委派給專門的子代理人
- Synthesized a report: 將調查結果匯總成一份連貫的回應