Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

Modified Wikipedia Search (#258)

Wenxuan Lida37bc524cb0
diff --git a/camel/functions/search_functions.py b/camel/functions/search_functions.py
index 5f37a4820..b3b213500 100644
--- a/camel/functions/search_functions.py
+++ b/camel/functions/search_functions.py
@@ -13,128 +13,39 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from typing import List
-import requests
import wikipedia
-from bs4 import BeautifulSoup
from .openai_function import OpenAIFunction
-def clean_str(p: str) -> str:
- r"""Cleans the input string by encoding and decoding it multiple times
- to ensure it can be properly read and processed by Python code.
-
- Args:
- p (str): The input string to be cleaned, typically from the webpage.
-
- Returns:
- str: The cleaned string.
- """
- return p.encode().decode("unicode-"
- "escape").encode("latin1").decode("utf-8")
-
-
-def get_page_abstract(page: str) -> str:
- r"""Returns the first :obj:`5` sentences of the fetched page.
-
- Args:
- page (str): The fetched page.
-
- Returns:
- str: The concatenation of the first :obj:`5` sentences in the
- given page.
- """
- paragraphs = page.split('\n')
- paragraphs = [p.strip() for p in paragraphs if p.strip()]
-
- # find all sentences
- sentences = []
- for p in paragraphs:
- sents = p.split('. ')
- if sents[-1].endswith('.'):
- sents[-1] = sents[-1][:-1]
- sentences += sents
- sentences = [s.strip() + '.' for s in sentences if s.strip()]
-
- return ' '.join(sentences[:5])
-
-
def search_wiki(entity: str) -> str:
- r"""Search the entity in WikiPedia and return (the first :obj:`5`
- sentences of) the required page, containing factual information
- about the given entity.
+ r"""Search the entity in WikiPedia and return the summary of the
+ required page, containing factual information about the given entity.
Args:
entity (string): The entity to be searched.
Returns:
string: The search result. If the page corresponding to the entity
- exists, return the first :obj:`5` sentences in a string.
- """
- entity_ = entity.replace(" ", "+")
- search_url = f"https://en.wikipedia.org/w/index.php?search={entity_}"
-
- # request the target page
- response_text = requests.get(search_url).text
-
- # parse the obtained page
- soup = BeautifulSoup(response_text, features="html.parser")
- result_divs = soup.find_all("div", {"class": "mw-search-result-heading"})
-
- observation: str
- if result_divs:
- # only similar concepts exist
- result_titles = [
- clean_str(div.get_text().strip()) for div in result_divs
- ]
- observation = (f"Could not find {entity}. "
- f"Similar: {result_titles[:5]}.")
- else:
- # the page corresponding to the entity exists
- page = [
- p.get_text().strip()
- for p in soup.find_all("p") + soup.find_all("ul")
- ]
-
- res_page = ""
- for p in page:
- if len(p.split(" ")) > 2:
- res_page += clean_str(p)
- if not p.endswith("\n"):
- res_page += "\n"
-
- observation = get_page_abstract(res_page)
-
- 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.
+ exists, return the summary of this entity in a string.
"""
result: str
try:
- result = wikipedia.summary(entity, sentences=5)
-
+ result = wikipedia.summary(entity, sentences=5, auto_suggest=False)
except wikipedia.exceptions.DisambiguationError as e:
- result = wikipedia.summary(e.options[0], sentences=5)
+ result = wikipedia.summary(e.options[0], sentences=5,
+ auto_suggest=False)
except wikipedia.exceptions.PageError:
- result = "Page does not exist!"
+ result = ("There is no page in Wikipedia corresponding to entity "
+ f"{entity}, please specify another word to describe the"
+ " entity to be searched.")
except wikipedia.exceptions.WikipediaException as e:
- result = f"An error occurred: {e}"
+ result = f"An exception occurred during the search: {e}"
return result
SEARCH_FUNCS: List[OpenAIFunction] = [
- OpenAIFunction(func) for func in [search_wiki, search_wiki_by_api]
+ OpenAIFunction(func) for func in [search_wiki]
]
diff --git a/examples/function_call/role_playing_with_function.py b/examples/function_call/role_playing_with_function.py
index b87b87fdc..a79410454 100644
--- a/examples/function_call/role_playing_with_function.py
+++ b/examples/function_call/role_playing_with_function.py
@@ -16,7 +16,7 @@ from typing import List
from colorama import Fore
from camel.agents.chat_agent import FunctionCallingRecord
-from camel.configs import FunctionCallingConfig
+from camel.configs import ChatGPTConfig, FunctionCallingConfig
from camel.functions import MATH_FUNCS, SEARCH_FUNCS
from camel.societies import RolePlaying
from camel.typing import ModelType
@@ -27,10 +27,12 @@ def main(model_type=ModelType.GPT_4) -> None:
task_prompt = ("Assuming the current year is 2023, estimate KAUST's "
"current age and then add 10 more years to this age.")
+ user_model_config = ChatGPTConfig(temperature=0.0)
+
function_list = [*MATH_FUNCS, *SEARCH_FUNCS]
assistant_model_config = FunctionCallingConfig.from_openai_function_list(
function_list=function_list,
- kwargs=dict(temperature=0),
+ kwargs=dict(temperature=0.0),
)
role_play_session = RolePlaying(
@@ -41,10 +43,12 @@ def main(model_type=ModelType.GPT_4) -> None:
model_config=assistant_model_config,
function_list=function_list,
),
- user_agent_kwargs=dict(model=model_type),
+ user_agent_kwargs=dict(
+ model=model_type,
+ model_config=user_model_config,
+ ),
task_prompt=task_prompt,
with_task_specify=False,
- task_specify_agent_kwargs=dict(model=model_type),
)
print(
diff --git a/test/functions/test_search_functions.py b/test/functions/test_search_functions.py
index 6ddec61f2..0c4283449 100644
--- a/test/functions/test_search_functions.py
+++ b/test/functions/test_search_functions.py
@@ -11,45 +11,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
-from camel.functions.search_functions import (
- clean_str,
- get_page_abstract,
- search_wiki,
- search_wiki_by_api,
-)
-
-
-def test_clean_str():
- input_str = "Some escaped string with unicode characters: \u2019"
- expected_output = "Some escaped string with unicode characters: ’"
- assert clean_str(input_str) == expected_output
-
-
-def test_get_page_abstract():
- input_page = "\n".join([
- "This is the first sentence", "This is the second sentence",
- "This is the third sentence", "This is the fourth sentence",
- "This is the fifth sentence", "This is the sixth sentence"
- ])
- expected_output = (
- "This is the first sentence. This is the second sentence. "
- "This is the third sentence. This is the fourth sentence. "
- "This is the fifth sentence.")
-
- assert get_page_abstract(input_page) == expected_output
+from camel.functions.search_functions import search_wiki
def test_search_wiki_normal():
expected_output = (
"Erygia sigillata is a species of moth in the family Erebidae found "
- "in Himachal Pradesh, Northern India.[2] The moth was officially "
- "recognized and classified in 1892. This Erebinae-related article "
- "is a stub. You can help Wikipedia by expanding it. Main "
- "pageContentsCurrent eventsRandom articleAbout WikipediaContact "
- "usDonate. HelpLearn to editCommunity portalRecent "
- "changesUpload file.")
+ "in Himachal Pradesh, Northern India. The moth was officially "
+ "recognized and classified in 1892.")
- # Test that `search_wiki` returns the expected output
assert search_wiki("Erygia sigillata") == expected_output
@@ -57,20 +27,22 @@ def test_search_wiki_not_found():
search_output = search_output = search_wiki(
"South Africa Women Football Team")
assert search_output.startswith(
- "Could not find South Africa Women Football Team.")
+ "There is no page in Wikipedia corresponding to entity South Africa "
+ "Women Football Team, please specify another word to describe the "
+ "entity to be searched.")
-def tests_earch_wiki_by_api():
+def tests_search_wiki_with_ambiguity():
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
+ "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("New York") == expected_output