camel-ai 0.2.10__py3-none-any.whl → 0.2.12__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of camel-ai might be problematic. Click here for more details.

Files changed (76) hide show
  1. camel/__init__.py +6 -1
  2. camel/agents/chat_agent.py +87 -6
  3. camel/agents/deductive_reasoner_agent.py +4 -1
  4. camel/benchmarks/__init__.py +18 -0
  5. camel/benchmarks/base.py +152 -0
  6. camel/benchmarks/gaia.py +478 -0
  7. camel/configs/__init__.py +6 -0
  8. camel/configs/mistral_config.py +0 -3
  9. camel/configs/nvidia_config.py +70 -0
  10. camel/configs/ollama_config.py +4 -2
  11. camel/configs/sglang_config.py +71 -0
  12. camel/configs/vllm_config.py +10 -1
  13. camel/data_collector/__init__.py +19 -0
  14. camel/data_collector/alpaca_collector.py +127 -0
  15. camel/data_collector/base.py +211 -0
  16. camel/data_collector/sharegpt_collector.py +205 -0
  17. camel/datahubs/__init__.py +23 -0
  18. camel/datahubs/base.py +136 -0
  19. camel/datahubs/huggingface.py +433 -0
  20. camel/datahubs/models.py +22 -0
  21. camel/embeddings/vlm_embedding.py +4 -1
  22. camel/interpreters/__init__.py +2 -0
  23. camel/interpreters/docker_interpreter.py +7 -2
  24. camel/interpreters/e2b_interpreter.py +136 -0
  25. camel/interpreters/subprocess_interpreter.py +7 -2
  26. camel/loaders/__init__.py +3 -1
  27. camel/loaders/base_io.py +41 -41
  28. camel/loaders/firecrawl_reader.py +0 -3
  29. camel/logger.py +112 -0
  30. camel/messages/__init__.py +3 -1
  31. camel/messages/base.py +10 -7
  32. camel/messages/conversion/__init__.py +3 -1
  33. camel/messages/conversion/alpaca.py +122 -0
  34. camel/models/__init__.py +7 -0
  35. camel/models/anthropic_model.py +14 -4
  36. camel/models/base_model.py +28 -0
  37. camel/models/groq_model.py +1 -1
  38. camel/models/model_factory.py +6 -0
  39. camel/models/model_manager.py +212 -0
  40. camel/models/nvidia_model.py +141 -0
  41. camel/models/ollama_model.py +12 -0
  42. camel/models/openai_model.py +0 -25
  43. camel/models/reward/__init__.py +22 -0
  44. camel/models/reward/base_reward_model.py +58 -0
  45. camel/models/reward/evaluator.py +63 -0
  46. camel/models/reward/nemotron_model.py +112 -0
  47. camel/models/sglang_model.py +225 -0
  48. camel/models/vllm_model.py +1 -1
  49. camel/personas/persona_hub.py +2 -2
  50. camel/retrievers/vector_retriever.py +22 -5
  51. camel/schemas/openai_converter.py +2 -2
  52. camel/societies/babyagi_playing.py +4 -1
  53. camel/societies/workforce/role_playing_worker.py +2 -2
  54. camel/societies/workforce/single_agent_worker.py +2 -2
  55. camel/societies/workforce/workforce.py +3 -3
  56. camel/storages/object_storages/amazon_s3.py +2 -2
  57. camel/storages/object_storages/azure_blob.py +2 -2
  58. camel/storages/object_storages/google_cloud.py +2 -2
  59. camel/toolkits/__init__.py +5 -0
  60. camel/toolkits/code_execution.py +42 -4
  61. camel/toolkits/function_tool.py +41 -0
  62. camel/toolkits/human_toolkit.py +1 -0
  63. camel/toolkits/math_toolkit.py +47 -16
  64. camel/toolkits/meshy_toolkit.py +185 -0
  65. camel/toolkits/search_toolkit.py +154 -2
  66. camel/toolkits/stripe_toolkit.py +273 -0
  67. camel/toolkits/twitter_toolkit.py +3 -0
  68. camel/types/__init__.py +2 -0
  69. camel/types/enums.py +68 -10
  70. camel/utils/commons.py +22 -5
  71. camel/utils/token_counting.py +26 -11
  72. {camel_ai-0.2.10.dist-info → camel_ai-0.2.12.dist-info}/METADATA +13 -6
  73. {camel_ai-0.2.10.dist-info → camel_ai-0.2.12.dist-info}/RECORD +76 -51
  74. /camel/messages/conversion/{models.py → conversation_models.py} +0 -0
  75. {camel_ai-0.2.10.dist-info → camel_ai-0.2.12.dist-info}/LICENSE +0 -0
  76. {camel_ai-0.2.10.dist-info → camel_ai-0.2.12.dist-info}/WHEEL +0 -0
