🐫 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
enhance: Semantic Scholar (#1562)
Wendong-Fan4c6e87d701de
diff --git a/camel/toolkits/__init__.py b/camel/toolkits/__init__.pyindex af7e00ff4..cf5b45463 100644--- a/camel/toolkits/__init__.py+++ b/camel/toolkits/__init__.py@@ -45,7 +45,7 @@ from .human_toolkit import HumanToolkitfrom .stripe_toolkit import StripeToolkitfrom .video_toolkit import VideoDownloaderToolkitfrom .dappier_toolkit import DappierToolkit-from .semanticscholar_toolkit import SemanticScholarToolkit+from .semantic_scholar_toolkit import SemanticScholarToolkit__all__ = ['BaseToolkit',diff --git a/camel/toolkits/semanticscholar_toolkit.py b/camel/toolkits/semantic_scholar_toolkit.pysimilarity index 53%rename from camel/toolkits/semanticscholar_toolkit.pyrename to camel/toolkits/semantic_scholar_toolkit.pyindex 627f9393a..809f4c01c 100644--- a/camel/toolkits/semanticscholar_toolkit.py+++ b/camel/toolkits/semantic_scholar_toolkit.py@@ -1,265 +1,308 @@-# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========--import json-from typing import List--import requests--from camel.toolkits import FunctionTool-from camel.toolkits.base import BaseToolkit---class SemanticScholarToolkit(BaseToolkit):- """A toolkit for interacting with the Semantic Scholar- API to fetch paper and author data."""-- def __init__(self):- """Initializes the SemanticScholarToolkit."""- self.base_url = "https://api.semanticscholar.org/graph/v1"-- def fetch_paper_data_title(- self,- paperTitle: str,- fields: str = """title,abstract,authors,year,citationCount,- publicationTypes,publicationDate,openAccessPdf""",- ) -> dict:- r"""Fetches a SINGLE paper from the Semantic Scholar- API based on a paper title.-- Args:- paperTitle (str): The title of the paper to fetch.- fields (str): A comma-separated list of fields to include- in the response (default includes title, abstract, authors, year,- citation count, publicationTypes,publicationDate,openAccessPdf).-- Returns:- dict: The response data from the API or error- information if the request fails.- """- url = f"{self.base_url}/paper/search"- query_params = {"query": paperTitle, "fields": fields}- try:- response = requests.get(url, params=query_params)- response.raise_for_status()- return response.json()- except requests.exceptions.RequestException as e:- return {- "error": f"Request failed: {e!s}",- "message": str(e),- }- except ValueError:- return {- "error": "Response is not valid JSON",- "message": response.text,- }-- def fetch_paper_data_id(- self,- paperID: str,- fields: str = """title,abstract,authors,year,citationCount,- publicationTypes,publicationDate,openAccessPdf""",- ) -> dict:- r"""Fetches a SINGLE paper from the Semantic Scholar- API based on a paper ID.-- Args:- paperID (str): The ID of the paper to fetch.- fields (str): A comma-separated list of fields to- include in the response (default includes title, abstract,- authors, year, citation count, publicationTypes,- publicationDate, openAccessPdf).-- Returns:- dict: The response data from the API or error information- if the request fails.- """- url = f"{self.base_url}/paper/{paperID}"- query_params = {"fields": fields}- try:- response = requests.get(url, params=query_params)- response.raise_for_status()- return response.json()- except requests.exceptions.RequestException as e:- return {- "error": f"Request failed: {e!s}",- "message": str(e),- }- except ValueError:- return {- "error": "Response is not valid JSON",- "message": response.text,- }-- def fetch_bulk_paper_data(- self,- query: str,- year: str = "2023-",- fields: str = """title,url,publicationTypes,- publicationDate,openAccessPdf""",- ) -> dict:- r"""Fetches MULTIPLE papers at once from the Semantic Scholar- API based on a related topic.- Args:- query (str):- The text query to match against the paper's title- and abstract.- For example, you can use the following operators- and techniques to construct your query:- Example 1:- ((cloud computing) | virtualization)- +security -privacy This will match papers- whose title or abstract contains "cloud"- and "computing", or contains the word- "virtualization". The papers must also- include the term "security" but exclude- papers that contain the word "privacy".- year (str): The year filter for papers (default is "2023-").- fields (str): The fields to include in the response- (e.g., 'title,url,publicationTypes,publicationDate,- openAccessPdf').- Returns:- dict: The response data from the API or- error information if the request fails.- """- url = f"{self.base_url}/paper/search/bulk"- query_params = {"query": query, "fields": fields, "year": year}- try:- response = requests.get(url, params=query_params)- response.raise_for_status()- return response.json()- except requests.exceptions.RequestException as e:- return {- "error": f"Request failed: {e!s}",- "message": str(e),- }- except ValueError:- return {- "error": "Response is not valid JSON",- "message": response.text,- }-- def fetch_recommended_papers(- self,- positive_paper_ids: List[str],- negative_paper_ids: List[str],- fields: str = """title,url,citationCount,authors,- publicationTypes,publicationDate,openAccessPdf""",- limit: int = 500,- save_to_file: bool = False,- ) -> dict:- r"""Fetches recommended papers from the Semantic Scholar- API based on the positive and negative paper IDs.-- Args:- positive_paper_ids (list): A list of paper IDs (as strings)- that are positively correlated to the recommendation.-- negative_paper_ids (list): A list of paper IDs (as strings)- that are negatively correlated to the recommendation.-- fields (str): The fields to include in the response- (e.g., 'title,url,citationCount,authors,publicationTypes,- publicationDate,openAccessPdf').-- limit (int): The maximum number of recommended papers to return.- Default is 500.-- save_to_file (bool): If True, saves the response data to a file- (default is False).-- Returns:- dict: A dictionary containing recommended papers sorted by- citation count.- """- url = "https://api.semanticscholar.org/recommendations/v1/papers"- query_params = {"fields": fields, "limit": str(limit)}- data = {- "positivePaperIds": positive_paper_ids,- "negativePaperIds": negative_paper_ids,- }- try:- response = requests.post(url, params=query_params, json=data)- response.raise_for_status()- papers = response.json()- if save_to_file:- with open('recommended_papers.json', 'w') as output:- json.dump(papers, output)- return papers- except requests.exceptions.RequestException as e:- return {"error": str(e)}- except ValueError:- return {- "error": "Response is not valid JSON",- "message": response.text,- }-- def fetch_author_data(- self,- ids: List[str],- fields: str = "name,url,paperCount,hIndex,papers",- save_to_file: bool = False,- ) -> dict:- r"""Fetches author information from the Semantic Scholar- API based on author IDs.-- Args:- ids (list): A list of author IDs (as strings) to fetch- data for.-- fields (str): A comma-separated list of fields to include- in the response (default includes name, URL, paper count,- hIndex, and papers).-- save_to_file (bool): If True, saves the response data to- a file (default is False).-- Returns:- dict: The response data from the API or error information if- the request fails.- """- url = f"{self.base_url}/author/batch"- query_params = {"fields": fields}- data = {"ids": ids}- try:- response = requests.post(url, params=query_params, json=data)- response.raise_for_status()- response_data = response.json()- if save_to_file:- with open('author_information.json', 'w') as output:- json.dump(response_data, output)- return response_data- except requests.exceptions.RequestException as e:- return {"error": str(e)}- except ValueError:- return {- "error": "Response is not valid JSON",- "message": response.text,- }-- def get_tools(self) -> List[FunctionTool]:- r"""Returns a list of FunctionTool objects representing the- functions in the toolkit.-- Returns:- List[FunctionTool]: A list of FunctionTool objects- representing the functions in the toolkit.- """- return [- FunctionTool(self.fetch_paper_data_title),- FunctionTool(self.fetch_paper_data_id),- FunctionTool(self.fetch_bulk_paper_data),- FunctionTool(self.fetch_recommended_papers),- FunctionTool(self.fetch_author_data),- ]+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========++import json+from typing import List, Optional++import requests++from camel.toolkits import FunctionTool+from camel.toolkits.base import BaseToolkit+++class SemanticScholarToolkit(BaseToolkit):+ r"""A toolkit for interacting with the Semantic Scholar+ API to fetch paper and author data.+ """++ def __init__(self):+ r"""Initializes the SemanticScholarToolkit."""+ self.base_url = "https://api.semanticscholar.org/graph/v1"++ def fetch_paper_data_title(+ self,+ paper_title: str,+ fields: Optional[List[str]] = None,+ ) -> dict:+ r"""Fetches a SINGLE paper from the Semantic Scholar+ API based on a paper title.++ Args:+ paper_title (str): The title of the paper to fetch.+ fields (Optional[List[str]], optional): The fields to include in+ the response (default: :obj:`None`). If not provided defaults+ to ["title", "abstract", "authors", "year", "citationCount",+ "publicationTypes", "publicationDate", "openAccessPdf"].++ Returns:+ dict: The response data from the API or error information if the+ request fails.+ """+ if fields is None:+ fields = [+ "title",+ "abstract",+ "authors",+ "year",+ "citationCount",+ "publicationTypes",+ "publicationDate",+ "openAccessPdf",+ ]++ url = f"{self.base_url}/paper/search"+ query_params = {"query": paper_title, "fields": ",".join(fields)}+ try:+ response = requests.get(url, params=query_params)+ response.raise_for_status()+ return response.json()+ except requests.exceptions.RequestException as e:+ return {+ "error": f"Request failed: {e!s}",+ "message": str(e),+ }+ except ValueError:+ return {+ "error": "Response is not valid JSON",+ "message": response.text,+ }++ def fetch_paper_data_id(+ self,+ paper_id: str,+ fields: Optional[List[str]] = None,+ ) -> dict:+ r"""Fetches a SINGLE paper from the Semantic Scholar+ API based on a paper ID.++ Args:+ paper_id (str): The ID of the paper to fetch.+ fields (Optional[List[str]], optional): The fields to include in+ the response (default: :obj:`None`). If not provided defaults+ to ["title", "abstract", "authors", "year", "citationCount",+ "publicationTypes", "publicationDate", "openAccessPdf"].++ Returns:+ dict: The response data from the API or error information+ if the request fails.+ """+ if fields is None:+ fields = [+ "title",+ "abstract",+ "authors",+ "year",+ "citationCount",+ "publicationTypes",+ "publicationDate",+ "openAccessPdf",+ ]++ url = f"{self.base_url}/paper/{paper_id}"+ query_params = {"fields": ",".join(fields)}+ try:+ response = requests.get(url, params=query_params)+ response.raise_for_status()+ return response.json()+ except requests.exceptions.RequestException as e:+ return {+ "error": f"Request failed: {e!s}",+ "message": str(e),+ }+ except ValueError:+ return {+ "error": "Response is not valid JSON",+ "message": response.text,+ }++ def fetch_bulk_paper_data(+ self,+ query: str,+ year: str = "2023-",+ fields: Optional[List[str]] = None,+ ) -> dict:+ r"""Fetches MULTIPLE papers at once from the Semantic Scholar+ API based on a related topic.++ Args:+ query (str): The text query to match against the paper's title and+ abstract. For example, you can use the following operators and+ techniques to construct your query: Example 1: ((cloud+ computing) | virtualization) +security -privacy This will+ match papers whose title or abstract contains "cloud" and+ "computing", or contains the word "virtualization". The papers+ must also include the term "security" but exclude papers that+ contain the word "privacy".+ year (str, optional): The year filter for papers (default:+ :obj:`"2023-"`).+ fields (Optional[List[str]], optional): The fields to include in+ the response (default: :obj:`None`). If not provided defaults+ to ["title", "url", "publicationTypes", "publicationDate",+ "openAccessPdf"].++ Returns:+ dict: The response data from the API or error information if the+ request fails.+ """+ if fields is None:+ fields = [+ "title",+ "url",+ "publicationTypes",+ "publicationDate",+ "openAccessPdf",+ ]++ url = f"{self.base_url}/paper/search/bulk"+ query_params = {+ "query": query,+ "fields": ",".join(fields),+ "year": year,+ }+ try:+ response = requests.get(url, params=query_params)+ response.raise_for_status()+ return response.json()+ except requests.exceptions.RequestException as e:+ return {+ "error": f"Request failed: {e!s}",+ "message": str(e),+ }+ except ValueError:+ return {+ "error": "Response is not valid JSON",+ "message": response.text,+ }++ def fetch_recommended_papers(+ self,+ positive_paper_ids: List[str],+ negative_paper_ids: List[str],+ fields: Optional[List[str]] = None,+ limit: int = 500,+ save_to_file: bool = False,+ ) -> dict:+ r"""Fetches recommended papers from the Semantic Scholar+ API based on the positive and negative paper IDs.++ Args:+ positive_paper_ids (list): A list of paper IDs (as strings)+ that are positively correlated to the recommendation.+ negative_paper_ids (list): A list of paper IDs (as strings)+ that are negatively correlated to the recommendation.+ fields (Optional[List[str]], optional): The fields to include in+ the response (default: :obj:`None`). If not provided defaults+ to ["title", "url", "citationCount", "authors",+ "publicationTypes", "publicationDate", "openAccessPdf"].+ limit (int, optional): The maximum number of recommended papers to+ return (default: :obj:`500`).+ save_to_file (bool, optional): If True, saves the response data to+ a file (default: :obj:`False`).++ Returns:+ dict: A dictionary containing recommended papers sorted by+ citation count.+ """+ if fields is None:+ fields = [+ "title",+ "url",+ "citationCount",+ "authors",+ "publicationTypes",+ "publicationDate",+ "openAccessPdf",+ ]++ url = "https://api.semanticscholar.org/recommendations/v1/papers"+ query_params = {"fields": ",".join(fields), "limit": str(limit)}+ data = {+ "positivepaper_ids": positive_paper_ids,+ "negativepaper_ids": negative_paper_ids,+ }+ try:+ response = requests.post(url, params=query_params, json=data)+ response.raise_for_status()+ papers = response.json()+ if save_to_file:+ with open('recommended_papers.json', 'w') as output:+ json.dump(papers, output)+ return papers+ except requests.exceptions.RequestException as e:+ return {"error": str(e)}+ except ValueError:+ return {+ "error": "Response is not valid JSON",+ "message": response.text,+ }++ def fetch_author_data(+ self,+ ids: List[str],+ fields: Optional[List[str]] = None,+ save_to_file: bool = False,+ ) -> dict:+ r"""Fetches author information from the Semantic Scholar+ API based on author IDs.++ Args:+ ids (list): A list of author IDs (as strings) to fetch+ data for.+ fields (Optional[List[str]], optional): The fields to include in+ the response (default: :obj:`None`). If not provided defaults+ to ["name", "url", "paperCount", "hIndex", "papers"].+ save_to_file (bool, optional): Whether to save the results to a+ file (default: :obj:`False`).++ Returns:+ dict: The response data from the API or error information if+ the request fails.+ """+ if fields is None:+ fields = ["name", "url", "paperCount", "hIndex", "papers"]++ url = f"{self.base_url}/author/batch"+ query_params = {"fields": ",".join(fields)}+ data = {"ids": ids}+ try:+ response = requests.post(url, params=query_params, json=data)+ response.raise_for_status()+ response_data = response.json()+ if save_to_file:+ with open('author_information.json', 'w') as output:+ json.dump(response_data, output)+ return response_data+ except requests.exceptions.RequestException as e:+ return {"error": str(e)}+ except ValueError:+ return {+ "error": "Response is not valid JSON",+ "message": response.text,+ }++ def get_tools(self) -> List[FunctionTool]:+ r"""Returns a list of FunctionTool objects representing the+ functions in the toolkit.++ Returns:+ List[FunctionTool]: A list of FunctionTool objects+ representing the functions in the toolkit.+ """+ return [+ FunctionTool(self.fetch_paper_data_title),+ FunctionTool(self.fetch_paper_data_id),+ FunctionTool(self.fetch_bulk_paper_data),+ FunctionTool(self.fetch_recommended_papers),+ FunctionTool(self.fetch_author_data),+ ]diff --git a/examples/toolkits/semanticscholar_toolkit.py b/examples/toolkits/semantic_scholar_toolkit.pysimilarity index 87%rename from examples/toolkits/semanticscholar_toolkit.pyrename to examples/toolkits/semantic_scholar_toolkit.pyindex 94df72982..e03cd6570 100644--- a/examples/toolkits/semanticscholar_toolkit.py+++ b/examples/toolkits/semantic_scholar_toolkit.py@@ -13,7 +13,6 @@# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========from camel.agents import ChatAgentfrom camel.configs import ChatGPTConfig-from camel.messages import BaseMessagefrom camel.models import ModelFactoryfrom camel.toolkits import SemanticScholarToolkitfrom camel.types import ModelPlatformType, ModelType@@ -26,10 +25,7 @@ model = ModelFactory.create()-sys_msg = BaseMessage.make_assistant_message(- role_name='Tools calling operator', content='You are a helpful assistant'-)-+sys_msg = "You are a helpful assistant"# Initialize a toolkittoolkit = SemanticScholarToolkit()@@ -43,13 +39,8 @@ camel_agent = ChatAgent(tools=tools,)-camel_agent.reset()--# Description of the added tools-usr_msg = BaseMessage.make_user_message(- role_name="CAMEL User", content="""Describe the tools you've added"""-)+usr_msg = "Describe the tools you've added"response = camel_agent.step(usr_msg)print(response.msgs[0].content)@@ -89,12 +80,9 @@ These tools can be used individually or in combination to'''# Search a paper through its id-usr_msg = BaseMessage.make_user_message(- role_name="CAMEL User",- content="""search the paper 'Construction of the Literature- Graph in Semantic Scholar' for me including its paperid""",-)-+usr_msg = """search the paper 'Construction of the Literature+ Graph in Semantic Scholar' for me including its paperid"""+camel_agent.reset()response = camel_agent.step(usr_msg)print(str(response.info['tool_calls'])[:1000])@@ -121,12 +109,9 @@ discovery. The resulting literature graph consists of more than'''# Search a paper through its title-usr_msg = BaseMessage.make_user_message(- role_name="CAMEL User",- content="""search the paper with paper id of- '649def34f8be52c8b66281af98ae884c09aef38b' for me""",-)-+usr_msg = """search the paper with paper id of+ '649def34f8be52c8b66281af98ae884c09aef38b' for me"""+camel_agent.reset()response = camel_agent.step(usr_msg)print(str(response.info['tool_calls'])[:1000])@@ -154,12 +139,9 @@ result={'paperId': '649def34f8be52c8b66281af98ae884c09aef38b','''# Search papers thourgh related topic-usr_msg = BaseMessage.make_user_message(- role_name="CAMEL User",- content="""search 10 papers with topic related to- 'generative ai' from 2024 for me""",-)-+usr_msg = """search 3 papers with topic related to+ 'generative ai' from 2024 for me"""+camel_agent.reset()response = camel_agent.step(usr_msg)print(str(response.info['tool_calls'])[:1000])@@ -186,12 +168,9 @@ f0883cd9', 'title': 'A Chitchat on Using ChatGPT for Cheating','''# Search papers through realted topic and operator-usr_msg = BaseMessage.make_user_message(- role_name="CAMEL User",- content="""search 20 papers with topic related to- 'ai and bio' from 2024 for me""",-)-+usr_msg = """search 2 papers with topic related to+ 'ai and bio' from 2024 for me"""+camel_agent.reset()response = camel_agent.step(usr_msg)print(str(response.info['tool_calls'])[:1000])@@ -218,15 +197,12 @@ cholar.org/paper/00c8477a9cc28b85e4f6da13d2a889c94a955291','''# Recommand papers thorugh positive and negative paper id-usr_msg = BaseMessage.make_user_message(- role_name="CAMEL User",- content="""recommend 20 papers with positive paper id+usr_msg = """recommend 2 papers with positive paper idof "02138d6d094d1e7511c157f0b1a3dd4e5b20ebee","018f58247a20ec6b3256fd3119f57980a6f37748" and negativepaper id of "0045ad0c1e14a4d1f4b011c92eb36b8df63d65bc"- for me""",-)-+ for me"""+camel_agent.reset()response = camel_agent.step(usr_msg)print(str(response.info['tool_calls'])[:1000])@@ -253,12 +229,9 @@ le': 'Embrace, Don't Avoid: Reimagining Higher Education with'''# Recommend papers and save the result in a file-usr_msg = BaseMessage.make_user_message(- role_name="CAMEL User",- content="""search the authors of author ids of "2281351310",- "2281342663","2300302076","2300141520" for me""",-)-+usr_msg = """search the authors of author ids of "2281351310",+ "2281342663","2300302076","2300141520" for me"""+camel_agent.reset()response = camel_agent.step(usr_msg)print(str(response.info['tool_calls'])[:1000])@@ -285,15 +258,12 @@ e_paper_ids': ['0045ad0c1e14a4d1f4b011c92eb36b8df63d65bc'],'''# Search author information through author id-usr_msg = BaseMessage.make_user_message(- role_name="CAMEL User",- content="""recommend 20 papers with positive paper id+usr_msg = """recommend 2 papers with positive paper idof "02138d6d094d1e7511c157f0b1a3dd4e5b20ebee", "018f58247a20ec6b3256fd3119f57980a6f37748" and negative paperid of "0045ad0c1e14a4d1f4b011c92eb36b8df63d65bc" for me,- and please save the result in a file.""",-)-+ and please save the result in a file."""+camel_agent.reset()response = camel_agent.step(usr_msg)print(str(response.info['tool_calls'])[:1000])@@ -320,13 +290,10 @@ ive_paper_ids': ['0045ad0c1e14a4d1f4b011c92eb36b8df63d65bc'],'''# Search author information and save the result in a file-usr_msg = BaseMessage.make_user_message(- role_name="CAMEL User",- content="""search the authors of author ids of "2281351310"+usr_msg = """search the authors of author ids of "2281351310","2281342663","2300302076","2300141520" for me, and please- save the record in a file.""",-)-+ save the record in a file."""+camel_agent.reset()response = camel_agent.step(usr_msg)print(str(response.info['tool_calls'])[:1000])diff --git a/test/toolkits/test_semantic_functions.py b/test/toolkits/test_semantic_scholar_functions.pysimilarity index 96%rename from test/toolkits/test_semantic_functions.pyrename to test/toolkits/test_semantic_scholar_functions.pyindex 8af11c5ef..2d9a0ed75 100644--- a/test/toolkits/test_semantic_functions.py+++ b/test/toolkits/test_semantic_scholar_functions.py@@ -14,7 +14,7 @@import unittestfrom unittest.mock import MagicMock, patch-from camel.toolkits.semanticscholar_toolkit import SemanticScholarToolkit+from camel.toolkits import SemanticScholarToolkitclass TestSemanticScholarToolkit(unittest.TestCase):