-
Couldn't load subscription status.
- Fork 947
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 4 comments 3 replies
-
Check the Bedrock code examples in our Developer Guide here:
Beta Was this translation helpful? Give feedback.
All reactions
-
thanks
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
/**
* 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();
}
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
do you have any way to invoke BedrockAgentRuntimeAsyncClient and get output in response
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.