This repository was archived by the owner on Nov 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 532
feat(you): add YouSearchTool (You.com Search API) #446
Open
tonykipkemboi
wants to merge
6
commits into
main
Choose a base branch
from
feat/parallel-search-tool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ff3842d
docs: add BUILDING_TOOLS.md
tonykipkemboi 0b01587
feat(parallel): add ParallelSearchTool (Search API v1beta), tests, RE…
tonykipkemboi 49fbb79
test(parallel): replace URL substring assertion with hostname allowli…
tonykipkemboi 8e5b6d5
feat(you): add YouSearchTool (You.com Search API), README, tests; exp…
tonykipkemboi bf4de91
Merge branch 'main' into feat/parallel-search-tool
tonykipkemboi 814b101
docs(you): add Parameters section and clarify purpose with doc links
tonykipkemboi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # YouSearchTool (You.com Search API) | ||
|
|
||
| Simple wrapper for the You.com Search API that returns structured web/news results with snippets and URLs. | ||
|
|
||
| Docs: | ||
| - Search API (Trusted, Recent info): https://documentation.you.com/api-modes/search-api#trustworthy-and-recent-informantion-for-your-research | ||
| - Quickstart (Search API): https://documentation.you.com/docs/quickstart#search-api | ||
|
|
||
| ## What it's for | ||
|
|
||
| Use when you need accurate, up-to-date web snippets and URLs from trusted sources to ground agent answers without extra scraping. Results include long snippets, titles, and links suitable for direct LLM consumption. | ||
|
|
||
| ## Environment | ||
|
|
||
| - YOU_API_KEY (required) — used as `X-API-Key` | ||
|
|
||
| ## Parameters | ||
|
|
||
| - `query` (str, required): Natural‑language search query. Returns structured results (web/news) with snippets and URLs. | ||
|
|
||
| ## Usage (published) | ||
|
|
||
| ```python | ||
| from crewai_tools import YouSearchTool | ||
|
|
||
| you = YouSearchTool() | ||
| resp = you.run(query="result of the political debate in EU") | ||
| print(resp) # JSON string containing results.web/news arrays | ||
| ``` | ||
|
|
||
| ## Example with agents | ||
|
|
||
| Here’s a minimal CrewAI agent that uses `YouSearchTool` as a tool. The agent will invoke the tool directly (no manual `run(...)` calls). | ||
|
|
||
| ```python | ||
| import os | ||
| from crewai import Agent, Task, Crew, LLM, Process | ||
| from crewai_tools import YouSearchTool | ||
|
|
||
| # LLM (configure your provider keys, e.g., GEMINI_API_KEY or OPENAI_API_KEY) | ||
| llm = LLM( | ||
| model="gemini/gemini-2.0-flash", | ||
| temperature=0.5, | ||
| api_key=os.getenv("GEMINI_API_KEY") | ||
| ) | ||
|
|
||
| # You.com Web Search Tool | ||
| you = YouSearchTool() | ||
|
|
||
| # User query | ||
| query = "all the recent CrewAI news" | ||
|
|
||
| researcher = Agent( | ||
| role="Senior Web Researcher", | ||
| backstory="You are an expert web researcher.", | ||
| goal="Find cited, high-quality sources and provide a detailed answer.", | ||
| tools=[you], | ||
| llm=llm, | ||
| verbose=True, | ||
| ) | ||
|
|
||
| # Research task | ||
| task = Task( | ||
| description="""Use the You.com Web Search tool to research: {query}. | ||
| Provide the answer in detail and cite sources (sources should be in the format of [source](url)).""", | ||
| expected_output="A detailed, sourced answer to the question.", | ||
| agent=researcher, | ||
| output_file="answer.md", | ||
| ) | ||
|
|
||
| # Crew | ||
| crew = Crew( | ||
| agents=[researcher], | ||
| tasks=[task], | ||
| verbose=True, | ||
| process=Process.sequential, | ||
| ) | ||
|
|
||
| # Kickoff the crew | ||
| result = crew.kickoff(inputs={'query': query}) | ||
| print(result) | ||
| ``` | ||
|
|
||
| ## Notes | ||
|
|
||
| - Endpoint: `GET https://api.ydc-index.io/v1/search` with headers `{ "X-API-Key": YOU_API_KEY }` and params `{ query: ... }` | ||
| - Returns results grouped by sections (e.g., `results.web`, `results.news`) with URLs, titles, descriptions, and snippets. | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import os | ||
| from typing import Any, Dict, Optional, Type | ||
|
|
||
| import requests | ||
| from typing import List | ||
| from crewai.tools import BaseTool, EnvVar | ||
| from pydantic import BaseModel, Field | ||
|
|
||
|
|
||
| class YouSearchToolSchema(BaseModel): | ||
| """Input for You.com Web Search.""" | ||
|
|
||
| query: str = Field(..., description="Search query text") | ||
|
|
||
|
|
||
| class YouSearchTool(BaseTool): | ||
| name: str = "You.com Web Search Tool" | ||
| description: str = ( | ||
| "Perform a web search using You.com's Search API and return structured results " | ||
| "(web/news) with snippets and URLs." | ||
| ) | ||
| args_schema: Type[BaseModel] = YouSearchToolSchema | ||
|
|
||
| env_vars: List[EnvVar] = [ | ||
| EnvVar( | ||
| name="YOU_API_KEY", | ||
| description="API key for You.com Search API (used as X-API-Key)", | ||
| required=True, | ||
| ) | ||
| ] | ||
| package_dependencies: List[str] = ["requests"] | ||
|
|
||
| base_url: str = "https://api.ydc-index.io/v1/search" | ||
|
|
||
| def _run(self, query: str, **_: Any) -> str: | ||
| api_key = os.environ.get("YOU_API_KEY") | ||
| if not api_key: | ||
| return "Error: YOU_API_KEY environment variable is required" | ||
|
|
||
| headers = {"X-API-Key": api_key} | ||
| params: Dict[str, Any] = {"query": query} | ||
|
|
||
| try: | ||
| resp = requests.get(self.base_url, headers=headers, params=params, timeout=20) | ||
| if resp.status_code >= 300: | ||
| return f"You.com Search API error: {resp.status_code} {resp.text[:200]}" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here? |
||
| data = resp.json() | ||
| # Return JSON string so agents can consume directly | ||
| try: | ||
| import json | ||
|
|
||
| return json.dumps(data, ensure_ascii=False) | ||
| except Exception: | ||
| return str(data) | ||
| except requests.Timeout: | ||
| return "You.com Search API timeout. Please try again later." | ||
| except Exception as exc: # noqa: BLE001 | ||
| return f"Unexpected error calling You.com Search API: {exc}" | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from crewai_tools.tools.you_search_tool.you_search_tool import YouSearchTool | ||
|
|
||
|
|
||
| def test_requires_env_var(monkeypatch): | ||
| monkeypatch.delenv("YOU_API_KEY", raising=False) | ||
| tool = YouSearchTool() | ||
| result = tool.run(query="test") | ||
| assert "YOU_API_KEY" in result | ||
|
|
||
|
|
||
| @patch("crewai_tools.tools.you_search_tool.you_search_tool.requests.get") | ||
| def test_happy_path(mock_get, monkeypatch): | ||
| monkeypatch.setenv("YOU_API_KEY", "test") | ||
|
|
||
| mock_get.return_value.status_code = 200 | ||
| mock_get.return_value.json.return_value = { | ||
| "results": { | ||
| "web": [ | ||
| { | ||
| "url": "https://www.europarl.europa.eu/topics/en/topic/state-of-the-eu-debates", | ||
| "title": "State of the EU debates", | ||
| "snippets": [ | ||
| "MEPs will scrutinise the work of the European Commission..." | ||
| ], | ||
| } | ||
| ] | ||
| }, | ||
| "metadata": {"query": "result of the political debate in EU"}, | ||
| } | ||
|
|
||
| tool = YouSearchTool() | ||
| result = tool.run(query="result of the political debate in EU") | ||
| assert "results" in result | ||
| assert "europarl.europa.eu" in result | ||
|
|
||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we raise error ?