Skip to content

Commit b471f03

Browse files
committed
v1.0.6 bedrock_retrieveツールにガードレール指定機能を追加
1 parent ee062d5 commit b471f03

11 files changed

Lines changed: 320 additions & 193 deletions

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# my_aws_tools
22

3-
**Author:** r3-yamauchi
4-
**Version:** 1.0.5
3+
**Author:** r3-yamauchi
4+
**Version:** 1.0.6
55
**Type:** tool
66

77
English | [Japanese](https://github.com/r3-yamauchi/dify-my-aws-tools-plugin/blob/main/readme/README_ja_JP.md)
@@ -55,7 +55,7 @@ This project is distributed under the Apache License 2.0. See `LICENSE` for the
5555

5656
### Amazon Bedrock
5757

58-
- **Bedrock Retrieve** – Calls the `bedrock-agent-runtime` Retrieve API to run semantic or hybrid searches against a selected Knowledge Base. You can switch metadata filters, result counts, and Bedrock Reranking models (cohere.rerank-v3-5 / amazon.rerank-v1), and receive outputs as JSON or ranked text.
58+
- **Bedrock Retrieve** – Calls the `bedrock-agent-runtime` Retrieve API to run semantic or hybrid searches against a selected Knowledge Base. You can switch metadata filters, result counts, Bedrock Reranking models (cohere.rerank-v3-5 / amazon.rerank-v1), and apply optional Guardrails via `guardrail_id` / `guardrail_version`, and receive outputs as JSON or ranked text.
5959

6060
```json
6161
{
@@ -84,17 +84,17 @@ This project is distributed under the Apache License 2.0. See `LICENSE` for the
8484

8585
- **Apply Guardrail** – Uses Bedrock Runtime `apply_guardrail` with these features:
8686
- Inputs: `content` array (multiple texts and/or images via bytes or S3 URI) or a single `text` that is auto-chunked into 1000-character pieces and wrapped as content.
87-
- `source`: PREPROCESS (default) or POSTPROCESS to target pre/post LLM stages.
87+
- `source`: INPUT (default) or OUTPUT to target pre/post LLM stages.
8888
- Outputs: action, processedOutputs (masked), outputs (raw), assessments, warnings/actionReasons; returned as human-readable text plus a JSON blob.
8989
- Long-text protection via chunking aligned with Guardrails billing/limits.
9090

9191
#### Apply Guardrail example (multi-text)
9292

9393
```json
9494
{
95-
"guardrail_id": "gr-123",
96-
"guardrail_version": "2",
97-
"source": "PREPROCESS",
95+
"guardrail_id": "ab1cd2e3f45g",
96+
"guardrail_version": "1",
97+
"source": "INPUT",
9898
"content": [
9999
{ "text": { "text": "User message 1" } },
100100
{ "text": { "text": "User message 2" } }
@@ -106,9 +106,9 @@ This project is distributed under the Apache License 2.0. See `LICENSE` for the
106106

107107
```json
108108
{
109-
"guardrail_id": "gr-123",
109+
"guardrail_id": "ab1cd2e3f45g",
110110
"guardrail_version": "2",
111-
"source": "POSTPROCESS",
111+
"source": "OUTPUT",
112112
"content": [
113113
{
114114
"image": {

manifest.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 1.0.5
1+
version: 1.0.6
22
type: plugin
33
author: r3-yamauchi
44
name: my_aws_tools

readme/README_ja_JP.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# my_aws_tools
22

33
**Author:** r3-yamauchi
4-
**Version:** 1.0.5
4+
**Version:** 1.0.6
55
**Type:** tool
66

77
英語版ドキュメントはリポジトリ直下の `README.md` を参照してください。
@@ -14,7 +14,7 @@
1414

1515
## 概要
1616

17-
My AWS Tools プラグインは、複数の AWS サービスに基づくツールセットを提供し、Dify アプリケーションの内部から AWS の機能を直接活用できるようにします。コンテンツモデレーション、テキストのリランク、テキスト読み上げ、音声認識など、幅広い機能領域をカバーします
17+
このツール・プラグインは、いくつかの AWS サービスに基づくツールセットを提供し、Dify アプリケーションの中で AWS の機能を直接活用できるようにします。
1818

1919
含まれるツール:
2020
- Apply Guardrail
@@ -51,7 +51,7 @@ My AWS Tools プラグインは、複数の AWS サービスに基づくツー
5151

5252
### Amazon Bedrock 系
5353

54-
- **Bedrock Retrieve**: `bedrock-agent-runtime` の Retrieve API を直接呼び出し、指定 Knowledge Base に対してセマンティックまたは HYBRID 検索を実行します。メタデータフィルタ、検索件数、Bedrock Reranking (cohere.rerank-v3-5 や amazon.rerank-v1) を切り替えられ、結果は JSON あるいは順位付きテキストで取得できます。
54+
- **Bedrock Retrieve**: `bedrock-agent-runtime` の Retrieve API を直接呼び出し、指定 Knowledge Base に対してセマンティックまたは HYBRID 検索を実行します。メタデータフィルタ、検索件数、Bedrock Reranking (cohere.rerank-v3-5 や amazon.rerank-v1) を切り替えられ、`guardrail_id` / `guardrail_version` で Bedrock ガードレールを任意適用し、結果は JSON あるいは順位付きテキストで取得できます。
5555

5656
```json
5757
{
@@ -80,17 +80,17 @@ My AWS Tools プラグインは、複数の AWS サービスに基づくツー
8080

8181
- **Apply Guardrail**: Bedrock Runtime の `apply_guardrail` を呼び出し、以下の特徴を持ちます。
8282
- 入力: `content` 配列(テキスト複数・画像 bytes/S3 URI)または単一 `text`(1000 文字ごとに自動分割し content 化)。
83-
- `source`: PREPROCESS(既定)/POSTPROCESS を指定可能
83+
- `source`: 入力(既定) に適用するか、 出力 に適用するかを指定可能
8484
- 出力: action、processedOutputs(マスク後)、outputs(マスク前)、assessments、warnings/actionReasons をテキストと JSON blob で返却。
8585
- チャンク分割で長文の課金/サイズ上限に対応。
8686

8787
#### Apply Guardrail サンプルリクエスト (複数テキスト)
8888

8989
```json
9090
{
91-
"guardrail_id": "gr-123",
92-
"guardrail_version": "2",
93-
"source": "PREPROCESS",
91+
"guardrail_id": "ab1cd2e3f45g",
92+
"guardrail_version": "1",
93+
"source": "入力",
9494
"content": [
9595
{ "text": { "text": "ユーザーの入力テキスト1" } },
9696
{ "text": { "text": "ユーザーの入力テキスト2" } }
@@ -102,9 +102,9 @@ My AWS Tools プラグインは、複数の AWS サービスに基づくツー
102102

103103
```json
104104
{
105-
"guardrail_id": "gr-123",
105+
"guardrail_id": "ab1cd2e3f45g",
106106
"guardrail_version": "2",
107-
"source": "POSTPROCESS",
107+
"source": "出力",
108108
"content": [
109109
{
110110
"image": {

requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
dify_plugin>=0.4.2,<0.5.0
2-
boto3>=1.40.45
3-
Pillow>=11.3.0
4-
bedrock-agentcore>=1.0.6
2+
boto3>=1.41.3
3+
Pillow>=12.0.0
4+
bedrock-agentcore>=1.1.1

tools/apply_guardrail.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,24 @@ def _one_of_text_or_image(cls, v, values):
5555

5656
class GuardrailParameters(BaseModel):
5757
guardrail_id: str = Field(..., description="The identifier of the guardrail")
58-
guardrail_version: str = Field(..., description="The version of the guardrail")
59-
source: str = Field("PREPROCESS", description="PREPROCESS or POSTPROCESS or custom source")
58+
guardrail_version: str = Field(
59+
"DRAFT",
60+
description="The version of the guardrail. Defaults to DRAFT when omitted.",
61+
)
62+
source: str = Field("INPUT", description="INPUT or OUTPUT")
6063
text: Optional[str] = Field(None, description="Text to apply the guardrail to (legacy single input)")
6164
content: Optional[list[ContentItem]] = Field(None, description="List of content items (text/image)")
6265
aws_region: Optional[str] = Field(None, description="AWS region for the Bedrock client")
6366

67+
@validator("guardrail_version", pre=True, always=True)
68+
def _default_guardrail_version(cls, value: Optional[str]) -> str:
69+
"""guardrail_version が空や未指定の場合は DRAFT を適用する。"""
70+
if value is None:
71+
return "DRAFT"
72+
if isinstance(value, str) and value.strip() == "":
73+
return "DRAFT"
74+
return value
75+
6476

6577
class ApplyGuardrailTool(Tool):
6678
def _invoke(
@@ -134,6 +146,10 @@ def _invoke(
134146
"warnings": warnings,
135147
"actionReasons": action_reasons,
136148
}
149+
150+
# JSON 形式でも返却し、ワークフロー内で扱いやすくする
151+
yield self.create_json_message(structured_payload)
152+
137153
yield self.create_blob_message(
138154
blob=json.dumps(structured_payload, ensure_ascii=False).encode("utf-8"),
139155
meta={"mime_type": "application/json"},

tools/apply_guardrail.yaml

Lines changed: 71 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,108 +4,121 @@ identity:
44
label:
55
en_US: Content Moderation Guardrails
66
zh_Hans: 内容审查护栏
7-
ja_JP: コンテンツモデレーションガードレール
7+
ja_JP: ガードレールによるコンテンツモデレーション
88
description:
99
human:
1010
en_US: "Content Moderation Guardrails utilizes the ApplyGuardrail API, a feature of Guardrails for Amazon Bedrock. This API is capable of evaluating input prompts and model responses for all Foundation Models (FMs), including those on Amazon Bedrock, custom FMs, and third-party FMs. By implementing this functionality, organizations can achieve centralized governance across all their generative AI applications, thereby enhancing control and consistency in content moderation."
1111
zh_Hans: "内容审查护栏采用 Guardrails for Amazon Bedrock 功能中的 ApplyGuardrail API 。ApplyGuardrail 可以评估所有基础模型(FMs)的输入提示和模型响应,包括 Amazon Bedrock 上的 FMs、自定义 FMs 和第三方 FMs。通过实施这一功能, 组织可以在所有生成式 AI 应用程序中实现集中化的治理,从而增强内容审核的控制力和一致性。"
1212
ja_JP: "Amazon Bedrock Guardrails の ApplyGuardrail API を利用し、ユーザー入力やモデル出力を横断的に審査できるコンテンツモデレーションツールです。Bedrock 標準モデルやカスタム/サードパーティ FM を統合的に管理し、生成 AI アプリ全体の統制と一貫性を高めます。"
1313
llm: "Content Moderation Guardrails utilizes the ApplyGuardrail API, a feature of Guardrails for Amazon Bedrock. This API is capable of evaluating input prompts and model responses for all Foundation Models (FMs), including those on Amazon Bedrock, custom FMs, and third-party FMs. By implementing this functionality, organizations can achieve centralized governance across all their generative AI applications, thereby enhancing control and consistency in content moderation."
1414
parameters:
15-
- name: aws_access_key_id
16-
type: string
17-
required: false
18-
label:
19-
en_US: AWS Access Key ID
20-
ja_JP: AWS アクセスキー ID
21-
human_description:
22-
en_US: Override the provider Access Key ID for this tool if needed.
23-
ja_JP: 必要に応じてプロバイダー設定を上書きする Access Key ID。
24-
form: form
25-
26-
- name: aws_secret_access_key
27-
type: string
28-
required: false
29-
label:
30-
en_US: AWS Secret Access Key
31-
ja_JP: AWS シークレットアクセスキー
32-
human_description:
33-
en_US: Override the provider Secret Access Key for this tool if needed.
34-
ja_JP: 必要に応じてプロバイダー設定を上書きするシークレットキー。
35-
form: form
36-
37-
- name: aws_region
38-
type: string
39-
required: false
40-
label:
41-
en_US: AWS Region
42-
ja_JP: AWS リージョン
43-
human_description:
44-
en_US: Override the default AWS Region for this tool.
45-
ja_JP: このツール固有の AWS リージョンを指定します。
46-
form: form
47-
4815
- name: guardrail_id
4916
type: string
5017
required: true
5118
label:
5219
en_US: Guardrail ID
5320
zh_Hans: Guardrail ID
54-
ja_JP: ガードレール ID
21+
ja_JP: "Amazon Bedrock ガードレール の ID"
5522
human_description:
56-
en_US: Please enter the ID of the Guardrail that has already been created on Amazon Bedrock, for example 'qk5nk0e4b77b'.
57-
zh_Hans: 请输入已经在 Amazon Bedrock 上创建好的 Guardrail ID, 例如 'qk5nk0e4b77b'.
58-
ja_JP: "Amazon Bedrock で作成済みの Guardrail ID(例: qk5nk0e4b77b)を入力してください。"
59-
llm_description: Please enter the ID of the Guardrail that has already been created on Amazon Bedrock, for example 'qk5nk0e4b77b'.
23+
en_US: Please enter the ID of the Guardrail that has already been created on Amazon Bedrock, for example 'ab1cd2e3f45g'.
24+
zh_Hans: 请输入已经在 Amazon Bedrock 上创建好的 Guardrail ID, 例如 'ab1cd2e3f45g'.
25+
ja_JP: "Amazon Bedrock で作成済みの Guardrail ID(例: ab1cd2e3f45g)を入力してください。"
26+
llm_description: Please enter the ID of the Guardrail that has already been created on Amazon Bedrock, for example 'ab1cd2e3f45g'.
6027
form: form
28+
6129
- name: guardrail_version
6230
type: string
63-
required: true
31+
required: false
6432
label:
6533
en_US: Guardrail Version Number
6634
zh_Hans: Guardrail 版本号码
67-
ja_JP: ガードレールのバージョン
35+
ja_JP: "適用するガードレールのバージョン"
6836
human_description:
69-
en_US: Please enter the published version of the Guardrail ID that has already been created on Amazon Bedrock. This is typically a version number, such as 2.
70-
zh_Hans: 请输入已经在Amazon Bedrock 上创建好的Guardrail ID发布的版本, 通常使用版本号, 例如2.
71-
ja_JP: "対象 Guardrail の公開バージョン番号(例: 2)を入力してください。"
72-
llm_description: Please enter the published version of the Guardrail ID that has already been created on Amazon Bedrock. This is typically a version number, such as 2.
37+
en_US: "Guardrail version (numeric or DRAFT). Defaults to DRAFT when omitted."
38+
zh_Hans: "Guardrail 版本(数字或 DRAFT)。未指定时使用 DRAFT。"
39+
ja_JP: "対象 Guardrail の公開バージョン番号(例: 1)または DRAFT。未指定時は DRAFT を使用"
40+
llm_description: "Guardrail version (numeric or DRAFT). Defaults to DRAFT when omitted."
7341
form: form
42+
7443
- name: source
75-
type: string
44+
type: select
7645
required: false
46+
default: INPUT
47+
options:
48+
- value: INPUT
49+
label:
50+
en_US: INPUT
51+
ja_JP: 入力
52+
- value: OUTPUT
53+
label:
54+
en_US: OUTPUT
55+
ja_JP: 出力
7756
label:
78-
en_US: Guardrail stage (PREPROCESS/POSTPROCESS)
79-
ja_JP: ガードレール適用段階(PREPROCESS/POSTPROCESS)
57+
en_US: "Guardrail stage (INPUT/OUTPUT)"
58+
ja_JP: "ガードレールを適用する段階"
8059
human_description:
81-
en_US: Stage to apply the guardrail. Default PREPROCESS. POSTPROCESS can be used for LLM outputs.
82-
ja_JP: ガードレールを適用する段階。デフォルト PREPROCESS。LLM 出力に適用する場合は POSTPROCESS を指定。
83-
llm_description: Stage to apply the guardrail. Default PREPROCESS. POSTPROCESS for post-response checks.
60+
en_US: Stage to apply the guardrail. Default INPUT. OUTPUT can be used for LLM outputs.
61+
ja_JP: "ガードレールを適用する段階。LLM 出力に対して適用する場合は 出力 を指定してください"
62+
llm_description: Stage to apply the guardrail. Default INPUT. OUTPUT for post-response checks.
8463
form: form
8564

8665
- name: text
8766
type: string
8867
required: false
8968
label:
90-
en_US: Text to be reviewed (single input)
91-
ja_JP: 審査対象テキスト(単一入力)
69+
en_US: "Text to be reviewed (single input)"
70+
ja_JP: "審査対象テキスト(単一入力)"
9271
human_description:
93-
en_US: Optional single text. If provided, it will be chunked by 1000 chars and sent as content array.
94-
ja_JP: 単一テキスト入力(任意)。指定すると 1000 文字ごとに分割され content 配列として送信されます。
72+
en_US: "Optional single text. If provided, it will be chunked by 1000 chars and sent as content array."
73+
ja_JP: "単一テキスト入力(任意) 1000文字ごとに分割され content 配列として送信されます。"
9574
llm_description: Optional single text to review. Will be chunked and wrapped into the content array.
9675
form: llm
9776

9877
- name: content
9978
type: string
10079
required: false
10180
label:
102-
en_US: Content array (JSON)
103-
ja_JP: content 配列(JSON)
81+
en_US: "Content array (JSON)"
82+
ja_JP: "content 配列(JSON)"
10483
human_description:
10584
en_US: "JSON array of items: {\"text\": {\"text\": \"...\"}} or {\"image\": {\"format\": \"png|jpeg\", \"source\": {\"bytes\": \"...\" | \"s3Uri\": \"s3://...\"}}}. Use this for multiple texts or images."
106-
ja_JP: "複数テキスト・画像を送る場合の JSON 配列。例 {\"text\":{\"text\":\"...\"}} または {\"image\":{\"format\":\"png|jpeg\",\"source\":{\"bytes\":\"...\" または \"s3Uri\":\"s3://...\"}}}"
85+
ja_JP: "複数テキストや画像を送る場合の JSON 配列。例 {\"text\":{\"text\":\"...\"}} または {\"image\":{\"format\":\"png|jpeg\",\"source\":{\"bytes\":\"...\" または \"s3Uri\":\"s3://...\"}}}"
10786
llm_description: JSON array for multi text/image inputs. See human description for shape.
10887
form: llm
88+
89+
- name: aws_access_key_id
90+
type: string
91+
required: false
92+
label:
93+
en_US: AWS Access Key ID
94+
ja_JP: AWS アクセスキー ID
95+
human_description:
96+
en_US: Override the provider Access Key ID for this tool if needed.
97+
ja_JP: 必要に応じてプロバイダー設定を上書きする Access Key ID。
98+
form: form
99+
100+
- name: aws_secret_access_key
101+
type: string
102+
required: false
103+
label:
104+
en_US: AWS Secret Access Key
105+
ja_JP: AWS シークレットアクセスキー
106+
human_description:
107+
en_US: Override the provider Secret Access Key for this tool if needed.
108+
ja_JP: 必要に応じてプロバイダー設定を上書きするシークレットキー。
109+
form: form
110+
111+
- name: aws_region
112+
type: string
113+
required: false
114+
label:
115+
en_US: AWS Region
116+
ja_JP: AWS リージョン
117+
human_description:
118+
en_US: Override the default AWS Region for this tool.
119+
ja_JP: このツール固有の AWS リージョンを指定します。
120+
form: form
121+
109122
extra:
110123
python:
111124
source: tools/apply_guardrail.py

0 commit comments

Comments
 (0)