|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Sample demonstrating CrewAI tool with **kwargs parameter handling. |
| 16 | +
|
| 17 | +This sample shows how CrewaiTool correctly passes arbitrary parameters |
| 18 | +through **kwargs, which is a common pattern in CrewAI tools. |
| 19 | +""" |
| 20 | + |
| 21 | +from typing import Optional |
| 22 | + |
| 23 | +from crewai.tools import BaseTool |
| 24 | +from google.adk import Agent |
| 25 | +from google.adk.tools.crewai_tool import CrewaiTool |
| 26 | +from pydantic import BaseModel |
| 27 | +from pydantic import Field |
| 28 | + |
| 29 | + |
| 30 | +class SearchInput(BaseModel): |
| 31 | + """Input schema for the search tool.""" |
| 32 | + |
| 33 | + query: str = Field(..., description="The search query string") |
| 34 | + category: Optional[str] = Field( |
| 35 | + None, description="Filter by category (e.g., 'technology', 'science')" |
| 36 | + ) |
| 37 | + date_range: Optional[str] = Field( |
| 38 | + None, description="Filter by date range (e.g., 'last_week', '2024')" |
| 39 | + ) |
| 40 | + limit: Optional[int] = Field( |
| 41 | + None, description="Limit the number of results (e.g., 10, 20)" |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +class CustomSearchTool(BaseTool): |
| 46 | + """A custom CrewAI tool that accepts arbitrary search parameters via **kwargs. |
| 47 | +
|
| 48 | + This demonstrates the key CrewAI tool pattern where tools accept |
| 49 | + flexible parameters through **kwargs. |
| 50 | + """ |
| 51 | + |
| 52 | + name: str = "custom_search" |
| 53 | + description: str = ( |
| 54 | + "Search for information with flexible filtering options. " |
| 55 | + "Accepts a query and optional filter parameters like category, " |
| 56 | + "date_range, limit, etc." |
| 57 | + ) |
| 58 | + args_schema: type[BaseModel] = SearchInput |
| 59 | + |
| 60 | + def _run(self, query: str, **kwargs) -> str: |
| 61 | + """Execute search with arbitrary filter parameters. |
| 62 | +
|
| 63 | + Args: |
| 64 | + query: The search query string. |
| 65 | + **kwargs: Additional filter parameters like category, date_range, limit. |
| 66 | +
|
| 67 | + Returns: |
| 68 | + A formatted string showing the query and applied filters. |
| 69 | + """ |
| 70 | + result_parts = [f"Searching for: '{query}'"] |
| 71 | + |
| 72 | + if kwargs: |
| 73 | + result_parts.append("Applied filters:") |
| 74 | + for key, value in kwargs.items(): |
| 75 | + result_parts.append(f" - {key}: {value}") |
| 76 | + else: |
| 77 | + result_parts.append("No additional filters applied.") |
| 78 | + |
| 79 | + # Simulate search results |
| 80 | + result_parts.append(f"\nFound 3 results matching your criteria.") |
| 81 | + |
| 82 | + return "\n".join(result_parts) |
| 83 | + |
| 84 | + |
| 85 | +crewai_search_tool = CustomSearchTool() |
| 86 | + |
| 87 | +# Wrap it with ADK's CrewaiTool |
| 88 | +adk_search_tool = CrewaiTool( |
| 89 | + crewai_search_tool, |
| 90 | + name="search_with_filters", |
| 91 | + description=( |
| 92 | + "Search for information with optional filters like category, " |
| 93 | + "date_range, or limit" |
| 94 | + ), |
| 95 | +) |
| 96 | + |
| 97 | +root_agent = Agent( |
| 98 | + model="gemini-2.0-flash", |
| 99 | + name="search_agent", |
| 100 | + description="An agent that can search with flexible filtering options", |
| 101 | + instruction=""" |
| 102 | + You are a helpful search assistant. |
| 103 | + When users ask you to search, use the search_with_filters tool. |
| 104 | + You can pass additional parameters like: |
| 105 | + - category: to filter by category (e.g., "technology", "science") |
| 106 | + - date_range: to filter by date (e.g., "last_week", "2024") |
| 107 | + - limit: to limit the number of results (e.g., 10, 20) |
| 108 | +
|
| 109 | + Always acknowledge what filters you're applying. |
| 110 | + """, |
| 111 | + tools=[adk_search_tool], |
| 112 | +) |
0 commit comments