Skip to content

Commit

Permalink
Go: Add code examples to demonstrate the InvokeModel and InvokeModelW…
Browse files Browse the repository at this point in the history
…ithResponseStream actions (#5679)
  • Loading branch information
DennisTraub authored Dec 4, 2023
1 parent 81f67b8 commit b41b95e
Show file tree
Hide file tree
Showing 14 changed files with 1,222 additions and 15 deletions.
101 changes: 94 additions & 7 deletions .doc_gen/metadata/bedrock-runtime_metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
# zexi 0.4.0
bedrock-runtime_Hello:
title: Hello &BR;
title_abbrev: Hello &BR;
synopsis: get started using &BR;.
category: Hello
languages:
Go:
versions:
- sdk_version: 2
github: gov2/bedrock-runtime
excerpts:
- description:
snippet_tags:
- gov2.bedrock-runtime.Hello
services:
bedrock-runtime: {InvokeModel}

bedrock-runtime_InvokeModel:
title: Invoke the specified &BR; model to run an inference
title_abbrev: Invoke a model
Expand All @@ -25,6 +42,14 @@ bedrock-runtime_InvokeClaude:
synopsis: invoke the Anthropic Claude model on &BR; to run an inference.
category:
languages:
Go:
versions:
- sdk_version: 2
github: gov2/bedrock-runtime
excerpts:
- description: Invoke the Anthropic Claude 2 foundation model.
snippet_tags:
- gov2.bedrock-runtime.InvokeClaude
Java:
versions:
- sdk_version: 2
Expand Down Expand Up @@ -75,6 +100,14 @@ bedrock-runtime_InvokeJurassic2:
synopsis: invoke the AI21 Labs Jurassic-2 model on &BR; to run an inference.
category:
languages:
Go:
versions:
- sdk_version: 2
github: gov2/bedrock-runtime
excerpts:
- description: Invoke the AI21 Labs Jurassic-2 foundation model.
snippet_tags:
- gov2.bedrock-runtime.InvokeJurassic2
Java:
versions:
- sdk_version: 2
Expand All @@ -95,7 +128,6 @@ bedrock-runtime_InvokeJurassic2:
versions:
- sdk_version: 3
github: python/example_code/bedrockruntime
sdkguide:
excerpts:
- description: Invoke the AI 21 Labs Jurassic-2 foundation model.
snippet_tags:
Expand Down Expand Up @@ -126,29 +158,36 @@ bedrock-runtime_InvokeLlama2:
synopsis: invoke the Meta Llama 2 Chat model on &BR; to run inference.
category:
languages:
Go:
versions:
- sdk_version: 2
github: gov2/bedrock-runtime
excerpts:
- description: Invoke the Meta Llama 2 Chat foundation model.
snippet_tags:
- gov2.bedrock-runtime.InvokeLlama2
Java:
versions:
- sdk_version: 2
github: javav2/example_code/bedrock-runtime
excerpts:
- description: Invoke the Meta Llama 2 foundation model.
- description: Invoke the Meta Llama 2 Chat foundation model.
snippet_tags:
- bedrock-runtime.java2.invoke_llama2.main
PHP:
versions:
- sdk_version: 3
github: php/example_code/bedrock-runtime
excerpts:
- description: Invoke the Meta Llama 2 foundation model.
- description: Invoke the Meta Llama 2 Chat foundation model.
snippet_tags:
- php.example_code.bedrock-runtime.service.invokeLlama2
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrockruntime
sdkguide:
excerpts:
- description: Invoke the Meta Llama 2 foundation model.
- description: Invoke the Meta Llama 2 Chat foundation model.
snippet_tags:
- python.example_code.bedrock-runtime.InvokeMetaLlama2
services:
Expand Down Expand Up @@ -213,26 +252,51 @@ bedrock-runtime_InvokeStableDiffusionAsync:
services:
bedrock-runtime: {InvokeModel}

bedrock-runtime_InvokeTitanImage:
title: Invoke Amazon Titan on &BR; to generate images
title_abbrev: Image generation with Amazon Titan
synopsis: invoke the Amazon Titan on &BR; to generate images.
category:
languages:
Go:
versions:
- sdk_version: 2
github: gov2/bedrock-runtime
excerpts:
- description: Invoke the Amazon Titan image generation model.
snippet_tags:
- gov2.bedrock-runtime.InvokeTitanImage
services:
bedrock-runtime: {InvokeModel}

bedrock-runtime_InvokeModelWithResponseStream:
title: Invoke Anthropic Claude on &BR; to run an inference with a response stream
title_abbrev: Invoke Anthropic Claude on &BR; and process the response stream
synopsis: invoke Anthropic Claude on &BR; to run an inference with a response stream.
category:
languages:
Go:
versions:
- sdk_version: 2
github: gov2/bedrock-runtime
excerpts:
- description: Invoke Anthropic Claude and process the response stream.
snippet_tags:
- gov2.bedrock-runtime.InvokeModelWithResponseStream
Java:
versions:
- sdk_version: 2
github: javav2/example_code/bedrock-runtime
excerpts:
- description: Invoke Anthropic Claude on &BR; and process the response stream.
- description: Invoke Anthropic Claude and process the response stream.
snippet_tags:
- bedrock-runtime.java2.invoke_model_with_response_stream.main
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-runtime
excerpts:
- description: Invoke Anthropic Claude on &BR; and process the response stream.
- description: Invoke Anthropic Claude and process the response stream.
snippet_tags:
- python.example_code.bedrock-runtime.InvokeModelWithResponseStream
services:
Expand All @@ -258,3 +322,26 @@ bedrock-runtime_Scenario_Invoke_models:
- php.example_code.bedrock-runtime.basics.scenario
services:
bedrock-runtime: {InvokeModel}

bedrock-runtime_Scenario_InvokeModelsInclResponseStream:
title: Invoke multiple foundation models on &BR;
title_abbrev: Invoke multiple foundation models on &BR;
synopsis: invoke multiple foundation models on &BR;.
synopsis_list:
- Generate text with Anthropic Claude.
- Generate text with AI21 Labs Jurassic-2.
- Generate text with Meta Llama 2 Chat.
- Asynchronously process the response stream from Anthropic Claude.
- Generate an image with the Amazon Titan Image Generator.
category: Scenarios
languages:
Go:
versions:
- sdk_version: 2
github: gov2/bedrock-runtime
excerpts:
- description: Invoke multiple foundation models on &BR;.
snippet_tags:
- gov2.bedrock-runtime.Scenario_InvokeModels
services:
bedrock-runtime: {InvokeModel, InvokeModelWithResponseStream}
1 change: 1 addition & 0 deletions gov2/bedrock-runtime/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
output/*
133 changes: 133 additions & 0 deletions gov2/bedrock-runtime/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<!--Generated by WRITEME on 2023-12-01 20:14:10.900860 (UTC)-->
# Amazon Bedrock Runtime code examples for the SDK for Go V2

## Overview

Shows how to use the AWS SDK for Go V2 to work with Amazon Bedrock Runtime.

<!--custom.overview.start-->
<!--custom.overview.end-->

*Amazon Bedrock Runtime is a fully managed service that makes it easy to use foundation models from third-party providers and Amazon.*

## ⚠ Important

* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/?aws-products-pricing.sort-by=item.additionalFields.productNameLowercase&aws-products-pricing.sort-order=asc&awsf.Free%20Tier%20Type=*all&awsf.tech-category=*all) and [Free Tier](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all).
* Running the tests might result in charges to your AWS account.
* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).

<!--custom.important.start-->
<!--custom.important.end-->

## Code examples

### Prerequisites

For prerequisites, see the [README](../README.md#Prerequisites) in the `gov2` folder.


<!--custom.prerequisites.start-->
> ⚠ You must request access to a model before you can use it. If you try to use the model (with the API or console) before you have requested access to it, you will receive an error message. For more information, see [Model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html).
<!--custom.prerequisites.end-->

### Get started

* [Hello Amazon Bedrock](hello/hello.go#L4) (`InvokeModel`)

### Single actions

Code excerpts that show you how to call individual service functions.

* [Image generation with Amazon Titan](actions/invoke_model.go#L178) (`InvokeModel`)
* [Invoke AI21 Labs Jurassic-2 on Amazon Bedrock](actions/invoke_model.go#L78) (`InvokeModel`)
* [Invoke Anthropic Claude on Amazon Bedrock](actions/invoke_model.go#L27) (`InvokeModel`)
* [Invoke Anthropic Claude on Amazon Bedrock and process the response stream](actions/invoke_model_with_response_stream.go#L30) (`InvokeModelWithResponseStream`)
* [Invoke Meta Llama 2 on Amazon Bedrock](actions/invoke_model.go#L130) (`InvokeModel`)

### Scenarios

Code examples that show you how to accomplish a specific task by calling multiple
functions within the same service.

* [Invoke multiple foundation models on Amazon Bedrock](scenarios/scenario_invoke_models.go)

## Run the examples

### Instructions



<!--custom.instructions.start-->
#### Region configuration
By default, examples are set to `us-east-1`. To specify a different region, use the `-region` flag as shown in this example:

```
go run ./hello -region=eu-central-1
```

Be aware that not all regions may support Bedrock and its models yet. Verify service availability for your region [here](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/). For available models per region, refer to the [Bedrock dashboard](https://console.aws.amazon.com/bedrock) in the AWS Management Console.
<!--custom.instructions.end-->

#### Hello Amazon Bedrock

This example shows you how to get started using Amazon Bedrock.

```
go run ./hello
```

#### Run a scenario

All scenarios can be run with the `cmd` runner. To get a list of scenarios
and to get help for running a scenario, use the following command:

```
go run ./cmd -h
```

#### Invoke multiple foundation models on Amazon Bedrock

This example shows you how to invoke multiple foundation models on Amazon Bedrock.

* Generate text with Anthropic Claude.
* Generate text with AI21 Labs Jurassic-2.
* Generate text with Meta Llama 2 Chat.
* Asynchronously process the response stream from Anthropic Claude.
* Generate an image with the Amazon Titan Image Generator.

<!--custom.scenario_prereqs.bedrock-runtime_Scenario_InvokeModelsInclResponseStream.start-->
<!--custom.scenario_prereqs.bedrock-runtime_Scenario_InvokeModelsInclResponseStream.end-->


<!--custom.scenarios.bedrock-runtime_Scenario_InvokeModelsInclResponseStream.start-->
<!--custom.scenarios.bedrock-runtime_Scenario_InvokeModelsInclResponseStream.end-->

### Tests

⚠ Running tests might result in charges to your AWS account.


To find instructions for running these tests, see the [README](../README.md#Tests)
in the `gov2` folder.



<!--custom.tests.start-->
<!--custom.tests.end-->

## Additional resources

* [Amazon Bedrock Runtime User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html)
* [Amazon Bedrock Runtime API Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html)
* [SDK for Go V2 Amazon Bedrock Runtime reference](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/bedrock-runtime)

<!--custom.resources.start-->
<!--custom.resources.end-->

---

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
Loading

0 comments on commit b41b95e

Please sign in to comment.