Skip to content

Commit

Permalink
make file-upload feature optional
Browse files Browse the repository at this point in the history
  • Loading branch information
gteu committed Feb 14, 2024
1 parent 75d8f7a commit 9951d53
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 26 deletions.
15 changes: 15 additions & 0 deletions docs/DEPLOY_OPTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,18 @@ context の `dashboard` に `true` を設定します。(デフォルトは `fal
続いて、Amazon Bedrock のログの出力を設定します。[Amazon Bedrock の Settings](https://console.aws.amazon.com/bedrock/home#settings) を開き、Model invocation logging を有効化します。Select the logging destinations には CloudWatch Logs only を選択してください。(S3 にも出力したい場合、Both S3 and CloudWatch Logs を選択しても構いません。) また、Log group name には `npm run cdk:deploy` 時に出力された `GenerativeAiUseCasesDashboardStack.BedrockLogGroup` を指定してください。(例: `GenerativeAiUseCasesDashboardStack-LogGroupAAAAAAAA-BBBBBBBBBBBB`) Service role は任意の名前で新規に作成してください。なお、Model invocation logging の設定は、context で `modelRegion` として指定しているリージョンで行うことに留意してください。

設定完了後、`npm run cdk:deploy` 時に出力された `GenerativeAiUseCasesDashboardStack.DashboardUrl` を開いてください。

## ファイルアップロード機能の有効化

PDF や Excel などのファイルをアップロードしてテキストを抽出する、ファイルアップロード機能を利用することができます。対応しているファイルは、csv, doc, docx, md, pdf, ppt, pptx, tsv, xlsx です。

**[packages/cdk/cdk.json](/packages/cdk/cdk.json) を編集**
```
{
"context": {
"recognizeFileEnabled": true
}
}
```

ファイルアップロード機能は ECS (Fargate) 上で実行されます。そのため、有効化すると VPC が新たに作成されます。
6 changes: 3 additions & 3 deletions packages/cdk/cdk.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
"agentRegion": "us-east-1",
"searchAgentEnabled": false,
"searchApiKey": "",
"agents": [
],
"agents": [],
"allowedIpV4AddressRanges": null,
"allowedIpV6AddressRanges": null,
"allowedCountryCodes": null,
"dashboard": false,
"anonymousUsageTracking": true,
"recognizeFileEnabled": false,
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
"@aws-cdk/core:checkSecretUsage": true,
"@aws-cdk/core:target-partitions": [
Expand Down Expand Up @@ -81,4 +81,4 @@
"@aws-cdk/core:includePrefixInUniqueNameGeneration": true,
"@aws-cdk/aws-opensearchservice:enableOpensearchMultiAzWithStandby": true
}
}
}
2 changes: 1 addition & 1 deletion packages/cdk/lib/construct/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export * from './rag';
export * from './transcribe';
export * from './common-web-acl';
export * from './agent';
export * from './file';
export * from './recognize-file';
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Duration, RemovalPolicy } from 'aws-cdk-lib';
import {
AuthorizationType,
CfnMethod,
CfnStage,
CognitoUserPoolsAuthorizer,
ConnectionType,
Integration,
Expand Down Expand Up @@ -30,13 +32,13 @@ import { LogGroup } from 'aws-cdk-lib/aws-logs';
import { NetworkLoadBalancedFargateService } from 'aws-cdk-lib/aws-ecs-patterns';
import { ServiceLinkedRole } from 'upsert-slr';

export interface FileProps {
export interface RecognizeFileProps {
userPool: UserPool;
api: RestApi;
}

export class File extends Construct {
constructor(scope: Construct, id: string, props: FileProps) {
export class RecognizeFile extends Construct {
constructor(scope: Construct, id: string, props: RecognizeFileProps) {
super(scope, id);

// S3 (File Bucket)
Expand Down Expand Up @@ -124,6 +126,7 @@ export class File extends Construct {
memoryLimitMiB: 1024,
cpu: 512,
taskDefinition: taskDefinition,
publicLoadBalancer: false,
}
);

Expand All @@ -138,6 +141,11 @@ export class File extends Construct {
});

// API Gateway
const stage = props.api.deploymentStage.node.defaultChild as CfnStage;
stage.variables = {
vpcLinkId: link.vpcLinkId,
};

const authorizer = new CognitoUserPoolsAuthorizer(this, 'Authorizer', {
cognitoUserPools: [props.userPool],
});
Expand All @@ -150,7 +158,7 @@ export class File extends Construct {
const fileResource = props.api.root.addResource('file');

// POST: /file/recognize
fileResource.addResource('recognize').addMethod(
const recognizeMethod = fileResource.addResource('recognize').addMethod(
'POST',
new Integration({
type: IntegrationType.HTTP_PROXY,
Expand All @@ -163,6 +171,16 @@ export class File extends Construct {
commonAuthorizerProps
);

// REST API の Method で VPC Link を参照すると、正しく削除できない問題がある。
// ステージ変数を利用するとその問題を回避できるため、ConnectionId (ここでは VPC Link ID) をステージ変数経由で使うように変更している。
// (L2 Construct では ConnectionId を設定できないため、L1 Construct のプロパティを直接変更している)
// 参考: https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/api-gateway-known-issues.html
const cfnRecognizeMethod = recognizeMethod.node.defaultChild as CfnMethod;
cfnRecognizeMethod.addPropertyOverride(
'Integration.ConnectionId',
'${stageVariables.vpcLinkId}'
);

// POST: /file/url
fileResource
.addResource('url')
Expand Down
8 changes: 6 additions & 2 deletions packages/cdk/lib/construct/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface WebProps {
samlCognitoDomainName: string;
samlCognitoFederatedIdentityProviderName: string;
agentNames: string[];
recognizeFileEnabled: boolean;
}

export class Web extends Construct {
Expand Down Expand Up @@ -117,9 +118,12 @@ export class Web extends Construct {
VITE_APP_IMAGE_MODEL_IDS: JSON.stringify(props.imageGenerationModelIds),
VITE_APP_ENDPOINT_NAMES: JSON.stringify(props.endpointNames),
VITE_APP_SAMLAUTH_ENABLED: props.samlAuthEnabled.toString(),
VITE_APP_SAML_COGNITO_DOMAIN_NAME: props.samlCognitoDomainName.toString(),
VITE_APP_SAML_COGNITO_FEDERATED_IDENTITY_PROVIDER_NAME: props.samlCognitoFederatedIdentityProviderName.toString(),
VITE_APP_SAML_COGNITO_DOMAIN_NAME:
props.samlCognitoDomainName.toString(),
VITE_APP_SAML_COGNITO_FEDERATED_IDENTITY_PROVIDER_NAME:
props.samlCognitoFederatedIdentityProviderName.toString(),
VITE_APP_AGENT_NAMES: JSON.stringify(props.agentNames),
VITE_APP_RECOGNIZE_FILE_ENABLED: props.recognizeFileEnabled.toString(),
},
});

Expand Down
24 changes: 19 additions & 5 deletions packages/cdk/lib/generative-ai-use-cases-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Rag,
Transcribe,
CommonWebAcl,
File,
RecognizeFile,
} from './construct';
import { CfnWebACLAssociation } from 'aws-cdk-lib/aws-wafv2';
import * as cognito from 'aws-cdk-lib/aws-cognito';
Expand Down Expand Up @@ -53,6 +53,9 @@ export class GenerativeAiUseCasesStack extends Stack {
const samlCognitoFederatedIdentityProviderName: string =
this.node.tryGetContext('samlCognitoFederatedIdentityProviderName')!;
const agentEnabled = this.node.tryGetContext('agentEnabled') || false;
const recognizeFileEnabled: boolean = this.node.tryGetContext(
'recognizeFileEnabled'
)!;

if (typeof ragEnabled !== 'boolean') {
throw new Error(errorMessageForBooleanContext('ragEnabled'));
Expand All @@ -66,6 +69,10 @@ export class GenerativeAiUseCasesStack extends Stack {
throw new Error(errorMessageForBooleanContext('samlAuthEnabled'));
}

if (typeof recognizeFileEnabled !== 'boolean') {
throw new Error(errorMessageForBooleanContext('recognizeFileEnabled'));
}

const auth = new Auth(this, 'Auth', {
selfSignUpEnabled,
allowedSignUpEmailDomains,
Expand Down Expand Up @@ -117,6 +124,7 @@ export class GenerativeAiUseCasesStack extends Stack {
samlCognitoDomainName,
samlCognitoFederatedIdentityProviderName,
agentNames: api.agentNames,
recognizeFileEnabled,
});

if (ragEnabled) {
Expand All @@ -132,10 +140,12 @@ export class GenerativeAiUseCasesStack extends Stack {
api: api.api,
});

new File(this, 'File', {
userPool: auth.userPool,
api: api.api,
});
if (recognizeFileEnabled) {
new RecognizeFile(this, 'RecognizeFile', {
userPool: auth.userPool,
api: api.api,
});
}

new CfnOutput(this, 'Region', {
value: this.region,
Expand Down Expand Up @@ -205,6 +215,10 @@ export class GenerativeAiUseCasesStack extends Stack {
value: JSON.stringify(api.agentNames),
});

new CfnOutput(this, 'RecognizeFileEnabled', {
value: recognizeFileEnabled.toString(),
});

this.userPool = auth.userPool;
this.userPoolClient = auth.client;
}
Expand Down
16 changes: 10 additions & 6 deletions packages/web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import useInterUseCases from './hooks/useInterUseCases';

const ragEnabled: boolean = import.meta.env.VITE_APP_RAG_ENABLED === 'true';
const agentEnabled: boolean = import.meta.env.VITE_APP_AGENT_ENABLED === 'true';
const recognizeFileEnabled: boolean =
import.meta.env.VITE_APP_RECOGNIZE_FILE_ENABLED === 'true';

const items: ItemProps[] = [
{
Expand Down Expand Up @@ -107,12 +109,14 @@ const items: ItemProps[] = [
icon: <PiSpeakerHighBold />,
display: 'tool' as const,
},
{
label: 'ファイルアップロード',
to: '/file',
icon: <PiUploadSimple />,
display: 'tool' as const,
},
recognizeFileEnabled
? {
label: 'ファイルアップロード',
to: '/file',
icon: <PiUploadSimple />,
display: 'tool' as const,
}
: null,
ragEnabled
? {
label: 'Kendra 検索',
Expand Down
12 changes: 8 additions & 4 deletions packages/web/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const ragEnabled: boolean = import.meta.env.VITE_APP_RAG_ENABLED === 'true';
const samlAuthEnabled: boolean =
import.meta.env.VITE_APP_SAMLAUTH_ENABLED === 'true';
const agentEnabled: boolean = import.meta.env.VITE_APP_AGENT_ENABLED === 'true';
const recognizeFileEnabled: boolean =
import.meta.env.VITE_APP_RECOGNIZE_FILE_ENABLED === 'true';

const routes: RouteObject[] = [
{
Expand Down Expand Up @@ -79,10 +81,12 @@ const routes: RouteObject[] = [
path: '/transcribe',
element: <TranscribePage />,
},
{
path: '/file',
element: <FileUploadPage />,
},
recognizeFileEnabled
? {
path: '/file',
element: <FileUploadPage />,
}
: null,
ragEnabled
? {
path: '/rag',
Expand Down
8 changes: 7 additions & 1 deletion packages/web/src/pages/Setting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { PiGithubLogoFill, PiArrowSquareOut } from 'react-icons/pi';

const ragEnabled: boolean = import.meta.env.VITE_APP_RAG_ENABLED === 'true';
const agentEnabled: boolean = import.meta.env.VITE_APP_AGENT_ENABLED === 'true';
const recognizeFileEnabled: boolean =
import.meta.env.VITE_APP_RECOGNIZE_FILE_ENABLED === 'true';

const SettingItem = (props: {
name: string;
Expand Down Expand Up @@ -45,7 +47,7 @@ const Setting = () => {

return (
<div>
<div className="invisible my-0 flex h-0 items-center justify-center text-xl font-semibold print:visible print:my-5 print:h-min lg:visible lg:my-5 lg:h-min">
<div className="invisible my-0 flex h-0 items-center justify-center text-xl font-semibold lg:visible lg:my-5 lg:h-min print:visible print:my-5 print:h-min">
設定情報
</div>

Expand Down Expand Up @@ -74,6 +76,10 @@ const Setting = () => {
/>
<SettingItem name="RAG 有効" value={ragEnabled.toString()} />
<SettingItem name="Agent 有効" value={agentEnabled.toString()} />
<SettingItem
name="ファイルアップロード 有効"
value={recognizeFileEnabled.toString()}
/>
</div>

<div className="my-3 flex justify-center font-semibold">生成 AI</div>
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface ImportMetaEnv {
readonly VITE_APP_IMAGE_MODEL_IDS: string;
readonly VITE_APP_ENDPOINT_NAMES: string;
readonly VITE_APP_AGENT_NAMES: string;
readonly VITE_APP_RECOGNIZE_FILE_ENABLED: string;
}

interface ImportMeta {
Expand Down
1 change: 1 addition & 0 deletions setup-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ export VITE_APP_SAMLAUTH_ENABLED=`stack_output SamlAuthEnabled`
export VITE_APP_SAML_COGNITO_DOMAIN_NAME=`stack_output SamlCognitoDomainName`
export VITE_APP_SAML_COGNITO_FEDERATED_IDENTITY_PROVIDER_NAME=`stack_output SamlCognitoFederatedIdentityProviderName`
export VITE_APP_AGENT_NAMES=`stack_output AgentNames`
export VITE_APP_RECOGNIZE_FILE_ENABLED=`stack_output RecognizeFileEnabled`

0 comments on commit 9951d53

Please sign in to comment.