SBB*_*SBB 5 amazon-web-services aws-lambda amazon-kendra
我正在使用 Kendra 和 Salesforce 开发 POC。该连接器允许我连接到我的 Salesforce 组织并索引知识文章。我已经能够进行设置,并且目前它正在按预期工作。
我想引入一些自定义字段和数据点来帮助进一步丰富数据。其中之一是附加答案/正文,其中将包含搜索的关键信息。
我的数据源中的该字段是包含 HTML 的富文本,通常大于 2048 个字符,这一限制似乎是在 Kendra 中的字符串数据字段中施加的。
我遇到了两个内置的用于预处理和后数据丰富的钩子。我的想法是,我可以使用 pre hook 来去除 HTML 标签并在字段存储在索引中之前截断该字段。
挂钩参考:https://docs.aws.amazon.com/kendra/latest/dg/API_CustomDocumentEnrichmentConfiguration.html
当前设置:
我在索引中添加了一个名为 的新字段sf_answer_preview。然后,我将数据源中的该字段映射到 Salesforce 组织中的富文本字段。
如果我按原样运行它,它将索引 1,000 篇文章中的大约 200 篇,并给出一个错误,指出其余文章超过该字段中的 2048 个字符限制,这就是我尝试设置丰富内容的原因。
我在我的数据源上设置了上述丰富内容。我指定了一个在预提取中使用的 lambda,并且没有额外的过滤,因此在每篇文章上运行它。由于我使用的是数据源,所以我不能 100% 确定 S3 存储桶的用途,但它似乎是需要的,所以我也添加了它。
对于我的 lambda,我创建了以下内容:
exports.handler = async (event) => {
// Debug
console.log(JSON.stringify(event))
// Vars
const s3Bucket = event.s3Bucket;
const s3ObjectKey = event.s3ObjectKey;
const meta = event.metadata;
// Answer
const answer = meta.attributes.find(o => o.name === 'sf_answer_preview');
// Remove HTML Tags
const removeTags = (str) => {
if ((str===null) || (str===''))
return false;
else
str = str.toString();
return str.replace( /(<([^>]+)>)/ig, '');
}
// Truncate
const truncate = (input) => input.length > 2000 ? `${input.substring(0, 2000)}...` : input;
let result = truncate(removeTags(answer.value.stringValue));
// Response
const response = {
"version" : "v0",
"s3ObjectKey": s3ObjectKey,
"metadataUpdates": [
{"name":"sf_answer_preview", "value":{"stringValue":result}}
]
}
// Debug
console.log(response)
// Response
return response
};
Run Code Online (Sandbox Code Playgroud)
根据此处描述的 lambda 合约,它看起来非常简单。我访问该事件,在数据中找到名为(来自 Salesforce 的富文本字段)的字段sf_answer_preview,然后将该值剥离并截断为 2,000 个字符。
对于响应,我告诉它将该字段更新为新格式的答案,以便它符合字段限制。
当我在 lambda 中记录数据时,预提取事件详细信息如下:
{
"s3Bucket": "kendrasfdev",
"s3ObjectKey": "pre-extraction/********/22736e62-c65e-4334-af60-8c925ef62034/https://*********.my.salesforce.com/ka1d0000000wkgVAAQ",
"metadata": {
"attributes": [
{
"name": "_document_title",
"value": {
"stringValue": "What majors are under the Exploratory track of Health and Life Sciences?"
}
},
{
"name": "sf_answer_preview",
"value": {
"stringValue": "A complete list of majors affiliated with the Exploratory Health and Life Sciences track is available <a href=\"https://cls.asu.edu/exploratory-health-and-life-sciences\" target=\"_blank\">online</a>. This track allows you to explore a variety of majors related to the health and life science professions. For more information, please visit the <a href=\"https://cls.asu.edu/exploratory\" target=\"_blank\">Exploratory program</a> description. "
}
},
{
"name": "_data_source_sync_job_execution_id",
"value": {
"stringValue": "0fbfb959-7206-4151-a2b7-fce761a46241"
}
},
]
}
}
Run Code Online (Sandbox Code Playgroud)
问题:
运行时,我仍然收到相同的字段限制错误,即内容超出字符限制。当我对原始数据运行 lambda 时,它会按预期剥离和截断它。我认为 lambda 中的响应由于某种原因没有正确地将字段值设置为新内容,并且仍然尝试直接使用来自 Salesforce 的数据,从而引发错误。
有没有人为 Kendra 设置过 lambda 之前可能知道我做错了什么?在索引之前删除 PII 信息之类的事情似乎很常见,所以我的设置一定在某个地方稍有偏差。
有什么想法吗?
小智 0
由于您仍然将富文本作为文档的元数据传递,因此字符限制仍然适用,因此文档将在 API 调用的验证步骤中失败,并且不会到达丰富步骤。解决方法是以某种方式将这些富文本字段附加到文档正文,以便您的 lambda 可以在那里访问它。但是,如果这些字段是从您的数据源为您的文档自动生成的,那么这可能并不容易。
| 归档时间: |
|
| 查看次数: |
354 次 |
| 最近记录: |