From 94b2f536f3f762bda6bef8ee8c5c97ad5d870988 Mon Sep 17 00:00:00 2001 From: akmhmgc <38002468+akmhmgc@users.noreply.github.com> Date: Mon, 3 Apr 2023 06:00:27 +0900 Subject: [PATCH] Modify output for wikipedia api wrapper (#2287) ## Description Thanks for the quick maintenance for great repository!! I modified wikipedia api wrapper ## Details - Add output for missing search results - Add tests --- langchain/utilities/wikipedia.py | 5 ++++- tests/integration_tests/test_wikipedia_api.py | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/integration_tests/test_wikipedia_api.py diff --git a/langchain/utilities/wikipedia.py b/langchain/utilities/wikipedia.py index 6685ead8..259e8a44 100644 --- a/langchain/utilities/wikipedia.py +++ b/langchain/utilities/wikipedia.py @@ -39,7 +39,10 @@ class WikipediaAPIWrapper(BaseModel): """Run Wikipedia search and get page summaries.""" search_results = self.wiki_client.search(query) summaries = [] - for i in range(min(self.top_k_results, len(search_results))): + len_search_results = len(search_results) + if len_search_results == 0: + return "No good Wikipedia Search Result was found" + for i in range(min(self.top_k_results, len_search_results)): summary = self.fetch_formatted_page_summary(search_results[i]) if summary is not None: summaries.append(summary) diff --git a/tests/integration_tests/test_wikipedia_api.py b/tests/integration_tests/test_wikipedia_api.py new file mode 100644 index 00000000..db82dde2 --- /dev/null +++ b/tests/integration_tests/test_wikipedia_api.py @@ -0,0 +1,19 @@ +"""Integration test for Wikipedia API Wrapper.""" +from langchain.utilities import WikipediaAPIWrapper + + +def test_call() -> None: + """Test that WikipediaAPIWrapper returns correct answer""" + + wikipedia = WikipediaAPIWrapper() + output = wikipedia.run("HUNTER X HUNTER") + assert "Yoshihiro Togashi" in output + + +def test_no_result_call() -> None: + """Test that call gives no result.""" + wikipedia = WikipediaAPIWrapper() + output = wikipedia.run( + "NORESULTCALL_NORESULTCALL_NORESULTCALL_NORESULTCALL_NORESULTCALL_NORESULTCALL" + ) + assert "No good Wikipedia Search Result was found" == output