Skip to content

Commit

Permalink
split common
Browse files Browse the repository at this point in the history
  • Loading branch information
maekawataiki committed Dec 6, 2024
1 parent a1e5d49 commit 3361502
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 101 deletions.
2 changes: 0 additions & 2 deletions docs/DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export VITE_APP_SELF_SIGN_UP_ENABLED=<Self Signup Flag>
export VITE_APP_MODEL_REGION=<Bedrock/SageMakerモデルのリージョン>
export VITE_APP_MODEL_IDS=<Bedrock モデルの JSON Array>
export VITE_APP_IMAGE_MODEL_IDS=<Bedrock 画像生成モデルの JSON Array>
export VITE_APP_MODEL_FEATURE_FLAGS=<Bedrock モデルの Feature Flag JSON>
export VITE_APP_ENDPOINT_NAMES=<SageMaker モデルの JSON Array>
export VITE_APP_SAMLAUTH_ENABLED=<SAML 認証 Flag>
export VITE_APP_SAML_COGNITO_DOMAIN_NAME=<SAML Cognito Domain>
Expand All @@ -68,7 +67,6 @@ export VITE_APP_SELF_SIGN_UP_ENABLED=true
export VITE_APP_MODEL_REGION=us-west-2
export VITE_APP_MODEL_IDS=["anthropic.claude-instant-v1","anthropic.claude-v2"]
export VITE_APP_IMAGE_MODEL_IDS=["stability.stable-diffusion-xl-v1","amazon.titan-image-generator-v1"]
export VITE_APP_MODEL_FEATURE_FLAGS=[...]
export VITE_APP_ENDPOINT_NAMES=[]
export VITE_APP_SAMLAUTH_ENABLED=true
export VITE_APP_SAML_COGNITO_DOMAIN_NAME=your-preferred-name.auth.ap-northeast-1.amazoncognito.com
Expand Down
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 2 additions & 84 deletions packages/cdk/lib/construct/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
BucketEncryption,
HttpMethods,
} from 'aws-cdk-lib/aws-s3';
import { Agent, AgentMap, FeatureFlags } from 'generative-ai-use-cases-jp';
import { Agent, AgentMap } from 'generative-ai-use-cases-jp';
import { modelFeatureFlags } from '@generative-ai-use-cases-jp/common';

export interface BackendApiProps {
userPool: UserPool;
Expand All @@ -38,7 +39,6 @@ export class Api extends Construct {
readonly optimizePromptFunction: NodejsFunction;
readonly modelRegion: string;
readonly modelIds: string[];
readonly modelFeatureFlags: Record<string, FeatureFlags>;
readonly imageGenerationModelIds: string[];
readonly endpointNames: string[];
readonly agentNames: string[];
Expand Down Expand Up @@ -70,87 +70,6 @@ export class Api extends Construct {
...(this.node.tryGetContext('agents') || []),
];

// Manage Model Feature
// https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference-supported-models-features.html
const MODEL_FEATURE: Record<string, FeatureFlags> = {
TEXT_ONLY: { text: true, doc: false, image: false, video: false },
TEXT_DOC: { text: true, doc: true, image: false, video: false },
TEXT_DOC_IMAGE: { text: true, doc: true, image: true, video: false },
TEXT_DOC_IMAGE_VIDEO: { text: true, doc: true, image: true, video: true },
IMAGE_GEN: { image_gen: true },
VIDEO_GEN: { video_gen: true },
};
const modelFeatureFlags: Record<string, FeatureFlags> = {
// Anthropic
'anthropic.claude-3-5-sonnet-20241022-v2:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'anthropic.claude-3-5-haiku-20241022-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'anthropic.claude-3-5-sonnet-20240620-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'anthropic.claude-3-opus-20240229-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'anthropic.claude-3-sonnet-20240229-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'anthropic.claude-3-haiku-20240307-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'us.anthropic.claude-3-5-sonnet-20241022-v2:0':
MODEL_FEATURE.TEXT_DOC_IMAGE,
'us.anthropic.claude-3-5-haiku-20241022-v1:0':
MODEL_FEATURE.TEXT_DOC_IMAGE,
'us.anthropic.claude-3-5-sonnet-20240620-v1:0':
MODEL_FEATURE.TEXT_DOC_IMAGE,
'us.anthropic.claude-3-opus-20240229-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'us.anthropic.claude-3-sonnet-20240229-v1:0':
MODEL_FEATURE.TEXT_DOC_IMAGE,
'us.anthropic.claude-3-haiku-20240307-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'eu.anthropic.claude-3-5-sonnet-20240620-v1:0':
MODEL_FEATURE.TEXT_DOC_IMAGE,
'eu.anthropic.claude-3-sonnet-20240229-v1:0':
MODEL_FEATURE.TEXT_DOC_IMAGE,
'eu.anthropic.claude-3-haiku-20240307-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'apac.anthropic.claude-3-5-sonnet-20240620-v1:0':
MODEL_FEATURE.TEXT_DOC_IMAGE,
'apac.anthropic.claude-3-sonnet-20240229-v1:0':
MODEL_FEATURE.TEXT_DOC_IMAGE,
'apac.anthropic.claude-3-haiku-20240307-v1:0':
MODEL_FEATURE.TEXT_DOC_IMAGE,
'anthropic.claude-v2:1': MODEL_FEATURE.TEXT_DOC,
'anthropic.claude-v2': MODEL_FEATURE.TEXT_DOC,
'anthropic.claude-instant-v1': MODEL_FEATURE.TEXT_DOC,
// Amazon Titan
'amazon.titan-text-express-v1': MODEL_FEATURE.TEXT_DOC,
'amazon.titan-text-premier-v1:0': MODEL_FEATURE.TEXT_ONLY,
// Meta
'meta.llama3-8b-instruct-v1:0': MODEL_FEATURE.TEXT_DOC,
'meta.llama3-70b-instruct-v1:0': MODEL_FEATURE.TEXT_DOC,
'meta.llama3-1-8b-instruct-v1:0': MODEL_FEATURE.TEXT_DOC,
'meta.llama3-1-70b-instruct-v1:0': MODEL_FEATURE.TEXT_DOC,
'meta.llama3-1-405b-instruct-v1:0': MODEL_FEATURE.TEXT_DOC,
'us.meta.llama3-2-1b-instruct-v1:0': MODEL_FEATURE.TEXT_DOC,
'us.meta.llama3-2-3b-instruct-v1:0': MODEL_FEATURE.TEXT_DOC,
'us.meta.llama3-2-11b-instruct-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'us.meta.llama3-2-90b-instruct-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
// Mistral
'mistral.mistral-7b-instruct-v0:2': MODEL_FEATURE.TEXT_DOC,
'mistral.mixtral-8x7b-instruct-v0:1': MODEL_FEATURE.TEXT_DOC,
'mistral.mistral-small-2402-v1:0': MODEL_FEATURE.TEXT_ONLY,
'mistral.mistral-large-2402-v1:0': MODEL_FEATURE.TEXT_DOC,
'mistral.mistral-large-2407-v1:0': MODEL_FEATURE.TEXT_DOC,
// Cohere
'cohere.command-r-v1:0': MODEL_FEATURE.TEXT_DOC,
'cohere.command-r-plus-v1:0': MODEL_FEATURE.TEXT_DOC,
// Amazon Nova
'amazon.nova-pro-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE_VIDEO,
'amazon.nova-lite-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE_VIDEO,
'amazon.nova-micro-v1:0': MODEL_FEATURE.TEXT_ONLY,
'us.amazon.nova-pro-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE_VIDEO,
'us.amazon.nova-lite-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE_VIDEO,
'us.amazon.nova-micro-v1:0': MODEL_FEATURE.TEXT_ONLY,
// Stability AI Image Gen
'stability.stable-diffusion-xl-v1': MODEL_FEATURE.IMAGE_GEN,
'stability.sd3-large-v1:0': MODEL_FEATURE.IMAGE_GEN,
'stability.stable-image-core-v1:0': MODEL_FEATURE.IMAGE_GEN,
'stability.stable-image-ultra-v1:0': MODEL_FEATURE.IMAGE_GEN,
// Amazon Image Gen
'amazon.titan-image-generator-v2:0': MODEL_FEATURE.IMAGE_GEN,
'amazon.titan-image-generator-v1': MODEL_FEATURE.IMAGE_GEN,
};

// Validate Model Names
const supportedModelIds = Object.keys(modelFeatureFlags);
for (const modelId of modelIds) {
Expand Down Expand Up @@ -833,7 +752,6 @@ export class Api extends Construct {
this.modelRegion = modelRegion;
this.modelIds = modelIds;
this.imageGenerationModelIds = imageGenerationModelIds;
this.modelFeatureFlags = modelFeatureFlags;
this.endpointNames = endpointNames;
this.agentNames = Object.keys(agentMap);
this.fileBucket = fileBucket;
Expand Down
4 changes: 1 addition & 3 deletions packages/cdk/lib/construct/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as s3 from 'aws-cdk-lib/aws-s3';
import { ARecord, HostedZone, RecordTarget } from 'aws-cdk-lib/aws-route53';
import { CloudFrontTarget } from 'aws-cdk-lib/aws-route53-targets';
import { ICertificate } from 'aws-cdk-lib/aws-certificatemanager';
import { FeatureFlags, PromptFlow } from 'generative-ai-use-cases-jp';
import { PromptFlow } from 'generative-ai-use-cases-jp';

export interface WebProps {
apiEndpointUrl: string;
Expand All @@ -28,7 +28,6 @@ export interface WebProps {
webAclId?: string;
modelRegion: string;
modelIds: string[];
modelFeatureFlags: Record<string, FeatureFlags>;
imageGenerationModelIds: string[];
endpointNames: string[];
samlAuthEnabled: boolean;
Expand Down Expand Up @@ -179,7 +178,6 @@ export class Web extends Construct {
VITE_APP_MODEL_REGION: props.modelRegion,
VITE_APP_MODEL_IDS: JSON.stringify(props.modelIds),
VITE_APP_IMAGE_MODEL_IDS: JSON.stringify(props.imageGenerationModelIds),
VITE_APP_MODEL_FEATURE_FLAGS: JSON.stringify(props.modelFeatureFlags),
VITE_APP_ENDPOINT_NAMES: JSON.stringify(props.endpointNames),
VITE_APP_SAMLAUTH_ENABLED: props.samlAuthEnabled.toString(),
VITE_APP_SAML_COGNITO_DOMAIN_NAME:
Expand Down
5 changes: 0 additions & 5 deletions packages/cdk/lib/generative-ai-use-cases-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ export class GenerativeAiUseCasesStack extends Stack {
webAclId: props.webAclId,
modelRegion: api.modelRegion,
modelIds: api.modelIds,
modelFeatureFlags: api.modelFeatureFlags,
imageGenerationModelIds: api.imageGenerationModelIds,
endpointNames: api.endpointNames,
samlAuthEnabled,
Expand Down Expand Up @@ -281,10 +280,6 @@ export class GenerativeAiUseCasesStack extends Stack {
value: JSON.stringify(api.imageGenerationModelIds),
});

new CfnOutput(this, 'ModelFeatureFlags', {
value: JSON.stringify(api.modelFeatureFlags),
});

new CfnOutput(this, 'EndpointNames', {
value: JSON.stringify(api.endpointNames),
});
Expand Down
2 changes: 1 addition & 1 deletion packages/cdk/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"typeRoots": [
"./node_modules/@types"
"../../node_modules/@types"
],
"types": [
"node"
Expand Down
6 changes: 6 additions & 0 deletions packages/common/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@generative-ai-use-cases-jp/common",
"version": "1.0.0",
"private": true,
"main": "src/index.ts"
}
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './model';
74 changes: 74 additions & 0 deletions packages/common/src/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { FeatureFlags } from 'generative-ai-use-cases-jp';

// Manage Model Feature
// https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference-supported-models-features.html
const MODEL_FEATURE: Record<string, FeatureFlags> = {
TEXT_ONLY: { text: true, doc: false, image: false, video: false },
TEXT_DOC: { text: true, doc: true, image: false, video: false },
TEXT_DOC_IMAGE: { text: true, doc: true, image: true, video: false },
TEXT_DOC_IMAGE_VIDEO: { text: true, doc: true, image: true, video: true },
IMAGE_GEN: { image_gen: true },
VIDEO_GEN: { video_gen: true },
};
export const modelFeatureFlags: Record<string, FeatureFlags> = {
// Anthropic
'anthropic.claude-3-5-sonnet-20241022-v2:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'anthropic.claude-3-5-haiku-20241022-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'anthropic.claude-3-5-sonnet-20240620-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'anthropic.claude-3-opus-20240229-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'anthropic.claude-3-sonnet-20240229-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'anthropic.claude-3-haiku-20240307-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'us.anthropic.claude-3-5-sonnet-20241022-v2:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'us.anthropic.claude-3-5-haiku-20241022-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'us.anthropic.claude-3-5-sonnet-20240620-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'us.anthropic.claude-3-opus-20240229-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'us.anthropic.claude-3-sonnet-20240229-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'us.anthropic.claude-3-haiku-20240307-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'eu.anthropic.claude-3-5-sonnet-20240620-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'eu.anthropic.claude-3-sonnet-20240229-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'eu.anthropic.claude-3-haiku-20240307-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'apac.anthropic.claude-3-5-sonnet-20240620-v1:0':
MODEL_FEATURE.TEXT_DOC_IMAGE,
'apac.anthropic.claude-3-sonnet-20240229-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'apac.anthropic.claude-3-haiku-20240307-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'anthropic.claude-v2:1': MODEL_FEATURE.TEXT_DOC,
'anthropic.claude-v2': MODEL_FEATURE.TEXT_DOC,
'anthropic.claude-instant-v1': MODEL_FEATURE.TEXT_DOC,
// Amazon Titan
'amazon.titan-text-express-v1': MODEL_FEATURE.TEXT_DOC,
'amazon.titan-text-premier-v1:0': MODEL_FEATURE.TEXT_ONLY,
// Meta
'meta.llama3-8b-instruct-v1:0': MODEL_FEATURE.TEXT_DOC,
'meta.llama3-70b-instruct-v1:0': MODEL_FEATURE.TEXT_DOC,
'meta.llama3-1-8b-instruct-v1:0': MODEL_FEATURE.TEXT_DOC,
'meta.llama3-1-70b-instruct-v1:0': MODEL_FEATURE.TEXT_DOC,
'meta.llama3-1-405b-instruct-v1:0': MODEL_FEATURE.TEXT_DOC,
'us.meta.llama3-2-1b-instruct-v1:0': MODEL_FEATURE.TEXT_DOC,
'us.meta.llama3-2-3b-instruct-v1:0': MODEL_FEATURE.TEXT_DOC,
'us.meta.llama3-2-11b-instruct-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
'us.meta.llama3-2-90b-instruct-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE,
// Mistral
'mistral.mistral-7b-instruct-v0:2': MODEL_FEATURE.TEXT_DOC,
'mistral.mixtral-8x7b-instruct-v0:1': MODEL_FEATURE.TEXT_DOC,
'mistral.mistral-small-2402-v1:0': MODEL_FEATURE.TEXT_ONLY,
'mistral.mistral-large-2402-v1:0': MODEL_FEATURE.TEXT_DOC,
'mistral.mistral-large-2407-v1:0': MODEL_FEATURE.TEXT_DOC,
// Cohere
'cohere.command-r-v1:0': MODEL_FEATURE.TEXT_DOC,
'cohere.command-r-plus-v1:0': MODEL_FEATURE.TEXT_DOC,
// Amazon Nova
'amazon.nova-pro-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE_VIDEO,
'amazon.nova-lite-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE_VIDEO,
'amazon.nova-micro-v1:0': MODEL_FEATURE.TEXT_ONLY,
'us.amazon.nova-pro-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE_VIDEO,
'us.amazon.nova-lite-v1:0': MODEL_FEATURE.TEXT_DOC_IMAGE_VIDEO,
'us.amazon.nova-micro-v1:0': MODEL_FEATURE.TEXT_ONLY,
// Stability AI Image Gen
'stability.stable-diffusion-xl-v1': MODEL_FEATURE.IMAGE_GEN,
'stability.sd3-large-v1:0': MODEL_FEATURE.IMAGE_GEN,
'stability.stable-image-core-v1:0': MODEL_FEATURE.IMAGE_GEN,
'stability.stable-image-ultra-v1:0': MODEL_FEATURE.IMAGE_GEN,
// Amazon Image Gen
'amazon.titan-image-generator-v2:0': MODEL_FEATURE.IMAGE_GEN,
'amazon.titan-image-generator-v1': MODEL_FEATURE.IMAGE_GEN,
};
12 changes: 12 additions & 0 deletions packages/common/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"rootDir": "src",
"composite": true,
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"skipLibCheck": true
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}
6 changes: 2 additions & 4 deletions packages/web/src/hooks/useModel.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { FeatureFlags, Model } from 'generative-ai-use-cases-jp';
import { Model } from 'generative-ai-use-cases-jp';
import { modelFeatureFlags } from '@generative-ai-use-cases-jp/common';

const modelRegion = import.meta.env.VITE_APP_MODEL_REGION;

// 環境変数からモデル名などを取得
const bedrockModelIds: string[] = JSON.parse(import.meta.env.VITE_APP_MODEL_IDS)
.map((name: string) => name.trim())
.filter((name: string) => name);
const modelFeatureFlags: Record<string, FeatureFlags> = JSON.parse(
import.meta.env.VITE_APP_MODEL_FEATURE_FLAGS
);
const visionModelIds: string[] = bedrockModelIds.filter(
(modelId) => modelFeatureFlags[modelId].image
);
Expand Down
1 change: 0 additions & 1 deletion packages/web/src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ interface ImportMetaEnv {
readonly VITE_APP_MODEL_REGION: string;
readonly VITE_APP_MODEL_IDS: string;
readonly VITE_APP_IMAGE_MODEL_IDS: string;
readonly VITE_APP_MODEL_FEATURE_FLAGS: string;
readonly VITE_APP_ENDPOINT_NAMES: string;
readonly VITE_APP_SAMLAUTH_ENABLED: string;
readonly VITE_APP_SAML_COGNITO_DOMAIN_NAME: string;
Expand Down
1 change: 0 additions & 1 deletion setup-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export VITE_APP_SELF_SIGN_UP_ENABLED=`extract_value "$stack_output" SelfSignUpEn
export VITE_APP_MODEL_REGION=`extract_value "$stack_output" ModelRegion`
export VITE_APP_MODEL_IDS=`extract_value "$stack_output" ModelIds`
export VITE_APP_IMAGE_MODEL_IDS=`extract_value "$stack_output" ImageGenerateModelIds`
export VITE_APP_MODEL_FEATURE_FLAGS=`extract_value "$stack_output" ModelFeatureFlags`
export VITE_APP_ENDPOINT_NAMES=`extract_value "$stack_output" EndpointNames`
export VITE_APP_SAMLAUTH_ENABLED=`extract_value "$stack_output" SamlAuthEnabled`
export VITE_APP_SAML_COGNITO_DOMAIN_NAME=`extract_value "$stack_output" SamlCognitoDomainName`
Expand Down

0 comments on commit 3361502

Please sign in to comment.