Skip to content

Commit

Permalink
Merge pull request #635 from aws-samples/fix/kendra-document-uri
Browse files Browse the repository at this point in the history
Kendra RAG の リンクの修正
  • Loading branch information
tbrand committed Aug 30, 2024
2 parents e1cdd77 + 9c89b4f commit b20f564
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
9 changes: 2 additions & 7 deletions packages/web/src/hooks/useRag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ShownMessage } from 'generative-ai-use-cases-jp';
import { findModelByModelId } from './useModel';
import { getPrompter } from '../prompts';
import { RetrieveResultItem, DocumentAttribute } from '@aws-sdk/client-kendra';
import { cleanEncode } from '../utils/URLUtils';

// 同一のドキュメントとみなす Key 値
const uniqueKeyOfItem = (item: RetrieveResultItem): string => {
Expand Down Expand Up @@ -154,13 +155,7 @@ const useRag = (id: string) => {
? `(${_excerpt_page_number} ページ)`
: ''
}](
${
item.DocumentURI
? encodeURI(item.DocumentURI)
.replace(/\(/g, '\\(')
.replace(/\)/g, '\\)')
: ''
}${
${item.DocumentURI ? cleanEncode(item.DocumentURI) : ''}${
_excerpt_page_number ? `#page=${_excerpt_page_number}` : ''
})`
: '';
Expand Down
17 changes: 17 additions & 0 deletions packages/web/src/utils/URLUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RFC3986 Encoding
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent#encoding_for_rfc3986
const encodeRFC3986URI = (uri: string) => {
return encodeURI(uri).replace(
/[!'()*]/g,
(c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`
);
};

export const cleanEncode = (uri: string): string => {
// Check if uri is encoded, and encode it if not already encoded
if (decodeURI(uri) === uri) {
return encodeRFC3986URI(uri);
} else {
return uri;
}
};

0 comments on commit b20f564

Please sign in to comment.