@@ -11,9 +11,15 @@
11
11
  # See the License for the specific language governing permissions and
12
12
  # limitations under the License.
13
13
  # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
14
- from typing import List, Literal, Optional
14
+ from typing import List, Literal, Optional, Union
15
15
 
16
- from camel.interpreters import InternalPythonInterpreter
16
+ from camel.interpreters import (
17
+ DockerInterpreter,
18
+ E2BInterpreter,
19
+ InternalPythonInterpreter,
20
+ JupyterKernelInterpreter,
21
+ SubprocessInterpreter,
22
+ )
17
23
  from camel.toolkits import FunctionTool
18
24
  from camel.toolkits.base import BaseToolkit
19
25
 
@@ -29,26 +35,58 @@ class CodeExecutionToolkit(BaseToolkit):
29
35
  by `eval()` without any security check. (default: :obj:`False`)
30
36
  import_white_list ( Optional[List[str]]): A list of allowed imports.
31
37
  (default: :obj:`None`)
38
+ require_confirm (bool): Whether to require confirmation before executing code.
39
+ (default: :obj:`False`)
32
40
  """
33
41
 
34
42
  def __init__(
35
43
  self,
36
44
  sandbox: Literal[
37
- "internal_python", "jupyter", "docker"
45
+ "internal_python", "jupyter", "docker", "subprocess", "e2b"
38
46
  ] = "internal_python",
39
47
  verbose: bool = False,
40
48
  unsafe_mode: bool = False,
41
49
  import_white_list: Optional[List[str]] = None,
50
+ require_confirm: bool = False,
42
51
  ) -> None:
43
- # TODO: Add support for docker and jupyter.
44
52
  self.verbose = verbose
45
53
  self.unsafe_mode = unsafe_mode
46
54
  self.import_white_list = import_white_list or list()
55
+
56
+ # Type annotation for interpreter to allow all possible types
57
+ self.interpreter: Union[
58
+ InternalPythonInterpreter,
59
+ JupyterKernelInterpreter,
60
+ DockerInterpreter,
61
+ SubprocessInterpreter,
62
+ E2BInterpreter,
63
+ ]
64
+
47
65
  if sandbox == "internal_python":
48
66
  self.interpreter = InternalPythonInterpreter(
49
67
  unsafe_mode=self.unsafe_mode,
50
68
  import_white_list=self.import_white_list,
51
69
  )
70
+ elif sandbox == "jupyter":
71
+ self.interpreter = JupyterKernelInterpreter(
72
+ require_confirm=require_confirm,
73
+ print_stdout=self.verbose,
74
+ print_stderr=self.verbose,
75
+ )
76
+ elif sandbox == "docker":
77
+ self.interpreter = DockerInterpreter(
78
+ require_confirm=require_confirm,
79
+ print_stdout=self.verbose,
80
+ print_stderr=self.verbose,
81
+ )
82
+ elif sandbox == "subprocess":
83
+ self.interpreter = SubprocessInterpreter(
84
+ require_confirm=require_confirm,
85
+ print_stdout=self.verbose,
86
+ print_stderr=self.verbose,
87
+ )
88
+ elif sandbox == "e2b":
89
+ self.interpreter = E2BInterpreter(require_confirm=require_confirm)
52
90
  else:
53
91
  raise RuntimeError(
54
92
  f"The sandbox type `{sandbox}` is not supported."
@@ -165,9 +165,15 @@ def get_openai_tool_schema(func: Callable) -> Dict[str, Any]:
165
165
  else:
166
166
  func_description = short_description
167
167
 
168
+ # OpenAI client.beta.chat.completions.parse for structured output has
169
+ # additional requirements for the schema, refer:
170
+ # https://platform.openai.com/docs/guides/structured-outputs/some-type-specific-keywords-are-not-yet-supported#supported-schemas
171
+ parameters_dict["additionalProperties"] = False
172
+
168
173
  openai_function_schema = {
169
174
  "name": func.__name__,
170
175
  "description": func_description,
176
+ "strict": True,
171
177
  "parameters": parameters_dict,
172
178
  }
173
179
 
@@ -175,9 +181,44 @@ def get_openai_tool_schema(func: Callable) -> Dict[str, Any]:
175
181
  "type": "function",
176
182
  "function": openai_function_schema,
177
183
  }
184
+
185
+ openai_tool_schema = sanitize_and_enforce_required(openai_tool_schema)
178
186
  return openai_tool_schema
179
187
 
180
188
 
189
+ def sanitize_and_enforce_required(parameters_dict):
190
+ r"""Cleans and updates the function schema to conform with OpenAI's
191
+ requirements:
192
+ - Removes invalid 'default' fields from the parameters schema.
193
+ - Ensures all fields or function parameters are marked as required.
194
+
195
+ Args:
196
+ parameters_dict (dict): The dictionary representing the function
197
+ schema.
198
+
199
+ Returns:
200
+ dict: The updated dictionary with invalid defaults removed and all
201
+ fields set as required.
202
+ """
203
+ # Check if 'function' and 'parameters' exist
204
+ if (
205
+ 'function' in parameters_dict
206
+ and 'parameters' in parameters_dict['function']
207
+ ):
208
+ # Access the 'parameters' section
209
+ parameters = parameters_dict['function']['parameters']
210
+ properties = parameters.get('properties', {})
211
+
212
+ # Remove 'default' key from each property
213
+ for field in properties.values():
214
+ field.pop('default', None)
215
+
216
+ # Mark all keys in 'properties' as required
217
+ parameters['required'] = list(properties.keys())
218
+
219
+ return parameters_dict
220
+
221
+
181
222
  def generate_docstring(
182
223
  code: str,
183
224
  model: Optional[BaseModelBackend] = None,
@@ -36,6 +36,7 @@ class HumanToolkit(BaseToolkit):
36
36
  Returns:
37
37
  str: The answer from the human.
38
38
  """
