开始使用
开始使用
安装Langchain
在沙箱环境中(.venv\Scripts\activate)执行:
pip install -U "langchain[anthropic]"
WARNING: The directory '/home/jovyan/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag.
Requirement already satisfied: langchain[anthropic] in /opt/conda/lib/python3.11/site-packages (1.3.1)
Requirement already satisfied: langchain-core<2.0.0,>=1.4.0 in /opt/conda/lib/python3.11/site-packages (from langchain[anthropic]) (1.4.0)
Requirement already satisfied: langgraph<1.3.0,>=1.2.0 in /opt/conda/lib/python3.11/site-packages (from langchain[anthropic]) (1.2.1)
Requirement already satisfied: pydantic<3.0.0,>=2.7.4 in /opt/conda/lib/python3.11/site-packages (from langchain[anthropic]) (2.13.4)
Requirement already satisfied: langchain-anthropic in /opt/conda/lib/python3.11/site-packages (from langchain[anthropic]) (1.4.3)
Requirement already satisfied: jsonpatch<2.0.0,>=1.33.0 in /opt/conda/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.4.0->langchain[anthropic]) (1.33)
Requirement already satisfied: langchain-protocol>=0.0.14 in /opt/conda/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.4.0->langchain[anthropic]) (0.0.15)
Requirement already satisfied: langsmith<1.0.0,>=0.3.45 in /opt/conda/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.4.0->langchain[anthropic]) (0.8.5)
Requirement already satisfied: packaging>=23.2.0 in /opt/conda/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.4.0->langchain[anthropic]) (23.2)
Requirement already satisfied: pyyaml<7.0.0,>=5.3.0 in /opt/conda/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.4.0->langchain[anthropic]) (6.0.1)
Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in /opt/conda/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.4.0->langchain[anthropic]) (9.1.4)
Requirement already satisfied: typing-extensions<5.0.0,>=4.7.0 in /opt/conda/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.4.0->langchain[anthropic]) (4.15.0)
Requirement already satisfied: uuid-utils<1.0,>=0.12.0 in /opt/conda/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.4.0->langchain[anthropic]) (0.16.0)
Requirement already satisfied: langgraph-checkpoint<5.0.0,>=4.1.0 in /opt/conda/lib/python3.11/site-packages (from langgraph<1.3.0,>=1.2.0->langchain[anthropic]) (4.1.1)
Requirement already satisfied: langgraph-prebuilt<1.2.0,>=1.1.0 in /opt/conda/lib/python3.11/site-packages (from langgraph<1.3.0,>=1.2.0->langchain[anthropic]) (1.1.0)
Requirement already satisfied: langgraph-sdk<0.4.0,>=0.3.0 in /opt/conda/lib/python3.11/site-packages (from langgraph<1.3.0,>=1.2.0->langchain[anthropic]) (0.3.15)
Requirement already satisfied: xxhash>=3.5.0 in /opt/conda/lib/python3.11/site-packages (from langgraph<1.3.0,>=1.2.0->langchain[anthropic]) (3.7.0)
Requirement already satisfied: annotated-types>=0.6.0 in /opt/conda/lib/python3.11/site-packages (from pydantic<3.0.0,>=2.7.4->langchain[anthropic]) (0.7.0)
Requirement already satisfied: pydantic-core==2.46.4 in /opt/conda/lib/python3.11/site-packages (from pydantic<3.0.0,>=2.7.4->langchain[anthropic]) (2.46.4)
Requirement already satisfied: typing-inspection>=0.4.2 in /opt/conda/lib/python3.11/site-packages (from pydantic<3.0.0,>=2.7.4->langchain[anthropic]) (0.4.2)
Requirement already satisfied: anthropic<1.0.0,>=0.96.0 in /opt/conda/lib/python3.11/site-packages (from langchain-anthropic->langchain[anthropic]) (0.104.1)
Requirement already satisfied: anyio<5,>=3.5.0 in /opt/conda/lib/python3.11/site-packages (from anthropic<1.0.0,>=0.96.0->langchain-anthropic->langchain[anthropic]) (4.13.0)
Requirement already satisfied: distro<2,>=1.7.0 in /opt/conda/lib/python3.11/site-packages (from anthropic<1.0.0,>=0.96.0->langchain-anthropic->langchain[anthropic]) (1.9.0)
Requirement already satisfied: docstring-parser<1,>=0.15 in /opt/conda/lib/python3.11/site-packages (from anthropic<1.0.0,>=0.96.0->langchain-anthropic->langchain[anthropic]) (0.18.0)
Requirement already satisfied: httpx<1,>=0.25.0 in /opt/conda/lib/python3.11/site-packages (from anthropic<1.0.0,>=0.96.0->langchain-anthropic->langchain[anthropic]) (0.28.1)
Requirement already satisfied: jiter<1,>=0.4.0 in /opt/conda/lib/python3.11/site-packages (from anthropic<1.0.0,>=0.96.0->langchain-anthropic->langchain[anthropic]) (0.15.0)
Requirement already satisfied: sniffio in /opt/conda/lib/python3.11/site-packages (from anthropic<1.0.0,>=0.96.0->langchain-anthropic->langchain[anthropic]) (1.3.0)
Requirement already satisfied: jsonpointer>=1.9 in /opt/conda/lib/python3.11/site-packages (from jsonpatch<2.0.0,>=1.33.0->langchain-core<2.0.0,>=1.4.0->langchain[anthropic]) (2.4)
Requirement already satisfied: ormsgpack>=1.12.0 in /opt/conda/lib/python3.11/site-packages (from langgraph-checkpoint<5.0.0,>=4.1.0->langgraph<1.3.0,>=1.2.0->langchain[anthropic]) (1.12.2)
Requirement already satisfied: orjson>=3.11.5 in /opt/conda/lib/python3.11/site-packages (from langgraph-sdk<0.4.0,>=0.3.0->langgraph<1.3.0,>=1.2.0->langchain[anthropic]) (3.11.9)
Requirement already satisfied: requests-toolbelt>=1.0.0 in /opt/conda/lib/python3.11/site-packages (from langsmith<1.0.0,>=0.3.45->langchain-core<2.0.0,>=1.4.0->langchain[anthropic]) (1.0.0)
Requirement already satisfied: requests>=2.0.0 in /opt/conda/lib/python3.11/site-packages (from langsmith<1.0.0,>=0.3.45->langchain-core<2.0.0,>=1.4.0->langchain[anthropic]) (2.31.0)
Requirement already satisfied: zstandard>=0.23.0 in /opt/conda/lib/python3.11/site-packages (from langsmith<1.0.0,>=0.3.45->langchain-core<2.0.0,>=1.4.0->langchain[anthropic]) (0.25.0)
Requirement already satisfied: idna>=2.8 in /opt/conda/lib/python3.11/site-packages (from anyio<5,>=3.5.0->anthropic<1.0.0,>=0.96.0->langchain-anthropic->langchain[anthropic]) (3.4)
Requirement already satisfied: certifi in /opt/conda/lib/python3.11/site-packages (from httpx<1,>=0.25.0->anthropic<1.0.0,>=0.96.0->langchain-anthropic->langchain[anthropic]) (2023.7.22)
Requirement already satisfied: httpcore==1.* in /opt/conda/lib/python3.11/site-packages (from httpx<1,>=0.25.0->anthropic<1.0.0,>=0.96.0->langchain-anthropic->langchain[anthropic]) (1.0.9)
Requirement already satisfied: h11>=0.16 in /opt/conda/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.25.0->anthropic<1.0.0,>=0.96.0->langchain-anthropic->langchain[anthropic]) (0.16.0)
Requirement already satisfied: charset-normalizer<4,>=2 in /opt/conda/lib/python3.11/site-packages (from requests>=2.0.0->langsmith<1.0.0,>=0.3.45->langchain-core<2.0.0,>=1.4.0->langchain[anthropic]) (3.3.0)
Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.11/site-packages (from requests>=2.0.0->langsmith<1.0.0,>=0.3.45->langchain-core<2.0.0,>=1.4.0->langchain[anthropic]) (2.0.7)
Note: you may need to restart the kernel to use updated packages.
安装Deepagents
pip install deepagents
WARNING: The directory '/home/jovyan/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag.
Requirement already satisfied: deepagents in /opt/conda/lib/python3.11/site-packages (0.6.3)
Requirement already satisfied: langchain-core<2.0.0,>=1.4.0 in /opt/conda/lib/python3.11/site-packages (from deepagents) (1.4.0)
Requirement already satisfied: langsmith>=0.8.3 in /opt/conda/lib/python3.11/site-packages (from deepagents) (0.8.5)
Requirement already satisfied: langchain<2.0.0,>=1.3.0 in /opt/conda/lib/python3.11/site-packages (from deepagents) (1.3.1)
Requirement already satisfied: langchain-anthropic<2.0.0,>=1.4.3 in /opt/conda/lib/python3.11/site-packages (from deepagents) (1.4.3)
Requirement already satisfied: langchain-google-genai<5.0.0,>=4.2.2 in /opt/conda/lib/python3.11/site-packages (from deepagents) (4.2.3)
Requirement already satisfied: wcmatch in /opt/conda/lib/python3.11/site-packages (from deepagents) (10.1)
Requirement already satisfied: langgraph<1.3.0,>=1.2.0 in /opt/conda/lib/python3.11/site-packages (from langchain<2.0.0,>=1.3.0->deepagents) (1.2.1)
Requirement already satisfied: pydantic<3.0.0,>=2.7.4 in /opt/conda/lib/python3.11/site-packages (from langchain<2.0.0,>=1.3.0->deepagents) (2.13.4)
Requirement already satisfied: anthropic<1.0.0,>=0.96.0 in /opt/conda/lib/python3.11/site-packages (from langchain-anthropic<2.0.0,>=1.4.3->deepagents) (0.104.1)
Requirement already satisfied: jsonpatch<2.0.0,>=1.33.0 in /opt/conda/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.4.0->deepagents) (1.33)
Requirement already satisfied: langchain-protocol>=0.0.14 in /opt/conda/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.4.0->deepagents) (0.0.15)
Requirement already satisfied: packaging>=23.2.0 in /opt/conda/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.4.0->deepagents) (23.2)
Requirement already satisfied: pyyaml<7.0.0,>=5.3.0 in /opt/conda/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.4.0->deepagents) (6.0.1)
Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in /opt/conda/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.4.0->deepagents) (9.1.4)
Requirement already satisfied: typing-extensions<5.0.0,>=4.7.0 in /opt/conda/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.4.0->deepagents) (4.15.0)
Requirement already satisfied: uuid-utils<1.0,>=0.12.0 in /opt/conda/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.4.0->deepagents) (0.16.0)
Requirement already satisfied: filetype<2.0.0,>=1.2.0 in /opt/conda/lib/python3.11/site-packages (from langchain-google-genai<5.0.0,>=4.2.2->deepagents) (1.2.0)
Requirement already satisfied: google-genai<2.0.0,>=1.65.0 in /opt/conda/lib/python3.11/site-packages (from langchain-google-genai<5.0.0,>=4.2.2->deepagents) (1.75.0)
Requirement already satisfied: httpx<1,>=0.23.0 in /opt/conda/lib/python3.11/site-packages (from langsmith>=0.8.3->deepagents) (0.28.1)
Requirement already satisfied: orjson>=3.9.14 in /opt/conda/lib/python3.11/site-packages (from langsmith>=0.8.3->deepagents) (3.11.9)
Requirement already satisfied: requests-toolbelt>=1.0.0 in /opt/conda/lib/python3.11/site-packages (from langsmith>=0.8.3->deepagents) (1.0.0)
Requirement already satisfied: requests>=2.0.0 in /opt/conda/lib/python3.11/site-packages (from langsmith>=0.8.3->deepagents) (2.31.0)
Requirement already satisfied: xxhash>=3.0.0 in /opt/conda/lib/python3.11/site-packages (from langsmith>=0.8.3->deepagents) (3.7.0)
Requirement already satisfied: zstandard>=0.23.0 in /opt/conda/lib/python3.11/site-packages (from langsmith>=0.8.3->deepagents) (0.25.0)
Requirement already satisfied: bracex>=2.1.1 in /opt/conda/lib/python3.11/site-packages (from wcmatch->deepagents) (2.6)
Requirement already satisfied: anyio<5,>=3.5.0 in /opt/conda/lib/python3.11/site-packages (from anthropic<1.0.0,>=0.96.0->langchain-anthropic<2.0.0,>=1.4.3->deepagents) (4.13.0)
Requirement already satisfied: distro<2,>=1.7.0 in /opt/conda/lib/python3.11/site-packages (from anthropic<1.0.0,>=0.96.0->langchain-anthropic<2.0.0,>=1.4.3->deepagents) (1.9.0)
Requirement already satisfied: docstring-parser<1,>=0.15 in /opt/conda/lib/python3.11/site-packages (from anthropic<1.0.0,>=0.96.0->langchain-anthropic<2.0.0,>=1.4.3->deepagents) (0.18.0)
Requirement already satisfied: jiter<1,>=0.4.0 in /opt/conda/lib/python3.11/site-packages (from anthropic<1.0.0,>=0.96.0->langchain-anthropic<2.0.0,>=1.4.3->deepagents) (0.15.0)
Requirement already satisfied: sniffio in /opt/conda/lib/python3.11/site-packages (from anthropic<1.0.0,>=0.96.0->langchain-anthropic<2.0.0,>=1.4.3->deepagents) (1.3.0)
Requirement already satisfied: google-auth<3.0.0,>=2.48.1 in /opt/conda/lib/python3.11/site-packages (from google-auth[requests]<3.0.0,>=2.48.1->google-genai<2.0.0,>=1.65.0->langchain-google-genai<5.0.0,>=4.2.2->deepagents) (2.53.0)
Requirement already satisfied: websockets<17.0,>=13.0.0 in /opt/conda/lib/python3.11/site-packages (from google-genai<2.0.0,>=1.65.0->langchain-google-genai<5.0.0,>=4.2.2->deepagents) (16.0)
Requirement already satisfied: certifi in /opt/conda/lib/python3.11/site-packages (from httpx<1,>=0.23.0->langsmith>=0.8.3->deepagents) (2023.7.22)
Requirement already satisfied: httpcore==1.* in /opt/conda/lib/python3.11/site-packages (from httpx<1,>=0.23.0->langsmith>=0.8.3->deepagents) (1.0.9)
Requirement already satisfied: idna in /opt/conda/lib/python3.11/site-packages (from httpx<1,>=0.23.0->langsmith>=0.8.3->deepagents) (3.4)
Requirement already satisfied: h11>=0.16 in /opt/conda/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->langsmith>=0.8.3->deepagents) (0.16.0)
Requirement already satisfied: jsonpointer>=1.9 in /opt/conda/lib/python3.11/site-packages (from jsonpatch<2.0.0,>=1.33.0->langchain-core<2.0.0,>=1.4.0->deepagents) (2.4)
Requirement already satisfied: langgraph-checkpoint<5.0.0,>=4.1.0 in /opt/conda/lib/python3.11/site-packages (from langgraph<1.3.0,>=1.2.0->langchain<2.0.0,>=1.3.0->deepagents) (4.1.1)
Requirement already satisfied: langgraph-prebuilt<1.2.0,>=1.1.0 in /opt/conda/lib/python3.11/site-packages (from langgraph<1.3.0,>=1.2.0->langchain<2.0.0,>=1.3.0->deepagents) (1.1.0)
Requirement already satisfied: langgraph-sdk<0.4.0,>=0.3.0 in /opt/conda/lib/python3.11/site-packages (from langgraph<1.3.0,>=1.2.0->langchain<2.0.0,>=1.3.0->deepagents) (0.3.15)
Requirement already satisfied: annotated-types>=0.6.0 in /opt/conda/lib/python3.11/site-packages (from pydantic<3.0.0,>=2.7.4->langchain<2.0.0,>=1.3.0->deepagents) (0.7.0)
Requirement already satisfied: pydantic-core==2.46.4 in /opt/conda/lib/python3.11/site-packages (from pydantic<3.0.0,>=2.7.4->langchain<2.0.0,>=1.3.0->deepagents) (2.46.4)
Requirement already satisfied: typing-inspection>=0.4.2 in /opt/conda/lib/python3.11/site-packages (from pydantic<3.0.0,>=2.7.4->langchain<2.0.0,>=1.3.0->deepagents) (0.4.2)
Requirement already satisfied: charset-normalizer<4,>=2 in /opt/conda/lib/python3.11/site-packages (from requests>=2.0.0->langsmith>=0.8.3->deepagents) (3.3.0)
Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.11/site-packages (from requests>=2.0.0->langsmith>=0.8.3->deepagents) (2.0.7)
Requirement already satisfied: pyasn1-modules>=0.2.1 in /opt/conda/lib/python3.11/site-packages (from google-auth<3.0.0,>=2.48.1->google-auth[requests]<3.0.0,>=2.48.1->google-genai<2.0.0,>=1.65.0->langchain-google-genai<5.0.0,>=4.2.2->deepagents) (0.4.2)
Requirement already satisfied: cryptography>=38.0.3 in /opt/conda/lib/python3.11/site-packages (from google-auth<3.0.0,>=2.48.1->google-auth[requests]<3.0.0,>=2.48.1->google-genai<2.0.0,>=1.65.0->langchain-google-genai<5.0.0,>=4.2.2->deepagents) (41.0.4)
Requirement already satisfied: ormsgpack>=1.12.0 in /opt/conda/lib/python3.11/site-packages (from langgraph-checkpoint<5.0.0,>=4.1.0->langgraph<1.3.0,>=1.2.0->langchain<2.0.0,>=1.3.0->deepagents) (1.12.2)
Requirement already satisfied: cffi>=1.12 in /opt/conda/lib/python3.11/site-packages (from cryptography>=38.0.3->google-auth<3.0.0,>=2.48.1->google-auth[requests]<3.0.0,>=2.48.1->google-genai<2.0.0,>=1.65.0->langchain-google-genai<5.0.0,>=4.2.2->deepagents) (1.16.0)
Requirement already satisfied: pyasn1<0.7.0,>=0.6.1 in /opt/conda/lib/python3.11/site-packages (from pyasn1-modules>=0.2.1->google-auth<3.0.0,>=2.48.1->google-auth[requests]<3.0.0,>=2.48.1->google-genai<2.0.0,>=1.65.0->langchain-google-genai<5.0.0,>=4.2.2->deepagents) (0.6.3)
Requirement already satisfied: pycparser in /opt/conda/lib/python3.11/site-packages (from cffi>=1.12->cryptography>=38.0.3->google-auth<3.0.0,>=2.48.1->google-auth[requests]<3.0.0,>=2.48.1->google-genai<2.0.0,>=1.65.0->langchain-google-genai<5.0.0,>=4.2.2->deepagents) (2.21)
Note: you may need to restart the kernel to use updated packages.
初始化模型参数
import os
from langchain_anthropic import ChatAnthropic
from deepagents import create_deep_agent
os.environ["ANTHROPIC_API_KEY"] = "sk-..."
model = ChatAnthropic(model="MiniMax-M2.7",base_url="https://api.minimaxi.com/anthropic")
#定义工具
def get_weather(city: str) -> str:
"""Get weather for a given city."""
return f"It's always cloudy in {city}!"
agent = create_deep_agent(
model=model,
tools=[get_weather],
system_prompt="You are a helpful assistant",
)
# this calls init_chat_model for the specified model with default parameters # to use specific model parameters, use init_chat_model directly
print("success");
success
运行Agent
result = agent.invoke({"messages": [{"role": "user", "content": "今天杭州天气怎么样?"}]})
# Print the agent's response
print(result["messages"][-1].content)
[{'signature': '918d19398e420c926bde6dd960f907fb0a5cab93dda683f3d468750b13936da7', 'thinking': "The weather result shows that it's always cloudy in Hangzhou (杭州). Let me provide this information to the user.\n", 'type': 'thinking'}, {'text': '今天杭州天气:多云 🌥️', 'type': 'text'}]
它是如何工作的?
您的 Deep Agent 会自动执行以下操作:
- 规划其方法:使用内置的
write\_todos工具来拆解研究任务。 - 进行研究:通过调用
internet_search工具来收集信息。 - 管理上下文:使用文件系统工具(
write\_file,read\_file)来卸载大型搜索结果。 - 生成子智能体:根据需要将复杂的子任务委派给专门的子智能体。
- 合成报告:将研究发现汇总为连贯的报告。
示例
有关您可以使用 Deep Agents 构建的智能体、模式和应用程序,请参阅 示例。
流式处理
Deep Agents 使用 LangGraph 内置了 流式传输 (streaming) 功能,可实现智能体执行过程的实时更新。这使您可以逐步观察输出,并检查和调试智能体及子智能体的工作,例如工具调用、工具结果和 LLM 响应。
后续步骤
现在您已经构建了第一个 Deep Agent