camel-ai 0.2.22__py3-none-any.whl → 0.2.23a0__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.
- camel/__init__.py +1 -1
- camel/agents/_types.py +41 -0
- camel/agents/_utils.py +188 -0
- camel/agents/chat_agent.py +556 -965
- camel/agents/knowledge_graph_agent.py +7 -1
- camel/agents/multi_hop_generator_agent.py +1 -1
- camel/configs/base_config.py +10 -13
- camel/configs/deepseek_config.py +4 -30
- camel/configs/gemini_config.py +5 -31
- camel/configs/openai_config.py +14 -32
- camel/configs/qwen_config.py +36 -36
- camel/datagen/self_improving_cot.py +79 -1
- camel/datagen/self_instruct/filter/instruction_filter.py +19 -3
- camel/datagen/self_instruct/self_instruct.py +6 -1
- camel/datasets/__init__.py +28 -0
- camel/datasets/base.py +969 -0
- camel/environments/__init__.py +16 -0
- camel/environments/base.py +503 -0
- camel/extractors/__init__.py +16 -0
- camel/extractors/base.py +263 -0
- camel/memories/agent_memories.py +16 -1
- camel/memories/blocks/chat_history_block.py +10 -2
- camel/memories/blocks/vectordb_block.py +1 -0
- camel/memories/context_creators/score_based.py +20 -3
- camel/memories/records.py +10 -0
- camel/messages/base.py +8 -8
- camel/models/_utils.py +57 -0
- camel/models/aiml_model.py +48 -17
- camel/models/anthropic_model.py +41 -3
- camel/models/azure_openai_model.py +39 -3
- camel/models/base_model.py +88 -13
- camel/models/cohere_model.py +88 -11
- camel/models/deepseek_model.py +107 -45
- camel/models/gemini_model.py +133 -15
- camel/models/groq_model.py +72 -10
- camel/models/internlm_model.py +14 -3
- camel/models/litellm_model.py +9 -2
- camel/models/mistral_model.py +42 -5
- camel/models/model_manager.py +48 -3
- camel/models/moonshot_model.py +33 -4
- camel/models/nemotron_model.py +32 -3
- camel/models/nvidia_model.py +43 -3
- camel/models/ollama_model.py +139 -17
- camel/models/openai_audio_models.py +7 -1
- camel/models/openai_compatible_model.py +37 -3
- camel/models/openai_model.py +158 -46
- camel/models/qwen_model.py +61 -4
- camel/models/reka_model.py +53 -3
- camel/models/samba_model.py +209 -4
- camel/models/sglang_model.py +153 -14
- camel/models/siliconflow_model.py +16 -3
- camel/models/stub_model.py +46 -4
- camel/models/togetherai_model.py +38 -3
- camel/models/vllm_model.py +37 -3
- camel/models/yi_model.py +36 -3
- camel/models/zhipuai_model.py +38 -3
- camel/retrievers/__init__.py +3 -0
- camel/retrievers/hybrid_retrival.py +237 -0
- camel/toolkits/__init__.py +4 -0
- camel/toolkits/arxiv_toolkit.py +2 -1
- camel/toolkits/ask_news_toolkit.py +4 -2
- camel/toolkits/base.py +22 -3
- camel/toolkits/code_execution.py +2 -0
- camel/toolkits/dappier_toolkit.py +2 -1
- camel/toolkits/data_commons_toolkit.py +38 -12
- camel/toolkits/function_tool.py +13 -0
- camel/toolkits/github_toolkit.py +5 -1
- camel/toolkits/google_maps_toolkit.py +2 -1
- camel/toolkits/google_scholar_toolkit.py +2 -0
- camel/toolkits/human_toolkit.py +0 -3
- camel/toolkits/linkedin_toolkit.py +3 -2
- camel/toolkits/meshy_toolkit.py +3 -2
- camel/toolkits/mineru_toolkit.py +2 -2
- camel/toolkits/networkx_toolkit.py +240 -0
- camel/toolkits/notion_toolkit.py +2 -0
- camel/toolkits/openbb_toolkit.py +3 -2
- camel/toolkits/reddit_toolkit.py +11 -3
- camel/toolkits/retrieval_toolkit.py +6 -1
- camel/toolkits/semantic_scholar_toolkit.py +2 -1
- camel/toolkits/stripe_toolkit.py +8 -2
- camel/toolkits/sympy_toolkit.py +6 -1
- camel/toolkits/video_toolkit.py +2 -0
- camel/toolkits/whatsapp_toolkit.py +3 -2
- camel/toolkits/zapier_toolkit.py +191 -0
- camel/types/__init__.py +2 -2
- camel/types/agents/__init__.py +16 -0
- camel/types/agents/tool_calling_record.py +52 -0
- camel/types/enums.py +3 -0
- camel/types/openai_types.py +16 -14
- camel/utils/__init__.py +2 -1
- camel/utils/async_func.py +2 -2
- camel/utils/commons.py +114 -1
- camel/verifiers/__init__.py +23 -0
- camel/verifiers/base.py +340 -0
- camel/verifiers/models.py +82 -0
- camel/verifiers/python_verifier.py +202 -0
- {camel_ai-0.2.22.dist-info → camel_ai-0.2.23a0.dist-info}/METADATA +273 -255
- {camel_ai-0.2.22.dist-info → camel_ai-0.2.23a0.dist-info}/RECORD +100 -82
- {camel_ai-0.2.22.dist-info → camel_ai-0.2.23a0.dist-info}/WHEEL +1 -1
- {camel_ai-0.2.22.dist-info → camel_ai-0.2.23a0.dist-info}/LICENSE +0 -0
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
import logging
|
|
15
15
|
from typing import Any, Dict, List, Optional, Union
|
|
16
16
|
|
|
17
|
+
from camel.toolkits import FunctionTool
|
|
17
18
|
from camel.toolkits.base import BaseToolkit
|
|
18
19
|
|
|
19
20
|
logger = logging.getLogger(__name__)
|
|
@@ -35,8 +36,18 @@ class DataCommonsToolkit(BaseToolkit):
|
|
|
35
36
|
Refer to https://datacommons.org/browser/ for more details.
|
|
36
37
|
"""
|
|
37
38
|
|
|
38
|
-
|
|
39
|
+
def __init__(self, timeout: Optional[float] = None):
|
|
40
|
+
r"""Initialize the DataCommonsToolkit.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
timeout (Optional[float], optional): Maximum time in seconds to
|
|
44
|
+
wait for API calls to complete. If None, will wait indefinitely.
|
|
45
|
+
(default: :obj:`None`)
|
|
46
|
+
"""
|
|
47
|
+
super().__init__(timeout=timeout)
|
|
48
|
+
|
|
39
49
|
def query_data_commons(
|
|
50
|
+
self,
|
|
40
51
|
query_string: str,
|
|
41
52
|
) -> Optional[List[Dict[str, Any]]]:
|
|
42
53
|
r"""Query the Data Commons knowledge graph using SPARQL.
|
|
@@ -76,9 +87,8 @@ class DataCommonsToolkit(BaseToolkit):
|
|
|
76
87
|
)
|
|
77
88
|
return None
|
|
78
89
|
|
|
79
|
-
@staticmethod
|
|
80
90
|
def get_triples(
|
|
81
|
-
dcids: Union[str, List[str]], limit: int = 500
|
|
91
|
+
self, dcids: Union[str, List[str]], limit: int = 500
|
|
82
92
|
) -> Optional[Dict[str, List[tuple]]]:
|
|
83
93
|
r"""Retrieve triples associated with nodes.
|
|
84
94
|
|
|
@@ -117,8 +127,8 @@ class DataCommonsToolkit(BaseToolkit):
|
|
|
117
127
|
logger.error(f"An error occurred: {e!s}")
|
|
118
128
|
return None
|
|
119
129
|
|
|
120
|
-
@staticmethod
|
|
121
130
|
def get_stat_time_series(
|
|
131
|
+
self,
|
|
122
132
|
place: str,
|
|
123
133
|
stat_var: str,
|
|
124
134
|
measurement_method: Optional[str] = None,
|
|
@@ -166,9 +176,8 @@ class DataCommonsToolkit(BaseToolkit):
|
|
|
166
176
|
)
|
|
167
177
|
return None
|
|
168
178
|
|
|
169
|
-
@staticmethod
|
|
170
179
|
def get_property_labels(
|
|
171
|
-
dcids: Union[str, List[str]], out: bool = True
|
|
180
|
+
self, dcids: Union[str, List[str]], out: bool = True
|
|
172
181
|
) -> Optional[Dict[str, List[str]]]:
|
|
173
182
|
r"""Retrieves and analyzes property labels for given DCIDs.
|
|
174
183
|
|
|
@@ -195,8 +204,8 @@ class DataCommonsToolkit(BaseToolkit):
|
|
|
195
204
|
)
|
|
196
205
|
return None
|
|
197
206
|
|
|
198
|
-
@staticmethod
|
|
199
207
|
def get_property_values(
|
|
208
|
+
self,
|
|
200
209
|
dcids: Union[str, List[str]],
|
|
201
210
|
prop: str,
|
|
202
211
|
out: Optional[bool] = True,
|
|
@@ -239,9 +248,8 @@ class DataCommonsToolkit(BaseToolkit):
|
|
|
239
248
|
)
|
|
240
249
|
return None
|
|
241
250
|
|
|
242
|
-
@staticmethod
|
|
243
251
|
def get_places_in(
|
|
244
|
-
dcids: list, place_type: str
|
|
252
|
+
self, dcids: list, place_type: str
|
|
245
253
|
) -> Optional[Dict[str, Any]]:
|
|
246
254
|
r"""Retrieves places within a given place type.
|
|
247
255
|
|
|
@@ -269,8 +277,8 @@ class DataCommonsToolkit(BaseToolkit):
|
|
|
269
277
|
)
|
|
270
278
|
return None
|
|
271
279
|
|
|
272
|
-
@staticmethod
|
|
273
280
|
def get_stat_value(
|
|
281
|
+
self,
|
|
274
282
|
place: str,
|
|
275
283
|
stat_var: str,
|
|
276
284
|
date: Optional[str] = None,
|
|
@@ -326,8 +334,7 @@ class DataCommonsToolkit(BaseToolkit):
|
|
|
326
334
|
)
|
|
327
335
|
return None
|
|
328
336
|
|
|
329
|
-
|
|
330
|
-
def get_stat_all(places: str, stat_vars: str) -> Optional[dict]:
|
|
337
|
+
def get_stat_all(self, places: str, stat_vars: str) -> Optional[dict]:
|
|
331
338
|
r"""Retrieves the value of a statistical variable for a given place
|
|
332
339
|
and date.
|
|
333
340
|
|
|
@@ -358,3 +365,22 @@ class DataCommonsToolkit(BaseToolkit):
|
|
|
358
365
|
f"statistical variable: {e!s}"
|
|
359
366
|
)
|
|
360
367
|
return None
|
|
368
|
+
|
|
369
|
+
def get_tools(self) -> List[FunctionTool]:
|
|
370
|
+
r"""Returns a list of FunctionTool objects representing the functions
|
|
371
|
+
in the toolkit.
|
|
372
|
+
|
|
373
|
+
Returns:
|
|
374
|
+
List[FunctionTool]: A list of FunctionTool objects representing
|
|
375
|
+
the functions in the toolkit.
|
|
376
|
+
"""
|
|
377
|
+
return [
|
|
378
|
+
FunctionTool(self.query_data_commons),
|
|
379
|
+
FunctionTool(self.get_triples),
|
|
380
|
+
FunctionTool(self.get_stat_time_series),
|
|
381
|
+
FunctionTool(self.get_property_labels),
|
|
382
|
+
FunctionTool(self.get_property_values),
|
|
383
|
+
FunctionTool(self.get_places_in),
|
|
384
|
+
FunctionTool(self.get_stat_value),
|
|
385
|
+
FunctionTool(self.get_stat_all),
|
|
386
|
+
]
|
camel/toolkits/function_tool.py
CHANGED
|
@@ -398,6 +398,19 @@ class FunctionTool:
|
|
|
398
398
|
f"Error: {e}"
|
|
399
399
|
)
|
|
400
400
|
|
|
401
|
+
async def async_call(self, *args: Any, **kwargs: Any) -> Any:
|
|
402
|
+
if self.synthesize_output:
|
|
403
|
+
result = self.synthesize_execution_output(args, kwargs)
|
|
404
|
+
return result
|
|
405
|
+
if self.is_async:
|
|
406
|
+
return await self.func(*args, **kwargs)
|
|
407
|
+
else:
|
|
408
|
+
return self.func(*args, **kwargs)
|
|
409
|
+
|
|
410
|
+
@property
|
|
411
|
+
def is_async(self) -> bool:
|
|
412
|
+
return inspect.iscoroutinefunction(self.func)
|
|
413
|
+
|
|
401
414
|
@staticmethod
|
|
402
415
|
def validate_openai_tool_schema(
|
|
403
416
|
openai_tool_schema: Dict[str, Any],
|
camel/toolkits/github_toolkit.py
CHANGED
|
@@ -39,7 +39,10 @@ class GithubToolkit(BaseToolkit):
|
|
|
39
39
|
|
|
40
40
|
@dependencies_required('github')
|
|
41
41
|
def __init__(
|
|
42
|
-
self,
|
|
42
|
+
self,
|
|
43
|
+
repo_name: str,
|
|
44
|
+
access_token: Optional[str] = None,
|
|
45
|
+
timeout: Optional[float] = None,
|
|
43
46
|
) -> None:
|
|
44
47
|
r"""Initializes a new instance of the GitHubToolkit class.
|
|
45
48
|
|
|
@@ -49,6 +52,7 @@ class GithubToolkit(BaseToolkit):
|
|
|
49
52
|
with GitHub. If not provided, it will be obtained using the
|
|
50
53
|
`get_github_access_token` method.
|
|
51
54
|
"""
|
|
55
|
+
super().__init__(timeout=timeout)
|
|
52
56
|
from github import Auth, Github
|
|
53
57
|
|
|
54
58
|
if access_token is None:
|
|
@@ -101,7 +101,8 @@ class GoogleMapsToolkit(BaseToolkit):
|
|
|
101
101
|
"""
|
|
102
102
|
|
|
103
103
|
@dependencies_required('googlemaps')
|
|
104
|
-
def __init__(self) -> None:
|
|
104
|
+
def __init__(self, timeout: Optional[float] = None) -> None:
|
|
105
|
+
super().__init__(timeout=timeout)
|
|
105
106
|
import googlemaps
|
|
106
107
|
|
|
107
108
|
api_key = os.environ.get('GOOGLE_API_KEY')
|
|
@@ -39,6 +39,7 @@ class GoogleScholarToolkit(BaseToolkit):
|
|
|
39
39
|
use_free_proxies: bool = False,
|
|
40
40
|
proxy_http: Optional[str] = None,
|
|
41
41
|
proxy_https: Optional[str] = None,
|
|
42
|
+
timeout: Optional[float] = None,
|
|
42
43
|
) -> None:
|
|
43
44
|
r"""Initializes the GoogleScholarToolkit with the author's identifier.
|
|
44
45
|
|
|
@@ -54,6 +55,7 @@ class GoogleScholarToolkit(BaseToolkit):
|
|
|
54
55
|
proxy_https ( Optional[str]): Proxy https address pass to pg.
|
|
55
56
|
SingleProxy. (default: :obj:`None`)
|
|
56
57
|
"""
|
|
58
|
+
super().__init__(timeout=timeout)
|
|
57
59
|
from scholarly import ProxyGenerator, scholarly
|
|
58
60
|
|
|
59
61
|
# Set Free Proxies is needed
|
camel/toolkits/human_toolkit.py
CHANGED
|
@@ -24,9 +24,6 @@ logger = logging.getLogger(__name__)
|
|
|
24
24
|
class HumanToolkit(BaseToolkit):
|
|
25
25
|
r"""A class representing a toolkit for human interaction."""
|
|
26
26
|
|
|
27
|
-
def __init__(self):
|
|
28
|
-
pass
|
|
29
|
-
|
|
30
27
|
def ask_human_via_console(self, question: str) -> str:
|
|
31
28
|
r"""Ask a question to the human via the console.
|
|
32
29
|
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
import json
|
|
16
16
|
import os
|
|
17
17
|
from http import HTTPStatus
|
|
18
|
-
from typing import List
|
|
18
|
+
from typing import List, Optional
|
|
19
19
|
|
|
20
20
|
import requests
|
|
21
21
|
|
|
@@ -33,7 +33,8 @@ class LinkedInToolkit(BaseToolkit):
|
|
|
33
33
|
retrieving the authenticated user's profile information.
|
|
34
34
|
"""
|
|
35
35
|
|
|
36
|
-
def __init__(self):
|
|
36
|
+
def __init__(self, timeout: Optional[float] = None):
|
|
37
|
+
super().__init__(timeout=timeout)
|
|
37
38
|
self._access_token = self._get_access_token()
|
|
38
39
|
|
|
39
40
|
def create_post(self, text: str) -> dict:
|
camel/toolkits/meshy_toolkit.py
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
|
14
14
|
|
|
15
15
|
import os
|
|
16
|
-
from typing import Any, Dict
|
|
16
|
+
from typing import Any, Dict, Optional
|
|
17
17
|
|
|
18
18
|
import requests
|
|
19
19
|
|
|
@@ -38,10 +38,11 @@ class MeshyToolkit(BaseToolkit):
|
|
|
38
38
|
(None, 'MESHY_API_KEY'),
|
|
39
39
|
]
|
|
40
40
|
)
|
|
41
|
-
def __init__(self):
|
|
41
|
+
def __init__(self, timeout: Optional[float] = None):
|
|
42
42
|
r"""Initializes the MeshyToolkit with the API key from the
|
|
43
43
|
environment.
|
|
44
44
|
"""
|
|
45
|
+
super().__init__(timeout=timeout)
|
|
45
46
|
self.api_key = os.getenv('MESHY_API_KEY')
|
|
46
47
|
|
|
47
48
|
def generate_3d_preview(
|
camel/toolkits/mineru_toolkit.py
CHANGED
|
@@ -111,7 +111,7 @@ class MinerUToolkit(BaseToolkit):
|
|
|
111
111
|
if self.wait:
|
|
112
112
|
return self.client.wait_for_completion(
|
|
113
113
|
response['task_id'],
|
|
114
|
-
timeout=self.timeout,
|
|
114
|
+
timeout=self.timeout, # type: ignore[arg-type]
|
|
115
115
|
)
|
|
116
116
|
return response
|
|
117
117
|
else:
|
|
@@ -125,7 +125,7 @@ class MinerUToolkit(BaseToolkit):
|
|
|
125
125
|
return self.client.wait_for_completion(
|
|
126
126
|
batch_id,
|
|
127
127
|
is_batch=True,
|
|
128
|
-
timeout=self.timeout if self.timeout > 300 else 600,
|
|
128
|
+
timeout=self.timeout if self.timeout > 300 else 600, # type: ignore[arg-type,operator]
|
|
129
129
|
)
|
|
130
130
|
return {"batch_id": batch_id}
|
|
131
131
|
|
|
@@ -0,0 +1,240 @@
|
|
|
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 json
|
|
16
|
+
from typing import Any, Callable, Dict, List, Literal, Optional, Tuple, Union
|
|
17
|
+
|
|
18
|
+
from camel.logger import get_logger
|
|
19
|
+
from camel.toolkits import FunctionTool
|
|
20
|
+
from camel.toolkits.base import BaseToolkit
|
|
21
|
+
|
|
22
|
+
logger = get_logger(__name__)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class NetworkXToolkit(BaseToolkit):
|
|
26
|
+
_nx = None # Class variable to store the networkx module
|
|
27
|
+
|
|
28
|
+
@classmethod
|
|
29
|
+
def _get_nx(cls):
|
|
30
|
+
r"""Lazily import networkx module when needed."""
|
|
31
|
+
if cls._nx is None:
|
|
32
|
+
import networkx
|
|
33
|
+
|
|
34
|
+
cls._nx = networkx
|
|
35
|
+
return cls._nx
|
|
36
|
+
|
|
37
|
+
def __init__(
|
|
38
|
+
self,
|
|
39
|
+
graph_type: Literal[
|
|
40
|
+
'graph', 'digraph', 'multigraph', 'multidigraph'
|
|
41
|
+
] = 'graph',
|
|
42
|
+
):
|
|
43
|
+
r"""Initializes the NetworkX graph client.
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
graph_type (Literal['graph', 'digraph', 'multigraph',
|
|
47
|
+
'multidigraph']):
|
|
48
|
+
Type of graph to create. Options are:
|
|
49
|
+
- 'graph': Undirected graph
|
|
50
|
+
- 'digraph': Directed graph
|
|
51
|
+
- 'multigraph': Undirected graph with parallel edges
|
|
52
|
+
- 'multidigraph': Directed graph with parallel edges
|
|
53
|
+
(default: :obj:`'graph'`)
|
|
54
|
+
"""
|
|
55
|
+
nx = self._get_nx()
|
|
56
|
+
graph_types = {
|
|
57
|
+
'graph': nx.Graph,
|
|
58
|
+
'digraph': nx.DiGraph,
|
|
59
|
+
'multigraph': nx.MultiGraph,
|
|
60
|
+
'multidigraph': nx.MultiDiGraph,
|
|
61
|
+
}
|
|
62
|
+
graph_class = graph_types.get(graph_type.lower())
|
|
63
|
+
if graph_class is None:
|
|
64
|
+
raise ValueError(
|
|
65
|
+
f"Invalid graph type: {graph_type}. Must be one "
|
|
66
|
+
f"of: {list(graph_types.keys())}"
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
self.graph = graph_class()
|
|
70
|
+
logger.info(f"Initialized NetworkX {graph_type} instance.")
|
|
71
|
+
|
|
72
|
+
def add_node(self, node_id: str, **attributes: Any) -> None:
|
|
73
|
+
r"""Adds a node to the graph.
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
node_id (str): The ID of the node.
|
|
77
|
+
attributes (dict): Additional node attributes.
|
|
78
|
+
"""
|
|
79
|
+
logger.info(f"Adding node: {node_id}, attributes: {attributes}")
|
|
80
|
+
self.graph.add_node(node_id, **attributes)
|
|
81
|
+
|
|
82
|
+
def add_edge(self, source: str, target: str, **attributes: Any) -> None:
|
|
83
|
+
r"""Adds an edge to the graph.
|
|
84
|
+
|
|
85
|
+
Args:
|
|
86
|
+
source (str): Source node ID.
|
|
87
|
+
target (str): Target node ID.
|
|
88
|
+
attributes (dict): Additional edge attributes.
|
|
89
|
+
"""
|
|
90
|
+
logger.info(
|
|
91
|
+
f"Adding edge: {source} -> {target}, attributes: {attributes}"
|
|
92
|
+
)
|
|
93
|
+
self.graph.add_edge(source, target, **attributes)
|
|
94
|
+
|
|
95
|
+
def get_nodes(self) -> List[str]:
|
|
96
|
+
r"""Returns all nodes in the graph.
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
List[str]: A list of node IDs.
|
|
100
|
+
"""
|
|
101
|
+
logger.info("Fetching all nodes.")
|
|
102
|
+
return list(self.graph.nodes)
|
|
103
|
+
|
|
104
|
+
def get_edges(self) -> List[Tuple[str, str]]:
|
|
105
|
+
r"""Returns all edges in the graph.
|
|
106
|
+
|
|
107
|
+
Returns:
|
|
108
|
+
List[Tuple[str, str]]: A list of edges as (source, target).
|
|
109
|
+
"""
|
|
110
|
+
logger.info("Fetching all edges.")
|
|
111
|
+
return list(self.graph.edges)
|
|
112
|
+
|
|
113
|
+
def get_shortest_path(
|
|
114
|
+
self,
|
|
115
|
+
source: str,
|
|
116
|
+
target: str,
|
|
117
|
+
weight: Optional[Union[str, Callable]] = None,
|
|
118
|
+
method: Literal['dijkstra', 'bellman-ford'] = 'dijkstra',
|
|
119
|
+
) -> List[str]:
|
|
120
|
+
r"""Finds the shortest path between two nodes.
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
source (str): The source node ID.
|
|
124
|
+
target (str): The target node ID.
|
|
125
|
+
weight (None, str or function, optional): Edge weights/distances.
|
|
126
|
+
If None, every edge has weight/distance/cost 1.
|
|
127
|
+
If string, use this edge attribute as the edge weight.
|
|
128
|
+
If function, the weight of an edge is the value returned by
|
|
129
|
+
the function. The function must accept three positional
|
|
130
|
+
arguments: the two endpoints and the edge attribute
|
|
131
|
+
dictionary. (default: :obj:`None`)
|
|
132
|
+
method (Literal['dijkstra', 'bellman-ford'], optional): Algorithm
|
|
133
|
+
to compute the path. Ignored if weight is None. (default:
|
|
134
|
+
:obj:`'dijkstra'`)
|
|
135
|
+
|
|
136
|
+
Returns:
|
|
137
|
+
List[str]: A list of nodes in the shortest path.
|
|
138
|
+
"""
|
|
139
|
+
logger.info(
|
|
140
|
+
f"Finding shortest path from '{source}' to '{target}' "
|
|
141
|
+
f"using {method} algorithm"
|
|
142
|
+
)
|
|
143
|
+
try:
|
|
144
|
+
nx = self._get_nx()
|
|
145
|
+
path = nx.shortest_path(
|
|
146
|
+
self.graph,
|
|
147
|
+
source=source,
|
|
148
|
+
target=target,
|
|
149
|
+
weight=weight,
|
|
150
|
+
method=method,
|
|
151
|
+
)
|
|
152
|
+
logger.debug(f"Found path: {' -> '.join(path)}")
|
|
153
|
+
return path
|
|
154
|
+
except nx.NetworkXNoPath:
|
|
155
|
+
error_msg = f"No path exists between '{source}' and '{target}'"
|
|
156
|
+
logger.error(error_msg)
|
|
157
|
+
return [error_msg]
|
|
158
|
+
except nx.NodeNotFound as e:
|
|
159
|
+
error_msg = f"Node not found in graph: {e!s}"
|
|
160
|
+
logger.error(error_msg)
|
|
161
|
+
return [error_msg]
|
|
162
|
+
|
|
163
|
+
def compute_centrality(self) -> Dict[str, float]:
|
|
164
|
+
r"""Computes centrality measures for the graph.
|
|
165
|
+
|
|
166
|
+
Returns:
|
|
167
|
+
Dict[str, float]: Centrality values for each node.
|
|
168
|
+
"""
|
|
169
|
+
logger.info("Computing centrality measures.")
|
|
170
|
+
nx = self._get_nx()
|
|
171
|
+
return nx.degree_centrality(self.graph)
|
|
172
|
+
|
|
173
|
+
def serialize_graph(self) -> str:
|
|
174
|
+
r"""Serializes the graph to a JSON string.
|
|
175
|
+
|
|
176
|
+
Returns:
|
|
177
|
+
str: The serialized graph in JSON format.
|
|
178
|
+
"""
|
|
179
|
+
logger.info("Serializing the graph.")
|
|
180
|
+
nx = self._get_nx()
|
|
181
|
+
return json.dumps(nx.node_link_data(self.graph))
|
|
182
|
+
|
|
183
|
+
def deserialize_graph(self, data: str) -> None:
|
|
184
|
+
r"""Loads a graph from a serialized JSON string.
|
|
185
|
+
|
|
186
|
+
Args:
|
|
187
|
+
data (str): The JSON string representing the graph.
|
|
188
|
+
"""
|
|
189
|
+
logger.info("Deserializing graph from JSON data.")
|
|
190
|
+
nx = self._get_nx()
|
|
191
|
+
self.graph = nx.node_link_graph(json.loads(data))
|
|
192
|
+
|
|
193
|
+
def export_to_file(self, file_path: str) -> None:
|
|
194
|
+
r"""Exports the graph to a file in JSON format.
|
|
195
|
+
|
|
196
|
+
Args:
|
|
197
|
+
file_path (str): The file path to save the graph.
|
|
198
|
+
"""
|
|
199
|
+
logger.info(f"Exporting graph to file: {file_path}")
|
|
200
|
+
nx = self._get_nx()
|
|
201
|
+
with open(file_path, "w") as file:
|
|
202
|
+
json.dump(nx.node_link_data(self.graph), file)
|
|
203
|
+
|
|
204
|
+
def import_from_file(self, file_path: str) -> None:
|
|
205
|
+
r"""Imports a graph from a JSON file.
|
|
206
|
+
|
|
207
|
+
Args:
|
|
208
|
+
file_path (str): The file path to load the graph from.
|
|
209
|
+
"""
|
|
210
|
+
logger.info(f"Importing graph from file: {file_path}")
|
|
211
|
+
nx = self._get_nx()
|
|
212
|
+
with open(file_path, "r") as file:
|
|
213
|
+
self.graph = nx.node_link_graph(json.load(file))
|
|
214
|
+
|
|
215
|
+
def clear_graph(self) -> None:
|
|
216
|
+
r"""Clears the current graph."""
|
|
217
|
+
logger.info("Clearing the graph.")
|
|
218
|
+
self.graph.clear()
|
|
219
|
+
|
|
220
|
+
def get_tools(self) -> List[FunctionTool]:
|
|
221
|
+
r"""Returns a list of FunctionTool objects representing the
|
|
222
|
+
functions in the toolkit.
|
|
223
|
+
|
|
224
|
+
Returns:
|
|
225
|
+
List[FunctionTool]: A list of FunctionTool objects for the
|
|
226
|
+
toolkit methods.
|
|
227
|
+
"""
|
|
228
|
+
return [
|
|
229
|
+
FunctionTool(self.add_edge),
|
|
230
|
+
FunctionTool(self.add_node),
|
|
231
|
+
FunctionTool(self.clear_graph),
|
|
232
|
+
FunctionTool(self.compute_centrality),
|
|
233
|
+
FunctionTool(self.deserialize_graph),
|
|
234
|
+
FunctionTool(self.export_to_file),
|
|
235
|
+
FunctionTool(self.get_edges),
|
|
236
|
+
FunctionTool(self.get_nodes),
|
|
237
|
+
FunctionTool(self.import_from_file),
|
|
238
|
+
FunctionTool(self.serialize_graph),
|
|
239
|
+
FunctionTool(self.get_shortest_path),
|
|
240
|
+
]
|
camel/toolkits/notion_toolkit.py
CHANGED
|
@@ -79,6 +79,7 @@ class NotionToolkit(BaseToolkit):
|
|
|
79
79
|
def __init__(
|
|
80
80
|
self,
|
|
81
81
|
notion_token: Optional[str] = None,
|
|
82
|
+
timeout: Optional[float] = None,
|
|
82
83
|
) -> None:
|
|
83
84
|
r"""Initializes the NotionToolkit.
|
|
84
85
|
|
|
@@ -86,6 +87,7 @@ class NotionToolkit(BaseToolkit):
|
|
|
86
87
|
notion_token (Optional[str], optional): The optional notion_token
|
|
87
88
|
used to interact with notion APIs.(default: :obj:`None`)
|
|
88
89
|
"""
|
|
90
|
+
super().__init__(timeout=timeout)
|
|
89
91
|
from notion_client import Client
|
|
90
92
|
|
|
91
93
|
self.notion_token = notion_token or os.environ.get("NOTION_TOKEN")
|
camel/toolkits/openbb_toolkit.py
CHANGED
|
@@ -37,15 +37,16 @@ class OpenBBToolkit(BaseToolkit):
|
|
|
37
37
|
(None, "OPENBB_TOKEN"),
|
|
38
38
|
]
|
|
39
39
|
)
|
|
40
|
-
def __init__(self) -> None:
|
|
40
|
+
def __init__(self, timeout: Optional[float] = None) -> None:
|
|
41
41
|
r"""Initialize the OpenBBToolkit.
|
|
42
42
|
|
|
43
43
|
This method sets up the OpenBB client and initializes the OpenBB
|
|
44
44
|
Hub account system.
|
|
45
45
|
"""
|
|
46
|
+
super().__init__(timeout=timeout)
|
|
46
47
|
import os
|
|
47
48
|
|
|
48
|
-
from openbb import obb
|
|
49
|
+
from openbb import obb # type: ignore[import-not-found]
|
|
49
50
|
|
|
50
51
|
self.client = obb
|
|
51
52
|
# Initialize OpenBB Hub account with access token
|
camel/toolkits/reddit_toolkit.py
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
import os
|
|
16
16
|
import time
|
|
17
|
-
from typing import Any, Dict, List, Union
|
|
17
|
+
from typing import Any, Dict, List, Optional, Union
|
|
18
18
|
|
|
19
19
|
from camel.toolkits import FunctionTool
|
|
20
20
|
from camel.toolkits.base import BaseToolkit
|
|
@@ -30,11 +30,16 @@ class RedditToolkit(BaseToolkit):
|
|
|
30
30
|
|
|
31
31
|
Attributes:
|
|
32
32
|
retries (int): Number of retries for API requests in case of failure.
|
|
33
|
-
delay (
|
|
33
|
+
delay (float): Delay between retries in seconds.
|
|
34
34
|
reddit (Reddit): An instance of the Reddit client.
|
|
35
35
|
"""
|
|
36
36
|
|
|
37
|
-
def __init__(
|
|
37
|
+
def __init__(
|
|
38
|
+
self,
|
|
39
|
+
retries: int = 3,
|
|
40
|
+
delay: float = 0.0,
|
|
41
|
+
timeout: Optional[float] = None,
|
|
42
|
+
):
|
|
38
43
|
r"""Initializes the RedditToolkit with the specified number of retries
|
|
39
44
|
and delay.
|
|
40
45
|
|
|
@@ -43,7 +48,10 @@ class RedditToolkit(BaseToolkit):
|
|
|
43
48
|
failure. Defaults to `3`.
|
|
44
49
|
delay (int): Time in seconds to wait between retries. Defaults to
|
|
45
50
|
`0`.
|
|
51
|
+
timeout (float): Timeout for API requests in seconds. Defaults to
|
|
52
|
+
`None`.
|
|
46
53
|
"""
|
|
54
|
+
super().__init__(timeout=timeout)
|
|
47
55
|
from praw import Reddit # type: ignore[import-untyped]
|
|
48
56
|
|
|
49
57
|
self.retries = retries
|
|
@@ -27,8 +27,13 @@ class RetrievalToolkit(BaseToolkit):
|
|
|
27
27
|
storage system based on a specified query.
|
|
28
28
|
"""
|
|
29
29
|
|
|
30
|
-
def __init__(
|
|
30
|
+
def __init__(
|
|
31
|
+
self,
|
|
32
|
+
auto_retriever: Optional[AutoRetriever] = None,
|
|
33
|
+
timeout: Optional[float] = None,
|
|
34
|
+
) -> None:
|
|
31
35
|
r"""Initializes a new instance of the RetrievalToolkit class."""
|
|
36
|
+
super().__init__(timeout=timeout)
|
|
32
37
|
self.ar = auto_retriever or AutoRetriever(
|
|
33
38
|
vector_storage_local_path="camel/temp_storage",
|
|
34
39
|
storage_type=StorageType.QDRANT,
|
|
@@ -26,8 +26,9 @@ class SemanticScholarToolkit(BaseToolkit):
|
|
|
26
26
|
API to fetch paper and author data.
|
|
27
27
|
"""
|
|
28
28
|
|
|
29
|
-
def __init__(self):
|
|
29
|
+
def __init__(self, timeout: Optional[float] = None):
|
|
30
30
|
r"""Initializes the SemanticScholarToolkit."""
|
|
31
|
+
super().__init__(timeout=timeout)
|
|
31
32
|
self.base_url = "https://api.semanticscholar.org/graph/v1"
|
|
32
33
|
|
|
33
34
|
def fetch_paper_data_title(
|
camel/toolkits/stripe_toolkit.py
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
import json
|
|
16
16
|
import logging
|
|
17
17
|
import os
|
|
18
|
-
from typing import List
|
|
18
|
+
from typing import List, Optional
|
|
19
19
|
|
|
20
20
|
from camel.toolkits import FunctionTool
|
|
21
21
|
from camel.toolkits.base import BaseToolkit
|
|
@@ -41,7 +41,12 @@ class StripeToolkit(BaseToolkit):
|
|
|
41
41
|
(None, "STRIPE_API_KEY"),
|
|
42
42
|
]
|
|
43
43
|
)
|
|
44
|
-
def __init__(
|
|
44
|
+
def __init__(
|
|
45
|
+
self,
|
|
46
|
+
retries: int = 3,
|
|
47
|
+
timeout: Optional[float] = None,
|
|
48
|
+
):
|
|
49
|
+
super().__init__(timeout=timeout)
|
|
45
50
|
r"""Initializes the StripeToolkit with the specified number of
|
|
46
51
|
retries.
|
|
47
52
|
|
|
@@ -49,6 +54,7 @@ class StripeToolkit(BaseToolkit):
|
|
|
49
54
|
retries (int,optional): Number of times to retry the request in
|
|
50
55
|
case of failure. (default: :obj:`3`)
|
|
51
56
|
"""
|
|
57
|
+
super().__init__(timeout=timeout)
|
|
52
58
|
import stripe
|
|
53
59
|
|
|
54
60
|
stripe.max_network_retries = retries
|
camel/toolkits/sympy_toolkit.py
CHANGED
|
@@ -28,13 +28,18 @@ class SymPyToolkit(BaseToolkit):
|
|
|
28
28
|
and Linear Algebra.
|
|
29
29
|
"""
|
|
30
30
|
|
|
31
|
-
def __init__(
|
|
31
|
+
def __init__(
|
|
32
|
+
self,
|
|
33
|
+
default_variable: str = 'x',
|
|
34
|
+
timeout: Optional[float] = None,
|
|
35
|
+
):
|
|
32
36
|
r"""Initializes the toolkit with a default variable and logging.
|
|
33
37
|
|
|
34
38
|
Args:
|
|
35
39
|
default_variable (str): The default variable for
|
|
36
40
|
operations (default: :obj: `x`)
|
|
37
41
|
"""
|
|
42
|
+
super().__init__(timeout=timeout)
|
|
38
43
|
self.default_variable = default_variable
|
|
39
44
|
logger.info(f"Default variable set to: {self.default_variable}")
|
|
40
45
|
|
camel/toolkits/video_toolkit.py
CHANGED
|
@@ -85,7 +85,9 @@ class VideoDownloaderToolkit(BaseToolkit):
|
|
|
85
85
|
self,
|
|
86
86
|
download_directory: Optional[str] = None,
|
|
87
87
|
cookies_path: Optional[str] = None,
|
|
88
|
+
timeout: Optional[float] = None,
|
|
88
89
|
) -> None:
|
|
90
|
+
super().__init__(timeout=timeout)
|
|
89
91
|
self._cleanup = download_directory is None
|
|
90
92
|
self._cookies_path = cookies_path
|
|
91
93
|
|