🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
add search_wiki_by_api (#253)
zhiyu-01ce8bc38926e0
diff --git a/camel/functions/search_functions.py b/camel/functions/search_functions.pyindex 53b77ae49..5f37a4820 100644--- a/camel/functions/search_functions.py+++ b/camel/functions/search_functions.py@@ -14,6 +14,7 @@from typing import Listimport requests+import wikipediafrom bs4 import BeautifulSoupfrom .openai_function import OpenAIFunction@@ -107,6 +108,33 @@ def search_wiki(entity: str) -> str:return observation+def search_wiki_by_api(entity: str) -> str:+ r"""Using wikipedia api to search the entity in WikiPedia and+ return the summary of the given entity. The auto suggest and+ redirect are enabled by default.++ Args:+ entity (string): The entity to be searched.++ Returns:+ string: The search result. If the page corresponding to the entity+ exists, return the summary in a string.+ """+ result: str++ try:+ result = wikipedia.summary(entity, sentences=5)++ except wikipedia.exceptions.DisambiguationError as e:+ result = wikipedia.summary(e.options[0], sentences=5)+ except wikipedia.exceptions.PageError:+ result = "Page does not exist!"+ except wikipedia.exceptions.WikipediaException as e:+ result = f"An error occurred: {e}"++ return result++SEARCH_FUNCS: List[OpenAIFunction] = [- OpenAIFunction(func) for func in [search_wiki]+ OpenAIFunction(func) for func in [search_wiki, search_wiki_by_api]]diff --git a/pyproject.toml b/pyproject.tomlindex 47aa5b5a5..4d69c5d8f 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -43,6 +43,7 @@ torch = { version = "^1", optional = true }soundfile = { version = "^0", optional = true }sentencepiece = { version = "^0", optional = true }opencv-python = { version = "^4", optional = true }+wikipedia = { version = "^1", optional = true }[tool.poetry.extras]huggingface-agent = [@@ -54,6 +55,7 @@ huggingface-agent = ["soundfile","sentencepiece","opencv-python",+ "wikipedia",]all = [@@ -65,6 +67,7 @@ all = ["soundfile","sentencepiece","opencv-python",+ "wikipedia",][tool.poetry.group.dev]@@ -128,5 +131,6 @@ module = ["database_connection","huggingface_hub","huggingface_hub.utils._errors",+ "wikipedia",]ignore_missing_imports = truediff --git a/test/functions/test_search_functions.py b/test/functions/test_search_functions.pyindex 0a5b7f05a..07f3fd75d 100644--- a/test/functions/test_search_functions.py+++ b/test/functions/test_search_functions.py@@ -15,6 +15,7 @@ from camel.functions.search_functions import (clean_str,get_page_abstract,search_wiki,+ search_wiki_by_api,)@@ -57,3 +58,19 @@ def test_search_wiki_not_found():"South Africa Women Football Team")assert search_output.startswith("Could not find South Africa Women Football Team.")+++def tests_earch_wiki_by_api():+ expected_output = (+ "New York, often called New York State, is a state in the "+ "Northeastern United States. A Mid-Atlantic state, New York "+ "borders New England, and has an international border with "+ "Canada. With almost 19.7 million residents, it is the fourth-most"+ " populous state in the United States and seventh-most densely "+ "populated as of 2022. New York is the 27th-largest U.S. state by "+ "area, with a total area of 54,556 square miles (141,300 km2).New "+ "York has a varied geography. The southeastern part of the state, "+ "known as Downstate, encompasses New York City (the most populous "+ "city in the United States), Long Island (the most populous island "+ "in the United States), and the lower Hudson Valley.")+ assert search_wiki_by_api("New York") == expected_output