generative-ts

Function createAwsBedrockModelProvider

  • Creates an AWS Bedrock ModelProvider with the provided ModelApi.

    import {
    AmazonTitanTextApi,
    createAwsBedrockModelProvider
    } from "generative-ts";

    // Bedrock supports many different APIs and models. See below for full list.
    const titanText = createAwsBedrockModelProvider({
    api: AmazonTitanTextApi,
    modelId: "amazon.titan-text-express-v1",
    // If your code is running in an AWS Environment (eg, Lambda) authorization will happen automatically. Otherwise, explicitly pass in `auth`
    });

    const response = await titanText.sendRequest({
    $prompt: "Brief history of NY Mets:"
    // all other options for the specified `api` available here
    });

    console.log(response.results[0]?.outputText);

    Compatible APIs

    Provider Setup and Notes

    In the Bedrock service in the AWS Console, use "Request Model Access" to enable access to Bedrock models.

    Authorization

    If your code is running in an AWS Environment (eg, Lambda) authorization should happen automatically. Otherwise, explicitly pass in AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to auth.

    Region is also specified in auth as AWS_REGION. If not passed, it will be read from process.env.

    Model Parameters

    Model IDs

    Type Parameters

    • TAwsBedrockApi extends AwsBedrockApi
    • THttpClientOptions = HttpClientOptions

    Parameters

    • params: {
          api: TAwsBedrockApi;
          auth?: AwsBedrockAuthConfig;
          client?: HttpClient<THttpClientOptions>;
          modelId: string;
      }
      • api: TAwsBedrockApi

        The API instance to use for making requests.

      • Optional auth?: AwsBedrockAuthConfig

        Authentication configuration for AWS. Pass AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY here if not running in an AWS environment. Pass AWS_REGION here if not set in process.env.

      • Optional client?: HttpClient<THttpClientOptions>

        HTTP client to use for requests. If not supplied, the built-in fetch-based implementation will be used.

      • modelId: string

        The model ID as defined by AWS Bedrock.

    Returns AwsBedrockModelProvider<InferRequestOptions<TAwsBedrockApi>, InferResponse<TAwsBedrockApi>, THttpClientOptions>

    The AWS Bedrock Model Provider with the specified ModelApi.

    See

    Example: Multiple APIs

    import {
    Ai21Jurassic2Api,
    AmazonTitanTextApi,
    CohereGenerateApi,
    createAwsBedrockModelProvider,
    Llama3ChatApi,
    MistralBedrockApi,
    } from "generative-ts";

    const titanText = createAwsBedrockModelProvider({
    api: AmazonTitanTextApi,
    modelId: "amazon.titan-text-express-v1",
    });

    const cohereCommand = createAwsBedrockModelProvider({
    api: CohereGenerateApi,
    modelId: "cohere.command-text-v14",
    });

    const llama3 = createAwsBedrockModelProvider({
    api: Llama3ChatApi,
    modelId: "meta.llama3-8b-instruct-v1:0",
    });

    const mistral = createAwsBedrockModelProvider({
    api: MistralBedrockApi,
    modelId: "mistral.mistral-7b-instruct-v0:2",
    });

    const jurassic = createAwsBedrockModelProvider({
    api: Ai21Jurassic2Api,
    modelId: "ai21.j2-mid-v1",
    });

    const params = { $prompt: "Brief history of NY Mets:" };

    const responses = await Promise.all([
    titanText.sendRequest(params),
    cohereCommand.sendRequest(params),
    llama3.sendRequest(params),
    mistral.sendRequest(params),
    jurassic.sendRequest(params),
    ]);