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

feat: Add more openapi functions and refactor open_api_function (#578)

Ziyi Yang2f26b55c26c3
diff --git a/camel/functions/__init__.py b/camel/functions/__init__.py
index 1f63cdfb8..bd6e96920 100644
--- a/camel/functions/__init__.py
+++ b/camel/functions/__init__.py
@@ -27,10 +27,17 @@ from .twitter_function import TWITTER_FUNCS
from .weather_functions import WEATHER_FUNCS
from .slack_functions import SLACK_FUNCS
+from .open_api_function import (
+ apinames_filepaths_to_funs_schemas,
+ generate_apinames_filepaths,
+)
+
__all__ = [
'OpenAIFunction',
'get_openai_function_schema',
'get_openai_tool_schema',
+ 'apinames_filepaths_to_funs_schemas',
+ 'generate_apinames_filepaths',
'MAP_FUNCS',
'MATH_FUNCS',
'OPENAPI_FUNCS',
diff --git a/camel/functions/open_api_function.py b/camel/functions/open_api_function.py
index 104cf9a25..55d698c1f 100644
--- a/camel/functions/open_api_function.py
+++ b/camel/functions/open_api_function.py
@@ -332,13 +332,22 @@ def generate_openapi_funcs(
return functions
-def combine_all_funcs_schemas() -> Tuple[List[Callable], List[Dict[str, Any]]]:
- r"""Combines functions and schemas from multiple OpenAPI specifications.
+def apinames_filepaths_to_funs_schemas(
+ apinames_filepaths: List[Tuple[str, str]],
+) -> Tuple[List[Callable], List[Dict[str, Any]]]:
+ r"""Combines functions and schemas from multiple OpenAPI specifications,
+ using API names as keys.
- Iterates over a list of OpenAPI specs, parses each, and generates functions
- and schemas based on the defined operations and schemas respectively.
- Assumes specification files are named 'openapi.yaml' located in
- `open_api_specs/<api_name>/`.
+ This function iterates over tuples of API names and OpenAPI spec file
+ paths, parsing each spec to generate callable functions and schema
+ dictionaries, all organized by API name.
+
+ Args:
+ apinames_filepaths (List[Tuple[str, str]]): A list of tuples, where each
+ tuple consists of:
+ - The API name (str) as the first element.
+ - The file path (str) to the API's OpenAPI specification file as the
+ second element.
Returns:
Tuple[List[Callable], List[Dict[str, Any]]]:: one of callable
@@ -347,34 +356,59 @@ def combine_all_funcs_schemas() -> Tuple[List[Callable], List[Dict[str, Any]]]:
"""
combined_func_lst = []
combined_schemas_list = []
-
- for api_name in OpenAPIName:
+ for api_name, file_path in apinames_filepaths:
# Parse the OpenAPI specification for each API
current_dir = os.path.dirname(__file__)
- spec_file_path = os.path.join(
- current_dir, 'open_api_specs', f'{api_name.value}', 'openapi.yaml'
+ file_path = os.path.join(
+ current_dir, 'open_api_specs', f'{api_name}', 'openapi.yaml'
)
- openapi_spec = parse_openapi_file(spec_file_path)
+ openapi_spec = parse_openapi_file(file_path)
# Generate and merge function schemas
openapi_functions_schemas = openapi_spec_to_openai_schemas(
- api_name.value, openapi_spec
+ api_name, openapi_spec
)
combined_schemas_list.extend(openapi_functions_schemas)
# Generate and merge function lists
- openapi_functions_list = generate_openapi_funcs(
- api_name.value, openapi_spec
- )
+ openapi_functions_list = generate_openapi_funcs(api_name, openapi_spec)
combined_func_lst.extend(openapi_functions_list)
return combined_func_lst, combined_schemas_list
-combined_funcs_lst, combined_schemas_list = combine_all_funcs_schemas()
+def generate_apinames_filepaths() -> List[Tuple[str, str]]:
+ """Generates a list of tuples containing API names and their corresponding
+ file paths.
+
+ This function iterates over the OpenAPIName enum, constructs the file path
+ for each API's OpenAPI specification file, and appends a tuple of the API
+ name and its file path to the list. The file paths are relative to the
+ 'open_api_specs' directory located in the same directory as this script.
+
+ Returns:
+ List[Tuple[str, str]]: A list of tuples where each tuple contains two
+ elements. The first element of each tuple is a string representing
+ the name of an API, and the second element is a string that
+ specifies the file path to that API's OpenAPI specification file.
+ """
+ apinames_filepaths = []
+ current_dir = os.path.dirname(__file__)
+ for api_name in OpenAPIName:
+ file_path = os.path.join(
+ current_dir, 'open_api_specs', f'{api_name.value}', 'openapi.yaml'
+ )
+ apinames_filepaths.append((api_name.value, file_path))
+ return apinames_filepaths
+
+
+apinames_filepaths = generate_apinames_filepaths()
+all_funcs_lst, all_schemas_lst = apinames_filepaths_to_funs_schemas(
+ apinames_filepaths
+)
OPENAPI_FUNCS: List[OpenAIFunction] = [
OpenAIFunction(a_func, a_schema)
- for a_func, a_schema in zip(combined_funcs_lst, combined_schemas_list)
+ for a_func, a_schema in zip(all_funcs_lst, all_schemas_lst)
]
diff --git a/camel/functions/open_api_specs/biztoc/__init__.py b/camel/functions/open_api_specs/biztoc/__init__.py
new file mode 100644
index 000000000..d963f8cc8
--- /dev/null
+++ b/camel/functions/open_api_specs/biztoc/__init__.py
@@ -0,0 +1,13 @@
+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========
diff --git a/camel/functions/open_api_specs/biztoc/ai-plugin.json b/camel/functions/open_api_specs/biztoc/ai-plugin.json
new file mode 100644
index 000000000..ab873b80b
--- /dev/null
+++ b/camel/functions/open_api_specs/biztoc/ai-plugin.json
@@ -0,0 +1,34 @@
+{
+ "id": "plugin-da9afb50-fc07-4d30-b606-51ed1b105bfc",
+ "domain": "biztoc.com",
+ "namespace": "biztoc",
+ "status": "approved",
+ "manifest": {
+ "schema_version": "v1",
+ "name_for_model": "biztoc",
+ "name_for_human": "BizToc",
+ "description_for_model": "Plugin for querying BizToc for business news.",
+ "description_for_human": "Search BizToc for business & finance news.",
+ "auth": {
+ "type": null
+ },
+ "api": {
+ "type": "openapi",
+ "url": "https://ai.biztoc.com/openapi.yaml"
+ },
+ "logo_url": "https://biztoc.com/favicon.png",
+ "contact_email": "[email protected]",
+ "legal_info_url": "https://biztoc.com/s/legal"
+ },
+ "oauth_client_id": null,
+ "user_settings": {
+ "is_installed": false,
+ "is_authenticated": true
+ },
+ "categories": [
+ {
+ "id": "newly_added",
+ "title": "New"
+ }
+ ]
+}
\ No newline at end of file
diff --git a/camel/functions/open_api_specs/biztoc/openapi.yaml b/camel/functions/open_api_specs/biztoc/openapi.yaml
new file mode 100644
index 000000000..97437bc23
--- /dev/null
+++ b/camel/functions/open_api_specs/biztoc/openapi.yaml
@@ -0,0 +1,21 @@
+openapi: 3.0.1
+info:
+ title: BizToc
+ description: Search BizToc for business & finance news.
+ version: 'v1'
+servers:
+ - url: https://ai.biztoc.com
+paths:
+ /ai/news:
+ get:
+ operationId: getNews
+ summary: Retrieves the latest news whose content contains the query string.
+ parameters:
+ - in: query
+ name: query
+ schema:
+ type: string
+ description: Used to query news articles on their title and body. For example, ?query=apple will return news stories that have 'apple' in their title or body.
+ responses:
+ "200":
+ description: OK
diff --git a/camel/functions/open_api_specs/create_qr_code/__init__.py b/camel/functions/open_api_specs/create_qr_code/__init__.py
new file mode 100644
index 000000000..d963f8cc8
--- /dev/null
+++ b/camel/functions/open_api_specs/create_qr_code/__init__.py
@@ -0,0 +1,13 @@
+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========
diff --git a/camel/functions/open_api_specs/create_qr_code/openapi.yaml b/camel/functions/open_api_specs/create_qr_code/openapi.yaml
new file mode 100644
index 000000000..3819a618d
--- /dev/null
+++ b/camel/functions/open_api_specs/create_qr_code/openapi.yaml
@@ -0,0 +1,44 @@
+openapi: 3.0.1
+info:
+ title: QR Code API
+ version: 1.0.0
+ description: Create a QR code for any text or url.
+servers:
+ - url: https://create-qr-code.modelxy.com
+paths:
+ /create-qr-code:
+ get:
+ operationId: getQRCode
+ summary: Create a QR code
+ parameters:
+ - in: query
+ name: data
+ schema:
+ type: string
+ description: The data to encode in the QR code.
+ - in: query
+ name: size
+ schema:
+ type: string
+ default: '100x100'
+ description: The size of the QR code.
+ - in: query
+ name: alt
+ schema:
+ type: string
+ description: The alt text for the QR code image.
+ - in: query
+ name: title
+ schema:
+ type: string
+ description: The title for the QR code image.
+ responses:
+ '200':
+ description: A JSON object containing the QR code image tag.
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ img_tag:
+ type: string
diff --git a/camel/functions/open_api_specs/outschool/__init__.py b/camel/functions/open_api_specs/outschool/__init__.py
new file mode 100644
index 000000000..d963f8cc8
--- /dev/null
+++ b/camel/functions/open_api_specs/outschool/__init__.py
@@ -0,0 +1,13 @@
+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========
diff --git a/camel/functions/open_api_specs/outschool/ai-plugin.json b/camel/functions/open_api_specs/outschool/ai-plugin.json
new file mode 100644
index 000000000..1189675d5
--- /dev/null
+++ b/camel/functions/open_api_specs/outschool/ai-plugin.json
@@ -0,0 +1,34 @@
+{
+ "id": "plugin-9335c256-4658-4376-bac8-a0baa5c1c889",
+ "domain": "chatgpt-plugin.outschool.com",
+ "namespace": "Outschool",
+ "status": "approved",
+ "manifest": {
+ "schema_version": "v1",
+ "name_for_model": "Outschool",
+ "name_for_human": "Outschool",
+ "description_for_model": "Search for top-quality online classes and teachers on Outschool.",
+ "description_for_human": "Search for top-quality online classes and teachers on Outschool.",
+ "auth": {
+ "type": "none"
+ },
+ "api": {
+ "type": "openapi",
+ "url": "https://chatgpt-plugin.outschool.com/openapi.json"
+ },
+ "logo_url": "https://chatgpt-plugin.outschool.com/logo.png",
+ "contact_email": "[email protected]",
+ "legal_info_url": "https://outschool.com/terms"
+ },
+ "oauth_client_id": null,
+ "user_settings": {
+ "is_installed": false,
+ "is_authenticated": true
+ },
+ "categories": [
+ {
+ "id": "newly_added",
+ "title": "New"
+ }
+ ]
+}
\ No newline at end of file
diff --git a/camel/functions/open_api_specs/outschool/openapi.yaml b/camel/functions/open_api_specs/outschool/openapi.yaml
new file mode 100644
index 000000000..422e9422f
--- /dev/null
+++ b/camel/functions/open_api_specs/outschool/openapi.yaml
@@ -0,0 +1 @@
+{"openapi":"3.0.1","info":{"title":"Outschool Plugin","description":"Search for top-quality online classes and teachers on Outschool.","version":"v1"},"servers":[{"url":"https://chatgpt-plugin.outschool.com/api"}],"paths":{"/classes":{"get":{"operationId":"searchClasses","description":"Returns a list of online classes","parameters":[{"name":"timeZone","in":"query","required":true,"description":"IANA Time Zone identifier of the user. Either provided by user or derived from their location. Since Outschool parents and teachers can be from different time zones, this is required to search classes that are available in parent's timezone at reasonable hours. Only IANA format is accepted.","schema":{"type":"string"},"examples":{"losAngeles":{"value":"America/Los_Angeles"},"newYork":{"value":"America/New_York"},"london":{"value":"Europe/London"}}},{"name":"age","in":"query","required":true,"description":"Outschool has several classes serving different age groups. The age of the learner(s) helps to find classes that match the best. This is a comma separated list. If the age difference between the children is more than 5 years, it may be better to search for different ages separately to get better search results.","schema":{"type":"string","minimum":3,"maximum":18},"examples":{"12":{"value":"12"},"1213":{"value":"12,13"},"5617":{"value":"5,6,17"}}},{"name":"q","in":"query","required":false,"description":"Keywords to use to search in the class list. Classes matching the keyword closest will be returned.","schema":{"type":"string"}},{"name":"delivery","in":"query","required":false,"explode":true,"description":"Filters classes by delivery type. Description for different enum values:\n One-time: Classes that meets once\n Ongoing: Weekly classes that learners can enroll in any week\n Semester course: Multi-week/session classes, usually more than 4 weeks\n Short course: Multi-week/session classes, usually around 4 weeks\n Camp: Semester or short courses during summer and school breaks\n Group: Async chat groups on a specific topic where learners share ideas and experiences, like clubs","schema":{"type":"array","items":{"type":"string","enum":["One-time","Ongoing","Semester course","Short course","Camp","Group"]}}},{"name":"userUid","in":"query","required":false,"description":"Only search classes taught by a specific teacher. The userUid is the id of the teacher","schema":{"type":"string","format":"uuid"}},{"name":"order","in":"query","description":"Sort results by either upcoming, new, or relevance. Upcoming sorts by next section start date in ascending order, new sorts by class published date in descending order, and relevance sorts by the keyword relevance and popularity of the class.","schema":{"type":"string","enum":["upcoming","new","relevance"],"default":"relevance"}},{"name":"offset","in":"query","required":false,"description":"The offset for the results. Offset and limit used in combination to paginate in results. For instance, if limit is 10, to get next 10 results, the offset should be set to 10.","schema":{"type":"number","default":0}},{"name":"limit","in":"query","required":false,"description":"Number of results to return.","schema":{"type":"number","default":10}},{"name":"startAfter","in":"query","required":false,"description":"Search classes that have a section starting on or after a given date. Only today or future dates are allowed.","schema":{"type":"string","format":"date"},"examples":{"April152023":{"value":"2023-04-15"}}},{"name":"dow","in":"query","description":"The day of week to filter classes and only return classes that have a section on given days of the week.","schema":{"type":"array","items":{"type":"string","enum":["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]}},"style":"form","explode":true,"required":false,"examples":{"Mon":{"value":"Mon"},"Mon_Tue":{"value":"Mon,Tue"},"Mon_Thu":{"value":"Mon,Tue,Wed,Thu"},"Weekdays":{"value":"Mon,Tue,Wed,Thu,Fri"},"Weekend":{"value":"Sat, Sun"}}},{"name":"startAfterTime","in":"query","description":"The start time of the class in 24 hour format as hour of the day normalized by the user's timezone","schema":{"type":"number","minimum":6,"maximum":22}},{"name":"endByTime","in":"query","description":"The end time of the class in 24 hour format as hour of the day normalized by the user's timezone","schema":{"type":"number","minimum":6,"maximum":22}}],"responses":{"200":{"description":"A list of classes","content":{"application/json":{"schema":{"type":"array","items":{"$ref":"#/components/schemas/class"}}}}}}}},"/teachers":{"get":{"operationId":"searchTeachers","description":"Returns a list of teachers","parameters":[{"name":"name","in":"query","required":true,"description":"Name of the teacher to search for","schema":{"type":"string"}},{"name":"limit","in":"query","required":false,"description":"Number of results to return.","schema":{"type":"number","default":10}}],"responses":{"200":{"description":"A list of teachers","content":{"application/json":{"schema":{"type":"array","items":{"$ref":"#/components/schemas/teacher"}}}}}}}}},"components":{"schemas":{"class":{"type":"object","properties":{"uid":{"type":"string","format":"uuid","description":"Unique ID of the class in the system that can be used in other API end points"},"title":{"type":"string","description":"Title of the class"},"summary":{"type":"string","description":"Summary of the class"},"url":{"type":"string","format":"uri","description":"URL to the class detail page"},"photo":{"type":"string","format":"uri","description":"Photo of the class"},"is_ongoing_weekly":{"type":"boolean","description":"Whether this class is an ongoing class or not. When a class is an ongoing class, parents can enroll their children for any week of an ongoing class, because the sections of that class meet every week and the weeks don't depend on each other."},"age_min":{"type":"number","description":"The minimum age a learner should be to enroll in the class. Although Outschool has classes for different age groups, individual classes may only be appropriate for a certain age range."},"age_max":{"type":"number","description":"The maximum age a learner should be to enroll in the class. Although Outschool has classes for different age groups, individual classes may only be appropriate for a certain age range."},"teacher":{"$ref":"#/components/schemas/teacher"},"nextSection":{"$ref":"#/components/schemas/section","nullable":true,"description":"The next section of the class that the parent/caregiver can enroll their children in. This is usually what parents are looking for to enroll in a class."}}},"teacher":{"type":"object","properties":{"uid":{"type":"string","format":"uuid","description":"Unique ID of the teacher in the system that can be used in other API end points"},"name":{"type":"string","description":"Name of the teacher"},"about":{"type":"string","description":"A short summary the teacher provides about themselves"},"photo":{"type":"string","format":"uri","description":"Photo of the teacher"},"url":{"type":"string","format":"uri","description":"URL to the Outschool profile page of the teacher"}}},"section":{"type":"object","description":"Sections are what parents enroll their children in for a given class. They are separate cohorts of a class.","properties":{"uid":{"type":"string","format":"uuid","description":"Unique ID of the section in the system that can be used in other API end points"},"url":{"type":"string","format":"uri","description":"URL pointing to the section page"},"start_time":{"type":"string","format":"datetime","description":"The start time for the first meeting of a section."},"end_time":{"type":"string","format":"datetime","description":"The end time for the last meeting of a section."},"size_max":{"type":"number","description":"How many learners can enroll in the section."},"filledSpaceCount":{"type":"number","description":"How many learners are enrolled in the section. size_max - filledSpaceCount gives how many seats are left to enroll in."},"nextOngoingMeeting":{"$ref":"#/components/schemas/meeting","nullable":true,"description":"If the class is an ongoing class, this points to the next meeting for the section."}}},"meeting":{"type":"object","description":"The online meeting for a section. Meetings are held on Zoom.","properties":{"uid":{"type":"string","format":"uuid","description":"Unique ID of the meeting in the system that can be used in other API end points"},"start_time":{"type":"string","format":"datetime","description":"The start time of the meeting."},"end_time":{"type":"string","format":"datetime","description":"The end time of the meeting."}}}}}}
\ No newline at end of file
diff --git a/camel/functions/open_api_specs/outschool/paths/__init__.py b/camel/functions/open_api_specs/outschool/paths/__init__.py
new file mode 100644
index 000000000..bd5187f28
--- /dev/null
+++ b/camel/functions/open_api_specs/outschool/paths/__init__.py
@@ -0,0 +1,14 @@
+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========
+path_dict = {"get_classes": "/classes", "search_teachers": "/teachers"}
diff --git a/camel/functions/open_api_specs/outschool/paths/get_classes.py b/camel/functions/open_api_specs/outschool/paths/get_classes.py
new file mode 100644
index 000000000..d5abef7ce
--- /dev/null
+++ b/camel/functions/open_api_specs/outschool/paths/get_classes.py
@@ -0,0 +1,29 @@
+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========
+"""Get classes from Outschool API."""
+
+from typing import Any, Dict
+
+import requests
+
+
+def call_api(input_json: Dict[str, Any]) -> Dict[str, Any]:
+ response = requests.get(
+ "https://chatgpt-plugin.outschool.com/api/classes", params=input_json
+ )
+
+ if response.status_code == 200:
+ return response.json()
+ else:
+ return {"status_code": response.status_code, "text": response.text}
diff --git a/camel/functions/open_api_specs/outschool/paths/search_teachers.py b/camel/functions/open_api_specs/outschool/paths/search_teachers.py
new file mode 100644
index 000000000..a67225b99
--- /dev/null
+++ b/camel/functions/open_api_specs/outschool/paths/search_teachers.py
@@ -0,0 +1,29 @@
+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========
+"""Search for teachers on Outschool."""
+
+from typing import Any, Dict
+
+import requests
+
+
+def call_api(input_json: Dict[str, Any]) -> Dict[str, Any]:
+ response = requests.get(
+ "https://chatgpt-plugin.outschool.com/api/teachers", params=input_json
+ )
+
+ if response.status_code == 200:
+ return response.json()
+ else:
+ return {"status_code": response.status_code, "text": response.text}
diff --git a/camel/functions/open_api_specs/web_scraper/__init__.py b/camel/functions/open_api_specs/web_scraper/__init__.py
new file mode 100644
index 000000000..d963f8cc8
--- /dev/null
+++ b/camel/functions/open_api_specs/web_scraper/__init__.py
@@ -0,0 +1,13 @@
+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========
diff --git a/camel/functions/open_api_specs/web_scraper/ai-plugin.json b/camel/functions/open_api_specs/web_scraper/ai-plugin.json
new file mode 100644
index 000000000..92f6b2080
--- /dev/null
+++ b/camel/functions/open_api_specs/web_scraper/ai-plugin.json
@@ -0,0 +1,34 @@
+{
+ "id": "plugin-0609b24f-5c80-4864-af90-c7c570d65375",
+ "domain": "scraper.gafo.tech",
+ "namespace": "web_scraper",
+ "status": "approved",
+ "manifest": {
+ "schema_version": "v1",
+ "name_for_model": "web_scraper",
+ "name_for_human": "Scraper",
+ "description_for_model": "Scrape content from webpages by providing a URL.",
+ "description_for_human": "Scrape content from webpages by providing a URL.",
+ "auth": {
+ "type": "none"
+ },
+ "api": {
+ "type": "openapi",
+ "url": "https://scraper.gafo.tech/openapi.yaml"
+ },
+ "logo_url": "https://scraper.gafo.tech/logo.png",
+ "contact_email": "[email protected]",
+ "legal_info_url": "https://scraper.gafo.tech/legal"
+ },
+ "oauth_client_id": null,
+ "user_settings": {
+ "is_installed": false,
+ "is_authenticated": true
+ },
+ "categories": [
+ {
+ "id": "newly_added",
+ "title": "New"
+ }
+ ]
+}
\ No newline at end of file
diff --git a/camel/functions/open_api_specs/web_scraper/openapi.yaml b/camel/functions/open_api_specs/web_scraper/openapi.yaml
new file mode 100644
index 000000000..3cf275bb8
--- /dev/null
+++ b/camel/functions/open_api_specs/web_scraper/openapi.yaml
@@ -0,0 +1,71 @@
+openapi: 3.0.1
+info:
+ title: Scraper
+ description: Scrape content from webpages by providing a URL.
+ version: "v1"
+servers:
+ - url: https://scraper.gafo.tech
+paths:
+ /scrape:
+ post:
+ operationId: scrape
+ summary: Scrape content from a webpage
+ requestBody:
+ required: true
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ url:
+ type: string
+ format: uri
+ example: https://example.com
+ type:
+ type: string
+ enum: [text, links, images]
+ default: text
+ example: text
+ required:
+ - url
+ responses:
+ "200":
+ description: OK
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ text:
+ type: string
+ description: The text content of the webpage. Returned when type is text or not provided.
+ links:
+ type: array
+ items:
+ type: object
+ description: The array of link objects with all attributes from the webpage. Returned when type is links.
+ images:
+ type: array
+ items:
+ type: object
+ description: The array of image objects with all attributes from the webpage. Returned when type is images.
+ "400":
+ description: Bad Request
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ error:
+ type: string
+ description: The error message.
+ "500":
+ description: Internal Server Error
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ error:
+ type: string
+ description: The error message.
diff --git a/camel/functions/open_api_specs/web_scraper/paths/__init__.py b/camel/functions/open_api_specs/web_scraper/paths/__init__.py
new file mode 100644
index 000000000..d963f8cc8
--- /dev/null
+++ b/camel/functions/open_api_specs/web_scraper/paths/__init__.py
@@ -0,0 +1,13 @@
+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========
diff --git a/camel/functions/open_api_specs/web_scraper/paths/scraper.py b/camel/functions/open_api_specs/web_scraper/paths/scraper.py
new file mode 100644
index 000000000..d1708bbcd
--- /dev/null
+++ b/camel/functions/open_api_specs/web_scraper/paths/scraper.py
@@ -0,0 +1,29 @@
+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========
+"""Scrape data from a website using the Scraper API."""
+
+from typing import Any, Dict
+
+import requests
+
+
+def call_api(input_json: Dict[str, Any]) -> Dict[str, Any]:
+ response = requests.post(
+ "https://scraper.gafo.tech/scrape", json=input_json
+ )
+
+ if response.status_code == 200:
+ return response.json()
+ else:
+ return {"status_code": response.status_code, "text": response.text}
diff --git a/camel/types/enums.py b/camel/types/enums.py
index 1a67693b0..dd3f1fc93 100644
--- a/camel/types/enums.py
+++ b/camel/types/enums.py
@@ -253,6 +253,10 @@ class OpenAPIName(Enum):
COURSERA = "coursera"
KLARNA = "klarna"
SPEAK = "speak"
+ BIZTOC = "biztoc"
+ CREATE_QR_CODE = "create_qr_code"
+ OUTSCHOOL = "outschool"
+ WEB_SCRAPER = "web_scraper"
class AudioModelType(Enum):
diff --git a/test/functions/test_open_api_function.py b/test/functions/test_open_api_function.py
index ea8867a18..ded3a580a 100644
--- a/test/functions/test_open_api_function.py
+++ b/test/functions/test_open_api_function.py
@@ -15,12 +15,18 @@ from unittest.mock import MagicMock, patch
import pytest
-from camel.functions.open_api_function import combine_all_funcs_schemas
+from camel.functions import (
+ apinames_filepaths_to_funs_schemas,
+ generate_apinames_filepaths,
+)
@pytest.fixture(scope="module")
def functions_dict():
- openapi_functions_list, _ = combine_all_funcs_schemas()
+ apinames_filepaths = generate_apinames_filepaths()
+ openapi_functions_list, _ = apinames_filepaths_to_funs_schemas(
+ apinames_filepaths
+ )
functions_dict = {func.__name__: func for func in openapi_functions_list}
return functions_dict
@@ -266,3 +272,121 @@ def test_speak_explainTask(get_function):
}
result = get_function(requestBody=explain_task_request)
assert result == mock_response_data
+
+
[email protected]('get_function', ['biztoc_getNews'], indirect=True)
+def test_biztoc_getNews(get_function):
+ mock_response_data = [
+ {
+ "title": "Republican on Fox Calls for the GOP to ‘Shut Down the Economy’ to Fix Immigration: ‘We Should, Really’",
+ "source": "mediaite.com",
+ "date": "Tue, 21 May 2024",
+ "summary": "Republican Congresswoman Victoria Spartz joined Fox Business host Maria Bartiromo to discuss border security, calling for drastic measures.",
+ "image": "https://c.biztoc.com/p/c7a5cea54ed2bdd5/s.webp",
+ "tags": ["victoriaspartz", "borderpatrol", "immigration"],
+ "url": "https://www.mediaite.com/tv/republican-on-fox-calls-for-the-gop-to-shut-down-the-economy-to-fix-immigration-we-should-really/?ref=biztoc.com",
+ }
+ ]
+ mock_response = MagicMock()
+ mock_response.json.return_value = mock_response_data
+ with patch('requests.request', return_value=mock_response):
+ result = get_function(query_in_query='llm Agent')
+ assert result == mock_response_data
+
+
+ 'get_function', ['create_qr_code_getQRCode'], indirect=True
+)
+def test_create_qr_code_getQRCode(get_function):
+ mock_response_data = {
+ 'img_tag': '<img src="https://api.qrserver.com/v1/create-qr-code/?data=The data to encode in the QR code.&size=120x120" '
+ 'alt="The alt text for the QR code image." title="The title for the QR code image." />'
+ }
+ mock_response = MagicMock()
+ mock_response.json.return_value = mock_response_data
+ with patch('requests.request', return_value=mock_response):
+ result = get_function(
+ data_in_query="The data to encode in the QR code.",
+ size_in_query="120x120",
+ alt_in_query="The alt text for the QR code image.",
+ title_in_query="The title for the QR code image.",
+ )
+ assert result == mock_response_data
+
+
+ 'get_function', ['outschool_searchClasses'], indirect=True
+)
+def test_out_school_searchClasses(get_function):
+ mock_response_data = [
+ {
+ "uid": "f170ecbb-4ceb-41ad-af1a-3ac68f120833",
+ "title": "1:1 Math Tutoring With a Math Major/Teacher",
+ "summary": "Personalized 1:1 math tutoring by an experienced Math Major/Teacher for Grades 2 through 10.",
+ "subject": "Math",
+ "duration_minutes": 40,
+ "price_cents": 4500,
+ "age_min": 7,
+ "age_max": 15,
+ "url": "https://outschool.com/classes/11-math-tutoring-with-a-math-majorteacher-fYCdDueY?utm_medium=chatgpt&utm_source=chatgpt-plugin",
+ "photo": "https://cdn.filestackcontent.com/vDVX6iIaQiKdguqAzI7e",
+ "teacher": {
+ "name": "Tess Monte, M.Ed.",
+ "photo": "https://cdn.filestackcontent.com/enevVCpZSKqAR4u0Nolt",
+ "averageActivityStarRating": 5,
+ "reviewCount": 2,
+ "url": "https://outschool.com/teachers/Ms-Tess?utm_medium=chatgpt&utm_source=chatgpt-plugin",
+ },
+ }
+ ]
+ mock_response = MagicMock()
+ mock_response.json.return_value = mock_response_data
+ with patch('requests.request', return_value=mock_response):
+ result = get_function(
+ timeZone_in_query="America/Los_Angeles",
+ age_in_query=12,
+ q_in_query='math',
+ )
+ assert result == mock_response_data
+
+
+ 'get_function', ['outschool_searchTeachers'], indirect=True
+)
+def test_out_school_searchTeachers(get_function):
+ mock_response_data = [
+ {
+ "uid": "9b4d48b8-80ee-474d-bdc9-73a2b76e3432",
+ "name": "Alice Maundrell, B.Mus, M.Ed ",
+ },
+ {"uid": "ce93ca8f-34a3-463f-a1c6-fedfc5ebd983", "name": "Ms Alice "},
+ {"uid": "7e09d0bb-ba47-4e1a-a024-d22063fe083c", "name": "Alice Wang"},
+ {"uid": "5ffdce7f-6eed-4721-ba6a-05227fc86f3d", "name": "Alice"},
+ {
+ "uid": "64ab28c8-35ac-4c23-8869-bd798bec969a",
+ "name": "Alice Campbell ",
+ },
+ {"uid": "af351235-624f-4a28-9835-ff5628bbc6ba", "name": "Alice H."},
+ ]
+ mock_response = MagicMock()
+ mock_response.json.return_value = mock_response_data
+ with patch('requests.request', return_value=mock_response):
+ result = get_function(name_in_query="Alice", limit_in_query=2)
+ assert result == mock_response_data
+
+
[email protected]('get_function', ['web_scraper_scrape'], indirect=True)
+def test_web_scraper_scrape(get_function):
+ mock_response_data = {
+ "text": 'Skip to content\nNavigation Menu\nSign in\ncamel-ai\n/\ncamel\nPublic\nNotifications\nFork 558\n Star 4.5k',
+ }
+ mock_response = MagicMock()
+ mock_response.json.return_value = mock_response_data
+ with patch('requests.request', return_value=mock_response):
+ result = get_function(
+ requestBody={
+ "url": "https://github.com/camel-ai/camel/tree/master",
+ "type": "text",
+ }
+ )
+ assert result == mock_response_data