39
+ print(f"Question: {question}")
39
40
  logger.info(f"Question: {question}")
40
41
  reply = input("Your reply: ")
41
42
  logger.info(f"User reply: {reply}")
@@ -22,44 +22,73 @@ class MathToolkit(BaseToolkit):
22
22
  r"""A class representing a toolkit for mathematical operations.
23
23
 
24
24
  This class provides methods for basic mathematical operations such as
25
- addition, subtraction, and multiplication.
25
+ addition, subtraction, multiplication, division, and rounding.
26
26
  """
27
27
 
28
- def add(self, a: int, b: int) -> int:
28
+ def add(self, a: float, b: float) -> float:
29
29
  r"""Adds two numbers.
30
30
 
31
31
  Args:
32
- a (int): The first number to be added.
33
- b (int): The second number to be added.
32
+ a (float): The first number to be added.
33
+ b (float): The second number to be added.
34
34
 
35
35
  Returns:
36
- integer: The sum of the two numbers.
36
+ float: The sum of the two numbers.
37
37
  """
38
38
  return a + b
39
39
 
40
- def sub(self, a: int, b: int) -> int:
40
+ def sub(self, a: float, b: float) -> float:
41
41
  r"""Do subtraction between two numbers.
42
42
 
43
43
  Args:
44
- a (int): The minuend in subtraction.
45
- b (int): The subtrahend in subtraction.
44
+ a (float): The minuend in subtraction.
45
+ b (float): The subtrahend in subtraction.
46
46
 
47
47
  Returns:
48
- integer: The result of subtracting :obj:`b` from :obj:`a`.
48
+ float: The result of subtracting :obj:`b` from :obj:`a`.
49
49
  """
50
50
  return a - b
51
51
 
52
- def mul(self, a: int, b: int) -> int:
53
- r"""Multiplies two integers.
52
+ def multiply(self, a: float, b: float, decimal_places: int = 2) -> float:
53
+ r"""Multiplies two numbers.
54
54
 
