+`weather_search.py` 代码模版,你可以在这里实现具体的业务逻辑。
- 红框上的为固定表单项,红框中的表单项可在 schema.json 中的 form\_schema 自定义,规范见:[.](./ "mention")
-
-### 实现代码
+> 注意:类变量 name 为自定义类型名称,需要跟目录和文件名保持一致,而且唯一。
```python
from typing import Optional
@@ -39,20 +83,30 @@ from typing import Optional
from core.external_data_tool.base import ExternalDataTool
-class YourToolNameTool(ExternalDataTool):
- name: str = "{your_tool_name}"
+class WeatherSearch(ExternalDataTool):
+ """
+ The name of custom type must be unique, keep the same with directory and file name.
+ """
+ name: str = "weather_search"
@classmethod
def validate_config(cls, tenant_id: str, config: dict) -> None:
"""
- Validate the incoming form config data.
+ schema.json validation. It will be called when user save the config.
+
+ Example:
+ .. code-block:: python
+ config = {
+ "temperature_unit": "centigrade"
+ }
:param tenant_id: the id of workspace
- :param config: the form config data
+ :param config: the variables of form config
:return:
"""
-
- # implement your own validate logic here
+
+ if not config.get('temperature_unit'):
+ raise ValueError('temperature unit is required')
def query(self, inputs: dict, query: Optional[str] = None) -> str:
"""
@@ -62,24 +116,76 @@ class YourToolNameTool(ExternalDataTool):
:param query: the query of chat app
:return: the tool query result
"""
- # TODO implement your own query logic here
-
- return ''
+ city = inputs.get('city')
+ temperature_unit = self.config.get('temperature_unit')
+
+ if temperature_unit == 'fahrenheit':
+ return f'Weather in {city} is 32°F'
+ else:
+ return f'Weather in {city} is 0°C'
```
-代码需要放置在 `api/core/external_data_tool`中,目录结构如下:
+### 4. **预览前端界面**
+
+按照上面步骤执行,运行服务即可见新增的自定义类型。
+
+
Add Tool
+
+### 5. **调试扩展**
+
+至此,即可在 Dify 应用编排界面选择自定义的 `Weather Search` 外部数据工具扩展类型进行调试。
+
+## 实现类模版
```python
-.
-└── api
- └── core
- └── external_data_tool
- ├── base.py
- ├── external_data_tool_factory.py
- ├── api
- │ ├── api.py
- │ └── schema.json
- ├── {your_tool_name}
- │ ├── {your_tool_name}.py
- │ └── schema.json
+from typing import Optional
+
+from core.external_data_tool.base import ExternalDataTool
+
+
+class WeatherSearch(ExternalDataTool):
+ """
+ The name of custom type must be unique, keep the same with directory and file name.
+ """
+ name: str = "weather_search"
+
+ @classmethod
+ def validate_config(cls, tenant_id: str, config: dict) -> None:
+ """
+ schema.json validation. It will be called when user save the config.
+
+ :param tenant_id: the id of workspace
+ :param config: the variables of form config
+ :return:
+ """
+
+ # implement your own logic here
+
+ def query(self, inputs: dict, query: Optional[str] = None) -> str:
+ """
+ Query the external data tool.
+
+ :param inputs: user inputs
+ :param query: the query of chat app
+ :return: the tool query result
+ """
+
+ # implement your own logic here
+ return "your own data."
```
+
+### 实现类开发详细介绍
+
+### def validate\_config
+
+`schema.json` 表单校验方法,当用户点击「发布」保存配置时调用
+
+* `config` 表单参数
+ * `{{variable}}` 表单自定义变量
+
+### def query
+
+用户自定义数据查询实现,返回的结果将会被替换到指定的变量。
+
+* `inputs` :终端用户传入变量值
+* `query` :终端用户当前对话输入内容,对话型应用固定参数。
diff --git a/zh_CN/advanced/extension/code_based_extension/moderation.md b/zh_CN/advanced/extension/code_based_extension/moderation.md
index 30fa178..593733c 100644
--- a/zh_CN/advanced/extension/code_based_extension/moderation.md
+++ b/zh_CN/advanced/extension/code_based_extension/moderation.md
@@ -1,12 +1,10 @@
# 敏感内容审查
-## **内容审查扩展**
-
-除了系统内置的内容审查类型,Dify 也支持用户扩展自定义实现,该方法适用于私有部署的用户定制开发,如果您愿意将扩展功能贡献给我们的话,非常欢迎给 Dify 提交 PR。
+除了系统内置的内容审查类型,Dify 也支持用户扩展自定义的内容审查规则,该方法适用于私有部署的开发者定制开发。比如企业内部客服,规定用户在查询的时候以及客服回复的时候,除了不可以输入暴力,性和非法行为等相关词语,也不能出现企业自己规定的禁词或违反内部制定的审查逻辑,那么开发者可以在私有部署的 Dify 代码层扩展自定义内容审查规则。
## 快速开始
-这里以一个 `Cloud Service` 内容审查扩展为例:大致步骤如下:
+这里以一个 `Cloud Service` 内容审查扩展为例,步骤如下:
1. 初始化目录
2. 添加前端组件定义文件
@@ -14,9 +12,7 @@
4. 预览前端界面
5. 调试扩展
-
-
-1. ### 初始化目录
+### 1. 初始化目录
新增自定义类型 `Cloud Service`,需要在 `api/core/moderation` 目录下新建相关的目录和文件。
@@ -31,11 +27,11 @@
└── schema.json
```
-2. ### 添加前端组件规范
+### 2.添加前端组件规范
-* `schema.json`,这里定义了前端组件规范,详细见 [.](./ "mention")
+* `schema.json`,这里定义了前端组件规范,详细见 [.](./ "mention") 。
-```JSON
+```json
{
"label": {
"en-US": "Cloud Service",
@@ -103,13 +99,15 @@
}
```
-3. ### 添加实现类
+### 3. 添加实现类
`cloud_service.py` 代码模版,你可以在这里实现具体的业务逻辑。
-> 注意:类变量 name 为自定义类型名称,需要跟目录和文件名保持一致,而且唯一。
+{% hint style="warning" %}
+注意:类变量 name 为自定义类型名称,需要跟目录和文件名保持一致,而且唯一。
+{% endhint %}
-```Python
+```python
from core.moderation.base import Moderation, ModerationAction, ModerationInputsResult, ModerationOutputsResult
class CloudServiceModeration(Moderation):
@@ -204,22 +202,20 @@ class CloudServiceModeration(Moderation):
return False
```
-4. ### 预览前端界面
+### 4. 预览前端界面
按照上面步骤执行,运行服务即可见新增的自定义类型。
-5. ### 调试扩展
+