Skip to content

Latest commit

 

History

History
132 lines (103 loc) · 4.79 KB

File metadata and controls

132 lines (103 loc) · 4.79 KB

@aigne/bedrock

AIGNE Logo

GitHub star chart Open Issues codecov NPM Version Elastic-2.0 licensed

AIGNE AWS Bedrock SDK for integrating with AWS foundation models within the AIGNE Framework.

Introduction

@aigne/bedrock provides a seamless integration between the AIGNE Framework and AWS Bedrock foundation models. This package enables developers to easily leverage various AI models available through AWS Bedrock in their AIGNE applications, providing a consistent interface across the framework while taking advantage of AWS's secure and scalable infrastructure.

AIGNE Arch

Features

  • AWS Bedrock Integration: Direct connection to AWS Bedrock services using the official AWS SDK
  • Multiple Model Support: Access to Claude, Llama, Titan, and other foundation models through AWS Bedrock
  • Chat Completions: Support for chat completions API with all available Bedrock models
  • Streaming Responses: Support for streaming responses for more responsive applications
  • Type-Safe: Comprehensive TypeScript typings for all APIs and models
  • Consistent Interface: Compatible with the AIGNE Framework's model interface
  • Error Handling: Robust error handling and retry mechanisms
  • Full Configuration: Extensive configuration options for fine-tuning behavior

Installation

Using npm

npm install @aigne/bedrock @aigne/core

Using yarn

yarn add @aigne/bedrock @aigne/core

Using pnpm

pnpm add @aigne/bedrock @aigne/core

Basic Usage

import { BedrockChatModel } from "@aigne/bedrock";

const model = new BedrockChatModel({
  // Provide API key directly or use environment variable AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
  accessKeyId: "YOUR_ACCESS_KEY_ID",
  secretAccessKey: "YOUR_SECRET_ACCESS_KEY",
  model: "us.amazon.nova-premier-v1:0",
  modelOptions: {
    temperature: 0.7,
  },
});

const result = await model.invoke({
  messages: [{ role: "user", content: "Hello, who are you?" }],
});

console.log(result);
/* Output:
  {
    text: "Hello! How can I assist you today?",
    model: "gpt-4o",
    usage: {
      inputTokens: 10,
      outputTokens: 9
    }
  }
  */

Streaming Responses

import { BedrockChatModel } from "@aigne/bedrock";
import { isAgentResponseDelta } from "@aigne/core";

const model = new BedrockChatModel({
  // Provide API key directly or use environment variable AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
  accessKeyId: "YOUR_ACCESS_KEY_ID",
  secretAccessKey: "YOUR_SECRET_ACCESS_KEY",
  model: "us.amazon.nova-premier-v1:0",
  modelOptions: {
    temperature: 0.7,
  },
});

const stream = await model.invoke(
  {
    messages: [{ role: "user", content: "Hello, who are you?" }],
  },
  { streaming: true },
);

let fullText = "";
const json = {};

for await (const chunk of stream) {
  if (isAgentResponseDelta(chunk)) {
    const text = chunk.delta.text?.text;
    if (text) fullText += text;
    if (chunk.delta.json) Object.assign(json, chunk.delta.json);
  }
}

console.log(fullText); // Output: "Hello! How can I assist you today?"
console.log(json); // { model: "gpt-4o", usage: { inputTokens: 10, outputTokens: 9 } }

License

Elastic-2.0