第三方工具 第三方工具使您能够集成来自 LangChain、CrewAI 等框架的现有工具生态系统。这极大地扩展了您的智能体功能,让您能够利用更广泛的 AI 社区中久经考验的工具。 您将学到的内容 LangChain 集成:使用 LangChain 丰富的工具库 CrewAI 工具:利用 CrewAI 的专用智能体工具 工具适配器:ADK 如何封装外部工具 生态系统优势:使用成熟工具库的优势 最佳实践:何时以及如何使用第三方工具 核心概念:第三方工具 第三方工具是为 ADK 封装的外部库: LangChain 工具:网络爬虫、文档加载器、API CrewAI 工具:网络爬虫、文件操作、专用功能 自定义集成:任何外部服务或库 包装类:ADK 提供适配器以实现无缝集成 主要优势 ✅
第三方工具使您能够集成来自 LangChain、CrewAI 等框架的现有工具生态系统。这极大地扩展了您的智能体功能,让您能够利用更广泛的 AI 社区中久经考验的工具。
第三方工具是为 ADK 封装的外部库:
本子示例包含两个实用实现:
位置:./langchain_agent/
位置:./crewai_agent/
4_3_thirdparty_tools/ ├── README.md # This file - third-party tools guide ├── requirements.txt # Dependencies for third-party tools ├── ../env.example # Environment variables template (shared) ├── langchain_agent/ # LangChain integration │ ├── __init__.py │ └── agent.py # Agent with LangChain tools └── crewai_agent/ # CrewAI integration ├── __init__.py └── agent.py # Agent with CrewAI tools
完成本子示例后,您将掌握:
设置环境:
cd 4_3_thirdparty_tools # Copy the environment template cp ../env.example .env # Edit .env and add your Google AI API key # Get your API key from: https://aistudio.google.com/
安装依赖:安装所需软件包
pip install -r requirements.txt
运行智能体:
# Start the ADK web interface adk web # In the web interface, select: # - langchain_agent: For web search and Wikipedia research # - crewai_agent: For website scraping and file operations
试用智能体:
比较方法:了解各工具生态系统的差异与优势
from google.adk.tools.langchain_tool import LangchainTool from langchain_community.tools import DuckDuckGoSearchRun # Wrap LangChain tool for ADK search_tool = LangchainTool(DuckDuckGoSearchRun())
from google.adk.tools.crewai_tool import CrewaiTool from crewai_tools import ScrapeWebsiteTool, DirectorySearchTool, FileReadTool # Basic tool - minimal configuration scrape_tool = CrewaiTool( name="scrape_website", description="Scrape and extract content from websites", tool=ScrapeWebsiteTool( config=dict( llm=dict( provider="google", # Use Google instead of default OpenAI config=dict(model="gemini-3-flash-preview"), ), ) ) ) # Search tool - needs embeddings for semantic search search_tool = CrewaiTool( name="website_search", description="Search for content within websites", tool=WebsiteSearchTool( config=dict( llm=dict( provider="google", config=dict(model="gemini-3-flash-preview"), ), embedder=dict( provider="google", config=dict( model="gemini-embedding-001", task_type="retrieval_document", ), ), ) ) )
from google.adk.tools import FunctionTool import external_library def custom_integration(query: str) -> dict: """Integrate with external library.""" result = external_library.process(query) return {"result": result, "status": "success"} # Use as function tool tool = FunctionTool(custom_integration)
⚠️ 重要:CrewAI 工具默认使用 OpenAI 模型。在使用 Google ADK 时,请将其配置为使用 Google 模型以保持一致性:
# ❌ Default - Uses OpenAI models tool = WebsiteSearchTool() # ✅ Correct configuration - All tools need both LLM and embeddings tool = ScrapeWebsiteTool( config=dict( llm=dict( provider="google", config=dict(model="gemini-3-flash-preview"), ), embedder=dict( provider="google", config=dict( model="gemini-embedding-001", task_type="retrieval_document", ), ), ) ) # ✅ Same configuration pattern for all tools tool = DirectorySearchTool( config=dict( llm=dict( provider="google", config=dict(model="gemini-3-flash-preview"), ), embedder=dict( provider="google", config=dict( model="gemini-embedding-001", task_type="retrieval_document", ), ), ) )
关键点:
provider="google" to avoid OpenAI defaultsgoogle, openai, anthropic, ollama, llama2 等。| 方面 | 第三方 | 自定义 | 内置 |
|---|---|---|---|
| 开发时间 | 快速 | 慢 | 即刻 |
| 灵活性 | 中等 | 高 | 低 |
| 性能 | 可变 | 高 | 最高 |
| 维护 | 外部 | 内部 | 无 |
| 功能 | 丰富 | 定制 | 基本 |
| 依赖 | 多 | 少 | 无 |
免责声明:
本文档采用基于机器的 AI 翻译服务进行翻译。尽管我们力求准确,但请注意,自动翻译可能存在错误或不准确之处。应以原文语言版本的文档作为权威依据。如需获取关键信息,建议使用专业的人工翻译。对于因使用本翻译而产生的任何误解或误读,我们概不负责。