Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

how to use InvokeAgent? #4986

noday started this conversation in General
Mar 1, 2024 · 4 comments · 3 replies
Discussion options

InvokeAgentRequest req = InvokeAgentRequest.builder()
				.agentId("")
				.agentAliasId("")
				.sessionId("")
				.inputText("who is jack ma")
				.build();
		BedrockAgentRuntimeAsyncClient client = BedrockAgentRuntimeAsyncClient.builder()
				.credentialsProvider(()-> AwsBasicCredentials.create("", ""))
				.region(Region.US_EAST_1)
				.build();

how to get the answer, can not find example code.

You must be logged in to vote

Replies: 4 comments 3 replies

Comment options

You must be logged in to vote
2 replies
Comment options

thanks

Comment options

I'd like to point out that 3 different AI services couldn't figure this out either. And to be honest, they are pretty smart.

Comment options

 /**
 * Invokes the Anthropic Claude 2 model and processes the response stream.
 *
 * @param prompt The prompt for Claude to complete.
 * @param silent Suppress console output of the individual response stream
 * chunks.
 * @return The generated response.
 */
 public static String invokeClaude(String prompt, boolean silent) {
 BedrockRuntimeAsyncClient client = BedrockRuntimeAsyncClient.builder()
 .region(Region.US_EAST_1)
 .credentialsProvider(ProfileCredentialsProvider.create())
 .build();
 var finalCompletion = new AtomicReference<>("");
 var payload = new JSONObject()
 .put("prompt", "Human: " + prompt + " Assistant:")
 .put("temperature", 0.8)
 .put("max_tokens_to_sample", 300)
 .toString();
 var request = InvokeModelWithResponseStreamRequest.builder()
 .body(SdkBytes.fromUtf8String(payload))
 .modelId("anthropic.claude-v2")
 .contentType("application/json")
 .accept("application/json")
 .build();
 var visitor = InvokeModelWithResponseStreamResponseHandler.Visitor.builder()
 .onChunk(chunk -> {
 var json = new JSONObject(chunk.bytes().asUtf8String());
 var completion = json.getString("completion");
 finalCompletion.set(finalCompletion.get() + completion);
 if (!silent) {
 System.out.print(completion);
 }
 })
 .build();
 var handler = InvokeModelWithResponseStreamResponseHandler.builder()
 .onEventStream(stream -> stream.subscribe(event -> event.accept(visitor)))
 .onComplete(() -> {
 })
 .onError(e -> System.out.println("\n\nError: " + e.getMessage()))
 .build();
 client.invokeModelWithResponseStream(request, handler).join();
 return finalCompletion.get();
 }
You must be logged in to vote
1 reply
Comment options

This code is great, but to the original question and one that I share. This is to invoke a specific model in bedrock, not an agent. An agent has pre-defined model(s) and is packaged in such a way that callers simply invoke the agent to accomplish a task.

Comment options

do you have any way to invoke BedrockAgentRuntimeAsyncClient and get output in response

You must be logged in to vote
0 replies
Comment options

Something approximating this:

 BedrockAgentRuntimeAsyncClient client = BedrockAgentRuntimeAsyncClient.create();
 // Prepare the request
 InvokeAgentRequest agentRequest = InvokeAgentRequest.builder()
 .agentId("your-agent-id")
 .agentAliasId("your-alias-id")
 .sessionId("your-session-id")
 .inputText("Hello, agent!")
 .build();
 // CompletableFuture to hold the final response
 CompletableFuture<String> responseFuture = new CompletableFuture<>();
 // Invoke the agent asynchronously with a custom response handler
 client.invokeAgent(agentRequest, new InvokeAgentResponseHandler() {
 private StringBuilder fullResponse = new StringBuilder();
 @Override
 public void complete() {
 responseFuture.complete(fullResponse.toString());
 }
 @Override
 public void responseReceived(InvokeAgentResponse invokeAgentResponse) {
 }
 @Override
 public void onEventStream(SdkPublisher<ResponseStream> sdkPublisher) {
 sdkPublisher.subscribe(event -> {
 if (event instanceof PayloadPart) {
 PayloadPart payload = (PayloadPart) event;
 String chunk = StandardCharsets.UTF_8.decode(payload.bytes().asByteBuffer()).toString();
 fullResponse.append(chunk);
 }
 });
 }
 @Override
 public void exceptionOccurred(Throwable throwable) {
 responseFuture.completeExceptionally(throwable);
 }
 });
 try {
 // Wait for the response synchronously
 String finalResponse = responseFuture.get();
 return ChatResponse.builder()
 .response(finalResponse)
 .build();
 }catch (Exception e) {
 throw new RuntimeException(e);
 } finally {
 client.close();
 }

Haven't tested it yet, but while this browser tab is open I figured I would share progress for anyone who googles their way in here.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /