-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Knowledge Base RAG の追加 #479
Conversation
const client = new S3Client({}); | ||
const client = new S3Client({ | ||
region: req.region, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
別リージョンの bucket のファイルもダウンロードできるようにオプションを追加しています。
req.region は string?
なので既存の動作には影響がないはずです。
overrideSearchType: 'HYBRID', | ||
}, | ||
}, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hybrid search で 10 件取得しています。
if (dataSourceBucket) { | ||
dataSourceBucket.grantRead(getDocDownloadSignedUrlFunction); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR のコメント通り、Rag Construct 独自のファイルダウンロード API は廃止して、file.ts に寄せています。
const vectorField = | ||
props.vectorField ?? 'bedrock-knowledge-base-default-vector'; | ||
const textField = props.textField ?? 'AMAZON_BEDROCK_TEXT_CHUNK'; | ||
const metadataField = props.metadataField ?? 'AMAZON_BEDROCK_METADATA'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OpenSearch Service の index の各フィールド名は一応 props から変更可能なコードにしていますが、今のところハードコーディング以外で変更可能な口 (cdk.json など) はありません。変更するメリットがないと判断したためです。
}; | ||
|
||
return `https://s3.${region}.amazonaws.com/${groups.bucketName}/${groups.prefix}`; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
KnowledgeBase の retrieve 結果は s3://...
のようになっていて region 情報が含まれないため、https://...
の形式にした上で region 情報を付与しています。これを useFileApi が改めて parse して bucket, key, region を API に投げます。API は region が指定されている場合は AWS SDK の S3 client 初期化時に region を明示的に指定します。以上により、KnowledgeBase data source 用の S3 Bucket が別の region にあっても signed url が発行可能になります。
}) | ||
.filter((x) => x) | ||
.join('\n'); | ||
return message + '\n' + footnote; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
KnowledgeBase の retrieve 結果にはページ番号はないです
custom_kuromoji_analyzer: { | ||
type: 'custom', | ||
tokenizer: 'kuromoji_tokenizer', | ||
filter: ['kuromoji_baseform', 'lowercase', 'ja_stop'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
以下の filter を追加した方が良いかもしれません
- kuromoji_part_of_speech (品詞ごとに不要な品詞を除去するフィルター)
- デフォルトだと 除去される品詞リスト に従ってフィルターが実行されます。(ja_stop filter は品詞ごとでなく除去するワードを指定できるもので、デフォルトは除去されるワードリスト)
- 一部の品詞に限定することもでき、以下のように "助詞-格助詞-一般"、"助動詞"、"助詞-終助詞" あたりが設定されることが多いです。設定例↓
{
"settings": {
"index": {
"analysis": {
"analyzer": {
"custom_kuromoji_analyzer": {
"tokenizer": "kuromoji_tokenizer",
"filter": ["kuromoji_baseform", "custom_kuromoji_part_of_speech"]
}
},
"filter": {
"custom_kuromoji_part_of_speech": {
"type": "kuromoji_part_of_speech",
"stoptags": [
"助詞-格助詞-一般",
"助動詞",
"助詞-終助詞"
]
}
}
}
}
}
}
以下は設定しなくても良いと思いますが、参考に
- kuromoji_readingform (カタカナ、ローマ字などの各トークンの読み仮名を生成するフィルター)
- 例えば「寿司」を「スシ」や「sushi」で検索しても引っかかるようにします。ただし英語はそのまま扱われます (例えば「Bedrock」はそのまま「Bedrock」)
- kuromoji_stemmer (トークン末尾の「ー」を削除するフィルター)
- 例えば「サーバー」を「サーバ」へ変換します
- この filter は片仮名が対象のため、平仮名でも対応させるには kuromoji_readingform との併用が必要です
- ワードによっては、「ー」の有無で意味が変わる点が注意
type: 'custom', | ||
tokenizer: 'kuromoji_tokenizer', | ||
filter: ['kuromoji_baseform', 'lowercase', 'ja_stop'], | ||
char_filter: ['icu_normalizer', 'html_strip'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
日本語検索だと、以下の filter が追加されることが多いです
- kuromoji_iteration_mark (踊り字(々, ゝ, ヽ)を直前の文字で置き換えるフィルター)
- 例えば「刻々」を「刻刻」、「すゞめ」を「すずめ」に変換します
docs/DEPLOY_OPTION.md
Outdated
|
||
変更後に `npm run cdk:deploy` で再度デプロイして反映させます。この際、`cdk.json` の `modelRegion` で指定されているリージョンに Knowledge Base がデプロイされます。よって、`modelRegion` で指定しているリージョンの Bedrock で `embeddingModelId` のモデルが有効化されている必要があります。 | ||
|
||
また、`/packages/cdk/kendra-docs/docs` に保存されているデータが、自動で Knowledge Base データソース用の S3 バケットにアップロードされます。(パス名に kendra が含まれますが、Amazon Kendra はデプロイされません。) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これを機にkendra-docs
をrag-docs
に変えても良いかなと思いました!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!!
設計
主に、既存の Kendra RAG チャットとの共通化をどこまで行うかをどう判断したかについて記述します。
Knowledge Base
Knowledge Base はメインとは別の Stack でデプロイすることとしました。理由は、embedding model がデフォルトの us-east-1 や us-west-2 の方が充実しているからです。Stack のデプロイ先は modelRegion と統一しました。agentRegion のように新たなパラメータは設けていません。
S3 Bucket
Kendra との共通化が望ましいですが、上記事情により Knowledge Base 本体が別の region にいる可能性があるため、Bucket を新規作成しています。ただし、sync するディレクトリは kendra-docs のままです。(パスに kendra が含まれる件についてはドキュメントで補足しています。)
Frontend
Knowledge Base は Kendra のように検索時にクエリを単語に分解する必要がないため、retrieve の前処理が不要です。
このように根本的に実行フローが異なるため、新たに useRagKnowledgeBase という hook を作成しました。(useRag との共通化は避けました。) その結果ユースケースページの共通化も困難なため、独立しています。
ファイルダウンロード API の共通化について
従来、マークダウンのリンクが s3 の URL であれば signed url を発行して別タブで開く、という動作でした。しかし、この API が CDK の Rag Construct に定義されており、RagKnowledgeBase "だけ" を有効化した場合にダウンロードできなくなります。そこで、Markdown 用のダウンロード API をマルチモーダルで使われていたファイルのダウンロード API と統一し (もともと Lambda のコードは同じものを流用していました)、Rag もそちらを使うように変更しました。これにより RagKnowledgeBase だけを有効化した場合も S3 のリンクがクリックできるようになっています。
プロンプト
プロンプトは Kendra RAG のものを流用しています。そのため、KnowledgeBase の retrieve 結果を Kendra の retrieve response の format に合わせる処理を入れています。(この点インラインコメントで補足しています。)
検証時のメモ (レビューには不要)