diff --git a/.doc_gen/metadata/bedrock-runtime_metadata.yaml b/.doc_gen/metadata/bedrock-runtime_metadata.yaml index 06fe8ec61f0..6e6f48b2ffd 100644 --- a/.doc_gen/metadata/bedrock-runtime_metadata.yaml +++ b/.doc_gen/metadata/bedrock-runtime_metadata.yaml @@ -765,64 +765,6 @@ bedrock-runtime_InvokeModel_CohereCommandR: services: bedrock-runtime: {InvokeModel} -bedrock-runtime_InvokeModel_MetaLlama2: - title: Invoke Meta Llama 2 on &BR; using the Invoke Model API - title_abbrev: "InvokeModel: Llama 2" - synopsis: send a text message to Meta Llama 2, using the Invoke Model API. - category: Meta Llama - languages: - Java: - versions: - - sdk_version: 2 - github: javav2/example_code/bedrock-runtime - excerpts: - - description: Use the Invoke Model API to send a text message. - snippet_tags: - - bedrock-runtime.java2.InvokeModel_MetaLlama2 - .NET: - versions: - - sdk_version: 3 - github: dotnetv3/Bedrock-runtime - excerpts: - - description: Use the Invoke Model API to send a text message. - snippet_tags: - - BedrockRuntime.dotnetv3.InvokeModel_MetaLlama2 - Python: - versions: - - sdk_version: 3 - github: python/example_code/bedrock-runtime - excerpts: - - description: Use the Invoke Model API to send a text message. - snippet_tags: - - python.example_code.bedrock-runtime.InvokeModel_MetaLlama2 - Go: - versions: - - sdk_version: 2 - github: gov2/bedrock-runtime - excerpts: - - description: Use the Invoke Model API to send a text message. - snippet_tags: - - gov2.bedrock-runtime.InvokeModelWrapper.struct - - gov2.bedrock-runtime.InvokeLlama2 - JavaScript: - versions: - - sdk_version: 3 - github: javascriptv3/example_code/bedrock-runtime - excerpts: - - description: Use the Invoke Model API to send a text message. - snippet_tags: - - javascript.v3.bedrock-runtime.InvokeModel_Llama2_Quickstart - PHP: - versions: - - sdk_version: 3 - github: php/example_code/bedrock-runtime - excerpts: - - description: Use the Invoke Model API to send a text message. - snippet_tags: - - php.example_code.bedrock-runtime.service.invokeLlama2 - services: - bedrock-runtime: {InvokeModel} - bedrock-runtime_InvokeModel_MetaLlama3: title: Invoke Meta Llama 3 on &BR; using the Invoke Model API title_abbrev: "InvokeModel: Llama 3" @@ -1055,47 +997,6 @@ bedrock-runtime_InvokeModelWithResponseStream_CohereCommandR: services: bedrock-runtime: {InvokeModel} -bedrock-runtime_InvokeModelWithResponseStream_MetaLlama2: - title: Invoke Meta Llama 2 on &BR; using the Invoke Model API with a response stream - title_abbrev: "InvokeModelWithResponseStream: Llama 2" - synopsis: send a text message to Meta Llama 2, using the Invoke Model API, and print the response stream. - category: Meta Llama - languages: - Java: - versions: - - sdk_version: 2 - github: javav2/example_code/bedrock-runtime - excerpts: - - description: Use the Invoke Model API to send a text message and process the response stream in real-time. - snippet_tags: - - bedrock-runtime.java2.InvokeModelWithResponseStream_MetaLlama2 - .NET: - versions: - - sdk_version: 3 - github: dotnetv3/Bedrock-runtime - excerpts: - - description: Use the Invoke Model API to send a text message and process the response stream in real-time. - snippet_tags: - - BedrockRuntime.dotnetv3.InvokeModelWithResponseStream_MetaLlama2 - Python: - versions: - - sdk_version: 3 - github: python/example_code/bedrock-runtime - excerpts: - - description: Use the Invoke Model API to send a text message and process the response stream in real-time. - snippet_tags: - - python.example_code.bedrock-runtime.InvokeModelWithResponseStream_MetaLlama2 - JavaScript: - versions: - - sdk_version: 3 - github: javascriptv3/example_code/bedrock-runtime - excerpts: - - description: Use the Invoke Model API to send a text message and process the response stream in real-time. - snippet_tags: - - javascript.v3.bedrock-runtime.InvokeModelWithResponseStream_Llama2_Quickstart - services: - bedrock-runtime: {InvokeModelWithResponseStream} - bedrock-runtime_InvokeModelWithResponseStream_MetaLlama3: title: Invoke Meta Llama 3 on &BR; using the Invoke Model API with a response stream title_abbrev: "InvokeModelWithResponseStream: Llama 3" diff --git a/dotnetv3/Bedrock-runtime/Actions/InvokeModelAsync.cs b/dotnetv3/Bedrock-runtime/Actions/InvokeModelAsync.cs index 99d16a27624..4838b916cd7 100644 --- a/dotnetv3/Bedrock-runtime/Actions/InvokeModelAsync.cs +++ b/dotnetv3/Bedrock-runtime/Actions/InvokeModelAsync.cs @@ -218,62 +218,6 @@ public static async Task InvokeJurassic2Async(string prompt) // snippet-end:[BedrockRuntime.dotnetv3.BedrockRuntimeActions.InvokeModelAsync.Jurassic2] - // snippet-start:[BedrockRuntime.dotnetv3.BedrockRuntimeActions.InvokeModelAsync.Llama2] - - /// - /// Asynchronously invokes the Meta Llama 2 Chat model to run an inference based on the provided input. - /// - /// The prompt that you want Llama 2 to complete. - /// The inference response from the model - /// - /// The different model providers have individual request and response formats. - /// For the format, ranges, and default values for Meta Llama 2 Chat, refer to: - /// https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-meta.html - /// - public static async Task InvokeLlama2Async(string prompt) - { - string llama2ModelId = "meta.llama2-13b-chat-v1"; - - AmazonBedrockRuntimeClient client = new(RegionEndpoint.USEast1); - - string payload = new JsonObject() - { - { "prompt", prompt }, - { "max_gen_len", 512 }, - { "temperature", 0.5 }, - { "top_p", 0.9 } - }.ToJsonString(); - - string generatedText = ""; - try - { - InvokeModelResponse response = await client.InvokeModelAsync(new InvokeModelRequest() - { - ModelId = llama2ModelId, - Body = AWSSDKUtils.GenerateMemoryStreamFromString(payload), - ContentType = "application/json", - Accept = "application/json" - }); - - if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) - { - return JsonNode.ParseAsync(response.Body) - .Result?["generation"]?.GetValue() ?? ""; - } - else - { - Console.WriteLine("InvokeModelAsync failed with status code " + response.HttpStatusCode); - } - } - catch (AmazonBedrockRuntimeException e) - { - Console.WriteLine(e.Message); - } - return generatedText; - } - - // snippet-end:[BedrockRuntime.dotnetv3.BedrockRuntimeActions.InvokeModelAsync.Llama2] - // snippet-start:[BedrockRuntime.dotnetv3.BedrockRuntimeActions.InvokeModelAsync.TitanTextG1] /// diff --git a/dotnetv3/Bedrock-runtime/BedrockRuntimeExamples.sln b/dotnetv3/Bedrock-runtime/BedrockRuntimeExamples.sln index 3d71e3f042e..f495e60e9b1 100644 --- a/dotnetv3/Bedrock-runtime/BedrockRuntimeExamples.sln +++ b/dotnetv3/Bedrock-runtime/BedrockRuntimeExamples.sln @@ -10,7 +10,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BedrockRuntimeTests", "Test {02823466-F5FF-43A2-B70A-EF3482A0CBDD} = {02823466-F5FF-43A2-B70A-EF3482A0CBDD} {0574B2F4-D4BE-4155-902B-BF3D7CE4804E} = {0574B2F4-D4BE-4155-902B-BF3D7CE4804E} {1E62D4FB-CC59-4F1E-BB22-574CEC08C94B} = {1E62D4FB-CC59-4F1E-BB22-574CEC08C94B} - {25DD40DB-953F-4AA4-A273-28848BAFFBD8} = {25DD40DB-953F-4AA4-A273-28848BAFFBD8} {2A6989CB-B273-4841-BD3E-7B1BBA4DD25F} = {2A6989CB-B273-4841-BD3E-7B1BBA4DD25F} {3D6441FC-0FE8-4D0C-910D-3D9310599C71} = {3D6441FC-0FE8-4D0C-910D-3D9310599C71} {4B5A00D6-B9F1-449F-A9D2-80E860D6BD75} = {4B5A00D6-B9F1-449F-A9D2-80E860D6BD75} @@ -23,7 +22,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BedrockRuntimeTests", "Test {B6924BBB-9993-44C1-BEF9-DEDEA42A12B2} = {B6924BBB-9993-44C1-BEF9-DEDEA42A12B2} {B753CEB9-EA53-4AE1-997E-B7D54A299D58} = {B753CEB9-EA53-4AE1-997E-B7D54A299D58} {BCC66C37-4980-484F-819D-066D2FF2669C} = {BCC66C37-4980-484F-819D-066D2FF2669C} - {C9049A40-6C4A-4071-9753-493FEB0C08BE} = {C9049A40-6C4A-4071-9753-493FEB0C08BE} {CDF1A045-0888-418C-8656-2BF5E3348A48} = {CDF1A045-0888-418C-8656-2BF5E3348A48} {D1B0719F-4F84-4DBC-BCAD-E856FB3193D7} = {D1B0719F-4F84-4DBC-BCAD-E856FB3193D7} {D3BA31F5-FF20-4321-9494-3F01439C4F61} = {D3BA31F5-FF20-4321-9494-3F01439C4F61} @@ -80,8 +78,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InvokeModel", "Models\Mistr EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Llama3_InvokeModel", "Models\MetaLlama\Llama3_InvokeModel\Llama3_InvokeModel.csproj", "{B753CEB9-EA53-4AE1-997E-B7D54A299D58}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Llama2_InvokeModel", "Models\MetaLlama\Llama2_InvokeModel\Llama2_InvokeModel.csproj", "{25DD40DB-953F-4AA4-A273-28848BAFFBD8}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Command_InvokeModel", "Models\CohereCommand\Command_InvokeModel\Command_InvokeModel.csproj", "{2A6989CB-B273-4841-BD3E-7B1BBA4DD25F}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Command_R_InvokeModel", "Models\CohereCommand\Command_R_InvokeModel\Command_R_InvokeModel.csproj", "{BCC66C37-4980-484F-819D-066D2FF2669C}" @@ -92,8 +88,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Command_InvokeModelWithResp EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Command_R_InvokeModelWithResponseStream", "Models\CohereCommand\Command_R_InvokeModelWithResponseStream\Command_R_InvokeModelWithResponseStream.csproj", "{02823466-F5FF-43A2-B70A-EF3482A0CBDD}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Llama2_InvokeModelWithResponseStream", "Models\MetaLlama\Llama2_InvokeModelWithResponseStream\Llama2_InvokeModelWithResponseStream.csproj", "{C9049A40-6C4A-4071-9753-493FEB0C08BE}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Llama3_InvokeModelWithResponseStream", "Models\MetaLlama\Llama3_InvokeModelWithResponseStream\Llama3_InvokeModelWithResponseStream.csproj", "{4B5A00D6-B9F1-449F-A9D2-80E860D6BD75}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InvokeModelWithResponseStream", "Models\Mistral\InvokeModelWithResponseStream\InvokeModelWithResponseStream.csproj", "{EFC7D088-EF45-464B-97CD-0BBA486B224A}" @@ -174,10 +168,6 @@ Global {B753CEB9-EA53-4AE1-997E-B7D54A299D58}.Debug|Any CPU.Build.0 = Debug|Any CPU {B753CEB9-EA53-4AE1-997E-B7D54A299D58}.Release|Any CPU.ActiveCfg = Release|Any CPU {B753CEB9-EA53-4AE1-997E-B7D54A299D58}.Release|Any CPU.Build.0 = Release|Any CPU - {25DD40DB-953F-4AA4-A273-28848BAFFBD8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {25DD40DB-953F-4AA4-A273-28848BAFFBD8}.Debug|Any CPU.Build.0 = Debug|Any CPU - {25DD40DB-953F-4AA4-A273-28848BAFFBD8}.Release|Any CPU.ActiveCfg = Release|Any CPU - {25DD40DB-953F-4AA4-A273-28848BAFFBD8}.Release|Any CPU.Build.0 = Release|Any CPU {2A6989CB-B273-4841-BD3E-7B1BBA4DD25F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {2A6989CB-B273-4841-BD3E-7B1BBA4DD25F}.Debug|Any CPU.Build.0 = Debug|Any CPU {2A6989CB-B273-4841-BD3E-7B1BBA4DD25F}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -198,10 +188,6 @@ Global {02823466-F5FF-43A2-B70A-EF3482A0CBDD}.Debug|Any CPU.Build.0 = Debug|Any CPU {02823466-F5FF-43A2-B70A-EF3482A0CBDD}.Release|Any CPU.ActiveCfg = Release|Any CPU {02823466-F5FF-43A2-B70A-EF3482A0CBDD}.Release|Any CPU.Build.0 = Release|Any CPU - {C9049A40-6C4A-4071-9753-493FEB0C08BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C9049A40-6C4A-4071-9753-493FEB0C08BE}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C9049A40-6C4A-4071-9753-493FEB0C08BE}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C9049A40-6C4A-4071-9753-493FEB0C08BE}.Release|Any CPU.Build.0 = Release|Any CPU {4B5A00D6-B9F1-449F-A9D2-80E860D6BD75}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {4B5A00D6-B9F1-449F-A9D2-80E860D6BD75}.Debug|Any CPU.Build.0 = Debug|Any CPU {4B5A00D6-B9F1-449F-A9D2-80E860D6BD75}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -241,14 +227,11 @@ Global {3D6441FC-0FE8-4D0C-910D-3D9310599C71} = {3F96ECB4-1644-43E8-8643-2CDCF9E679F1} {D1B0719F-4F84-4DBC-BCAD-E856FB3193D7} = {8BAC2322-AD3C-484A-B51D-8263BC4E6646} {1E62D4FB-CC59-4F1E-BB22-574CEC08C94B} = {BBB79D3E-5DF2-4FF6-B467-52D0EEB91C4B} - {B753CEB9-EA53-4AE1-997E-B7D54A299D58} = {65504C76-7E32-4A12-A42E-BCDA4FE79BC1} - {25DD40DB-953F-4AA4-A273-28848BAFFBD8} = {65504C76-7E32-4A12-A42E-BCDA4FE79BC1} {2A6989CB-B273-4841-BD3E-7B1BBA4DD25F} = {EF45C0B9-ED76-4B7A-A0A7-F102E979B71C} {BCC66C37-4980-484F-819D-066D2FF2669C} = {EF45C0B9-ED76-4B7A-A0A7-F102E979B71C} {52CDA3F4-F090-4224-978A-5F42388DCF92} = {3F96ECB4-1644-43E8-8643-2CDCF9E679F1} {63984664-8230-40F3-BFF5-7AC4988D7FE7} = {EF45C0B9-ED76-4B7A-A0A7-F102E979B71C} {02823466-F5FF-43A2-B70A-EF3482A0CBDD} = {EF45C0B9-ED76-4B7A-A0A7-F102E979B71C} - {C9049A40-6C4A-4071-9753-493FEB0C08BE} = {65504C76-7E32-4A12-A42E-BCDA4FE79BC1} {4B5A00D6-B9F1-449F-A9D2-80E860D6BD75} = {65504C76-7E32-4A12-A42E-BCDA4FE79BC1} {EFC7D088-EF45-464B-97CD-0BBA486B224A} = {BBB79D3E-5DF2-4FF6-B467-52D0EEB91C4B} {C75F2BBE-7C84-4B01-9836-7279DAE41499} = {8BAC2322-AD3C-484A-B51D-8263BC4E6646} diff --git a/dotnetv3/Bedrock-runtime/Models/MetaLlama/Llama2_InvokeModel/InvokeModel.cs b/dotnetv3/Bedrock-runtime/Models/MetaLlama/Llama2_InvokeModel/InvokeModel.cs deleted file mode 100644 index 79781e59a0e..00000000000 --- a/dotnetv3/Bedrock-runtime/Models/MetaLlama/Llama2_InvokeModel/InvokeModel.cs +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -// snippet-start:[BedrockRuntime.dotnetv3.InvokeModel_MetaLlama2] -// Use the native inference API to send a text message to Meta Llama 2. - -using System; -using System.IO; -using System.Text.Json; -using System.Text.Json.Nodes; -using Amazon; -using Amazon.BedrockRuntime; -using Amazon.BedrockRuntime.Model; - -// Create a Bedrock Runtime client in the AWS Region you want to use. -var client = new AmazonBedrockRuntimeClient(RegionEndpoint.USEast1); - -// Set the model ID, e.g., Llama 2 Chat 13B. -var modelId = "meta.llama2-13b-chat-v1"; - -// Define the prompt for the model. -var prompt = "Describe the purpose of a 'hello world' program in one line."; - -// Embed the prompt in Llama 2's instruction format. -var formattedPrompt = $"[INST] {prompt} [/INST]"; - -//Format the request payload using the model's native structure. -var nativeRequest = JsonSerializer.Serialize(new -{ - prompt = formattedPrompt, - max_gen_len = 512, - temperature = 0.5 -}); - -// Create a request with the model ID and the model's native request payload. -var request = new InvokeModelRequest() -{ - ModelId = modelId, - Body = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(nativeRequest)), - ContentType = "application/json" -}; - -try -{ - // Send the request to the Bedrock Runtime and wait for the response. - var response = await client.InvokeModelAsync(request); - - // Decode the response body. - var modelResponse = await JsonNode.ParseAsync(response.Body); - - // Extract and print the response text. - var responseText = modelResponse["generation"] ?? ""; - Console.WriteLine(responseText); -} -catch (AmazonBedrockRuntimeException e) -{ - Console.WriteLine($"ERROR: Can't invoke '{modelId}'. Reason: {e.Message}"); - throw; -} - -// snippet-end:[BedrockRuntime.dotnetv3.InvokeModel_MetaLlama2] - -// Create a partial class to make the top-level script testable. -namespace MetaLlama2 { public partial class InvokeModel { } } \ No newline at end of file diff --git a/dotnetv3/Bedrock-runtime/Models/MetaLlama/Llama2_InvokeModel/Llama2_InvokeModel.csproj b/dotnetv3/Bedrock-runtime/Models/MetaLlama/Llama2_InvokeModel/Llama2_InvokeModel.csproj deleted file mode 100644 index f91317c7fa6..00000000000 --- a/dotnetv3/Bedrock-runtime/Models/MetaLlama/Llama2_InvokeModel/Llama2_InvokeModel.csproj +++ /dev/null @@ -1,12 +0,0 @@ - - - Exe - net8.0 - MetaLlama.$(MSBuildProjectName) - - - - - - - diff --git a/dotnetv3/Bedrock-runtime/Models/MetaLlama/Llama2_InvokeModelWithResponseStream/InvokeModelWithResponseStream.cs b/dotnetv3/Bedrock-runtime/Models/MetaLlama/Llama2_InvokeModelWithResponseStream/InvokeModelWithResponseStream.cs deleted file mode 100644 index 1c2109c6765..00000000000 --- a/dotnetv3/Bedrock-runtime/Models/MetaLlama/Llama2_InvokeModelWithResponseStream/InvokeModelWithResponseStream.cs +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -// snippet-start:[BedrockRuntime.dotnetv3.InvokeModelWithResponseStream_MetaLlama2] -// Use the native inference API to send a text message to Meta Llama 2 -// and print the response stream. - -using System; -using System.IO; -using System.Text.Json; -using System.Text.Json.Nodes; -using Amazon; -using Amazon.BedrockRuntime; -using Amazon.BedrockRuntime.Model; - -// Create a Bedrock Runtime client in the AWS Region you want to use. -var client = new AmazonBedrockRuntimeClient(RegionEndpoint.USEast1); - -// Set the model ID, e.g., Llama 2 Chat 13B. -var modelId = "meta.llama2-13b-chat-v1"; - -// Define the prompt for the model. -var prompt = "Describe the purpose of a 'hello world' program in one line."; - -// Embed the prompt in Llama 2's instruction format. -var formattedPrompt = $"[INST] {prompt} [/INST]"; - -//Format the request payload using the model's native structure. -var nativeRequest = JsonSerializer.Serialize(new -{ - prompt = formattedPrompt, - max_gen_len = 512, - temperature = 0.5 -}); - -// Create a request with the model ID and the model's native request payload. -var request = new InvokeModelWithResponseStreamRequest() -{ - ModelId = modelId, - Body = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(nativeRequest)), - ContentType = "application/json" -}; - -try -{ - // Send the request to the Bedrock Runtime and wait for the response. - var streamingResponse = await client.InvokeModelWithResponseStreamAsync(request); - - // Extract and print the streamed response text in real-time. - foreach (var item in streamingResponse.Body) - { - var chunk = JsonSerializer.Deserialize((item as PayloadPart).Bytes); - var text = chunk["generation"] ?? ""; - Console.Write(text); - } -} -catch (AmazonBedrockRuntimeException e) -{ - Console.WriteLine($"ERROR: Can't invoke '{modelId}'. Reason: {e.Message}"); - throw; -} - -// snippet-end:[BedrockRuntime.dotnetv3.InvokeModelWithResponseStream_MetaLlama2] - -// Create a partial class to make the top-level script testable. -namespace MetaLlama2 { public partial class InvokeModelWithResponseStream { } } \ No newline at end of file diff --git a/dotnetv3/Bedrock-runtime/Models/MetaLlama/Llama2_InvokeModelWithResponseStream/Llama2_InvokeModelWithResponseStream.csproj b/dotnetv3/Bedrock-runtime/Models/MetaLlama/Llama2_InvokeModelWithResponseStream/Llama2_InvokeModelWithResponseStream.csproj deleted file mode 100644 index f91317c7fa6..00000000000 --- a/dotnetv3/Bedrock-runtime/Models/MetaLlama/Llama2_InvokeModelWithResponseStream/Llama2_InvokeModelWithResponseStream.csproj +++ /dev/null @@ -1,12 +0,0 @@ - - - Exe - net8.0 - MetaLlama.$(MSBuildProjectName) - - - - - - - diff --git a/dotnetv3/Bedrock-runtime/README.md b/dotnetv3/Bedrock-runtime/README.md index 1e7747b8e88..9e153544bb9 100644 --- a/dotnetv3/Bedrock-runtime/README.md +++ b/dotnetv3/Bedrock-runtime/README.md @@ -60,9 +60,7 @@ For prerequisites, see the [README](../README.md#Prerequisites) in the `dotnetv3 - [Converse](Models/MetaLlama/Converse/Converse.cs#L4) - [ConverseStream](Models/MetaLlama/ConverseStream/ConverseStream.cs#L4) -- [InvokeModel: Llama 2](Models/MetaLlama/Llama2_InvokeModel/InvokeModel.cs#L4) - [InvokeModel: Llama 3](Models/MetaLlama/Llama3_InvokeModel/InvokeModel.cs#L4) -- [InvokeModelWithResponseStream: Llama 2](Models/MetaLlama/Llama2_InvokeModelWithResponseStream/InvokeModelWithResponseStream.cs#L4) - [InvokeModelWithResponseStream: Llama 3](Models/MetaLlama/Llama3_InvokeModelWithResponseStream/InvokeModelWithResponseStream.cs#L4) ### Mistral AI diff --git a/dotnetv3/Bedrock-runtime/Tests/ActionTest_InvokeModelWithResponseStream.cs b/dotnetv3/Bedrock-runtime/Tests/ActionTest_InvokeModelWithResponseStream.cs index e2a3f21b6e5..c0520fa0d25 100644 --- a/dotnetv3/Bedrock-runtime/Tests/ActionTest_InvokeModelWithResponseStream.cs +++ b/dotnetv3/Bedrock-runtime/Tests/ActionTest_InvokeModelWithResponseStream.cs @@ -7,7 +7,6 @@ public class ActionTest_InvokeModelWithResponseStream { [Theory, Trait("Category", "Integration")] [InlineData(typeof(Mistral.InvokeModelWithResponseStream))] - [InlineData(typeof(MetaLlama2.InvokeModelWithResponseStream))] [InlineData(typeof(MetaLlama3.InvokeModelWithResponseStream))] [InlineData(typeof(CohereCommand.InvokeModelWithResponseStream))] [InlineData(typeof(CohereCommandR.InvokeModelWithResponseStream))] diff --git a/dotnetv3/Bedrock-runtime/Tests/ActionTests_InvokeModel.cs b/dotnetv3/Bedrock-runtime/Tests/ActionTests_InvokeModel.cs index 09822765e42..0584cf61793 100644 --- a/dotnetv3/Bedrock-runtime/Tests/ActionTests_InvokeModel.cs +++ b/dotnetv3/Bedrock-runtime/Tests/ActionTests_InvokeModel.cs @@ -7,7 +7,6 @@ public class ActionTest_InvokeModel { [Theory, Trait("Category", "Integration")] [InlineData(typeof(Mistral.InvokeModel))] - [InlineData(typeof(MetaLlama2.InvokeModel))] [InlineData(typeof(MetaLlama3.InvokeModel))] [InlineData(typeof(CohereCommand.InvokeModel))] [InlineData(typeof(CohereCommandR.InvokeModel))] diff --git a/dotnetv3/Bedrock-runtime/Tests/BedrockRuntimeTests.csproj b/dotnetv3/Bedrock-runtime/Tests/BedrockRuntimeTests.csproj index 71853be681c..b499eb4f7a0 100644 --- a/dotnetv3/Bedrock-runtime/Tests/BedrockRuntimeTests.csproj +++ b/dotnetv3/Bedrock-runtime/Tests/BedrockRuntimeTests.csproj @@ -45,8 +45,6 @@ - - diff --git a/dotnetv3/DotNetV3Examples.sln b/dotnetv3/DotNetV3Examples.sln index df23b2d6cf3..54a02263eb2 100644 --- a/dotnetv3/DotNetV3Examples.sln +++ b/dotnetv3/DotNetV3Examples.sln @@ -761,10 +761,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Llama3_InvokeModelWithRespo EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Llama3_InvokeModel", "Bedrock-runtime\Models\MetaLlama\Llama3_InvokeModel\Llama3_InvokeModel.csproj", "{FF724338-934A-45B3-90A5-9DB9CDFD3E1D}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Llama2_InvokeModelWithResponseStream", "Bedrock-runtime\Models\MetaLlama\Llama2_InvokeModelWithResponseStream\Llama2_InvokeModelWithResponseStream.csproj", "{E3F8818A-4ED8-4453-9D46-2B3A5FC404BC}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Llama2_InvokeModel", "Bedrock-runtime\Models\MetaLlama\Llama2_InvokeModel\Llama2_InvokeModel.csproj", "{A7C68A53-0A9D-4006-BEE9-0EDBDC17DD39}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConverseStream", "Bedrock-runtime\Models\MetaLlama\ConverseStream\ConverseStream.csproj", "{8D49CCB6-F604-49DF-A687-8CBDE52EF1E5}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Converse", "Bedrock-runtime\Models\MetaLlama\Converse\Converse.csproj", "{31051885-0A31-4547-A8E8-6C31923B7D40}" @@ -1803,14 +1799,6 @@ Global {FF724338-934A-45B3-90A5-9DB9CDFD3E1D}.Debug|Any CPU.Build.0 = Debug|Any CPU {FF724338-934A-45B3-90A5-9DB9CDFD3E1D}.Release|Any CPU.ActiveCfg = Release|Any CPU {FF724338-934A-45B3-90A5-9DB9CDFD3E1D}.Release|Any CPU.Build.0 = Release|Any CPU - {E3F8818A-4ED8-4453-9D46-2B3A5FC404BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E3F8818A-4ED8-4453-9D46-2B3A5FC404BC}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E3F8818A-4ED8-4453-9D46-2B3A5FC404BC}.Release|Any CPU.ActiveCfg = Release|Any CPU - {E3F8818A-4ED8-4453-9D46-2B3A5FC404BC}.Release|Any CPU.Build.0 = Release|Any CPU - {A7C68A53-0A9D-4006-BEE9-0EDBDC17DD39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A7C68A53-0A9D-4006-BEE9-0EDBDC17DD39}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A7C68A53-0A9D-4006-BEE9-0EDBDC17DD39}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A7C68A53-0A9D-4006-BEE9-0EDBDC17DD39}.Release|Any CPU.Build.0 = Release|Any CPU {8D49CCB6-F604-49DF-A687-8CBDE52EF1E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {8D49CCB6-F604-49DF-A687-8CBDE52EF1E5}.Debug|Any CPU.Build.0 = Debug|Any CPU {8D49CCB6-F604-49DF-A687-8CBDE52EF1E5}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -2273,8 +2261,6 @@ Global {391431C4-F764-4F12-A20C-10DAFA764315} = {6520EB28-F7B4-4581-B3D8-A06E9303B16B} {6DE6B4D8-3DF8-415E-9461-DA0E91217DC5} = {391431C4-F764-4F12-A20C-10DAFA764315} {FF724338-934A-45B3-90A5-9DB9CDFD3E1D} = {391431C4-F764-4F12-A20C-10DAFA764315} - {E3F8818A-4ED8-4453-9D46-2B3A5FC404BC} = {391431C4-F764-4F12-A20C-10DAFA764315} - {A7C68A53-0A9D-4006-BEE9-0EDBDC17DD39} = {391431C4-F764-4F12-A20C-10DAFA764315} {8D49CCB6-F604-49DF-A687-8CBDE52EF1E5} = {391431C4-F764-4F12-A20C-10DAFA764315} {31051885-0A31-4547-A8E8-6C31923B7D40} = {391431C4-F764-4F12-A20C-10DAFA764315} {E4B34C61-78B4-4E2B-98FB-7CA945724740} = {6520EB28-F7B4-4581-B3D8-A06E9303B16B} diff --git a/gov2/bedrock-runtime/README.md b/gov2/bedrock-runtime/README.md index 6b8f6d67156..348cb1de378 100644 --- a/gov2/bedrock-runtime/README.md +++ b/gov2/bedrock-runtime/README.md @@ -57,10 +57,6 @@ functions within the same service. - [InvokeModel](actions/invoke_model.go#L7) - [InvokeModelWithResponseStream](actions/invoke_model_with_response_stream.go#L30) -### Meta Llama - -- [InvokeModel: Llama 2](actions/invoke_model.go#L7) - > ⚠ 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). diff --git a/gov2/bedrock-runtime/actions/invoke_model.go b/gov2/bedrock-runtime/actions/invoke_model.go index 9863ffd5c79..ed210723ddd 100644 --- a/gov2/bedrock-runtime/actions/invoke_model.go +++ b/gov2/bedrock-runtime/actions/invoke_model.go @@ -137,57 +137,6 @@ func (wrapper InvokeModelWrapper) InvokeJurassic2(ctx context.Context, prompt st // snippet-end:[gov2.bedrock-runtime.InvokeJurassic2] -// snippet-start:[gov2.bedrock-runtime.InvokeLlama2] - -// Each model provider has their own individual request and response formats. -// For the format, ranges, and default values for Meta Llama 2 Chat, refer to: -// https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-meta.html - -type Llama2Request struct { - Prompt string `json:"prompt"` - MaxGenLength int `json:"max_gen_len,omitempty"` - Temperature float64 `json:"temperature,omitempty"` -} - -type Llama2Response struct { - Generation string `json:"generation"` -} - -// Invokes Meta Llama 2 Chat on Amazon Bedrock to run an inference using the input -// provided in the request body. -func (wrapper InvokeModelWrapper) InvokeLlama2(ctx context.Context, prompt string) (string, error) { - modelId := "meta.llama2-13b-chat-v1" - - body, err := json.Marshal(Llama2Request{ - Prompt: prompt, - MaxGenLength: 512, - Temperature: 0.5, - }) - - if err != nil { - log.Fatal("failed to marshal", err) - } - - output, err := wrapper.BedrockRuntimeClient.InvokeModel(ctx, &bedrockruntime.InvokeModelInput{ - ModelId: aws.String(modelId), - ContentType: aws.String("application/json"), - Body: body, - }) - - if err != nil { - ProcessError(err, modelId) - } - - var response Llama2Response - if err := json.Unmarshal(output.Body, &response); err != nil { - log.Fatal("failed to unmarshal", err) - } - - return response.Generation, nil -} - -// snippet-end:[gov2.bedrock-runtime.InvokeLlama2] - // snippet-start:[gov2.bedrock-runtime.InvokeTitanImage] type TitanImageRequest struct { diff --git a/gov2/bedrock-runtime/actions/invoke_model_test.go b/gov2/bedrock-runtime/actions/invoke_model_test.go index 6824b74df50..10a371692fa 100644 --- a/gov2/bedrock-runtime/actions/invoke_model_test.go +++ b/gov2/bedrock-runtime/actions/invoke_model_test.go @@ -19,7 +19,6 @@ import ( const CLAUDE_MODEL_ID = "anthropic.claude-v2" const JURASSIC2_MODEL_ID = "ai21.j2-mid-v1" -const LLAMA2_MODEL_ID = "meta.llama2-13b-chat-v1" const TITAN_IMAGE_MODEL_ID = "amazon.titan-image-generator-v1" const TITAN_TEXT_EXPRESS_MODEL_ID = "amazon.titan-text-express-v1" @@ -48,12 +47,6 @@ func CallInvokeModelActions(sdkConfig aws.Config) { } log.Println(jurassic2Completion) - llama2Completion, err := wrapper.InvokeLlama2(ctx, prompt) - if err != nil { - panic(err) - } - log.Println(llama2Completion) - seed := int64(0) titanImageCompletion, err := wrapper.InvokeTitanImage(ctx, prompt, seed) if err != nil { @@ -81,7 +74,6 @@ func (scenTest *InvokeModelActionsTest) SetupDataAndStubs() []testtools.Stub { var stubList []testtools.Stub stubList = append(stubList, stubInvokeModel(CLAUDE_MODEL_ID)) stubList = append(stubList, stubInvokeModel(JURASSIC2_MODEL_ID)) - stubList = append(stubList, stubInvokeModel(LLAMA2_MODEL_ID)) stubList = append(stubList, stubInvokeModel(TITAN_IMAGE_MODEL_ID)) stubList = append(stubList, stubInvokeModel(TITAN_TEXT_EXPRESS_MODEL_ID)) @@ -122,16 +114,6 @@ func stubInvokeModel(modelId string) testtools.Stub { }, }) - case LLAMA2_MODEL_ID: - request, _ = json.Marshal(Llama2Request{ - Prompt: prompt, - MaxGenLength: 512, - Temperature: 0.5, - }) - response, _ = json.Marshal(Llama2Response{ - Generation: "A fake response", - }) - case TITAN_IMAGE_MODEL_ID: request, _ = json.Marshal(TitanImageRequest{ TaskType: "TEXT_IMAGE", diff --git a/gov2/bedrock-runtime/scenarios/scenario_invoke_models.go b/gov2/bedrock-runtime/scenarios/scenario_invoke_models.go index 2a297333728..56c0e96c590 100644 --- a/gov2/bedrock-runtime/scenarios/scenario_invoke_models.go +++ b/gov2/bedrock-runtime/scenarios/scenario_invoke_models.go @@ -74,10 +74,6 @@ func (scenario InvokeModelsScenario) Run(ctx context.Context) { log.Printf("Invoking Jurassic-2 with prompt: %v\n", text2textPrompt) scenario.InvokeJurassic2(ctx, text2textPrompt) - log.Println(strings.Repeat("-", 77)) - log.Printf("Invoking Llama2 with prompt: %v\n", text2textPrompt) - scenario.InvokeLlama2(ctx, text2textPrompt) - log.Println(strings.Repeat("=", 77)) log.Printf("Now, let's invoke Claude with the asynchronous client and process the response stream:\n\n") @@ -120,14 +116,6 @@ func (scenario InvokeModelsScenario) InvokeJurassic2(ctx context.Context, prompt log.Printf("\nJurassic-2 : %v\n", strings.TrimSpace(completion)) } -func (scenario InvokeModelsScenario) InvokeLlama2(ctx context.Context, prompt string) { - completion, err := scenario.invokeModelWrapper.InvokeLlama2(ctx, prompt) - if err != nil { - panic(err) - } - log.Printf("\nLlama 2 : %v\n\n", strings.TrimSpace(completion)) -} - func (scenario InvokeModelsScenario) InvokeWithResponseStream(ctx context.Context, prompt string) { log.Println("\nClaude with response stream:") _, err := scenario.responseStreamWrapper.InvokeModelWithResponseStream(ctx, prompt) diff --git a/javascriptv3/example_code/bedrock-runtime/README.md b/javascriptv3/example_code/bedrock-runtime/README.md index 6e78a957523..11856802302 100644 --- a/javascriptv3/example_code/bedrock-runtime/README.md +++ b/javascriptv3/example_code/bedrock-runtime/README.md @@ -72,9 +72,7 @@ functions within the same service. - [Converse](models/metaLlama/converse.js#L4) - [ConverseStream](models/metaLlama/converseStream.js#L4) -- [InvokeModel: Llama 2](models/metaLlama/llama2/invoke_model_quickstart.js#L4) - [InvokeModel: Llama 3](models/metaLlama/llama3/invoke_model_quickstart.js#L4) -- [InvokeModelWithResponseStream: Llama 2](models/metaLlama/llama2/invoke_model_with_response_stream_quickstart.js#L4) - [InvokeModelWithResponseStream: Llama 3](models/metaLlama/llama3/invoke_model_with_response_stream_quickstart.js#L4) ### Mistral AI diff --git a/javascriptv3/example_code/bedrock-runtime/models/metaLlama/llama2/invoke_model_quickstart.js b/javascriptv3/example_code/bedrock-runtime/models/metaLlama/llama2/invoke_model_quickstart.js deleted file mode 100644 index 210aa789188..00000000000 --- a/javascriptv3/example_code/bedrock-runtime/models/metaLlama/llama2/invoke_model_quickstart.js +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -// snippet-start:[javascript.v3.bedrock-runtime.InvokeModel_Llama2_Quickstart] -// Send a prompt to Meta Llama 2 and print the response. - -import { - BedrockRuntimeClient, - InvokeModelCommand, -} from "@aws-sdk/client-bedrock-runtime"; - -// Create a Bedrock Runtime client in the AWS Region of your choice. -const client = new BedrockRuntimeClient({ region: "us-west-2" }); - -// Set the model ID, e.g., Llama 2 Chat 13B. -const modelId = "meta.llama2-13b-chat-v1"; - -// Define the user message to send. -const userMessage = - "Describe the purpose of a 'hello world' program in one sentence."; - -// Embed the message in Llama 2's prompt format. -const prompt = `[INST] ${userMessage} [/INST]`; - -// Format the request payload using the model's native structure. -const request = { - prompt, - // Optional inference parameters: - max_gen_len: 512, - temperature: 0.5, - top_p: 0.9, -}; - -// Encode and send the request. -const response = await client.send( - new InvokeModelCommand({ - contentType: "application/json", - body: JSON.stringify(request), - modelId, - }), -); - -// Decode the native response body. -/** @type {{ generation: string }} */ -const nativeResponse = JSON.parse(new TextDecoder().decode(response.body)); - -// Extract and print the generated text. -const responseText = nativeResponse.generation; -console.log(responseText); - -// Learn more about the Llama 2 prompt format at: -// https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-2 - -// snippet-end:[javascript.v3.bedrock-runtime.InvokeModel_Llama2_Quickstart] diff --git a/javascriptv3/example_code/bedrock-runtime/models/metaLlama/llama2/invoke_model_with_response_stream_quickstart.js b/javascriptv3/example_code/bedrock-runtime/models/metaLlama/llama2/invoke_model_with_response_stream_quickstart.js deleted file mode 100644 index 91012dc0c66..00000000000 --- a/javascriptv3/example_code/bedrock-runtime/models/metaLlama/llama2/invoke_model_with_response_stream_quickstart.js +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -// snippet-start:[javascript.v3.bedrock-runtime.InvokeModelWithResponseStream_Llama2_Quickstart] -// Send a prompt to Meta Llama 2 and print the response stream in real-time. - -import { - BedrockRuntimeClient, - InvokeModelWithResponseStreamCommand, -} from "@aws-sdk/client-bedrock-runtime"; - -// Create a Bedrock Runtime client in the AWS Region of your choice. -const client = new BedrockRuntimeClient({ region: "us-west-2" }); - -// Set the model ID, e.g., Llama 2 Chat 13B. -const modelId = "meta.llama2-13b-chat-v1"; - -// Define the user message to send. -const userMessage = - "Describe the purpose of a 'hello world' program in one sentence."; - -// Embed the message in Llama 2's prompt format. -const prompt = `[INST] ${userMessage} [/INST]`; - -// Format the request payload using the model's native structure. -const request = { - prompt, - // Optional inference parameters: - max_gen_len: 512, - temperature: 0.5, - top_p: 0.9, -}; - -// Encode and send the request. -const responseStream = await client.send( - new InvokeModelWithResponseStreamCommand({ - contentType: "application/json", - body: JSON.stringify(request), - modelId, - }), -); - -// Extract and print the response stream in real-time. -for await (const event of responseStream.body) { - /** @type {{ generation: string }} */ - const chunk = JSON.parse(new TextDecoder().decode(event.chunk.bytes)); - if (chunk.generation) { - process.stdout.write(chunk.generation); - } -} - -// Learn more about the Llama 3 prompt format at: -// https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-3/#special-tokens-used-with-meta-llama-3 -// snippet-end:[javascript.v3.bedrock-runtime.InvokeModelWithResponseStream_Llama2_Quickstart] diff --git a/javascriptv3/example_code/bedrock-runtime/tests/meta_llama.integration.test.js b/javascriptv3/example_code/bedrock-runtime/tests/meta_llama.integration.test.js index 8b468765d04..f70cc94dcd5 100644 --- a/javascriptv3/example_code/bedrock-runtime/tests/meta_llama.integration.test.js +++ b/javascriptv3/example_code/bedrock-runtime/tests/meta_llama.integration.test.js @@ -5,32 +5,9 @@ import { describe, expect, it, vi } from "vitest"; /** * Integration tests for: - * - models/meta/llama2/*.js * - models/meta/llama3/*.js */ -describe("Running the Llama2 InvokeModel quickstart", () => { - it("should run and log the model's response", async () => { - const log = vi.spyOn(console, "log").mockImplementation(() => {}); - await import("../models/metaLlama/llama2/invoke_model_quickstart.js"); - expect(log).toHaveBeenCalledTimes(1); - log.mockRestore(); - }); -}); - -describe("Running the Llama2 InvokeModelWithResponseStream quickstart", () => { - it("should run and log the model's response", async () => { - const write = vi - .spyOn(process.stdout, "write") - .mockImplementation(() => {}); - await import( - "../models/metaLlama/llama2/invoke_model_with_response_stream_quickstart.js" - ); - expect(write).toHaveBeenCalled(); - write.mockRestore(); - }); -}); - describe("Running the Llama3 InvokeModel quickstart", () => { it("should run and log the model's response", async () => { const log = vi.spyOn(console, "log").mockImplementation(() => {}); diff --git a/javav2/example_code/bedrock-runtime/README.md b/javav2/example_code/bedrock-runtime/README.md index 1a83388faf9..ba38ab71ac1 100644 --- a/javav2/example_code/bedrock-runtime/README.md +++ b/javav2/example_code/bedrock-runtime/README.md @@ -73,9 +73,7 @@ For prerequisites, see the [README](../../README.md#Prerequisites) in the `javav - [Converse](src/main/java/com/example/bedrockruntime/models/metaLlama/Converse.java#L6) - [ConverseStream](src/main/java/com/example/bedrockruntime/models/metaLlama/ConverseStream.java#L6) -- [InvokeModel: Llama 2](src/main/java/com/example/bedrockruntime/models/metaLlama/Llama2_InvokeModel.java#L6) - [InvokeModel: Llama 3](src/main/java/com/example/bedrockruntime/models/metaLlama/Llama3_InvokeModel.java#L6) -- [InvokeModelWithResponseStream: Llama 2](src/main/java/com/example/bedrockruntime/models/metaLlama/Llama2_InvokeModelWithResponseStream.java#L6) - [InvokeModelWithResponseStream: Llama 3](src/main/java/com/example/bedrockruntime/models/metaLlama/Llama3_InvokeModelWithResponseStream.java#L6) ### Mistral AI diff --git a/javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/models/metaLlama/Llama2_InvokeModel.java b/javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/models/metaLlama/Llama2_InvokeModel.java deleted file mode 100644 index 58218c2e9a8..00000000000 --- a/javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/models/metaLlama/Llama2_InvokeModel.java +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.example.bedrockruntime.models.metaLlama; - -// snippet-start:[bedrock-runtime.java2.InvokeModel_MetaLlama2] -// Use the native inference API to send a text message to Meta Llama 2. - -import org.json.JSONObject; -import org.json.JSONPointer; -import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; -import software.amazon.awssdk.core.SdkBytes; -import software.amazon.awssdk.core.exception.SdkClientException; -import software.amazon.awssdk.regions.Region; -import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient; - -public class Llama2_InvokeModel { - - public static String invokeModel() { - - // Create a Bedrock Runtime client in the AWS Region you want to use. - // Replace the DefaultCredentialsProvider with your preferred credentials provider. - var client = BedrockRuntimeClient.builder() - .credentialsProvider(DefaultCredentialsProvider.create()) - .region(Region.US_EAST_1) - .build(); - - // Set the model ID, e.g., Llama 2 Chat 13B. - var modelId = "meta.llama2-13b-chat-v1"; - - // The InvokeModel API uses the model's native payload. - // Learn more about the available inference parameters and response fields at: - // https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-meta.html - var nativeRequestTemplate = "{ \"prompt\": \"{{instruction}}\" }"; - - // Define the prompt for the model. - var prompt = "Describe the purpose of a 'hello world' program in one line."; - - // Embed the prompt in Llama 2's instruction format. - var instruction = "[INST] {{prompt}} [/INST]\\n".replace("{{prompt}}", prompt); - - // Embed the instruction in the the native request payload. - var nativeRequest = nativeRequestTemplate.replace("{{instruction}}", instruction); - - try { - // Encode and send the request to the Bedrock Runtime. - var response = client.invokeModel(request -> request - .body(SdkBytes.fromUtf8String(nativeRequest)) - .modelId(modelId) - ); - - // Decode the response body. - var responseBody = new JSONObject(response.body().asUtf8String()); - - // Retrieve the generated text from the model's response. - var text = new JSONPointer("/generation").queryFrom(responseBody).toString(); - System.out.println(text); - - return text; - - } catch (SdkClientException e) { - System.err.printf("ERROR: Can't invoke '%s'. Reason: %s", modelId, e.getMessage()); - throw new RuntimeException(e); - } - } - - public static void main(String[] args) { - invokeModel(); - } -} -// snippet-end:[bedrock-runtime.java2.InvokeModel_MetaLlama2] diff --git a/javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/models/metaLlama/Llama2_InvokeModelWithResponseStream.java b/javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/models/metaLlama/Llama2_InvokeModelWithResponseStream.java deleted file mode 100644 index 968fbfefe0a..00000000000 --- a/javav2/example_code/bedrock-runtime/src/main/java/com/example/bedrockruntime/models/metaLlama/Llama2_InvokeModelWithResponseStream.java +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.example.bedrockruntime.models.metaLlama; - -// snippet-start:[bedrock-runtime.java2.InvokeModelWithResponseStream_MetaLlama2] -// Use the native inference API to send a text message to Meta Llama 2 -// and print the response stream. - -import org.json.JSONObject; -import org.json.JSONPointer; -import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; -import software.amazon.awssdk.core.SdkBytes; -import software.amazon.awssdk.regions.Region; -import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeAsyncClient; -import software.amazon.awssdk.services.bedrockruntime.model.InvokeModelWithResponseStreamRequest; -import software.amazon.awssdk.services.bedrockruntime.model.InvokeModelWithResponseStreamResponseHandler; - -import java.util.concurrent.ExecutionException; - -import static software.amazon.awssdk.services.bedrockruntime.model.InvokeModelWithResponseStreamResponseHandler.Visitor; - -public class Llama2_InvokeModelWithResponseStream { - - public static String invokeModelWithResponseStream() throws ExecutionException, InterruptedException { - - // Create a Bedrock Runtime client in the AWS Region you want to use. - // Replace the DefaultCredentialsProvider with your preferred credentials provider. - var client = BedrockRuntimeAsyncClient.builder() - .credentialsProvider(DefaultCredentialsProvider.create()) - .region(Region.US_EAST_1) - .build(); - - // Set the model ID, e.g., Llama 2 Chat 13B. - var modelId = "meta.llama2-13b-chat-v1"; - - // The InvokeModelWithResponseStream API uses the model's native payload. - // Learn more about the available inference parameters and response fields at: - // https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-meta.html - var nativeRequestTemplate = "{ \"prompt\": \"{{instruction}}\" }"; - - // Define the prompt for the model. - var prompt = "Describe the purpose of a 'hello world' program in one line."; - - // Embed the prompt in Llama 2's instruction format. - var instruction = "[INST] {{prompt}} [/INST]\\n".replace("{{prompt}}", prompt); - - // Embed the instruction in the the native request payload. - var nativeRequest = nativeRequestTemplate.replace("{{instruction}}", instruction); - - // Create a request with the model ID and the model's native request payload. - var request = InvokeModelWithResponseStreamRequest.builder() - .body(SdkBytes.fromUtf8String(nativeRequest)) - .modelId(modelId) - .build(); - - // Prepare a buffer to accumulate the generated response text. - var completeResponseTextBuffer = new StringBuilder(); - - // Prepare a handler to extract, accumulate, and print the response text in real-time. - var responseStreamHandler = InvokeModelWithResponseStreamResponseHandler.builder() - .subscriber(Visitor.builder().onChunk(chunk -> { - // Extract and print the text from the model's native response. - var response = new JSONObject(chunk.bytes().asUtf8String()); - var text = new JSONPointer("/generation").queryFrom(response); - System.out.print(text); - - // Append the text to the response text buffer. - completeResponseTextBuffer.append(text); - }).build()).build(); - - try { - // Send the request and wait for the handler to process the response. - client.invokeModelWithResponseStream(request, responseStreamHandler).get(); - - // Return the complete response text. - return completeResponseTextBuffer.toString(); - - } catch (ExecutionException | InterruptedException e) { - System.err.printf("Can't invoke '%s': %s", modelId, e.getCause().getMessage()); - throw new RuntimeException(e); - } - } - - public static void main(String[] args) throws ExecutionException, InterruptedException { - invokeModelWithResponseStream(); - } -} -// snippet-end:[bedrock-runtime.java2.InvokeModelWithResponseStream_MetaLlama2] diff --git a/javav2/example_code/bedrock-runtime/src/test/java/actions/TestConverse.java b/javav2/example_code/bedrock-runtime/src/test/java/actions/TestConverse.java index efe8c74f367..889de5b62d7 100644 --- a/javav2/example_code/bedrock-runtime/src/test/java/actions/TestConverse.java +++ b/javav2/example_code/bedrock-runtime/src/test/java/actions/TestConverse.java @@ -30,12 +30,6 @@ void testCohereCommand() { assertNotNullOrEmpty(result); } - @Test - void testLlama2() { - String result = com.example.bedrockruntime.models.metaLlama.Converse.converse(); - assertNotNullOrEmpty(result); - } - @Test void testMistral() { String result = com.example.bedrockruntime.models.mistral.Converse.converse(); diff --git a/javav2/example_code/bedrock-runtime/src/test/java/actions/TestConverseAsync.java b/javav2/example_code/bedrock-runtime/src/test/java/actions/TestConverseAsync.java index 2df8c04bb8e..6b36a7e1b5a 100644 --- a/javav2/example_code/bedrock-runtime/src/test/java/actions/TestConverseAsync.java +++ b/javav2/example_code/bedrock-runtime/src/test/java/actions/TestConverseAsync.java @@ -30,12 +30,6 @@ void testCohereCommand() { assertNotNullOrEmpty(result); } - @Test - void testLlama2() { - String result = com.example.bedrockruntime.models.metaLlama.ConverseAsync.converseAsync(); - assertNotNullOrEmpty(result); - } - @Test void testMistral() { String result = com.example.bedrockruntime.models.mistral.ConverseAsync.converseAsync(); diff --git a/javav2/example_code/bedrock-runtime/src/test/java/actions/TestInvokeModel.java b/javav2/example_code/bedrock-runtime/src/test/java/actions/TestInvokeModel.java index 8c5d223eacc..e446933d547 100644 --- a/javav2/example_code/bedrock-runtime/src/test/java/actions/TestInvokeModel.java +++ b/javav2/example_code/bedrock-runtime/src/test/java/actions/TestInvokeModel.java @@ -48,12 +48,6 @@ void testCohereCommandR() { assertNotNullOrEmpty(result); } - @Test - void testLlama2() { - String result = com.example.bedrockruntime.models.metaLlama.Llama2_InvokeModel.invokeModel(); - assertNotNullOrEmpty(result); - } - @Test void testLlama3() { String result = com.example.bedrockruntime.models.metaLlama.Llama3_InvokeModel.invokeModel(); diff --git a/javav2/example_code/bedrock-runtime/src/test/java/actions/TestInvokeModelWithResponseStream.java b/javav2/example_code/bedrock-runtime/src/test/java/actions/TestInvokeModelWithResponseStream.java index 0b0eed003a4..76cb1983dea 100644 --- a/javav2/example_code/bedrock-runtime/src/test/java/actions/TestInvokeModelWithResponseStream.java +++ b/javav2/example_code/bedrock-runtime/src/test/java/actions/TestInvokeModelWithResponseStream.java @@ -34,13 +34,7 @@ void testCohereCommandR() throws ExecutionException, InterruptedException { } @Test - void testLlama2() throws ExecutionException, InterruptedException { - String result = com.example.bedrockruntime.models.metaLlama.Llama2_InvokeModelWithResponseStream.invokeModelWithResponseStream(); - assertNotNullOrEmpty(result); - } - - @Test - void testLlama3() throws ExecutionException, InterruptedException { + void testLlama3() { String result = com.example.bedrockruntime.models.metaLlama.Llama3_InvokeModelWithResponseStream.invokeModelWithResponseStream(); assertNotNullOrEmpty(result); } diff --git a/php/example_code/bedrock-runtime/BedrockRuntimeService.php b/php/example_code/bedrock-runtime/BedrockRuntimeService.php index bcbf48a1bd0..db0439b8b2c 100644 --- a/php/example_code/bedrock-runtime/BedrockRuntimeService.php +++ b/php/example_code/bedrock-runtime/BedrockRuntimeService.php @@ -91,36 +91,6 @@ public function invokeJurassic2($prompt) } // snippet-end:[php.example_code.bedrock-runtime.service.invokeJurassic2] - // snippet-start:[php.example_code.bedrock-runtime.service.invokeLlama2] - public function invokeLlama2($prompt) - { - // The different model providers have individual request and response formats. - // For the format, ranges, and default values for Meta Llama 2 Chat, refer to: - // https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-meta.html - - $completion = ""; - try { - $modelId = 'meta.llama2-13b-chat-v1'; - $body = [ - 'prompt' => $prompt, - 'temperature' => 0.5, - 'max_gen_len' => 512, - ]; - $result = $this->bedrockRuntimeClient->invokeModel([ - 'contentType' => 'application/json', - 'body' => json_encode($body), - 'modelId' => $modelId, - ]); - $response_body = json_decode($result['body']); - $completion = $response_body->generation; - } catch (Exception $e) { - echo "Error: ({$e->getCode()}) - {$e->getMessage()}\n"; - } - - return $completion; - } - // snippet-end:[php.example_code.bedrock-runtime.service.invokeLlama2] - // snippet-start:[php.example_code.bedrock-runtime.service.invokeStableDiffusion] public function invokeStableDiffusion(string $prompt, int $seed, string $style_preset) { diff --git a/php/example_code/bedrock-runtime/GettingStartedWithBedrockRuntime.php b/php/example_code/bedrock-runtime/GettingStartedWithBedrockRuntime.php index fb27160ce76..50006863ae4 100644 --- a/php/example_code/bedrock-runtime/GettingStartedWithBedrockRuntime.php +++ b/php/example_code/bedrock-runtime/GettingStartedWithBedrockRuntime.php @@ -27,8 +27,6 @@ public function runExample() echo $bedrockRuntimeService->invokeClaude($prompt); echo "\n\nAI21 Labs Jurassic-2: "; echo $bedrockRuntimeService->invokeJurassic2($prompt); - echo "\n\nMeta Llama 2 Chat: "; - echo $bedrockRuntimeService->invokeLlama2($prompt); echo "\n---------------------------------------------------------------------\n"; $image_prompt = 'stylized picture of a cute old steampunk robot'; echo "\nImage prompt: " . $image_prompt; diff --git a/php/example_code/bedrock-runtime/README.md b/php/example_code/bedrock-runtime/README.md index 5dd7078b4a9..524789fa3fd 100644 --- a/php/example_code/bedrock-runtime/README.md +++ b/php/example_code/bedrock-runtime/README.md @@ -48,19 +48,15 @@ functions within the same service. ### Amazon Titan Image Generator -- [InvokeModel](BedrockRuntimeService.php#L161) +- [InvokeModel](BedrockRuntimeService.php#L131) ### Anthropic Claude - [InvokeModel](BedrockRuntimeService.php#L31) -### Meta Llama - -- [InvokeModel: Llama 2](BedrockRuntimeService.php#L94) - ### Stable Diffusion -- [InvokeModel](BedrockRuntimeService.php#L124) +- [InvokeModel](BedrockRuntimeService.php#L94) diff --git a/php/example_code/bedrock-runtime/tests/BedrockRuntimeTests.php b/php/example_code/bedrock-runtime/tests/BedrockRuntimeTests.php index 57ebef540de..2d9a14cca9c 100644 --- a/php/example_code/bedrock-runtime/tests/BedrockRuntimeTests.php +++ b/php/example_code/bedrock-runtime/tests/BedrockRuntimeTests.php @@ -53,12 +53,6 @@ public function test_jurassic2_can_be_invoked() self::assertNotEmpty($completion); } - public function test_llama2_can_be_invoked() - { - $completion = $this->bedrockRuntimeService->invokeLlama2($this->prompt); - self::assertNotEmpty($completion); - } - public function test_stable_diffusion_can_be_invoked() { $seed = 0; diff --git a/python/example_code/bedrock-runtime/README.md b/python/example_code/bedrock-runtime/README.md index f220d6d331b..03eb70ca24b 100644 --- a/python/example_code/bedrock-runtime/README.md +++ b/python/example_code/bedrock-runtime/README.md @@ -80,9 +80,7 @@ python -m pip install -r requirements.txt - [Converse](models/meta_llama/converse.py#L4) - [ConverseStream](models/meta_llama/converse_stream.py#L4) -- [InvokeModel: Llama 2](models/meta_llama/llama2_invoke_model.py#L4) - [InvokeModel: Llama 3](models/meta_llama/llama3_invoke_model.py#L4) -- [InvokeModelWithResponseStream: Llama 2](models/meta_llama/llama2_invoke_model_with_response_stream.py#L4) - [InvokeModelWithResponseStream: Llama 3](models/meta_llama/llama3_invoke_model_with_response_stream.py#L4) ### Mistral AI diff --git a/python/example_code/bedrock-runtime/models/meta_llama/llama2_invoke_model.py b/python/example_code/bedrock-runtime/models/meta_llama/llama2_invoke_model.py deleted file mode 100644 index 2b6555b08df..00000000000 --- a/python/example_code/bedrock-runtime/models/meta_llama/llama2_invoke_model.py +++ /dev/null @@ -1,49 +0,0 @@ -# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -# SPDX-License-Identifier: Apache-2.0 - -# snippet-start:[python.example_code.bedrock-runtime.InvokeModel_MetaLlama2] -# Use the native inference API to send a text message to Meta Llama 2. - -import boto3 -import json - -from botocore.exceptions import ClientError - -# Create a Bedrock Runtime client in the AWS Region of your choice. -client = boto3.client("bedrock-runtime", region_name="us-east-1") - -# Set the model ID, e.g., Llama 2 Chat 13B. -model_id = "meta.llama2-13b-chat-v1" - -# Define the prompt for the model. -prompt = "Describe the purpose of a 'hello world' program in one line." - -# Embed the prompt in Llama 2's instruction format. -formatted_prompt = f"[INST] {prompt} [/INST]" - -# Format the request payload using the model's native structure. -native_request = { - "prompt": formatted_prompt, - "max_gen_len": 512, - "temperature": 0.5, -} - -# Convert the native request to JSON. -request = json.dumps(native_request) - -try: - # Invoke the model with the request. - response = client.invoke_model(modelId=model_id, body=request) - -except (ClientError, Exception) as e: - print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") - exit(1) - -# Decode the response body. -model_response = json.loads(response["body"].read()) - -# Extract and print the response text. -response_text = model_response["generation"] -print(response_text) - -# snippet-end:[python.example_code.bedrock-runtime.InvokeModel_MetaLlama2] diff --git a/python/example_code/bedrock-runtime/models/meta_llama/llama2_invoke_model_with_response_stream.py b/python/example_code/bedrock-runtime/models/meta_llama/llama2_invoke_model_with_response_stream.py deleted file mode 100644 index f3c7ad5713a..00000000000 --- a/python/example_code/bedrock-runtime/models/meta_llama/llama2_invoke_model_with_response_stream.py +++ /dev/null @@ -1,51 +0,0 @@ -# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -# SPDX-License-Identifier: Apache-2.0 - -# snippet-start:[python.example_code.bedrock-runtime.InvokeModelWithResponseStream_MetaLlama2] -# Use the native inference API to send a text message to Meta Llama 2 -# and print the response stream. - -import boto3 -import json - -from botocore.exceptions import ClientError - -# Create a Bedrock Runtime client in the AWS Region of your choice. -client = boto3.client("bedrock-runtime", region_name="us-east-1") - -# Set the model ID, e.g., Llama 2 Chat 13B. -model_id = "meta.llama2-13b-chat-v1" - -# Define the prompt for the model. -prompt = "Describe the purpose of a 'hello world' program in one line." - -# Embed the prompt in Llama 2's instruction format. -formatted_prompt = f"[INST] {prompt} [/INST]" - -# Format the request payload using the model's native structure. -native_request = { - "prompt": formatted_prompt, - "max_gen_len": 512, - "temperature": 0.5, -} - -# Convert the native request to JSON. -request = json.dumps(native_request) - -try: - # Invoke the model with the request. - streaming_response = client.invoke_model_with_response_stream( - modelId=model_id, body=request - ) - - # Extract and print the response text in real-time. - for event in streaming_response["body"]: - chunk = json.loads(event["chunk"]["bytes"]) - if "generation" in chunk: - print(chunk["generation"], end="") - -except (ClientError, Exception) as e: - print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") - exit(1) - -# snippet-end:[python.example_code.bedrock-runtime.InvokeModelWithResponseStream_MetaLlama2] diff --git a/python/example_code/bedrock-runtime/test/test_invoke_model.py b/python/example_code/bedrock-runtime/test/test_invoke_model.py index c6069235734..1193de8d162 100644 --- a/python/example_code/bedrock-runtime/test/test_invoke_model.py +++ b/python/example_code/bedrock-runtime/test/test_invoke_model.py @@ -12,7 +12,6 @@ "models/anthropic_claude/invoke_model.py", "models/cohere_command/command_invoke_model.py", "models/cohere_command/command_r_invoke_model.py", - "models/meta_llama/llama2_invoke_model.py", "models/meta_llama/llama3_invoke_model.py", "models/mistral_ai/invoke_model.py", # Embeddings models