From 9ece3fd9c31ce3f983c9cf465b0b1e26c1c49579 Mon Sep 17 00:00:00 2001 From: Willem Jiang Date: Wed, 22 Oct 2025 22:56:02 +0800 Subject: [PATCH] fix: support additional Tavily search parameters via configuration to fix #548 (#643) * fix: support additional Tavily search parameters via configuration to fix #548 - Add include_answer, search_depth, include_raw_content, include_images, include_image_descriptions to SEARCH_ENGINE config - Update get_web_search_tool() to load these parameters from configuration with sensible defaults - Parameters are now properly passed to TavilySearchWithImages during initialization - This fixes 'got an unexpected keyword argument' errors when using web_search tool - Update tests to verify new parameters are correctly set * test: add comprehensive unit tests for web search configuration loading - Add test for custom configuration values (include_answer, search_depth, etc.) - Add test for empty configuration (all defaults) - Add test for image_descriptions logic when include_images is false - Add test for partial configuration - Add test for missing config file - Add test for multiple domains in include/exclude lists All 7 new tests pass and provide comprehensive coverage of configuration loading and parameter handling for Tavily search tool initialization. * test: verify all Tavily configuration parameters are optional Add 8 comprehensive tests to verify that all Tavily engine configuration parameters are truly optional: - test_tavily_with_no_search_engine_section: SEARCH_ENGINE section missing - test_tavily_with_completely_empty_config: Entire config missing - test_tavily_with_only_include_answer_param: Single param, rest default - test_tavily_with_only_search_depth_param: Single param, rest default - test_tavily_with_only_include_domains_param: Domain param, rest default - test_tavily_with_explicit_false_boolean_values: False values work correctly - test_tavily_with_empty_domain_lists: Empty lists handled correctly - test_tavily_all_parameters_optional_mix: Multiple missing params work These tests verify: - Tool creation never fails regardless of missing configuration - All parameters have sensible defaults - Boolean parameters can be explicitly set to False - Any combination of optional parameters works - Domain lists can be empty or omitted All 15 Tavily configuration tests pass successfully. --- conf.yaml.example | 14 ++ src/tools/search.py | 17 ++- tests/unit/tools/test_search.py | 219 ++++++++++++++++++++++++++++++++ 3 files changed, 245 insertions(+), 5 deletions(-) diff --git a/conf.yaml.example b/conf.yaml.example index 85ad12c..543311e 100644 --- a/conf.yaml.example +++ b/conf.yaml.example @@ -55,3 +55,17 @@ BASIC_MODEL: # # Exclude results from these domains # exclude_domains: # - example.com +# # Include an answer in the search results +# include_answer: false +# # Search depth: "basic" or "advanced" +# search_depth: "advanced" +# # Include raw content from pages +# include_raw_content: true +# # Include images in search results +# include_images: true +# # Include descriptions for images +# include_image_descriptions: true +# # Minimum score threshold for results (0-1) +# min_score_threshold: 0.0 +# # Maximum content length per page +# max_content_length_per_page: 4000 diff --git a/src/tools/search.py b/src/tools/search.py index 5597525..11421d0 100644 --- a/src/tools/search.py +++ b/src/tools/search.py @@ -47,22 +47,29 @@ def get_web_search_tool(max_search_results: int): search_config = get_search_config() if SELECTED_SEARCH_ENGINE == SearchEngine.TAVILY.value: - # Only get and apply include/exclude domains for Tavily + # Get all Tavily search parameters from configuration with defaults include_domains: Optional[List[str]] = search_config.get("include_domains", []) exclude_domains: Optional[List[str]] = search_config.get("exclude_domains", []) - include_raw_content = search_config.get("include_raw_content", True) - include_images: Optional[bool] = search_config.get("include_images", True) - include_image_descriptions: Optional[bool] = ( + include_answer: bool = search_config.get("include_answer", False) + search_depth: str = search_config.get("search_depth", "advanced") + include_raw_content: bool = search_config.get("include_raw_content", True) + include_images: bool = search_config.get("include_images", True) + include_image_descriptions: bool = ( include_images and search_config.get("include_image_descriptions", True) ) logger.info( - f"Tavily search configuration loaded: include_domains={include_domains}, exclude_domains={exclude_domains}" + f"Tavily search configuration loaded: include_domains={include_domains}, " + f"exclude_domains={exclude_domains}, include_answer={include_answer}, " + f"search_depth={search_depth}, include_raw_content={include_raw_content}, " + f"include_images={include_images}, include_image_descriptions={include_image_descriptions}" ) return LoggedTavilySearch( name="web_search", max_results=max_search_results, + include_answer=include_answer, + search_depth=search_depth, include_raw_content=include_raw_content, include_images=include_images, include_image_descriptions=include_image_descriptions, diff --git a/tests/unit/tools/test_search.py b/tests/unit/tools/test_search.py index 78ac14a..a719bda 100644 --- a/tests/unit/tools/test_search.py +++ b/tests/unit/tools/test_search.py @@ -19,6 +19,8 @@ class TestGetWebSearchTool: assert tool.include_raw_content is True assert tool.include_images is True assert tool.include_image_descriptions is True + assert tool.include_answer is False + assert tool.search_depth == "advanced" @patch("src.tools.search.SELECTED_SEARCH_ENGINE", SearchEngine.DUCKDUCKGO.value) def test_get_web_search_tool_duckduckgo(self): @@ -53,3 +55,220 @@ class TestGetWebSearchTool: def test_get_web_search_tool_brave_no_api_key(self): tool = get_web_search_tool(max_search_results=1) assert tool.search_wrapper.api_key == "" + + @patch("src.tools.search.SELECTED_SEARCH_ENGINE", SearchEngine.TAVILY.value) + @patch("src.tools.search.load_yaml_config") + def test_get_web_search_tool_tavily_with_custom_config(self, mock_config): + """Test Tavily tool with custom configuration values.""" + mock_config.return_value = { + "SEARCH_ENGINE": { + "include_answer": True, + "search_depth": "basic", + "include_raw_content": False, + "include_images": False, + "include_image_descriptions": True, + "include_domains": ["example.com"], + "exclude_domains": ["spam.com"], + } + } + tool = get_web_search_tool(max_search_results=5) + assert tool.name == "web_search" + assert tool.max_results == 5 + assert tool.include_answer is True + assert tool.search_depth == "basic" + assert tool.include_raw_content is False + assert tool.include_images is False + # include_image_descriptions should be False because include_images is False + assert tool.include_image_descriptions is False + assert tool.include_domains == ["example.com"] + assert tool.exclude_domains == ["spam.com"] + + @patch("src.tools.search.SELECTED_SEARCH_ENGINE", SearchEngine.TAVILY.value) + @patch("src.tools.search.load_yaml_config") + def test_get_web_search_tool_tavily_with_empty_config(self, mock_config): + """Test Tavily tool uses defaults when config is empty.""" + mock_config.return_value = {"SEARCH_ENGINE": {}} + tool = get_web_search_tool(max_search_results=10) + assert tool.name == "web_search" + assert tool.max_results == 10 + assert tool.include_answer is False + assert tool.search_depth == "advanced" + assert tool.include_raw_content is True + assert tool.include_images is True + assert tool.include_image_descriptions is True + assert tool.include_domains == [] + assert tool.exclude_domains == [] + + @patch("src.tools.search.SELECTED_SEARCH_ENGINE", SearchEngine.TAVILY.value) + @patch("src.tools.search.load_yaml_config") + def test_get_web_search_tool_tavily_image_descriptions_disabled_when_images_disabled( + self, mock_config + ): + """Test that include_image_descriptions is False when include_images is False.""" + mock_config.return_value = { + "SEARCH_ENGINE": { + "include_images": False, + "include_image_descriptions": True, # This should be ignored + } + } + tool = get_web_search_tool(max_search_results=5) + assert tool.include_images is False + assert tool.include_image_descriptions is False + + @patch("src.tools.search.SELECTED_SEARCH_ENGINE", SearchEngine.TAVILY.value) + @patch("src.tools.search.load_yaml_config") + def test_get_web_search_tool_tavily_partial_config(self, mock_config): + """Test Tavily tool with partial configuration.""" + mock_config.return_value = { + "SEARCH_ENGINE": { + "include_answer": True, + "include_domains": ["trusted.com"], + } + } + tool = get_web_search_tool(max_search_results=3) + assert tool.include_answer is True + assert tool.search_depth == "advanced" # default + assert tool.include_raw_content is True # default + assert tool.include_domains == ["trusted.com"] + assert tool.exclude_domains == [] # default + + @patch("src.tools.search.SELECTED_SEARCH_ENGINE", SearchEngine.TAVILY.value) + @patch("src.tools.search.load_yaml_config") + def test_get_web_search_tool_tavily_with_no_config_file(self, mock_config): + """Test Tavily tool when config file doesn't exist.""" + mock_config.return_value = {} + tool = get_web_search_tool(max_search_results=5) + assert tool.name == "web_search" + assert tool.max_results == 5 + assert tool.include_answer is False + assert tool.search_depth == "advanced" + assert tool.include_raw_content is True + assert tool.include_images is True + + @patch("src.tools.search.SELECTED_SEARCH_ENGINE", SearchEngine.TAVILY.value) + @patch("src.tools.search.load_yaml_config") + def test_get_web_search_tool_tavily_multiple_domains(self, mock_config): + """Test Tavily tool with multiple domains in include/exclude lists.""" + mock_config.return_value = { + "SEARCH_ENGINE": { + "include_domains": ["example.com", "trusted.com", "gov.cn"], + "exclude_domains": ["spam.com", "scam.org"], + } + } + tool = get_web_search_tool(max_search_results=5) + assert tool.include_domains == ["example.com", "trusted.com", "gov.cn"] + assert tool.exclude_domains == ["spam.com", "scam.org"] + + @patch("src.tools.search.SELECTED_SEARCH_ENGINE", SearchEngine.TAVILY.value) + @patch("src.tools.search.load_yaml_config") + def test_tavily_with_no_search_engine_section(self, mock_config): + """Test Tavily tool when SEARCH_ENGINE section doesn't exist in config.""" + mock_config.return_value = {"OTHER_CONFIG": {}} + tool = get_web_search_tool(max_search_results=5) + assert tool.name == "web_search" + assert tool.max_results == 5 + assert tool.include_answer is False + assert tool.search_depth == "advanced" + assert tool.include_raw_content is True + assert tool.include_images is True + assert tool.include_domains == [] + assert tool.exclude_domains == [] + + @patch("src.tools.search.SELECTED_SEARCH_ENGINE", SearchEngine.TAVILY.value) + @patch("src.tools.search.load_yaml_config") + def test_tavily_with_completely_empty_config(self, mock_config): + """Test Tavily tool with completely empty config.""" + mock_config.return_value = {} + tool = get_web_search_tool(max_search_results=5) + assert tool.name == "web_search" + assert tool.max_results == 5 + assert tool.include_answer is False + assert tool.search_depth == "advanced" + assert tool.include_raw_content is True + assert tool.include_images is True + + @patch("src.tools.search.SELECTED_SEARCH_ENGINE", SearchEngine.TAVILY.value) + @patch("src.tools.search.load_yaml_config") + def test_tavily_with_only_include_answer_param(self, mock_config): + """Test Tavily tool with only include_answer parameter specified.""" + mock_config.return_value = {"SEARCH_ENGINE": {"include_answer": True}} + tool = get_web_search_tool(max_search_results=5) + assert tool.include_answer is True + assert tool.search_depth == "advanced" + assert tool.include_raw_content is True + assert tool.include_images is True + + @patch("src.tools.search.SELECTED_SEARCH_ENGINE", SearchEngine.TAVILY.value) + @patch("src.tools.search.load_yaml_config") + def test_tavily_with_only_search_depth_param(self, mock_config): + """Test Tavily tool with only search_depth parameter specified.""" + mock_config.return_value = {"SEARCH_ENGINE": {"search_depth": "basic"}} + tool = get_web_search_tool(max_search_results=5) + assert tool.search_depth == "basic" + assert tool.include_answer is False + assert tool.include_raw_content is True + assert tool.include_images is True + + @patch("src.tools.search.SELECTED_SEARCH_ENGINE", SearchEngine.TAVILY.value) + @patch("src.tools.search.load_yaml_config") + def test_tavily_with_only_include_domains_param(self, mock_config): + """Test Tavily tool with only include_domains parameter specified.""" + mock_config.return_value = { + "SEARCH_ENGINE": {"include_domains": ["example.com"]} + } + tool = get_web_search_tool(max_search_results=5) + assert tool.include_domains == ["example.com"] + assert tool.exclude_domains == [] + assert tool.include_answer is False + assert tool.search_depth == "advanced" + + @patch("src.tools.search.SELECTED_SEARCH_ENGINE", SearchEngine.TAVILY.value) + @patch("src.tools.search.load_yaml_config") + def test_tavily_with_explicit_false_boolean_values(self, mock_config): + """Test that explicitly False boolean values are respected (not treated as missing).""" + mock_config.return_value = { + "SEARCH_ENGINE": { + "include_answer": False, + "include_raw_content": False, + "include_images": False, + } + } + tool = get_web_search_tool(max_search_results=5) + assert tool.include_answer is False + assert tool.include_raw_content is False + assert tool.include_images is False + assert tool.include_image_descriptions is False + + @patch("src.tools.search.SELECTED_SEARCH_ENGINE", SearchEngine.TAVILY.value) + @patch("src.tools.search.load_yaml_config") + def test_tavily_with_empty_domain_lists(self, mock_config): + """Test that empty domain lists are treated as optional.""" + mock_config.return_value = { + "SEARCH_ENGINE": { + "include_domains": [], + "exclude_domains": [], + } + } + tool = get_web_search_tool(max_search_results=5) + assert tool.include_domains == [] + assert tool.exclude_domains == [] + + @patch("src.tools.search.SELECTED_SEARCH_ENGINE", SearchEngine.TAVILY.value) + @patch("src.tools.search.load_yaml_config") + def test_tavily_all_parameters_optional_mix(self, mock_config): + """Test that any combination of optional parameters works.""" + mock_config.return_value = { + "SEARCH_ENGINE": { + "include_answer": True, + "include_images": False, + # Deliberately omit search_depth, include_raw_content, domains + } + } + tool = get_web_search_tool(max_search_results=5) + assert tool.include_answer is True + assert tool.include_images is False + assert tool.include_image_descriptions is False # should be False since include_images is False + assert tool.search_depth == "advanced" # default + assert tool.include_raw_content is True # default + assert tool.include_domains == [] # default + assert tool.exclude_domains == [] # default