Skip to content

Commit

Permalink
Enable client side to control model id and region
Browse files Browse the repository at this point in the history
  • Loading branch information
ysekiy committed May 24, 2024
1 parent 2fdab56 commit 65e6d2d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .env.development-template
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
VITE_INDEX_ID=*****
# VITE_SERVER_URL=http://localhost:8080
# VITE_SERVER_URL=http://localhost:8080
VITE_MODEL_ID=anthropic.claude-3-haiku-20240307-v1:0
VITE_BEDROCK_REGION=us-west-2
17 changes: 17 additions & 0 deletions amplify/backend/awscloudformation/override.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ export function override(resources: AmplifyRootStackTemplate, amplifyProjectInfo
envContent += `\nVITE_STREAM_FUNC_NAME=streamClaude3-${envName}`;
}

// VITE_MODEL_ID の値を追加
const modelId = "anthropic.claude-3-haiku-20240307-v1:0"
const modelIdPattern = /^VITE_MODEL_ID=.*/gm;
if (modelIdPattern.test(envContent)) {
envContent = envContent.replace(modelIdPattern, `VITE_MODEL_ID=${modelId}`);
} else {
envContent += `\nVITE_MODEL_ID=${modelId}`;
}

// VITE_BEDROCK_REGION の値を追加
const bedrockRegionPattern = /^VITE_BEDROCK_REGION=.*/gm;
if (bedrockRegionPattern.test(envContent)) {
envContent = envContent.replace(bedrockRegionPattern, `VITE_BEDROCK_REGION=${region_name}`);
} else {
envContent += `\nVITE_BEDROCK_REGION=${region_name}`;
}

// .env ファイルに書き込む
fs.writeFileSync('.env', envContent);
}
8 changes: 4 additions & 4 deletions amplify/backend/function/streamClaude3/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ const {
ThrottlingException,
} = require('@aws-sdk/client-bedrock-runtime');

const client = new BedrockRuntimeClient();


const extractOutputTextClaude3Message = (body) => {
if (body.type === 'message') {
Expand All @@ -20,7 +18,9 @@ const extractOutputTextClaude3Message = (body) => {
return '';
};

async function* invokeStream(input) {
async function* invokeStream(region, input) {
const client = new BedrockRuntimeClient({ region });

try {

const command = new InvokeModelWithResponseStreamCommand(input);
Expand Down Expand Up @@ -63,7 +63,7 @@ exports.handler = awslambda.streamifyResponse(
async (event, responseStream, context) => {
context.callbackWaitsForEmptyEventLoop = false;

for await (const token of invokeStream?.(event.body) ?? []) {
for await (const token of invokeStream?.(event.body.bedrockRegion, event.body) ?? []) {
responseStream.write(token);
}
responseStream.end();
Expand Down
15 changes: 14 additions & 1 deletion src/utils/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ if (!import.meta.env.VITE_STREAM_FUNC_NAME) {
"環境変数にSTREAM_FUNC_ARNがありません"
);
}
if (!import.meta.env.VITE_MODEL_ID) {
_loadingErrors.push(
"環境変数にMODEL_IDがありません"
);
}
if (!import.meta.env.VITE_BEDROCK_REGION) {
_loadingErrors.push(
"環境変数にBEDROCK_REGIONがありません"
);
}
const hasErrors = _loadingErrors.length > 0;
if (hasErrors) {
console.error(JSON.stringify(_loadingErrors));
Expand All @@ -44,6 +54,8 @@ const stream_func_name: string = import.meta.env.VITE_STREAM_FUNC_NAME ?? ""
const local_server = import.meta.env.VITE_SERVER_URL ?? ""
const remote_server = awsconfig.aws_cloud_logic_custom[0].endpoint ?? ""
export const serverUrl: string = local_server ? local_server : remote_server;
const model_id: string = import.meta.env.VITE_MODEL_ID ?? ""
const bedrock_region: string = import.meta.env.VITE_BEDROCK_REGION ?? ""
let jwtToken = "";

Amplify.configure({
Expand Down Expand Up @@ -284,7 +296,8 @@ export async function* infStreamClaude(user_prompt: string) {
}
const req = {
"body": {
"modelId": "anthropic.claude-3-haiku-20240307-v1:0",
"bedrockRegion": bedrock_region,
"modelId": model_id,
"accept": "application/json",
"contentType": "application/json",
"body": JSON.stringify(body)
Expand Down

0 comments on commit 65e6d2d

Please sign in to comment.