aws-doc-sdk-examples icon indicating copy to clipboard operation
aws-doc-sdk-examples copied to clipboard

dotnetv3: please add BedrockAgentRuntime sample

Open hamano-yu opened this issue 1 year ago • 1 comments

Background story

Hello, Could you provide sample code for .NET's Amazon.BedrockAgentRuntime?

I have created an AWS Bedrock Agent on the AWS Console and confirmed its operation. Afterward, I am trying to retrieve the results of the AWS Bedrock Agent using C# (with the intention of ultimately running it on Unity).

I referred to this code (https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/dotnetv3/Bedrock-runtime/Models/AnthropicClaude/ConverseStream/ConverseStream.cs) and wrote my source code, but I am unable to display the response results on the console.

Below is the source code I wrote. I would greatly appreciate your assistance. Thank you in advance!

using System;
using System.Collections.Generic;
using System.Linq;
using Amazon;
using Amazon.BedrockAgentRuntime;
using Amazon.BedrockAgentRuntime.Model;
using Amazon.BedrockRuntime.Model;

// Create a Bedrock Runtime client in the AWS Region you want to use.
var client = new AmazonBedrockAgentRuntimeClient(RegionEndpoint.APNortheast1);

string agentId = <agentid>;
string agentAliasId = <agentaliasid>;
string sessionId = <sessionid>;
string prompt = "hello agent.";

// Create a request with the model ID, the user message, and an inference configuration.
// Prepare the request
var request = new InvokeAgentRequest
{
    AgentId = agentId,
    AgentAliasId = agentAliasId,
    SessionId = sessionId,
    InputText = prompt
}; 

try
{
    // Send the request to the Bedrock Runtime and wait for the result.
    var response = await client.InvokeAgentAsync(request);

    // Extract and print the streamed response text in real-time.
    foreach (var chunk in response.Completion.AsEnumerable())
    {
        Console.Write(chunk);

    }
}
catch (BedrockAgentRuntimeEventStreamException e)
{
    Console.WriteLine($"ERROR: Can't invoke '{agentId}'. Reason: {e.Message}");
}

What does this example accomplish?

I want to retrieve the results of AWS Bedrock Agent using .NET.

Which AWS service(s)?

Amazon Bedrock

Which AWS SDKs or tools?

  • [ ] All languages
  • [X] .NET
  • [ ] C++
  • [ ] Go (v2)
  • [ ] Java
  • [ ] Java (v2)
  • [ ] JavaScript
  • [ ] JavaScript (v3)
  • [ ] Kotlin
  • [ ] PHP
  • [ ] Python
  • [ ] Ruby
  • [ ] Rust
  • [ ] Swift
  • [ ] Not applicable

Are there existing code examples to leverage?

No response

Do you have any reference code?

No response

hamano-yu avatar Nov 25 '24 02:11 hamano-yu

Back from PTO, happy new year!

I'm planning to add some Bedrock Agents code examples across a few languages anyway, so I'm happy to pick this up.

DennisTraub avatar Jan 14 '25 10:01 DennisTraub