Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go: Add code examples to demonstrate the InvokeModel and InvokeModelWithResponseStream actions #5679

Merged
merged 63 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
d05bc1a
Invoke Claude
DennisTraub Nov 16, 2023
da8fc8c
Add hello example
DennisTraub Nov 16, 2023
939f08b
Add invoke models scenario
DennisTraub Nov 16, 2023
5a6fb43
Add Jurassic-2
DennisTraub Nov 16, 2023
a71055b
Add Llama 2
DennisTraub Nov 16, 2023
cc1f5cc
Some documentation
DennisTraub Nov 16, 2023
bfab506
Add variant with reponse stream
DennisTraub Nov 16, 2023
9a752ad
Fix test
DennisTraub Nov 16, 2023
29f6cf0
Add test for Jurassic-2
DennisTraub Nov 16, 2023
f771603
Add test for Llama2
DennisTraub Nov 16, 2023
b9a9224
Rgenerate readme
DennisTraub Nov 16, 2023
3b24903
Run validation
DennisTraub Nov 16, 2023
acb57a8
Clean up metadata and regenerate readmes
DennisTraub Nov 16, 2023
aa1abb9
Some corrections
DennisTraub Nov 16, 2023
aa02ae1
Invoke Claude
DennisTraub Nov 16, 2023
e60087f
Add hello example
DennisTraub Nov 16, 2023
6cf95bc
Add invoke models scenario
DennisTraub Nov 16, 2023
d0ae3ab
Fix test
DennisTraub Nov 16, 2023
a64057e
Add test for Jurassic-2
DennisTraub Nov 16, 2023
fab4493
Fix metadata and regenerate readme
DennisTraub Nov 23, 2023
0bf4d89
Small fix
DennisTraub Nov 28, 2023
61f5f02
Remove go.mod from subdirectories
DennisTraub Nov 28, 2023
20719d8
Regenerate readmes
DennisTraub Nov 28, 2023
0608723
Add region flag to hello
DennisTraub Nov 28, 2023
a336192
Add region flag and error handling
DennisTraub Nov 28, 2023
ffd2683
Add custom instructions to readme
DennisTraub Nov 28, 2023
372b2bd
Add region configuration
DennisTraub Nov 28, 2023
26a5758
Add Amazon Titan image generation
DennisTraub Dec 1, 2023
d18c246
Add Amazon Titan image generation
DennisTraub Dec 1, 2023
d529ba1
Add metadata and regenerate Readme
DennisTraub Dec 1, 2023
5cdd9cb
Invoke Claude
DennisTraub Nov 16, 2023
bf700c9
Add hello example
DennisTraub Nov 16, 2023
e0d309d
Add invoke models scenario
DennisTraub Nov 16, 2023
7b0f2d3
Add Jurassic-2
DennisTraub Nov 16, 2023
218ac7d
Add Llama 2
DennisTraub Nov 16, 2023
20af1e1
Some documentation
DennisTraub Nov 16, 2023
903e198
Add variant with reponse stream
DennisTraub Nov 16, 2023
d5f5369
Fix test
DennisTraub Nov 16, 2023
f0828a8
Add test for Jurassic-2
DennisTraub Nov 16, 2023
0e15ee0
Add test for Llama2
DennisTraub Nov 16, 2023
1519b43
Rgenerate readme
DennisTraub Nov 16, 2023
539c0dc
Run validation
DennisTraub Nov 16, 2023
3df3d93
Clean up metadata and regenerate readmes
DennisTraub Nov 16, 2023
f6a3946
Some corrections
DennisTraub Nov 16, 2023
5c8adb4
Invoke Claude
DennisTraub Nov 16, 2023
0b88f08
Add hello example
DennisTraub Nov 16, 2023
fdf232a
Add invoke models scenario
DennisTraub Nov 16, 2023
82c6c42
Fix test
DennisTraub Nov 16, 2023
6be2065
Add test for Jurassic-2
DennisTraub Nov 16, 2023
82e74a7
Fix metadata and regenerate readme
DennisTraub Nov 23, 2023
845be92
Small fix
DennisTraub Nov 28, 2023
80ba330
Remove go.mod from subdirectories
DennisTraub Nov 28, 2023
41a9d61
Regenerate readmes
DennisTraub Nov 28, 2023
90003a5
Add region flag to hello
DennisTraub Nov 28, 2023
45259ca
Add region flag and error handling
DennisTraub Nov 28, 2023
6dadde9
Add custom instructions to readme
DennisTraub Nov 28, 2023
03e450f
Add region configuration
DennisTraub Nov 28, 2023
9d1cb67
Add Amazon Titan image generation
DennisTraub Dec 1, 2023
18cf775
Add Amazon Titan image generation
DennisTraub Dec 1, 2023
06df740
Add metadata and regenerate Readme
DennisTraub Dec 1, 2023
d75e25b
Merge branch 'go-invokemodel' of github.com:DennisTraub/aws-doc-sdk-e…
DennisTraub Dec 1, 2023
dbb4a65
Random seed generator now covers the whole range of seeds for Amazon …
DennisTraub Dec 1, 2023
6f588ac
Fix issue with Bedrock scenario
DennisTraub Dec 1, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading