feat: add a builtin tool to support regex extraction. (#7081) (#7087)

This commit is contained in:
zhuhao 2024-08-08 14:23:57 +08:00 committed by GitHub
parent 169cde6c3c
commit 4ffa706e4f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 100 additions and 0 deletions

View File

@ -0,0 +1 @@
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1723087850395" class="icon" viewBox="0 0 1188 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="39503" xmlns:xlink="http://www.w3.org/1999/xlink" width="232.03125" height="200"><path d="M800.736395 0C906.556049 0 992.395062 92.513975 992.395062 206.569877V278.818765h-145.869432L865.975309 447.841975h-75.889778l-19.449679-169.035852H283.369877c13.520593-26.737778 21.162667-57.413531 21.162666-89.979259 0-48.159605-16.813827-79.492741-44.329086-112.829629h-22.338371c-21.168988 0-29.879309 2.932938-44.22795 19.658271-14.348642 16.731654-15.075556 32.085333-13.432099 54.771358l67.141531 793.34084h601.878123V1024H176.374519v-0.25284C78.07684 1018.424889 0 930.980346 0 823.763753L1.175704 745.876543h152.974222l-49.739852-590.127407C98.183901 71.863309 159.674469 0 237.871407 0zM160.079012 806.918321H58.228938l-0.126419 5.499259c0.12642 35.858963 13.482667 69.594074 37.692049 94.953877 20.751802 21.744198 47.344198 35.239506 76.092049 38.608592l-11.807605-139.061728zM1033.399309 524.641975c49.967407 0 71.667358 14.159012 82.267654 39.948642V529.698765H1188.345679v251.828149c0 74.840494-37.856395 122.374321-150.91358 122.374321-24.727704 0-55.517235-2.528395-77.722864-6.573828v-58.153086c21.699951 5.562469 48.45037 8.090864 72.173037 8.090864 59.050667 0 83.784691-16.687407 83.784691-68.772345v-14.664692c-11.611654 24.272593-33.311605 38.937284-82.267654 38.937284-88.329481 0-110.535111-56.13037-110.535111-139.061728 0-74.840494 22.20563-139.061728 110.535111-139.061729z m-284.703605 0c95.446914 0 117.279605 49.777778 117.279605 120.38321 0 15.739259-1.011358 30.473481-2.541037 38.090272l-176.677926 11.175506c2.534716 39.114272 25.385086 56.888889 74.119901 56.888889 33.513877 0 73.114864-9.645827 90.88316-19.297975v57.900246c-16.244938 9.652148-57.369284 19.304296-105.099061 19.304297C662.888296 809.08642 613.135802 778.100938 613.135802 666.864198S662.888296 524.641975 748.695704 524.641975zM424.005531 448.790123c95.560691 0 136.950519 30.599901 136.950518 112.210173 0 59.164444-20.947753 90.788346-66.43358 104.561778L594.17284 802.765432H502.701827l-83.297975-129.042963h-38.324148V802.765432H303.407407V448.790123z m631.258074 126.419754C1004.720988 575.209877 998.716049 615.847506 998.716049 660.037531v1.396938c0.132741 46.111605 7.003654 84.442074 56.547556 84.442074 54.044444 0 63.55121-31.49116 63.55121-85.839012 0-52.318815-9.506765-84.827654-63.55121-84.827654z m-303.470617 0c-45.814519 0-61.263012 19.879506-62.805334 63.209876l113.777778-8.666074c0-30.075259-7.205926-54.543802-50.966123-54.543802zM412.204247 512H379.259259v107.45679h30.694716C461.280395 619.45679 480.395062 609.121975 480.395062 563.661432 480.395062 520.786173 461.274074 512 409.953975 512z m372.577975-442.127802H361.370864c17.66084 33.842568 27.660642 73.007407 27.660642 114.725925 0 8.116148-0.423506 16.225975-1.169383 24.335803h517.12v-8.109827c0-34.999309-12.559802-67.90637-35.214222-92.589827-22.660741-24.683457-52.875062-38.362074-84.998321-38.362074z" fill="#000000" p-id="39504"></path></svg>

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@ -0,0 +1,19 @@
from typing import Any
from core.tools.errors import ToolProviderCredentialValidationError
from core.tools.provider.builtin.regex.tools.regex_extract import RegexExpressionTool
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController
class RegexProvider(BuiltinToolProviderController):
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
try:
RegexExpressionTool().invoke(
user_id='',
tool_parameters={
'content': '1+(2+3)*4',
'expression': r'(\d+)',
},
)
except Exception as e:
raise ToolProviderCredentialValidationError(str(e))

View File

@ -0,0 +1,15 @@
identity:
author: zhuhao
name: regex
label:
en_US: Regex
zh_Hans: 正则表达式提取
pt_BR: Regex
description:
en_US: A tool for regex extraction.
zh_Hans: 一个用于正则表达式内容提取的工具。
pt_BR: A tool for regex extraction.
icon: icon.svg
tags:
- utilities
- productivity

View File

@ -0,0 +1,27 @@
import re
from typing import Any, Union
from core.tools.entities.tool_entities import ToolInvokeMessage
from core.tools.tool.builtin_tool import BuiltinTool
class RegexExpressionTool(BuiltinTool):
def _invoke(self,
user_id: str,
tool_parameters: dict[str, Any],
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
"""
invoke tools
"""
# get expression
content = tool_parameters.get('content', '').strip()
if not content:
return self.create_text_message('Invalid content')
expression = tool_parameters.get('expression', '').strip()
if not expression:
return self.create_text_message('Invalid expression')
try:
result = re.findall(expression, content)
return self.create_text_message(str(result))
except Exception as e:
return self.create_text_message(f'Failed to extract result, error: {str(e)}')

View File

@ -0,0 +1,38 @@
identity:
name: regex_extract
author: zhuhao
label:
en_US: Regex Extract
zh_Hans: 正则表达式内容提取
pt_BR: Regex Extract
description:
human:
en_US: A tool for extracting matching content using regular expressions.
zh_Hans: 一个用于利用正则表达式提取匹配内容结果的工具。
pt_BR: A tool for extracting matching content using regular expressions.
llm: A tool for extracting matching content using regular expressions.
parameters:
- name: content
type: string
required: true
label:
en_US: Content to be extracted
zh_Hans: 内容
pt_BR: Content to be extracted
human_description:
en_US: Content to be extracted
zh_Hans: 内容
pt_BR: Content to be extracted
form: llm
- name: expression
type: string
required: true
label:
en_US: Regular expression
zh_Hans: 正则表达式
pt_BR: Regular expression
human_description:
en_US: Regular expression
zh_Hans: 正则表达式
pt_BR: Regular expression
form: llm