mirror of
https://gitee.com/dify_ai/dify.git
synced 2024-11-30 02:08:37 +08:00
add built-in maths tool for local expression evaluation on NumExpr (#2390)
This commit is contained in:
parent
c2d47cd2e1
commit
d4cfd3e7ac
@ -13,3 +13,4 @@
|
||||
- vectorizer
|
||||
- youtube
|
||||
- gaode
|
||||
- maths
|
||||
|
10
api/core/tools/provider/builtin/maths/_assets/icon.svg
Normal file
10
api/core/tools/provider/builtin/maths/_assets/icon.svg
Normal file
@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="512px" height="512px" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<g><path style="opacity:0.976" fill="#010101" d="M 354.5,-0.5 C 364.5,-0.5 374.5,-0.5 384.5,-0.5C 438.965,10.4672 470.465,43.4672 479,98.5C 481.412,127.587 473.912,153.753 456.5,177C 473.277,199.338 480.777,224.505 479,252.5C 477.092,257.205 473.592,259.538 468.5,259.5C 463.661,258.828 460.495,256.162 459,251.5C 460.432,229.538 454.598,209.871 441.5,192.5C 432.642,197.031 426.976,194.531 424.5,185C 424.909,182.679 425.743,180.513 427,178.5C 460.028,146.677 467.361,109.677 449,67.5C 422.577,25.5507 385.41,11.384 337.5,25C 311.514,35.6522 293.681,54.1522 284,80.5C 280.753,89.5232 280.753,98.5232 284,107.5C 292.532,118.302 303.032,120.469 315.5,114C 321.767,108.633 325.267,101.8 326,93.5C 336.331,71.081 353.831,61.2477 378.5,64C 400.878,70.54 413.211,85.3734 415.5,108.5C 412.61,138.225 396.276,153.725 366.5,155C 349.702,160.095 344.202,170.929 350,187.5C 357.228,197.452 367.062,201.618 379.5,200C 401.515,206.191 413.515,220.691 415.5,243.5C 414.092,267.737 401.759,283.237 378.5,290C 343.224,291.728 324.724,274.895 323,239.5C 317.83,226.332 308.33,221.165 294.5,224C 284.918,228.005 279.918,235.171 279.5,245.5C 281.411,284.518 300.077,312.018 335.5,328C 358.938,336.837 382.271,336.504 405.5,327C 412.76,322.871 420.093,319.038 427.5,315.5C 430.916,316.044 433.916,317.378 436.5,319.5C 439.366,324.763 438.699,329.596 434.5,334C 405.492,354.002 373.825,359.669 339.5,351C 332.724,348.594 326.058,346.261 319.5,344C 300.325,372.018 280.658,399.685 260.5,427C 280.495,426.72 300.495,426.72 320.5,427C 348.67,433.811 360.503,451.644 356,480.5C 350.502,497.644 339.002,507.977 321.5,511.5C 273.833,511.5 226.167,511.5 178.5,511.5C 155.982,508.98 142.149,496.646 137,474.5C 135.493,458.689 139.826,444.689 150,432.5C 183.556,389.053 215.889,344.72 247,299.5C 254.022,289.129 258.522,277.796 260.5,265.5C 258.869,251.037 250.703,243.037 236,241.5C 228.116,242.442 221.449,245.775 216,251.5C 212.245,258.836 209.245,266.503 207,274.5C 199.293,287.695 187.793,295.028 172.5,296.5C 176.465,317.119 170.465,333.952 154.5,347C 136.508,356.482 119.174,355.482 102.5,344C 95.553,337.61 91.053,329.777 89,320.5C 88.5,243.167 88.3333,165.834 88.5,88.5C 63.3361,93.0839 45.1694,84.0839 34,61.5C 28.1052,40.8522 33.6052,24.0188 50.5,11C 55.1719,8.16348 60.1719,6.16348 65.5,5C 89.8333,4.33333 114.167,4.33333 138.5,5C 155.9,9.40004 167.067,20.2334 172,37.5C 172.167,83.8369 172.667,130.17 173.5,176.5C 206.996,155.157 242.329,151.657 279.5,166C 316.965,183.136 338.798,211.969 345,252.5C 351.754,268.089 363.254,273.256 379.5,268C 394.184,259.279 398.018,247.112 391,231.5C 388.433,227.3 384.933,224.133 380.5,222C 373.295,220.071 365.961,218.737 358.5,218C 331.532,207.563 322.032,188.396 330,160.5C 335.808,148.024 345.308,139.858 358.5,136C 365.961,135.263 373.295,133.929 380.5,132C 395.003,122.489 398.17,109.989 390,94.5C 375.586,79.6848 361.586,80.0181 348,95.5C 345.395,103.313 342.395,110.98 339,118.5C 325.26,137.063 307.427,142.563 285.5,135C 266.698,125.234 258.532,109.734 261,88.5C 273.213,38.7842 304.38,9.11757 354.5,-0.5 Z"/></g>
|
||||
<g><path style="opacity:0.999" fill="#9ee3a3" d="M 468.5,259.5 C 467.281,267.376 465.281,275.043 462.5,282.5C 451.52,279.98 446.687,284.313 448,295.5C 449.332,297.66 450.832,299.66 452.5,301.5C 447.567,307.772 442.233,313.772 436.5,319.5C 433.916,317.378 430.916,316.044 427.5,315.5C 420.093,319.038 412.76,322.871 405.5,327C 382.271,336.504 358.938,336.837 335.5,328C 300.077,312.018 281.411,284.518 279.5,245.5C 279.918,235.171 284.918,228.005 294.5,224C 308.33,221.165 317.83,226.332 323,239.5C 324.724,274.895 343.224,291.728 378.5,290C 401.759,283.237 414.092,267.737 415.5,243.5C 413.515,220.691 401.515,206.191 379.5,200C 367.062,201.618 357.228,197.452 350,187.5C 344.202,170.929 349.702,160.095 366.5,155C 396.276,153.725 412.61,138.225 415.5,108.5C 413.211,85.3734 400.878,70.54 378.5,64C 353.831,61.2477 336.331,71.081 326,93.5C 325.267,101.8 321.767,108.633 315.5,114C 303.032,120.469 292.532,118.302 284,107.5C 280.753,98.5232 280.753,89.5232 284,80.5C 293.681,54.1522 311.514,35.6522 337.5,25C 385.41,11.384 422.577,25.5507 449,67.5C 467.361,109.677 460.028,146.677 427,178.5C 425.743,180.513 424.909,182.679 424.5,185C 426.976,194.531 432.642,197.031 441.5,192.5C 454.598,209.871 460.432,229.538 459,251.5C 460.495,256.162 463.661,258.828 468.5,259.5 Z"/></g>
|
||||
<g><path style="opacity:1" fill="#fc9a99" d="M 69.5,24.5 C 90.8359,24.3334 112.169,24.5 133.5,25C 142.793,26.9579 148.96,32.4579 152,41.5C 152.667,93.1667 152.667,144.833 152,196.5C 138.503,212.996 130.337,231.829 127.5,253C 127.929,271.035 136.096,284.201 152,292.5C 152.667,300.5 152.667,308.5 152,316.5C 145.467,332.517 134.301,337.017 118.5,330C 113.781,326.611 110.614,322.111 109,316.5C 108.5,240.501 108.333,164.501 108.5,88.5C 116.872,87.4331 120.039,82.7664 118,74.5C 117.215,72.714 116.049,71.214 114.5,70C 99.234,68.7139 83.9007,68.0473 68.5,68C 55.3901,62.9539 50.2234,53.4539 53,39.5C 56.1733,31.8218 61.6733,26.8218 69.5,24.5 Z"/></g>
|
||||
<g><path style="opacity:1" fill="#fce4b2" d="M 224.5,177.5 C 252.959,175.374 277.626,183.874 298.5,203C 281.3,204.863 269.133,213.696 262,229.5C 238.897,216.285 218.23,219.285 200,238.5C 197.268,242.241 194.934,246.241 193,250.5C 189.531,272.81 177.364,279.977 156.5,272C 149.298,265.76 146.465,257.927 148,248.5C 158.982,209.013 184.482,185.346 224.5,177.5 Z"/></g>
|
||||
<g><path style="opacity:0.941" fill="#010201" d="M 462.5,282.5 C 467.503,286.476 468.669,291.476 466,297.5C 462.265,301.096 457.765,302.429 452.5,301.5C 450.832,299.66 449.332,297.66 448,295.5C 446.687,284.313 451.52,279.98 462.5,282.5 Z"/></g>
|
||||
<g><path style="opacity:1" fill="#fde4b2" d="M 271.5,297.5 C 272.496,297.414 273.329,297.748 274,298.5C 281.267,311.512 290.767,322.679 302.5,332C 281.138,364.194 258.805,395.694 235.5,426.5C 230.073,426.627 225.239,428.294 221,431.5C 217.958,438.085 219.458,443.252 225.5,447C 257.5,447.333 289.5,447.667 321.5,448C 336.675,455.19 340.508,466.356 333,481.5C 329.902,485.935 325.736,488.768 320.5,490C 272.5,490.667 224.5,490.667 176.5,490C 161.021,485.549 154.854,475.382 158,459.5C 159.333,456.833 160.667,454.167 162,451.5C 196.067,406.365 229.4,360.699 262,314.5C 265.179,308.806 268.345,303.139 271.5,297.5 Z"/></g>
|
||||
</svg>
|
After Width: | Height: | Size: 6.5 KiB |
18
api/core/tools/provider/builtin/maths/maths.py
Normal file
18
api/core/tools/provider/builtin/maths/maths.py
Normal file
@ -0,0 +1,18 @@
|
||||
from typing import Any, Dict
|
||||
|
||||
from core.tools.errors import ToolProviderCredentialValidationError
|
||||
from core.tools.provider.builtin.maths.tools.eval_expression import EvaluateExpressionTool
|
||||
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController
|
||||
|
||||
|
||||
class MathsProvider(BuiltinToolProviderController):
|
||||
def _validate_credentials(self, credentials: Dict[str, Any]) -> None:
|
||||
try:
|
||||
EvaluateExpressionTool().invoke(
|
||||
user_id='',
|
||||
tool_parameters={
|
||||
'expression': '1+(2+3)*4',
|
||||
},
|
||||
)
|
||||
except Exception as e:
|
||||
raise ToolProviderCredentialValidationError(str(e))
|
12
api/core/tools/provider/builtin/maths/maths.yaml
Normal file
12
api/core/tools/provider/builtin/maths/maths.yaml
Normal file
@ -0,0 +1,12 @@
|
||||
identity:
|
||||
author: Bowen Liang
|
||||
name: maths
|
||||
label:
|
||||
en_US: Maths
|
||||
zh_Hans: 数学工具
|
||||
pt_BR: Maths
|
||||
description:
|
||||
en_US: A tool for maths.
|
||||
zh_Hans: 一个用于数学计算的工具。
|
||||
pt_BR: A tool for maths.
|
||||
icon: icon.svg
|
@ -0,0 +1,29 @@
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, Dict, List, Union
|
||||
|
||||
from core.tools.entities.tool_entities import ToolInvokeMessage
|
||||
from core.tools.tool.builtin_tool import BuiltinTool
|
||||
from pytz import timezone as pytz_timezone
|
||||
import numexpr as ne
|
||||
|
||||
class EvaluateExpressionTool(BuiltinTool):
|
||||
def _invoke(self,
|
||||
user_id: str,
|
||||
tool_parameters: Dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
|
||||
"""
|
||||
invoke tools
|
||||
"""
|
||||
# get expression
|
||||
expression = tool_parameters.get('expression', '').strip()
|
||||
if not expression:
|
||||
return self.create_text_message('Invalid expression')
|
||||
|
||||
try:
|
||||
result = ne.evaluate(expression)
|
||||
result_str = str(result)
|
||||
except Exception as e:
|
||||
logging.exception(f'Error evaluating expression: {expression}')
|
||||
return self.create_text_message(f'Invalid expression: {expression}, error: {str(e)}')
|
||||
return self.create_text_message(f'The result of the expression "{expression}" is {result_str}')
|
@ -0,0 +1,26 @@
|
||||
identity:
|
||||
name: eval_expression
|
||||
author: Bowen Liang
|
||||
label:
|
||||
en_US: Evaluate Math Expression
|
||||
zh_Hans: 计算数学表达式
|
||||
pt_BR: Evaluate Math Expression
|
||||
description:
|
||||
human:
|
||||
en_US: A tool for evaluating an math expression, calculated locally with NumExpr.
|
||||
zh_Hans: 一个用于计算数学表达式的工具,表达式将通过NumExpr本地执行。
|
||||
pt_BR: A tool for evaluating an math expression, calculated locally with NumExpr.
|
||||
llm: A tool for evaluating an math expression.
|
||||
parameters:
|
||||
- name: expression
|
||||
type: string
|
||||
required: true
|
||||
label:
|
||||
en_US: Math Expression
|
||||
zh_Hans: 数学计算表达式
|
||||
pt_BR: Math Expression
|
||||
human_description:
|
||||
en_US: Math Expression
|
||||
zh_Hans: 数学计算表达式
|
||||
pt_BR: Math Expression
|
||||
form: llm
|
@ -64,4 +64,5 @@ httpx[socks]~=0.24.1
|
||||
matplotlib~=3.8.2
|
||||
yfinance~=0.2.35
|
||||
pydub~=0.25.1
|
||||
gmpy2~=2.1.5
|
||||
gmpy2~=2.1.5
|
||||
numexpr~=2.9.0
|
Loading…
Reference in New Issue
Block a user