55
55
  Args:
56
- a (int): The multiplier in the multiplication.
57
- b (int): The multiplicand in the multiplication.
56
+ a (float): The multiplier in the multiplication.
57
+ b (float): The multiplicand in the multiplication.
58
+ decimal_places (int, optional): The number of decimal
59
+ places to round to. Defaults to 2.
58
60
 
59
61
  Returns:
60
- integer: The product of the two numbers.
62
+ float: The product of the two numbers.
61
63
  """
62
- return a * b
64
+ return round(a * b, decimal_places)
65
+
66
+ def divide(self, a: float, b: float, decimal_places: int = 2) -> float:
67
+ r"""Divides two numbers.
68
+
69
+ Args:
70
+ a (float): The dividend in the division.
71
+ b (float): The divisor in the division.
72
+ decimal_places (int, optional): The number of
73
+ decimal places to round to. Defaults to 2.
74
+
75
+ Returns:
76
+ float: The result of dividing :obj:`a` by :obj:`b`.
77
+ """
78
+ return round(a / b, decimal_places)
79
+
80
+ def round(self, a: float, decimal_places: int = 0) -> float:
81
+ r"""Rounds a number to a specified number of decimal places.
82
+
83
+ Args:
84
+ a (float): The number to be rounded.
85
+ decimal_places (int, optional): The number of decimal places
86
+ to round to. Defaults to 0.
87
+
88
+ Returns:
89
+ float: The rounded number.
90
+ """
91
+ return round(a, decimal_places)
63
92
 
64
93
  def get_tools(self) -> List[FunctionTool]:
65
94
  r"""Returns a list of FunctionTool objects representing the
@@ -72,5 +101,7 @@ class MathToolkit(BaseToolkit):
72
101
  return [
73
102
  FunctionTool(self.add),
74
103
  FunctionTool(self.sub),
75
- FunctionTool(self.mul),
104
+ FunctionTool(self.multiply),
105
+ FunctionTool(self.divide),
106
+ FunctionTool(self.round),
76
107
  ]
