@@ -60,14 +60,36 @@ def _parse_positive_int_config(
6060 return value
6161
6262
63+ def provider_requires_api_key (provider_type : WebSearchProviderType ) -> bool :
64+ """Return True if the given provider type requires an API key.
65+ This list is most likely just going to contain SEARXNG. The way it works is that it uses public search engines that do not
66+ require an API key. You can also set it up in a way which requires a key but SearXNG itself does not require a key.
67+ """
68+ return provider_type != WebSearchProviderType .SEARXNG
69+
70+
6371def build_search_provider_from_config (
6472 provider_type : WebSearchProviderType ,
65- api_key : str ,
73+ api_key : str | None ,
6674 config : dict [str , str ] | None , # TODO use a typed object
6775) -> WebSearchProvider :
6876 config = config or {}
6977 num_results = int (config .get ("num_results" ) or DEFAULT_MAX_RESULTS )
7078
79+ # SearXNG does not require an API key
80+ if provider_type == WebSearchProviderType .SEARXNG :
81+ searxng_base_url = config .get ("searxng_base_url" )
82+ if not searxng_base_url :
83+ raise ValueError ("Please provide a URL for your private SearXNG instance." )
84+ return SearXNGClient (
85+ searxng_base_url ,
86+ num_results = num_results ,
87+ )
88+
89+ # All other providers require an API key
90+ if not api_key :
91+ raise ValueError (f"API key is required for { provider_type .value } provider." )
92+
7193 if provider_type == WebSearchProviderType .EXA :
7294 return ExaClient (api_key = api_key , num_results = num_results )
7395 if provider_type == WebSearchProviderType .BRAVE :
@@ -105,20 +127,13 @@ def build_search_provider_from_config(
105127 num_results = num_results ,
106128 timeout_seconds = int (config .get ("timeout_seconds" ) or 10 ),
107129 )
108- if provider_type == WebSearchProviderType .SEARXNG :
109- searxng_base_url = config .get ("searxng_base_url" )
110- if not searxng_base_url :
111- raise ValueError ("Please provide a URL for your private SearXNG instance." )
112- return SearXNGClient (
113- searxng_base_url ,
114- num_results = num_results ,
115- )
130+ raise ValueError (f"Unknown provider type: { provider_type .value } " )
116131
117132
118133def _build_search_provider (provider_model : InternetSearchProvider ) -> WebSearchProvider :
119134 return build_search_provider_from_config (
120135 provider_type = WebSearchProviderType (provider_model .provider_type ),
121- api_key = provider_model .api_key or "" ,
136+ api_key = provider_model .api_key ,
122137 config = provider_model .config or {},
123138 )
124139
0 commit comments