Code Monkey home page Code Monkey logo

Comments (4)

ashishdhingra avatar ashishdhingra commented on June 26, 2024

@JulesMBooth Good morning. Thanks for opening the issue. Could you please enable verbose logging using code below and share the detailed logs:

Amazon.AWSConfigs.LoggingConfig.LogResponses = Amazon.ResponseLoggingOption.Always;
Amazon.AWSConfigs.LoggingConfig.LogTo = Amazon.LoggingOptions.Console;
Amazon.AWSConfigs.AddTraceListener("Amazon", new System.Diagnostics.ConsoleTraceListener());

Also, could you examine the behavior of the similar API call made using AWS CLI (preferably with --debug switch to examine detailed logs)?

Thanks,
Ashish

from aws-sdk-net.

JulesMBooth avatar JulesMBooth commented on June 26, 2024

Hi,

I noticed when attempting the request by CLI that I had missed a parameter in the RetrieveAndGenerateConfiguration object: "Type" I set that to "KNOWLEDGE_BASE". Having examined the error response to that I realised I could only pass in a Session Id once I had got a response. So my code now looks like:

....
    Console.WriteLine("Please enter the Knowledge Base Id");

    string? kbId = Console.ReadLine();

    Console.WriteLine("Please enter your first prompt, after this type exit to stop prompting bedrock.");

    var input = Console.ReadLine();

    var sessionId = "";
    while (input?.ToUpperInvariant() != "EXIT")
    {    
        var response = await InvokeBedrockRetrieveAndGenerateKnowledgeBaseAsync(kbId, input, sessionId);

        if (response != null)
        {
            Console.WriteLine(response.Output.Text);

            sessionId = response.SessionId;
        }
        else
        {
            Console.WriteLine("Error Occurred");
        }

        Console.WriteLine("Please write your next prompt or type exit to leave.");
        input = Console.ReadLine();
    }
.....
  public static async Task<RetrieveAndGenerateResponse> InvokeBedrockRetrieveAndGenerateKnowledgeBaseAsync(string knowledgeBaseId, string prompt, string sessionId = "")
  {
      RetrieveAndGenerateResponse? result = null;
      try
      {
          AmazonBedrockAgentRuntimeClient client = new(RegionEndpoint.USEast1);

          Console.WriteLine($"Calling bedrock with Knowledge Base Id: {knowledgeBaseId}, prompt: {prompt} and {(String.IsNullOrWhiteSpace(sessionId) ? "No session Id" : $"Session Id: {sessionId}")}.");

          var request = new RetrieveAndGenerateRequest
          {
              Input = new RetrieveAndGenerateInput
              {
                  Text = prompt
              },
              RetrieveAndGenerateConfiguration = new RetrieveAndGenerateConfiguration
              {
                  Type = "KNOWLEDGE_BASE",
                  KnowledgeBaseConfiguration = new KnowledgeBaseRetrieveAndGenerateConfiguration
                  {
                      KnowledgeBaseId = knowledgeBaseId,
                      ModelArn = "arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-v2",
                  }
              },
          };

          if (!String.IsNullOrWhiteSpace(sessionId))
              request.SessionId = sessionId;

          RetrieveAndGenerateResponse response = await client.RetrieveAndGenerateAsync(request);
          
          if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
          {
              result = response;
          }
          else
          {
              Console.WriteLine("InvokeModelAsync failed with status code " + response.HttpStatusCode);
          }
      }
      catch (Exception ex)
      {
          Console.WriteLine(ex.ToString());
      }

      return result;
  }

And this works as expected. Not clear from the .Net docs how RetrieveAndGenerateConfiguration.Type should be set however so that could be improved.

I also found that I coudn't get requests to work on the CLI I have attached output.
BedrockCLI.txt

from aws-sdk-net.

ashishdhingra avatar ashishdhingra commented on June 26, 2024

@JulesMBooth Thanks for the update and glad that it works for you now. AWS CLI appears to be failing with error Could not connect to the endpoint URL.. Unsure about the root cause of the error.

Regarding documentation:

  • Agents for Amazon Bedrock Runtime RetrieveAndGenerateConfiguration API reference does specify valid values (currently only KNOWLEDGE_BASE) for type property.
  • The AWS .NET SDK API reference for RetrieveAndGenerateConfiguration does not appear to specify list of valid values. This would be the case with any property, not just this property.
    • This is also the case with Java SDK API reference here.
    • The API reference for SDK(s) are generated from the models shared by service team, for AWS .NET SDK it's here. These are same across all SDK(s).
    • I would advise you to submit the feedback request by clicking link. It would be routed through appropriate channels.

Closing this ticket for now since this is no longer an issue.

Thanks,
Ashish

from aws-sdk-net.

github-actions avatar github-actions commented on June 26, 2024

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

from aws-sdk-net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.