@@ -0,0 +1,185 @@
1
+ # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
14
+
15
+ import os
16
+ from typing import Any, Dict
17
+
18
+ import requests
19
+
20
+ from camel.toolkits.base import BaseToolkit
21
+ from camel.utils import api_keys_required
22
+
23
+
24
+ class MeshyToolkit(BaseToolkit):
25
+ r"""A class representing a toolkit for 3D model generation using Meshy.
26
+
27
+ This class provides methods that handle text/image to 3D model
28
+ generation using Meshy.
29
+
30
+ Call the generate_3d_model_complete method to generate a refined 3D model.
31
+
32
+ Ref:
33
+ https://docs.meshy.ai/api-text-to-3d-beta#create-a-text-to-3d-preview-task
34
+ """
35
+
36
+ @api_keys_required("MESHY_API_KEY")
37
+ def __init__(self):
38
+ r"""Initializes the MeshyToolkit with the API key from the
39
+ environment.
40
+ """
41
+ self.api_key = os.getenv('MESHY_API_KEY')
42
+
43
+ def generate_3d_preview(
44
+ self, prompt: str, art_style: str, negative_prompt: str
45
+ ) -> Dict[str, Any]:
46
+ r"""Generates a 3D preview using the Meshy API.
47
+
48
+ Args:
49
+ prompt (str): Description of the object.
50
+ art_style (str): Art style for the 3D model.
51
+ negative_prompt (str): What the model should not look like.
52
+
53
+ Returns:
54
+ Dict[str, Any]: The result property of the response contains the
55
+ task id of the newly created Text to 3D task.
56
+ """
57
+ payload = {
58
+ "mode": "preview",
59
+ "prompt": prompt,
60
+ "art_style": art_style,
61
+ "negative_prompt": negative_prompt,
62
+ }
63
+ headers = {"Authorization": f"Bearer {self.api_key}"}
64
+
65
+ response = requests.post(
66
+ "https://api.meshy.ai/v2/text-to-3d",
67
+ headers=headers,
68
+ json=payload,
69
+ )
70
+ response.raise_for_status()
71
+ return response.json()
72
+
73
+ def refine_3d_model(self, preview_task_id: str) -> Dict[str, Any]:
74
+ r"""Refines a 3D model using the Meshy API.
75
+
76
+ Args:
77
+ preview_task_id (str): The task ID of the preview to refine.
78
+
79
+ Returns:
80
+ Dict[str, Any]: The response from the Meshy API.
81
+ """
82
+ payload = {"mode": "refine", "preview_task_id": preview_task_id}
83
+ headers = {"Authorization": f"Bearer {self.api_key}"}
84
+
85
+ response = requests.post(
86
+ "https://api.meshy.ai/v2/text-to-3d",
87
+ headers=headers,
88
+ json=payload,
89
+ )
90
+ response.raise_for_status()
91
+ return response.json()
92
+
93
+ def get_task_status(self, task_id: str) -> Dict[str, Any]:
94
+ r"""Retrieves the status or result of a specific 3D model generation
95
+ task using the Meshy API.
96
+
97
+ Args:
98
+ task_id (str): The ID of the task to retrieve.
99
+
100
+ Returns:
101
+ Dict[str, Any]: The response from the Meshy API.
102
+ """
103
+ headers = {"Authorization": f"Bearer {self.api_key}"}
104
+
105
+ response = requests.get(
106
+ f"https://api.meshy.ai/v2/text-to-3d/{task_id}",
107
+ headers=headers,
108
+ )
109
+ response.raise_for_status()
110
+ return response.json()
111
+
112
+ def wait_for_task_completion(
113
+ self, task_id: str, polling_interval: int = 10, timeout: int = 3600
114
+ ) -> Dict[str, Any]:
115
+ r"""Waits for a task to complete by polling its status.
116
+
117
+ Args:
118
+ task_id (str): The ID of the task to monitor.
119
+ polling_interval (int): Seconds to wait between status checks.
120
+ (default::obj:`10`)
121
+ timeout (int): Maximum seconds to wait before timing out.
122
+ (default::obj:`3600`)
123
+
124
+ Returns:
125
+ Dict[str, Any]: Final response from the API when task completes.
126
+
127
+ Raises:
128
+ TimeoutError: If task doesn't complete within timeout period.
129
+ RuntimeError: If task fails or is canceled.
130
+ """
131
+ import time
132
+
133
+ start_time = time.time()
134
+
135
+ while True:
136
+ if time.time() - start_time > timeout:
137
+ raise TimeoutError(
138
+ f"Task {task_id} timed out after {timeout} seconds"
139
+ )
140
+
141
+ response = self.get_task_status(task_id)
142
+ status = response.get("status") # Direct access to status field
143
+ elapsed = int(time.time() - start_time)
144
+
145
+ print(f"Status after {elapsed}s: {status}")
146
+
147
+ if status == "SUCCEEDED":
148
+ return response
149
+ elif status in [
150
+ "FAILED",
151
+ "CANCELED",
152
+ ]: # Also updating these status values
153
+ raise RuntimeError(f"Task {task_id} {status}")
154
+
155
+ time.sleep(polling_interval)
156
+
157
+ def generate_3d_model_complete(
158
+ self, prompt: str, art_style: str, negative_prompt: str
159
+ ) -> Dict[str, Any]:
160
+ r"""Generates a complete 3D model by handling preview and refinement
161
+ stages
162
+
163
+ Args:
164
+ prompt (str): Description of the object.
165
+ art_style (str): Art style for the 3D model.
166
+ negative_prompt (str): What the model should not look like.
167
+
168
+ Returns:
169
+ Dict[str, Any]: The final refined 3D model response.
170
+ """
171
+ # Generate preview
172
+ preview_response = self.generate_3d_preview(
173
+ prompt, art_style, negative_prompt
174
+ )
175
+ preview_task_id = str(preview_response.get("result"))
176
+
177
+ # Wait for preview completion
178
+ self.wait_for_task_completion(preview_task_id)
179
+
180
+ # Start refinement
181
+ refine_response = self.refine_3d_model(preview_task_id)
182
+ refine_task_id = str(refine_response.get("result"))
183
+
184
+ # Wait for refinement completion and return final result
185
+ return self.wait_for_task_completion(refine_task_id)
@@ -13,7 +13,7 @@
13
13
  # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
