Skip to content

Commit cc0b92b

Browse files
ybalbert001Yuanbo Li
and
Yuanbo Li
authored
Update aws tools (#11174)
Co-authored-by: Yuanbo Li <[email protected]>
1 parent e576d32 commit cc0b92b

6 files changed

+668
-4
lines changed

api/core/tools/provider/builtin/aws/tools/lambda_translate_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class LambdaTranslateUtilsTool(BuiltinTool):
1212

1313
def _invoke_lambda(self, text_content, src_lang, dest_lang, model_id, dictionary_name, request_type, lambda_name):
1414
msg = {
15-
"src_content": text_content,
15+
"src_contents": [text_content],
1616
"src_lang": src_lang,
1717
"dest_lang": dest_lang,
1818
"dictionary_id": dictionary_name,

api/core/tools/provider/builtin/aws/tools/lambda_translate_utils.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ identity:
88
icon: icon.svg
99
description:
1010
human:
11-
en_US: A util tools for LLM translation, extra deployment is needed on AWS. Please refer Github Repo - https://github.com/ybalbert001/dynamodb-rag
12-
zh_Hans: 大语言模型翻译工具(专词映射获取),需要在AWS上进行额外部署,可参考Github Repo - https://github.com/ybalbert001/dynamodb-rag
13-
pt_BR: A util tools for LLM translation, specific Lambda Function deployment is needed on AWS. Please refer Github Repo - https://github.com/ybalbert001/dynamodb-rag
11+
en_US: A util tools for LLM translation, extra deployment is needed on AWS. Please refer Github Repo - https://github.com/aws-samples/rag-based-translation-with-dynamodb-and-bedrock
12+
zh_Hans: 大语言模型翻译工具(专词映射获取),需要在AWS上进行额外部署,可参考Github Repo - https://github.com/aws-samples/rag-based-translation-with-dynamodb-and-bedrock
13+
pt_BR: A util tools for LLM translation, specific Lambda Function deployment is needed on AWS. Please refer Github Repo - https://github.com/aws-samples/rag-based-translation-with-dynamodb-and-bedrock
1414
llm: A util tools for translation.
1515
parameters:
1616
- name: text_content
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import json
2+
from typing import Any, Union
3+
4+
import boto3
5+
6+
from core.tools.entities.tool_entities import ToolInvokeMessage
7+
from core.tools.tool.builtin_tool import BuiltinTool
8+
9+
# 定义标签映射
10+
LABEL_MAPPING = {"LABEL_0": "SAFE", "LABEL_1": "NO_SAFE"}
11+
12+
13+
class ContentModerationTool(BuiltinTool):
14+
sagemaker_client: Any = None
15+
sagemaker_endpoint: str = None
16+
17+
def _invoke_sagemaker(self, payload: dict, endpoint: str):
18+
response = self.sagemaker_client.invoke_endpoint(
19+
EndpointName=endpoint,
20+
Body=json.dumps(payload),
21+
ContentType="application/json",
22+
)
23+
# Parse response
24+
response_body = response["Body"].read().decode("utf8")
25+
26+
json_obj = json.loads(response_body)
27+
28+
# Handle nested JSON if present
29+
if isinstance(json_obj, dict) and "body" in json_obj:
30+
body_content = json.loads(json_obj["body"])
31+
raw_label = body_content.get("label")
32+
else:
33+
raw_label = json_obj.get("label")
34+
35+
# 映射标签并返回
36+
result = LABEL_MAPPING.get(raw_label, "NO_SAFE") # 如果映射中没有找到,默认返回NO_SAFE
37+
return result
38+
39+
def _invoke(
40+
self,
41+
user_id: str,
42+
tool_parameters: dict[str, Any],
43+
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
44+
"""
45+
invoke tools
46+
"""
47+
try:
48+
if not self.sagemaker_client:
49+
aws_region = tool_parameters.get("aws_region")
50+
if aws_region:
51+
self.sagemaker_client = boto3.client("sagemaker-runtime", region_name=aws_region)
52+
else:
53+
self.sagemaker_client = boto3.client("sagemaker-runtime")
54+
55+
if not self.sagemaker_endpoint:
56+
self.sagemaker_endpoint = tool_parameters.get("sagemaker_endpoint")
57+
58+
content_text = tool_parameters.get("content_text")
59+
60+
payload = {"text": content_text}
61+
62+
result = self._invoke_sagemaker(payload, self.sagemaker_endpoint)
63+
64+
return self.create_text_message(text=result)
65+
66+
except Exception as e:
67+
return self.create_text_message(f"Exception {str(e)}")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
identity:
2+
name: chinese_toxicity_detector
3+
author: AWS
4+
label:
5+
en_US: Chinese Toxicity Detector
6+
zh_Hans: 中文有害内容检测
7+
icon: icon.svg
8+
description:
9+
human:
10+
en_US: A tool to detect Chinese toxicity
11+
zh_Hans: 检测中文有害内容的工具
12+
llm: A tool that checks if Chinese content is safe for work
13+
parameters:
14+
- name: sagemaker_endpoint
15+
type: string
16+
required: true
17+
label:
18+
en_US: sagemaker endpoint for moderation
19+
zh_Hans: 内容审核的SageMaker端点
20+
human_description:
21+
en_US: sagemaker endpoint for content moderation
22+
zh_Hans: 内容审核的SageMaker端点
23+
llm_description: sagemaker endpoint for content moderation
24+
form: form
25+
- name: content_text
26+
type: string
27+
required: true
28+
label:
29+
en_US: content text
30+
zh_Hans: 待审核文本
31+
human_description:
32+
en_US: text content to be moderated
33+
zh_Hans: 需要审核的文本内容
34+
llm_description: text content to be moderated
35+
form: llm
36+
- name: aws_region
37+
type: string
38+
required: false
39+
label:
40+
en_US: region of sagemaker endpoint
41+
zh_Hans: SageMaker 端点所在的region
42+
human_description:
43+
en_US: region of sagemaker endpoint
44+
zh_Hans: SageMaker 端点所在的region
45+
llm_description: region of sagemaker endpoint
46+
form: form

0 commit comments

Comments
 (0)