Skip to content

Commit

Permalink
chore: Update search engine names to match case-sensitive constants
Browse files Browse the repository at this point in the history
  • Loading branch information
jaigouk committed Sep 7, 2024
1 parent c6e217c commit 1face9a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 39 deletions.
2 changes: 1 addition & 1 deletion frontend/storm_wiki/pages_util/Settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_available_search_engines():
engine_settings = search_options.get("engine_settings", {})

for engine, config in SEARCH_ENGINES.items():
if config["env_var"] is None or engine == "searxng":
if config["env_var"] is None or engine == "SearXNG":
available_engines[engine] = None
elif engine in config.get("settings", {}):
required_settings = [
Expand Down
16 changes: 8 additions & 8 deletions frontend/storm_wiki/tests/test_db_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ def test_db():
@pytest.fixture
def default_search_options():
return {
"primary_engine": "duckduckgo",
"primary_engine": "Duckduckgo",
"fallback_engine": None,
"search_top_k": 3,
"retrieve_top_k": 3,
"engine_settings": {
"searxng": {"base_url": "", "api_key": ""},
"bing": {"api_key": ""},
"yourdm": {"api_key": ""},
"SearXNG": {"base_url": "", "api_key": ""},
"Bing": {"api_key": ""},
"YouRM": {"api_key": ""},
},
}

Expand Down Expand Up @@ -92,17 +92,17 @@ def test_save_and_load_search_options(default_search_options):

def test_update_search_option_valid(default_search_options):
save_search_options(default_search_options)
update_search_option("primary_engine", "bing")
update_search_option("primary_engine", "Bing")
loaded_options = load_search_options()
assert loaded_options["primary_engine"] == "bing"
assert loaded_options["primary_engine"] == "Bing"


def test_update_search_option_top_level(test_db):
initial_options = load_search_options()
initial_primary_engine = initial_options["primary_engine"]

# Update to a different engine
new_engine = "bing" if initial_primary_engine != "bing" else "duckduckgo"
new_engine = "Bing" if initial_primary_engine != "Bing" else "Duckduckgo"
update_search_option("primary_engine", new_engine)

updated_options = load_search_options()
Expand Down Expand Up @@ -151,7 +151,7 @@ def test_update_search_option_numeric(test_db):
def test_load_search_options_default(test_db):
save_setting("search_options", None)
default_options = load_search_options()
assert default_options["primary_engine"] == "duckduckgo"
assert default_options["primary_engine"] == "Duckduckgo"
assert "engine_settings" in default_options


Expand Down
30 changes: 15 additions & 15 deletions frontend/storm_wiki/tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def mock_file_content():
def mock_load_search_options():
with patch("db.db_operations.load_search_options") as mock:
mock.return_value = {
"primary_engine": "duckduckgo",
"primary_engine": "Duckduckgo",
"fallback_engine": None,
"search_top_k": 3,
"retrieve_top_k": 3,
Expand Down Expand Up @@ -73,7 +73,7 @@ def test_is_valid_wikipedia_source(self, combined_search_api):
def test_duckduckgo_failure_searxng_success(
self, mock_requests_get, mock_ddg_wrapper, combined_search_api
):
combined_search_api.primary_engine = "duckduckgo"
combined_search_api.primary_engine = "Duckduckgo"
combined_search_api.fallback_engine = "searxng"

# Mock DuckDuckGo failure
Expand Down Expand Up @@ -105,7 +105,7 @@ def test_duckduckgo_failure_searxng_success(

@patch("util.search.DuckDuckGoSearchAPIWrapper")
def test_duckduckgo_success(self, mock_ddg_wrapper, combined_search_api):
combined_search_api.primary_engine = "duckduckgo"
combined_search_api.primary_engine = "Duckduckgo"
mock_ddg_instance = MagicMock()
mock_ddg_instance.results.return_value = [
{
Expand All @@ -125,7 +125,7 @@ def test_duckduckgo_success(self, mock_ddg_wrapper, combined_search_api):

@patch("util.search.DuckDuckGoSearchAPIWrapper")
def test_multiple_queries(self, mock_ddg_wrapper, combined_search_api):
combined_search_api.primary_engine = "duckduckgo"
combined_search_api.primary_engine = "Duckduckgo"
mock_ddg_instance = MagicMock()
mock_ddg_instance.results.side_effect = [
[
Expand Down Expand Up @@ -198,8 +198,8 @@ def test_calculate_relevance(self, combined_search_api):
def test_arxiv_failure_searxng_fallback(
self, mock_ddg_wrapper, mock_requests_get, combined_search_api
):
combined_search_api.primary_engine = "arxiv"
combined_search_api.fallback_engine = "searxng"
combined_search_api.primary_engine = "Arxiv"
combined_search_api.fallback_engine = "SearXNG"

# Mock ArXiv failure
mock_arxiv_response = MagicMock()
Expand Down Expand Up @@ -232,15 +232,15 @@ def test_arxiv_failure_searxng_fallback(
def test_searxng_failure_duckduckgo_fallback(
self, mock_ddg_wrapper, mock_requests_get, combined_search_api
):
combined_search_api.primary_engine = "searxng"
combined_search_api.fallback_engine = "duckduckgo"
combined_search_api.primary_engine = "SearXNG"
combined_search_api.fallback_engine = "Duckduckgo"

# Mock SearxNG failure
mock_searxng_response = MagicMock()
mock_searxng_response.status_code = 500
mock_requests_get.return_value = mock_searxng_response

# Mock DuckDuckGo success
# Mock Duckduckgo success
mock_ddg_instance = MagicMock()
mock_ddg_instance.results.return_value = [
{
Expand All @@ -264,17 +264,17 @@ def test_searxng_failure_duckduckgo_fallback(
def test_all_engines_failure(
self, mock_ddg_wrapper, mock_requests_get, combined_search_api
):
combined_search_api.primary_engine = "searxng"
combined_search_api.fallback_engine = "duckduckgo"
combined_search_api.primary_engine = "SearXNG"
combined_search_api.fallback_engine = "Duckduckgo"

# Mock SearxNG failure
mock_searxng_response = MagicMock()
mock_searxng_response.status_code = 500
mock_requests_get.return_value = mock_searxng_response

# Mock DuckDuckGo failure
# Mock Duckduckgo failure
mock_ddg_instance = MagicMock()
mock_ddg_instance.results.side_effect = Exception("DuckDuckGo failed")
mock_ddg_instance.results.side_effect = Exception("Duckduckgo failed")
mock_ddg_wrapper.return_value = mock_ddg_instance
combined_search_api.ddg_search = mock_ddg_instance

Expand All @@ -284,13 +284,13 @@ def test_all_engines_failure(

@patch("util.search.requests.get")
def test_searxng_error_response(self, mock_requests_get, combined_search_api):
combined_search_api.primary_engine = "searxng"
combined_search_api.primary_engine = "SearXNG"
combined_search_api.fallback_engine = None

# Mock SearxNG error response
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {"error": "SearxNG error message"}
mock_response.json.return_value = {"error": "SearXNG error message"}
mock_requests_get.return_value = mock_response

results = combined_search_api.forward("test query", [])
Expand Down
20 changes: 10 additions & 10 deletions frontend/storm_wiki/util/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
DB_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), "db", "settings.db")

SEARCH_ENGINES = {
"searxng": {
"SearXNG": {
"env_var": "SEARXNG_BASE_URL",
"settings": {
"base_url": {"type": "text", "required": True, "label": "SearXNG Base URL"},
Expand All @@ -14,24 +14,24 @@
},
},
},
"bing": {
"Bing": {
"env_var": "BING_SEARCH_API_KEY",
"settings": {
"api_key": {"type": "password", "required": True, "label": "Bing API Key"},
},
},
"yourdm": {
"YouRM": {
"env_var": "YDC_API_KEY",
"settings": {
"api_key": {
"type": "password",
"required": True,
"label": "YourDM API Key",
"label": "YouRM API Key",
},
},
},
"duckduckgo": {"env_var": None, "settings": {}},
"arxiv": {"env_var": None, "settings": {}},
"Duckduckgo": {"env_var": None, "settings": {}},
"Arxiv": {"env_var": None, "settings": {}},
}

LLM_MODELS = {
Expand All @@ -41,14 +41,14 @@
}

DEFAULT_SEARCH_OPTIONS = {
"primary_engine": "duckduckgo",
"primary_engine": "Duckduckgo",
"fallback_engine": None,
"search_top_k": 3,
"retrieve_top_k": 3,
"engine_settings": {
"searxng": {"base_url": "", "api_key": ""},
"bing": {"api_key": ""},
"yourdm": {"api_key": ""},
"SearXNG": {"base_url": "", "api_key": ""},
"Bing": {"api_key": ""},
"YouRM": {"api_key": ""},
},
}

Expand Down
11 changes: 6 additions & 5 deletions frontend/storm_wiki/util/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def __init__(self, max_results=20):

def _initialize_search_engines(self):
return {
"duckduckgo": self._search_duckduckgo,
"searxng": self._search_searxng,
"arxiv": self._search_arxiv,
"Duckduckgo": self._search_duckduckgo,
"SearXNG": self._search_searxng,
"Arxiv": self._search_arxiv,
}

def _load_search_options(self):
Expand Down Expand Up @@ -143,10 +143,11 @@ def _search_with_fallback(self, query: str) -> List[Dict[str, Any]]:
return results

def _search(self, engine: str, query: str) -> List[Dict[str, Any]]:
if engine not in self.search_engines:
engine = engine.lower() # Convert to lowercase for case-insensitive comparison
if engine not in map(str.lower, self.search_engines.keys()):
raise ValueError(f"Unsupported or unavailable search engine: {engine}")

search_engine = self.search_engines[engine]
search_engine = next(func for key, func in self.search_engines.items() if key.lower() == engine)
results = search_engine(query)

logger.info(f"Raw results from {engine}: {results}")
Expand Down

0 comments on commit 1face9a

Please sign in to comment.