14
14
  import os
15
15
  import xml.etree.ElementTree as ET
16
- from typing import Any, Dict, List, Union
16
+ from typing import Any, Dict, List, Optional, TypeAlias, Union
17
17
 
18
18
  import requests
19
19
 
@@ -26,7 +26,7 @@ class SearchToolkit(BaseToolkit):
26
26
  r"""A class representing a toolkit for web search.
27
27
 
28
28
  This class provides methods for searching information on the web using
29
- search engines like Google, DuckDuckGo, Wikipedia and Wolfram Alpha.
29
+ search engines like Google, DuckDuckGo, Wikipedia and Wolfram Alpha, Brave.
30
30
  """
31
31
 
32
32
  @dependencies_required("wikipedia")
@@ -151,6 +151,152 @@ class SearchToolkit(BaseToolkit):
151
151
  # If no answer found, return an empty list
152
152
  return responses
153
153
 
154
+ @api_keys_required("BRAVE_API_KEY")
155
+ def search_brave(
156
+ self,
157
+ q: str,
158
+ country: str = "US",
159
+ search_lang: str = "en",
160
+ ui_lang: str = "en-US",
161
+ count: int = 20,
162
+ offset: int = 0,
163
+ safesearch: str = "moderate",
164
+ freshness: Optional[str] = None,
165
+ text_decorations: bool = True,
166
+ spellcheck: bool = True,
167
+ result_filter: Optional[str] = None,
168
+ goggles_id: Optional[str] = None,
169
+ units: Optional[str] = None,
170
+ extra_snippets: Optional[bool] = None,
171
+ summary: Optional[bool] = None,
172
+ ) -> Dict[str, Any]:
173
+ r"""This function queries the Brave search engine API and returns a
174
+ dictionary, representing a search result.
175
+ See https://api.search.brave.com/app/documentation/web-search/query
176
+ for more details.
177
+
178
+ Args:
179
+ q (str): The user's search query term. Query cannot be empty.
180
+ Maximum of 400 characters and 50 words in the query.
181
+ country (str): The search query country where results come from.
182
+ The country string is limited to 2 character country codes of
183
+ supported countries. For a list of supported values, see
184
+ Country Codes. (default::obj:`US `)
185
+ search_lang (str): The search language preference. The 2 or more
186
+ character language code for which search results are provided.
187
+ For a list of possible values, see Language Codes.
188
+ ui_lang (str): User interface language preferred in response.
189
+ Usually of the format '<language_code>-<country_code>'. For
190
+ more, see RFC 9110. For a list of supported values, see UI
191
+ Language Codes.
192
+ count (int): The number of search results returned in response.
193
+ The maximum is 20. The actual number delivered may be less than
194
+ requested. Combine this parameter with offset to paginate
195
+ search results.
196
+ offset (int): The zero based offset that indicates number of search
197
+ results per page (count) to skip before returning the result.
198
+ The maximum is 9. The actual number delivered may be less than
199
+ requested based on the query. In order to paginate results use
200
+ this parameter together with count. For example, if your user
201
+ interface displays 20 search results per page, set count to 20
202
+ and offset to 0 to show the first page of results. To get
203
+ subsequent pages, increment offset by 1 (e.g. 0, 1, 2). The
204
+ results may overlap across multiple pages.
205
+ safesearch (str): Filters search results for adult content.
206
+ The following values are supported:
207
+ - 'off': No filtering is done.
208
+ - 'moderate': Filters explicit content, like images and videos,
209
+ but allows adult domains in the search results.
210
+ - 'strict': Drops all adult content from search results.
211
+ freshness (Optional[str]): Filters search results by when they were
212
+ discovered:
213
+ - 'pd': Discovered within the last 24 hours.
214
+ - 'pw': Discovered within the last 7 Days.
215
+ - 'pm': Discovered within the last 31 Days.
216
+ - 'py': Discovered within the last 365 Days.
217
+ - 'YYYY-MM-DDtoYYYY-MM-DD': Timeframe is also supported by
218
+ specifying the date range e.g. '2022-04-01to2022-07-30'.
219
+ text_decorations (bool): Whether display strings (e.g. result
220
+ snippets) should include decoration markers (e.g. highlighting
221
+ characters).
222
+ spellcheck (bool): Whether to spellcheck provided query. If the
223
+ spellchecker is enabled, the modified query is always used for
224
+ search. The modified query can be found in altered key from the
225
+ query response model.
226
+ result_filter (Optional[str]): A comma delimited string of result
227
+ types to include in the search response. Not specifying this
228
+ parameter will return back all result types in search response
229
+ where data is available and a plan with the corresponding
230
+ option is subscribed. The response always includes query and
231
+ type to identify any query modifications and response type
232
+ respectively. Available result filter values are:
233
+ - 'discussions'
234
+ - 'faq'
235
+ - 'infobox'
236
+ - 'news'
237
+ - 'query'
238
+ - 'summarizer'
239
+ - 'videos'
240
+ - 'web'
241
+ - 'locations'
242
+ goggles_id (Optional[str]): Goggles act as a custom re-ranking on
243
+ top of Brave's search index. For more details, refer to the
244
+ Goggles repository.
245
+ units (Optional[str]): The measurement units. If not provided,
246
+ units are derived from search country. Possible values are:
247
+ - 'metric': The standardized measurement system
248
+ - 'imperial': The British Imperial system of units.
249
+ extra_snippets (Optional[bool]): A snippet is an excerpt from a
250
+ page you get as a result of the query, and extra_snippets
251
+ allow you to get up to 5 additional, alternative excerpts. Only
252
+ available under Free AI, Base AI, Pro AI, Base Data, Pro Data
253
+ and Custom plans.
254
+ summary (Optional[bool]): This parameter enables summary key
255
+ generation in web search results. This is required for
256
+ summarizer to be enabled.
257
+
258
+ Returns:
259
+ Dict[str, Any]: A dictionary representing a search result.
260
+ """
261
+
262
+ import requests
263
+
264
+ BRAVE_API_KEY = os.getenv("BRAVE_API_KEY")
265
+
266
+ url = "https://api.search.brave.com/res/v1/web/search"
267
+ headers = {
268
+ "Content-Type": "application/json",
269
+ "X-BCP-APIV": "1.0",
270
+ "X-Subscription-Token": BRAVE_API_KEY,
271
+ }
272
+
273
+ ParamsType: TypeAlias = Dict[
274
+ str,
275
+ Union[str, int, float, List[Union[str, int, float]], None],
276
+ ]
277
+
278
+ params: ParamsType = {
279
+ "q": q,
280
+ "country": country,
281
+ "search_lang": search_lang,
282
+ "ui_lang": ui_lang,
283
+ "count": count,
284
+ "offset": offset,
285
+ "safesearch": safesearch,
286
+ "freshness": freshness,
287
+ "text_decorations": text_decorations,
288
+ "spellcheck": spellcheck,
289
+ "result_filter": result_filter,
290
+ "goggles_id": goggles_id,
291
+ "units": units,
292
+ "extra_snippets": extra_snippets,
293
+ "summary": summary,
294
+ }
295
+
296
+ response = requests.get(url, headers=headers, params=params)
297
+ data = response.json()["web"]
298
+ return data
299
+
154
300
  @api_keys_required("GOOGLE_API_KEY", "SEARCH_ENGINE_ID")
155
301
  def search_google(
156
302
  self, query: str, num_result_pages: int = 5
@@ -219,6 +365,11 @@ class SearchToolkit(BaseToolkit):
219
365
 
220
366
  # Iterate over 10 results found
221
367
  for i, search_item in enumerate(search_items, start=1):
368
+ # Check metatags are present
369
+ if "pagemap" not in search_item:
370
+ continue
371
+ if "metatags" not in search_item["pagemap"]:
372
+ continue
222
373
  if (
223
374
  "og:description"
224
375
  in search_item["pagemap"]["metatags"][0]
@@ -471,4 +622,5 @@ class SearchToolkit(BaseToolkit):
471
622
  FunctionTool(self.search_duckduckgo),
472
623
  FunctionTool(self.query_wolfram_alpha),
473
624
  FunctionTool(self.tavily_search),
625
+ FunctionTool(self.search_brave),
474
626
  ]