DEV Community

Cover image for Amplify AI Kit helps your backend work with Amazon Bedrock

Amplify AI Kit helps your backend work with Amazon Bedrock

AI Kit has been added to AWS Amplify.
This mechanism makes it easy to use Amazon Bedrock from the frontend.
However, if appropriately used, the Amplify AI Kit can be used from the back end.

In this article, I will introduce a sample of creating a function that uses AI Kit to make a blog summary from Amplify Functions, which I use to build the backend.

Setup

On your machine, please go ahead and execute the below commands.

% mkdir amplify-lambda-bedrock
% cd amplify-lambda-bedrock

% npm create amplify@latest
...
? Where should we create your project? (.) # press enter
...

%
Enter fullscreen mode Exit fullscreen mode

If you want to get more information, please check the official document.

Manual installation - JavaScript - AWS Amplify Gen 2 Documentation

Learn how to get started with AWS Amplify Gen 2 by manually installing. AWS Amplify Documentation

favicon docs.amplify.aws

Implementation

There are some points for implementation.

  • Set the generation method in the Data schema without authorization.
  • Keywords in the prompt in systemPrompt must be included in a property in arguments.
  • Within our Lambda function, we must provide a special notation for the client object to call the generation method.

I show sample codes below.

amplify/functions/post-overview/resource.ts

import { defineFunction } from "@aws-amplify/backend";

export const postOverview = defineFunction({
  name: "post-overview",
  entry: "./handler.ts",
  timeoutSeconds: 60,
  memoryMB: 128,
  runtime: 22,
  architecture: "arm64",
  environment: {
    ARTICLE_URL:
      "https://aws.amazon.com/jp/blogs/mobile/building-a-gen-ai-powered-manufacturing-search-engine-with-aws-amplify-gen-2/",
    LANGUAGE: "Japanese",
    LENGTH: "300",
  },
});
Enter fullscreen mode Exit fullscreen mode

amplify/functions/post-overview/handler.ts

import type { Handler } from "aws-lambda";

import { Amplify } from "aws-amplify";
import { generateClient } from "aws-amplify/data";
import { getAmplifyDataClientConfig } from "@aws-amplify/backend/function/runtime";
import { env } from "$amplify/env/post-overview";
import { Schema } from "../../data/resource";

const { resourceConfig, libraryOptions } = await getAmplifyDataClientConfig(
  env
);
Amplify.configure(resourceConfig, libraryOptions);

const client = generateClient<Schema>();

export const handler: Handler = async (event, context) => {
  if (!process.env.ARTICLE_URL || process.env.ARTICLE_URL.length === 0) {
    return `ARTICLE_URL is wrong value: ${process.env.ARTICLE_URL}`;
  }
  if (!process.env.LANGUAGE || process.env.LANGUAGE.length === 0) {
    return `LANGUAGE is wrong value: ${process.env.LANGUAGE}`;
  }
  if (
    !process.env.LENGTH ||
    process.env.LENGTH.length === 0 ||
    parseInt(process.env.LENGTH) <= 0
  ) {
    return `LENGTH is wrong value: ${process.env.LENGTH}`;
  }

  const { data: dataGen, errors: errorsGen } =
    await client.generations.generateSummary({
      url: process.env.ARTICLE_URL,
      language: process.env.LANGUAGE,
      length: parseInt(process.env.LENGTH),
    });
  console.log(JSON.stringify(dataGen));
  console.log(errorsGen);

  return dataGen;
};
Enter fullscreen mode Exit fullscreen mode

amplify/data/resource.ts

import { type ClientSchema, a, defineData } from "@aws-amplify/backend";
import { postOverview } from "../functions/post-overview/resource";

const schema = a
  .schema({
    generateSummary: a
      .generation({
        aiModel: a.ai.model("Claude 3 Haiku"),
        systemPrompt: `
Please summarize the key points and main topics covered in the content at the following url.
Please provide a summary answer in the language and length specified.
`,
      })
      .arguments({
        url: a.string().required(),
        language: a.string().required(),
        length: a.integer().required(),
      })
      .returns(
        a.customType({
          title: a.string(),
          summary: a.string(),
        })
      ),
  })
  .authorization((allow) => [allow.resource(postOverview)]);

export type Schema = ClientSchema<typeof schema>;

export const data = defineData({
  schema,
  authorizationModes: {
    defaultAuthorizationMode: "iam",
  },
});
Enter fullscreen mode Exit fullscreen mode

amplify/backend.ts

import { defineBackend } from "@aws-amplify/backend";
import { data } from "./data/resource";
import { postOverview } from "./functions/post-overview/resource";

defineBackend({
  data,
  postOverview,
});
Enter fullscreen mode Exit fullscreen mode

All sample codes are in this repository.

GitHub logo tacck / amplify-lambda-ai-kit-bedrock

Sample for Amplify AI Kit (Bedrock) called by Lambda functions

Try

You can try this sample in an Amplify project.

Open the Amplify Console and click "Create new app".
Open the Amplify Console and click

Select "GitHub" and click "Next".
Select

Select your repository and branch, and click "Next".
Select your repository and branch, and click

Click "Next".
Click

Click "Save and deploy".
Click

Wait a few minutes for the project to deploy, then click the "main" environment.
Wait a few minutes for the project to deploy, then click the

Click the left side menu "Functions" and click a function that name started "amplify-".
Click the left side menu

Click "View in Lambda".
Click

Click the "Test" tab and click "Test".
Click the

Click "Details", and you can see the result that summarized and translated the blog.
Click

Please modify the URL, language, and length, and check your results!

Top comments (0)