camel-ai 0.2.19__py3-none-any.whl → 0.2.20__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/chat_agent.py +29 -30
- camel/agents/knowledge_graph_agent.py +1 -5
- camel/benchmarks/apibench.py +1 -5
- camel/benchmarks/nexus.py +1 -5
- camel/benchmarks/ragbench.py +2 -2
- camel/bots/telegram_bot.py +1 -5
- camel/configs/__init__.py +9 -0
- camel/configs/aiml_config.py +80 -0
- camel/configs/moonshot_config.py +63 -0
- camel/configs/siliconflow_config.py +91 -0
- camel/datagen/__init__.py +3 -1
- camel/datagen/self_improving_cot.py +821 -0
- camel/datahubs/huggingface.py +3 -3
- camel/embeddings/jina_embedding.py +6 -1
- camel/models/__init__.py +4 -0
- camel/models/aiml_model.py +147 -0
- camel/models/model_factory.py +9 -0
- camel/models/moonshot_model.py +138 -0
- camel/models/siliconflow_model.py +142 -0
- camel/societies/workforce/role_playing_worker.py +2 -4
- camel/societies/workforce/single_agent_worker.py +1 -6
- camel/societies/workforce/workforce.py +3 -9
- camel/toolkits/__init__.py +4 -0
- camel/toolkits/reddit_toolkit.py +8 -38
- camel/toolkits/search_toolkit.py +12 -0
- camel/toolkits/semantic_scholar_toolkit.py +308 -0
- camel/toolkits/sympy_toolkit.py +778 -0
- camel/toolkits/whatsapp_toolkit.py +11 -32
- camel/types/enums.py +137 -6
- camel/types/unified_model_type.py +5 -0
- camel/utils/__init__.py +7 -2
- camel/utils/commons.py +198 -21
- camel/utils/deduplication.py +199 -0
- camel/utils/token_counting.py +0 -38
- {camel_ai-0.2.19.dist-info → camel_ai-0.2.20.dist-info}/METADATA +13 -11
- {camel_ai-0.2.19.dist-info → camel_ai-0.2.20.dist-info}/RECORD +40 -30
- /camel/datagen/{cotdatagen.py → cot_datagen.py} +0 -0
- {camel_ai-0.2.19.dist-info → camel_ai-0.2.20.dist-info}/LICENSE +0 -0
- {camel_ai-0.2.19.dist-info → camel_ai-0.2.20.dist-info}/WHEEL +0 -0
|
@@ -0,0 +1,778 @@
|
|
|
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 List, Optional
|
|
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 SymPyToolkit(BaseToolkit):
|
|
26
|
+
r"""A toolkit for performing symbolic computations using SymPy.
|
|
27
|
+
This includes methods for Algebraic manipulation calculus
|
|
28
|
+
and Linear Algebra.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def __init__(self, default_variable: str = 'x'):
|
|
32
|
+
r"""Initializes the toolkit with a default variable and logging.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
default_variable (str): The default variable for
|
|
36
|
+
operations (default: :obj: `x`)
|
|
37
|
+
"""
|
|
38
|
+
self.default_variable = default_variable
|
|
39
|
+
logger.info(f"Default variable set to: {self.default_variable}")
|
|
40
|
+
|
|
41
|
+
def simplify_expression(self, expression: str) -> str:
|
|
42
|
+
r"""Simplifies a mathematical expression.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
expression (str): The mathematical expression to simplify,
|
|
46
|
+
provided as a string.
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
str: JSON string containing the simplified mathematical
|
|
50
|
+
expression in the `"result"` field. If an error occurs,
|
|
51
|
+
the `"status"` field will be set to `"error"` with a
|
|
52
|
+
corresponding `"message"`.
|
|
53
|
+
"""
|
|
54
|
+
import sympy as sp
|
|
55
|
+
|
|
56
|
+
try:
|
|
57
|
+
expr = sp.parsing.sympy_parser.parse_expr(expression)
|
|
58
|
+
simplified = sp.simplify(expr)
|
|
59
|
+
return json.dumps({"status": "success", "result": str(simplified)})
|
|
60
|
+
except Exception as e:
|
|
61
|
+
return self.handle_exception("simplify_expression", e)
|
|
62
|
+
|
|
63
|
+
def expand_expression(self, expression: str) -> str:
|
|
64
|
+
r"""Expands an algebraic expression.
|
|
65
|
+
|
|
66
|
+
Args:
|
|
67
|
+
expression (str): The algebraic expression to expand,
|
|
68
|
+
provided as a string.
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
str: JSON string containing the expanded algebraic expression
|
|
72
|
+
in the `"result"` field. If an error occurs, the JSON
|
|
73
|
+
string will include an `"error"` field with the corresponding
|
|
74
|
+
error message.
|
|
75
|
+
"""
|
|
76
|
+
import sympy as sp
|
|
77
|
+
|
|
78
|
+
try:
|
|
79
|
+
expr = sp.parsing.sympy_parser.parse_expr(expression)
|
|
80
|
+
expanded_expr = sp.expand(expr)
|
|
81
|
+
return json.dumps({"result": str(expanded_expr)})
|
|
82
|
+
except Exception as e:
|
|
83
|
+
return self.handle_exception("expand_expression", e)
|
|
84
|
+
|
|
85
|
+
def factor_expression(self, expression: str) -> str:
|
|
86
|
+
r"""Factors an algebraic expression.
|
|
87
|
+
|
|
88
|
+
Args:
|
|
89
|
+
expression (str): The algebraic expression to factor,
|
|
90
|
+
provided as a string.
|
|
91
|
+
|
|
92
|
+
Returns:
|
|
93
|
+
str: JSON string containing the factored algebraic expression
|
|
94
|
+
in the `"result"` field. If an error occurs, the JSON string
|
|
95
|
+
will include an `"error"` field with the corresponding error
|
|
96
|
+
message.
|
|
97
|
+
"""
|
|
98
|
+
import sympy as sp
|
|
99
|
+
|
|
100
|
+
try:
|
|
101
|
+
expr = sp.parsing.sympy_parser.parse_expr(expression)
|
|
102
|
+
factored_expr = sp.factor(expr)
|
|
103
|
+
return json.dumps({"result": str(factored_expr)})
|
|
104
|
+
except Exception as e:
|
|
105
|
+
return self.handle_exception("factor_expression", e)
|
|
106
|
+
|
|
107
|
+
def solve_linear_system(
|
|
108
|
+
self, equations: List[str], variables: List[str]
|
|
109
|
+
) -> str:
|
|
110
|
+
r"""Solves a system of linear equations.
|
|
111
|
+
|
|
112
|
+
Args:
|
|
113
|
+
equations (List[str]): A list of strings representing the linear
|
|
114
|
+
equations to be solved.
|
|
115
|
+
variables (List[str]): A list of strings representing the variables
|
|
116
|
+
involved in the equations.
|
|
117
|
+
|
|
118
|
+
Returns:
|
|
119
|
+
str: JSON string containing the solution to the system of equations
|
|
120
|
+
in the `"result"` field. Each solution is represented as
|
|
121
|
+
a tuple of values corresponding to the variables. If an
|
|
122
|
+
error occurs, the JSON string will include an `"error"`
|
|
123
|
+
field with the corresponding error message.
|
|
124
|
+
"""
|
|
125
|
+
import sympy as sp
|
|
126
|
+
|
|
127
|
+
try:
|
|
128
|
+
eqs = [sp.sympify(eq) for eq in equations]
|
|
129
|
+
vars = sp.symbols(variables)
|
|
130
|
+
solution = sp.linsolve(eqs, vars)
|
|
131
|
+
return json.dumps({"result": [str(sol) for sol in solution]})
|
|
132
|
+
except Exception as e:
|
|
133
|
+
return self.handle_exception("solve_linear_system", e)
|
|
134
|
+
|
|
135
|
+
def solve_nonlinear_system(
|
|
136
|
+
self, sympy_equations: List[str], variables: List[str]
|
|
137
|
+
) -> str:
|
|
138
|
+
r"""Solves a system of nonlinear equations.
|
|
139
|
+
|
|
140
|
+
Args:
|
|
141
|
+
sympy_equations (List[str]): A list of strings representing the
|
|
142
|
+
nonlinear equations to be solved. The equation to solve, must
|
|
143
|
+
be compatible with SymPy, provided as a string.
|
|
144
|
+
|
|
145
|
+
variables (List[str]): A list of strings representing the variables
|
|
146
|
+
involved in the equations.
|
|
147
|
+
|
|
148
|
+
Returns:
|
|
149
|
+
str: JSON string containing the solutions to the system of
|
|
150
|
+
equations in the `"result"` field. Each solution is
|
|
151
|
+
represented as a tuple of values corresponding to the
|
|
152
|
+
variables. If an error occurs, the JSON string will
|
|
153
|
+
include an `"error"` field with the corresponding
|
|
154
|
+
error message.
|
|
155
|
+
"""
|
|
156
|
+
import sympy as sp
|
|
157
|
+
|
|
158
|
+
try:
|
|
159
|
+
eqs = [sp.sympify(eq) for eq in sympy_equations]
|
|
160
|
+
vars = sp.symbols(variables)
|
|
161
|
+
solution = sp.nonlinsolve(eqs, vars)
|
|
162
|
+
return json.dumps({"result": [str(sol) for sol in solution]})
|
|
163
|
+
except Exception as e:
|
|
164
|
+
return self.handle_exception("solve_nonlinear_system", e)
|
|
165
|
+
|
|
166
|
+
def solve_univariate_inequality(
|
|
167
|
+
self, inequality: str, variable: str
|
|
168
|
+
) -> str:
|
|
169
|
+
r"""Solves a single-variable inequality.
|
|
170
|
+
|
|
171
|
+
Args:
|
|
172
|
+
inequality (str): A string representing the inequality
|
|
173
|
+
to be solved.
|
|
174
|
+
variable (str): The variable in the inequality.
|
|
175
|
+
|
|
176
|
+
Returns:
|
|
177
|
+
str: JSON string containing the solution to the inequality in the
|
|
178
|
+
`"result"` field. The solution is represented in a symbolic
|
|
179
|
+
format (e.g., intervals or expressions). If an error occurs,
|
|
180
|
+
the JSON string will include an `"error"` field with the
|
|
181
|
+
corresponding error message.
|
|
182
|
+
"""
|
|
183
|
+
import sympy as sp
|
|
184
|
+
|
|
185
|
+
try:
|
|
186
|
+
var = sp.symbols(variable)
|
|
187
|
+
ineq = sp.sympify(inequality)
|
|
188
|
+
solution = sp.solve_univariate_inequality(ineq, var)
|
|
189
|
+
return json.dumps({"result": str(solution)})
|
|
190
|
+
except Exception as e:
|
|
191
|
+
return self.handle_exception("solve_univariate_inequality", e)
|
|
192
|
+
|
|
193
|
+
def reduce_inequalities(self, inequalities: List[str]) -> str:
|
|
194
|
+
r"""Reduces a system of inequalities.
|
|
195
|
+
|
|
196
|
+
Args:
|
|
197
|
+
inequalities (List[str]): A list of strings representing the
|
|
198
|
+
inequalities to be reduced.
|
|
199
|
+
|
|
200
|
+
Returns:
|
|
201
|
+
str: JSON string containing the reduced system of inequalities
|
|
202
|
+
in the `"result"` field. The solution is represented in
|
|
203
|
+
a symbolic format (e.g., combined intervals or expressions).
|
|
204
|
+
If an error occurs, the JSON string will include an `"error"`
|
|
205
|
+
field with the corresponding error message.
|
|
206
|
+
"""
|
|
207
|
+
import sympy as sp
|
|
208
|
+
|
|
209
|
+
try:
|
|
210
|
+
ineqs = [sp.sympify(ineq) for ineq in inequalities]
|
|
211
|
+
solution = sp.reduce_inequalities(ineqs)
|
|
212
|
+
return json.dumps({"result": str(solution)})
|
|
213
|
+
except Exception as e:
|
|
214
|
+
return self.handle_exception("reduce_inequalities", e)
|
|
215
|
+
|
|
216
|
+
def polynomial_representation(self, expression: str, variable: str) -> str:
|
|
217
|
+
r"""Represents an expression as a polynomial.
|
|
218
|
+
|
|
219
|
+
Args:
|
|
220
|
+
expression (str): The mathematical expression to represent as
|
|
221
|
+
a polynomial, provided as a string.
|
|
222
|
+
variable (str): The variable with respect to which the polynomial
|
|
223
|
+
representation will be created.
|
|
224
|
+
|
|
225
|
+
Returns:
|
|
226
|
+
str: JSON string containing the polynomial representation of the
|
|
227
|
+
expression in the `"result"` field. The polynomial is returned
|
|
228
|
+
in a symbolic format. If an error occurs, the JSON string will
|
|
229
|
+
include an `"error"` field with the corresponding error
|
|
230
|
+
message.
|
|
231
|
+
"""
|
|
232
|
+
|
|
233
|
+
import sympy as sp
|
|
234
|
+
|
|
235
|
+
try:
|
|
236
|
+
var = sp.symbols(variable)
|
|
237
|
+
expr = sp.parsing.sympy_parser.parse_expr(expression)
|
|
238
|
+
poly = sp.Poly(expr, var)
|
|
239
|
+
return json.dumps({"result": str(poly)})
|
|
240
|
+
except Exception as e:
|
|
241
|
+
return self.handle_exception("polynomial_representation", e)
|
|
242
|
+
|
|
243
|
+
def polynomial_degree(self, expression: str, variable: str) -> str:
|
|
244
|
+
r"""Returns the degree of a polynomial.
|
|
245
|
+
|
|
246
|
+
Args:
|
|
247
|
+
expression (str): The polynomial expression for which the degree
|
|
248
|
+
is to be determined, provided as a string.
|
|
249
|
+
variable (str): The variable with respect to which the degree
|
|
250
|
+
of the polynomial is calculated.
|
|
251
|
+
|
|
252
|
+
Returns:
|
|
253
|
+
str: JSON string containing the degree of the polynomial in the
|
|
254
|
+
`"result"` field. If an error occurs, the JSON string will
|
|
255
|
+
include an `"error"` field with the corresponding error
|
|
256
|
+
message.
|
|
257
|
+
"""
|
|
258
|
+
import sympy as sp
|
|
259
|
+
|
|
260
|
+
try:
|
|
261
|
+
var = sp.symbols(variable)
|
|
262
|
+
expr = sp.parsing.sympy_parser.parse_expr(expression)
|
|
263
|
+
degree = int(sp.degree(expr, var))
|
|
264
|
+
return json.dumps({"result": degree})
|
|
265
|
+
except Exception as e:
|
|
266
|
+
return self.handle_exception("polynomial_degree", e)
|
|
267
|
+
|
|
268
|
+
def polynomial_coefficients(self, expression: str, variable: str) -> str:
|
|
269
|
+
r"""Returns the coefficients of a polynomial.
|
|
270
|
+
|
|
271
|
+
Args:
|
|
272
|
+
expression (str): The polynomial expression from which the
|
|
273
|
+
coefficients are to be extracted, provided as a string.
|
|
274
|
+
variable (str): The variable with respect to which the polynomial
|
|
275
|
+
coefficients are determined.
|
|
276
|
+
|
|
277
|
+
Returns:
|
|
278
|
+
str: JSON string containing the list of coefficients of the
|
|
279
|
+
polynomial in the `"result"` field. The coefficients are
|
|
280
|
+
ordered from the highest degree term to the constant term.
|
|
281
|
+
If an error occurs, the JSON string will include an `"error"
|
|
282
|
+
field with the corresponding error message.
|
|
283
|
+
"""
|
|
284
|
+
import sympy as sp
|
|
285
|
+
|
|
286
|
+
try:
|
|
287
|
+
var = sp.symbols(variable)
|
|
288
|
+
expr = sp.parsing.sympy_parser.parse_expr(expression)
|
|
289
|
+
coeffs = sp.Poly(expr, var).all_coeffs()
|
|
290
|
+
return json.dumps({"result": [str(coeff) for coeff in coeffs]})
|
|
291
|
+
except Exception as e:
|
|
292
|
+
return self.handle_exception("polynomial_coefficients", e)
|
|
293
|
+
|
|
294
|
+
def solve_equation(
|
|
295
|
+
self, sympy_equation: str, variable: Optional[str] = None
|
|
296
|
+
) -> str:
|
|
297
|
+
r"""Solves an equation for a specific variable.
|
|
298
|
+
|
|
299
|
+
Args:
|
|
300
|
+
sympy_equation(str): The equation to solve, must be compatible
|
|
301
|
+
with SymPy, provided as a string.
|
|
302
|
+
variable (str, optional): The variable to solve for. If not
|
|
303
|
+
specified, the function will use the default variable.
|
|
304
|
+
|
|
305
|
+
Returns:
|
|
306
|
+
str: JSON string containing the solutions to the equation in the
|
|
307
|
+
`"result"` field. Each solution is represented as a string.
|
|
308
|
+
If an error occurs, the JSON string will include an `"error"`
|
|
309
|
+
field with the corresponding error message.
|
|
310
|
+
"""
|
|
311
|
+
import sympy as sp
|
|
312
|
+
|
|
313
|
+
try:
|
|
314
|
+
variable = (
|
|
315
|
+
sp.symbols(variable)
|
|
316
|
+
if variable
|
|
317
|
+
else sp.symbols(self.default_variable)
|
|
318
|
+
)
|
|
319
|
+
eq = sp.sympify(sympy_equation)
|
|
320
|
+
solutions = sp.solve(eq, variable)
|
|
321
|
+
return json.dumps({"result": [str(sol) for sol in solutions]})
|
|
322
|
+
except Exception as e:
|
|
323
|
+
return self.handle_exception("solve_equation", e)
|
|
324
|
+
|
|
325
|
+
def find_roots(self, expression: str) -> str:
|
|
326
|
+
r"""Finds the roots of a polynomial or algebraic equation.
|
|
327
|
+
|
|
328
|
+
Args:
|
|
329
|
+
expression (str): The polynomial or algebraic equation for which
|
|
330
|
+
the roots are to be found, provided as a string.
|
|
331
|
+
|
|
332
|
+
Returns:
|
|
333
|
+
str: JSON string containing the roots of the expression in the
|
|
334
|
+
`"result"` field. The roots are represented as a list of
|
|
335
|
+
solutions. If an error occurs, the JSON string will include
|
|
336
|
+
a `"status"` field set to `"error"` and a `"message"` field
|
|
337
|
+
with the corresponding error description.
|
|
338
|
+
"""
|
|
339
|
+
import sympy as sp
|
|
340
|
+
|
|
341
|
+
try:
|
|
342
|
+
expr = sp.parsing.sympy_parser.parse_expr(expression)
|
|
343
|
+
roots = sp.solve(expr)
|
|
344
|
+
return json.dumps({"status": "success", "result": str(roots)})
|
|
345
|
+
|
|
346
|
+
except Exception as e:
|
|
347
|
+
return self.handle_exception("find_roots", e)
|
|
348
|
+
|
|
349
|
+
def differentiate(
|
|
350
|
+
self, expression: str, variable: Optional[str] = None
|
|
351
|
+
) -> str:
|
|
352
|
+
r"""Differentiates an expression with respect to a variable.
|
|
353
|
+
|
|
354
|
+
Args:
|
|
355
|
+
expression (str): The mathematical expression to differentiate,
|
|
356
|
+
provided as a string.
|
|
357
|
+
variable (str, optional): The variable with respect to which the
|
|
358
|
+
differentiation is performed. If not specified, the default
|
|
359
|
+
variable is used.
|
|
360
|
+
|
|
361
|
+
Returns:
|
|
362
|
+
str: JSON string containing the derivative of the expression in the
|
|
363
|
+
`"result"` field. If an error occurs, the JSON string will
|
|
364
|
+
include an `"error"` field with the corresponding error
|
|
365
|
+
message.
|
|
366
|
+
"""
|
|
367
|
+
import sympy as sp
|
|
368
|
+
|
|
369
|
+
try:
|
|
370
|
+
variable = (
|
|
371
|
+
sp.symbols(variable)
|
|
372
|
+
if variable
|
|
373
|
+
else sp.symbols(self.default_variable)
|
|
374
|
+
)
|
|
375
|
+
expr = sp.parsing.sympy_parser.parse_expr(expression)
|
|
376
|
+
derivative = sp.diff(expr, variable)
|
|
377
|
+
return json.dumps({"result": str(derivative)})
|
|
378
|
+
except Exception as e:
|
|
379
|
+
return self.handle_exception("differentiate", e)
|
|
380
|
+
|
|
381
|
+
def integrate(
|
|
382
|
+
self, expression: str, variable: Optional[str] = None
|
|
383
|
+
) -> str:
|
|
384
|
+
r"""Integrates an expression with respect to a variable.
|
|
385
|
+
|
|
386
|
+
Args:
|
|
387
|
+
expression (str): The mathematical expression to integrate,
|
|
388
|
+
provided as a string.
|
|
389
|
+
variable (str, optional): The variable with respect to which the
|
|
390
|
+
integration is performed. If not specified, the default
|
|
391
|
+
variable is used.
|
|
392
|
+
|
|
393
|
+
Returns:
|
|
394
|
+
str: JSON string containing the integral of the expression in the
|
|
395
|
+
`"result"` field. If an error occurs, the JSON string will
|
|
396
|
+
include an `"error"` field with the corresponding error
|
|
397
|
+
message.
|
|
398
|
+
"""
|
|
399
|
+
import sympy as sp
|
|
400
|
+
|
|
401
|
+
try:
|
|
402
|
+
variable = (
|
|
403
|
+
sp.symbols(variable)
|
|
404
|
+
if variable
|
|
405
|
+
else sp.symbols(self.default_variable)
|
|
406
|
+
)
|
|
407
|
+
expr = sp.parsing.sympy_parser.parse_expr(expression)
|
|
408
|
+
integral = sp.integrate(expr, variable)
|
|
409
|
+
return json.dumps({"result": str(integral)})
|
|
410
|
+
except Exception as e:
|
|
411
|
+
return self.handle_exception("integrate", e)
|
|
412
|
+
|
|
413
|
+
def definite_integral(
|
|
414
|
+
self, expression: str, variable: str, lower: float, upper: float
|
|
415
|
+
) -> str:
|
|
416
|
+
r"""Computes the definite integral of an expression within given
|
|
417
|
+
bounds.
|
|
418
|
+
|
|
419
|
+
Args:
|
|
420
|
+
expression (str): The mathematical expression to integrate,
|
|
421
|
+
provided as a string.
|
|
422
|
+
variable (str): The variable with respect to which the definite
|
|
423
|
+
integration is performed.
|
|
424
|
+
lower (float): The lower limit of the integration.
|
|
425
|
+
upper (float): The upper limit of the integration.
|
|
426
|
+
|
|
427
|
+
Returns:
|
|
428
|
+
str: JSON string containing the result of the definite integral
|
|
429
|
+
in the `"result"` field. If an error occurs, the JSON string
|
|
430
|
+
will include an `"error"` field with the corresponding error
|
|
431
|
+
message.
|
|
432
|
+
"""
|
|
433
|
+
import sympy as sp
|
|
434
|
+
|
|
435
|
+
try:
|
|
436
|
+
var = sp.symbols(variable)
|
|
437
|
+
expr = sp.parsing.sympy_parser.parse_expr(expression)
|
|
438
|
+
integral = sp.integrate(expr, (var, lower, upper))
|
|
439
|
+
return json.dumps({"result": str(integral)})
|
|
440
|
+
except Exception as e:
|
|
441
|
+
return self.handle_exception("definite_integral", e)
|
|
442
|
+
|
|
443
|
+
def series_expansion(
|
|
444
|
+
self, expression: str, variable: str, point: float, order: int
|
|
445
|
+
) -> str:
|
|
446
|
+
r"""Expands an expression into a Taylor series around a given point up
|
|
447
|
+
to a specified order.
|
|
448
|
+
|
|
449
|
+
Args:
|
|
450
|
+
expression (str): The mathematical expression to expand, provided
|
|
451
|
+
as a string.
|
|
452
|
+
variable (str): The variable with respect to which the series
|
|
453
|
+
expansion is performed.
|
|
454
|
+
point (float): The point around which the Taylor series is
|
|
455
|
+
expanded.
|
|
456
|
+
order (int): The order up to which the series expansion is
|
|
457
|
+
computed.
|
|
458
|
+
|
|
459
|
+
Returns:
|
|
460
|
+
str: JSON string containing the Taylor series expansion of the
|
|
461
|
+
expression in the `"result"` field. If an error occurs,
|
|
462
|
+
the JSON string will include an `"error"` field with the
|
|
463
|
+
corresponding error message.
|
|
464
|
+
"""
|
|
465
|
+
import sympy as sp
|
|
466
|
+
|
|
467
|
+
try:
|
|
468
|
+
var = sp.symbols(variable)
|
|
469
|
+
expr = sp.parsing.sympy_parser.parse_expr(expression)
|
|
470
|
+
series = sp.series(expr, var, point, order)
|
|
471
|
+
return json.dumps({"result": str(series)})
|
|
472
|
+
except Exception as e:
|
|
473
|
+
return self.handle_exception("series_expansion", e)
|
|
474
|
+
|
|
475
|
+
def compute_limit(
|
|
476
|
+
self,
|
|
477
|
+
expression: str,
|
|
478
|
+
variable: str,
|
|
479
|
+
point: float,
|
|
480
|
+
) -> str:
|
|
481
|
+
r"""Computes the limit of an expression as a variable approaches
|
|
482
|
+
a point.
|
|
483
|
+
|
|
484
|
+
Args:
|
|
485
|
+
expression (str): The mathematical expression for which the limit
|
|
486
|
+
is to be computed, provided as a string.
|
|
487
|
+
variable (str): The variable with respect to which the limit is
|
|
488
|
+
computed.
|
|
489
|
+
point (float): The point that the variable approaches.
|
|
490
|
+
|
|
491
|
+
Returns:
|
|
492
|
+
str: JSON string containing the computed limit of the expression
|
|
493
|
+
in the `"result"` field. If an error occurs, the JSON string
|
|
494
|
+
will include an `"error"` field with the corresponding error
|
|
495
|
+
message.
|
|
496
|
+
"""
|
|
497
|
+
import sympy as sp
|
|
498
|
+
|
|
499
|
+
try:
|
|
500
|
+
var = sp.symbols(variable)
|
|
501
|
+
expr = sp.parsing.sympy_parser.parse_expr(expression)
|
|
502
|
+
limit = sp.limit(expr, var, point)
|
|
503
|
+
return json.dumps({"result": str(limit)})
|
|
504
|
+
except Exception as e:
|
|
505
|
+
return self.handle_exception("compute_limit", e)
|
|
506
|
+
|
|
507
|
+
def find_critical_points(self, expression: str, variable: str) -> str:
|
|
508
|
+
r"""Finds the critical points of an expression by setting its
|
|
509
|
+
derivative to zero.
|
|
510
|
+
|
|
511
|
+
Args:
|
|
512
|
+
expression (str): The mathematical expression for which critical
|
|
513
|
+
points are to be found, provided as a string.
|
|
514
|
+
variable (str): The variable with respect to which the critical
|
|
515
|
+
points are determined.
|
|
516
|
+
|
|
517
|
+
Returns:
|
|
518
|
+
str: JSON string containing the critical points of the expression
|
|
519
|
+
in the `"result"` field. The critical points are returned as a
|
|
520
|
+
list of values corresponding to the variable. If an error
|
|
521
|
+
occurs, the JSON string will include an `"error"` field with
|
|
522
|
+
the corresponding error message.
|
|
523
|
+
"""
|
|
524
|
+
import sympy as sp
|
|
525
|
+
|
|
526
|
+
try:
|
|
527
|
+
var = sp.symbols(variable)
|
|
528
|
+
expr = sp.parsing.sympy_parser.parse_expr(expression)
|
|
529
|
+
derivative = sp.diff(expr, var)
|
|
530
|
+
critical_points = sp.solve(derivative, var)
|
|
531
|
+
return json.dumps(
|
|
532
|
+
{"result": [str(point) for point in critical_points]}
|
|
533
|
+
)
|
|
534
|
+
except Exception as e:
|
|
535
|
+
return self.handle_exception("find_critical_points", e)
|
|
536
|
+
|
|
537
|
+
def check_continuity(
|
|
538
|
+
self, expression: str, variable: str, point: float
|
|
539
|
+
) -> str:
|
|
540
|
+
r"""Checks if an expression is continuous at a given point.
|
|
541
|
+
|
|
542
|
+
Args:
|
|
543
|
+
expression (str): The mathematical expression to check for
|
|
544
|
+
continuity, provided as a string.
|
|
545
|
+
variable (str): The variable with respect to which continuity
|
|
546
|
+
is checked.
|
|
547
|
+
point (float): The point at which the continuity of the expression
|
|
548
|
+
is checked.
|
|
549
|
+
|
|
550
|
+
Returns:
|
|
551
|
+
str: JSON string containing the result of the continuity check in
|
|
552
|
+
the `"result"` field. The result will be `"True"` if the
|
|
553
|
+
expression is continuous at the given point, otherwise
|
|
554
|
+
`"False"`. If an error occurs, the JSON string will include
|
|
555
|
+
an `"error"` field with the corresponding error message.
|
|
556
|
+
"""
|
|
557
|
+
import sympy as sp
|
|
558
|
+
|
|
559
|
+
try:
|
|
560
|
+
var = sp.symbols(variable)
|
|
561
|
+
expr = sp.parsing.sympy_parser.parse_expr(expression)
|
|
562
|
+
left_limit = sp.limit(expr, var, point, dir='-')
|
|
563
|
+
right_limit = sp.limit(expr, var, point, dir='+')
|
|
564
|
+
value_at_point = expr.subs(var, point)
|
|
565
|
+
is_continuous = left_limit == right_limit == value_at_point
|
|
566
|
+
return json.dumps({"result": str(is_continuous)})
|
|
567
|
+
except Exception as e:
|
|
568
|
+
return self.handle_exception("check_continuity", e)
|
|
569
|
+
|
|
570
|
+
def compute_determinant(self, matrix: List[List[float]]) -> str:
|
|
571
|
+
r"""Computes the determinant of a matrix.
|
|
572
|
+
|
|
573
|
+
Args:
|
|
574
|
+
matrix (List[List[float]]): A two-dimensional list representing
|
|
575
|
+
the matrix for which the determinant is to be computed.
|
|
576
|
+
|
|
577
|
+
Returns:
|
|
578
|
+
str: JSON string containing the determinant of the matrix in the
|
|
579
|
+
`"result"` field. If an error occurs, the JSON string will
|
|
580
|
+
include an `"error"` field with the corresponding error
|
|
581
|
+
message.
|
|
582
|
+
"""
|
|
583
|
+
import sympy as sp
|
|
584
|
+
|
|
585
|
+
try:
|
|
586
|
+
mat = sp.Matrix(matrix)
|
|
587
|
+
determinant = mat.det()
|
|
588
|
+
return json.dumps({"result": str(determinant)})
|
|
589
|
+
except Exception as e:
|
|
590
|
+
return self.handle_exception("compute_determinant", e)
|
|
591
|
+
|
|
592
|
+
def compute_inverse(self, matrix: List[List[float]]) -> str:
|
|
593
|
+
r"""Computes the inverse of a matrix.
|
|
594
|
+
|
|
595
|
+
Args:
|
|
596
|
+
matrix (List[List[float]]): A two-dimensional list representing
|
|
597
|
+
the matrix for which the inverse is to be computed.
|
|
598
|
+
|
|
599
|
+
Returns:
|
|
600
|
+
str: JSON string containing the inverse of the matrix in the
|
|
601
|
+
`"result"` field. The inverse is represented in a symbolic
|
|
602
|
+
matrix format. If an error occurs, the JSON string will
|
|
603
|
+
include an `"error"` field with the corresponding error
|
|
604
|
+
message.
|
|
605
|
+
"""
|
|
606
|
+
import sympy as sp
|
|
607
|
+
|
|
608
|
+
try:
|
|
609
|
+
mat = sp.Matrix(matrix)
|
|
610
|
+
inverse = mat.inv()
|
|
611
|
+
return json.dumps({"result": str(inverse)})
|
|
612
|
+
except Exception as e:
|
|
613
|
+
return self.handle_exception("compute_inverse", e)
|
|
614
|
+
|
|
615
|
+
def compute_eigenvalues(self, matrix: List[List[float]]) -> str:
|
|
616
|
+
r"""Computes the eigenvalues of a matrix.
|
|
617
|
+
|
|
618
|
+
Args:
|
|
619
|
+
matrix (List[List[float]]): A two-dimensional list representing
|
|
620
|
+
the matrix for which the eigenvalues are to be computed.
|
|
621
|
+
|
|
622
|
+
Returns:
|
|
623
|
+
str: JSON string containing the eigenvalues of the matrix in the
|
|
624
|
+
`"result"` field. The eigenvalues are represented as a
|
|
625
|
+
dictionary where keys are the eigenvalues (as strings) and
|
|
626
|
+
values are their multiplicities (as strings). If an error
|
|
627
|
+
occurs, the JSON string will include an `"error"` field
|
|
628
|
+
with the corresponding error message.
|
|
629
|
+
"""
|
|
630
|
+
import sympy as sp
|
|
631
|
+
|
|
632
|
+
try:
|
|
633
|
+
mat = sp.Matrix(matrix)
|
|
634
|
+
eigenvalues = mat.eigenvals()
|
|
635
|
+
return json.dumps(
|
|
636
|
+
{"result": {str(k): str(v) for k, v in eigenvalues.items()}}
|
|
637
|
+
)
|
|
638
|
+
except Exception as e:
|
|
639
|
+
return self.handle_exception("compute_eigenvalues", e)
|
|
640
|
+
|
|
641
|
+
def compute_eigenvectors(self, matrix: List[List[float]]) -> str:
|
|
642
|
+
r"""Computes the eigenvectors of a matrix.
|
|
643
|
+
|
|
644
|
+
Args:
|
|
645
|
+
matrix (List[List[float]]): A two-dimensional list representing
|
|
646
|
+
the matrix for which the eigenvectors are to be computed.
|
|
647
|
+
|
|
648
|
+
Returns:
|
|
649
|
+
str: JSON string containing the eigenvectors of the matrix in the
|
|
650
|
+
`"result"` field. Each eigenvalue is represented as a
|
|
651
|
+
dictionary with the following keys:
|
|
652
|
+
- `"eigenvalue"`: The eigenvalue (as a string).
|
|
653
|
+
- `"multiplicity"`: The multiplicity of the eigenvalue
|
|
654
|
+
(as an integer).
|
|
655
|
+
- `"eigenvectors"`: A list of eigenvectors
|
|
656
|
+
(each represented as a string).
|
|
657
|
+
|
|
658
|
+
If an error occurs, the JSON string will include an `"error"`
|
|
659
|
+
field with the corresponding error message.
|
|
660
|
+
"""
|
|
661
|
+
import sympy as sp
|
|
662
|
+
|
|
663
|
+
try:
|
|
664
|
+
mat = sp.Matrix(matrix)
|
|
665
|
+
eigenvectors = mat.eigenvects()
|
|
666
|
+
result = [
|
|
667
|
+
{
|
|
668
|
+
"eigenvalue": str(eigenvalue),
|
|
669
|
+
"multiplicity": multiplicity,
|
|
670
|
+
"eigenvectors": [str(v) for v in vectors],
|
|
671
|
+
}
|
|
672
|
+
for eigenvalue, multiplicity, vectors in eigenvectors
|
|
673
|
+
]
|
|
674
|
+
return json.dumps({"result": result})
|
|
675
|
+
except Exception as e:
|
|
676
|
+
return self.handle_exception("compute_eigenvectors", e)
|
|
677
|
+
|
|
678
|
+
def compute_nullspace(self, matrix: List[List[float]]) -> str:
|
|
679
|
+
r"""Computes the null space of a matrix.
|
|
680
|
+
|
|
681
|
+
Args:
|
|
682
|
+
matrix (List[List[float]]): A two-dimensional list representing
|
|
683
|
+
the matrix for which the null space is to be computed.
|
|
684
|
+
|
|
685
|
+
Returns:
|
|
686
|
+
str: JSON string containing the null space of the matrix in the
|
|
687
|
+
`"result"` field. The null space is represented as a list of
|
|
688
|
+
basis vectors, where each vector is given as a string in
|
|
689
|
+
symbolic format. If an error occurs, the JSON string will
|
|
690
|
+
include an `"error"` field with the corresponding error
|
|
691
|
+
message.
|
|
692
|
+
"""
|
|
693
|
+
import sympy as sp
|
|
694
|
+
|
|
695
|
+
try:
|
|
696
|
+
mat = sp.Matrix(matrix)
|
|
697
|
+
nullspace = mat.nullspace()
|
|
698
|
+
return json.dumps({"result": [str(vec) for vec in nullspace]})
|
|
699
|
+
except Exception as e:
|
|
700
|
+
return self.handle_exception("compute_nullspace", e)
|
|
701
|
+
|
|
702
|
+
def compute_rank(self, matrix: List[List[float]]) -> str:
|
|
703
|
+
r"""Computes the rank of a matrix.
|
|
704
|
+
|
|
705
|
+
Args:
|
|
706
|
+
matrix (List[List[float]]): A two-dimensional list representing
|
|
707
|
+
the matrix for which the rank is to be computed.
|
|
708
|
+
|
|
709
|
+
Returns:
|
|
710
|
+
str: JSON string containing the rank of the matrix in the
|
|
711
|
+
`"result"` field. The rank is represented as an integer.
|
|
712
|
+
If an error occurs,the JSON string will include an
|
|
713
|
+
`"error"` field with the corresponding error message.
|
|
714
|
+
"""
|
|
715
|
+
import sympy as sp
|
|
716
|
+
|
|
717
|
+
try:
|
|
718
|
+
mat = sp.Matrix(matrix)
|
|
719
|
+
rank = mat.rank()
|
|
720
|
+
return json.dumps({"result": rank})
|
|
721
|
+
except Exception as e:
|
|
722
|
+
return self.handle_exception("compute_rank", e)
|
|
723
|
+
|
|
724
|
+
def handle_exception(self, func_name: str, error: Exception) -> str:
|
|
725
|
+
r"""Handles exceptions by logging and returning error details.
|
|
726
|
+
|
|
727
|
+
Args:
|
|
728
|
+
func_name (str): The name of the function where the
|
|
729
|
+
exception occurred.
|
|
730
|
+
error (Exception): The exception object containing
|
|
731
|
+
details about the error.
|
|
732
|
+
|
|
733
|
+
Returns:
|
|
734
|
+
str: JSON string containing the error details.
|
|
735
|
+
The JSON includes:
|
|
736
|
+
- `"status"`: Always set to `"error"`.
|
|
737
|
+
- `"message"`: A string representation of the
|
|
738
|
+
exception message.
|
|
739
|
+
"""
|
|
740
|
+
logger.error(f"Error in {func_name}: {error}")
|
|
741
|
+
return json.dumps(
|
|
742
|
+
{"status": "error", "message": f"Error in {func_name}: {error}"}
|
|
743
|
+
)
|
|
744
|
+
|
|
745
|
+
def get_tools(self) -> List[FunctionTool]:
|
|
746
|
+
r"""Exposes the tool's methods to the agent framework.
|
|
747
|
+
|
|
748
|
+
Returns:
|
|
749
|
+
List[FunctionTool]: A list of `FunctionTool` objects representing
|
|
750
|
+
the toolkit's methods, making them accessible to the agent.
|
|
751
|
+
"""
|
|
752
|
+
return [
|
|
753
|
+
FunctionTool(self.simplify_expression),
|
|
754
|
+
FunctionTool(self.expand_expression),
|
|
755
|
+
FunctionTool(self.factor_expression),
|
|
756
|
+
FunctionTool(self.solve_linear_system),
|
|
757
|
+
FunctionTool(self.solve_nonlinear_system),
|
|
758
|
+
FunctionTool(self.solve_univariate_inequality),
|
|
759
|
+
FunctionTool(self.reduce_inequalities),
|
|
760
|
+
FunctionTool(self.polynomial_representation),
|
|
761
|
+
FunctionTool(self.polynomial_degree),
|
|
762
|
+
FunctionTool(self.polynomial_coefficients),
|
|
763
|
+
FunctionTool(self.solve_equation),
|
|
764
|
+
FunctionTool(self.find_roots),
|
|
765
|
+
FunctionTool(self.differentiate),
|
|
766
|
+
FunctionTool(self.integrate),
|
|
767
|
+
FunctionTool(self.definite_integral),
|
|
768
|
+
FunctionTool(self.series_expansion),
|
|
769
|
+
FunctionTool(self.compute_limit),
|
|
770
|
+
FunctionTool(self.find_critical_points),
|
|
771
|
+
FunctionTool(self.check_continuity),
|
|
772
|
+
FunctionTool(self.compute_determinant),
|
|
773
|
+
FunctionTool(self.compute_inverse),
|
|
774
|
+
FunctionTool(self.compute_eigenvalues),
|
|
775
|
+
FunctionTool(self.compute_eigenvectors),
|
|
776
|
+
FunctionTool(self.compute_nullspace),
|
|
777
|
+
FunctionTool(self.compute_rank),
|
|
778
|
+
]
|