invokeAgent method

Future<InvokeAgentResponse> invokeAgent({
  1. required String agentAliasId,
  2. required String agentId,
  3. required String sessionId,
  4. BedrockModelConfigurations? bedrockModelConfigurations,
  5. bool? enableTrace,
  6. bool? endSession,
  7. String? inputText,
  8. String? memoryId,
  9. PromptCreationConfigurations? promptCreationConfigurations,
  10. SessionState? sessionState,
  11. String? sourceArn,
  12. StreamingConfigurations? streamingConfigurations,
})

Sends a prompt for the agent to process and respond to. Note the following fields for the request:

  • To continue the same conversation with an agent, use the same sessionId value in the request.
  • To activate trace enablement, turn enableTrace to true. Trace enablement helps you follow the agent's reasoning process that led it to the information it processed, the actions it took, and the final result it yielded. For more information, see Trace enablement.
  • End a conversation by setting endSession to true.
  • In the sessionState object, you can include attributes for the session or prompt or, if you configured an action group to return control, results from invocation of the action group.
The response contains both chunk and trace attributes.

The final response is returned in the bytes field of the chunk object. The InvokeAgent returns one chunk for the entire interaction.

  • The attribution object contains citations for parts of the response.
  • If you set enableTrace to true in the request, you can trace the agent's steps and reasoning process that led it to the response.
  • If the action predicted was configured to return control, the response returns parameters for the action, elicited from the user, in the returnControl field.
  • Errors are also surfaced in the response.

May throw AccessDeniedException. May throw BadGatewayException. May throw ConflictException. May throw DependencyFailedException. May throw InternalServerException. May throw ModelNotReadyException. May throw ResourceNotFoundException. May throw ServiceQuotaExceededException. May throw ThrottlingException. May throw ValidationException.

Parameter agentAliasId : The alias of the agent to use.

Parameter agentId : The unique identifier of the agent to use.

Parameter sessionId : The unique identifier of the session. Use the same value across requests to continue the same conversation.

Parameter bedrockModelConfigurations : Model performance settings for the request.

Parameter enableTrace : Specifies whether to turn on the trace or not to track the agent's reasoning process. For more information, see Trace enablement.

Parameter endSession : Specifies whether to end the session with the agent or not.

Parameter inputText : The prompt text to send the agent.

Parameter memoryId : The unique identifier of the agent memory.

Parameter promptCreationConfigurations : Specifies parameters that control how the service populates the agent prompt for an InvokeAgent request. You can control which aspects of previous invocations in the same agent session the service uses to populate the agent prompt. This gives you more granular control over the contextual history that is used to process the current request.

Parameter sessionState : Contains parameters that specify various attributes of the session. For more information, see Control session context.

Parameter sourceArn : The ARN of the resource making the request.

Parameter streamingConfigurations : Specifies the configurations for streaming.

Implementation

Future<InvokeAgentResponse> invokeAgent({
  required String agentAliasId,
  required String agentId,
  required String sessionId,
  BedrockModelConfigurations? bedrockModelConfigurations,
  bool? enableTrace,
  bool? endSession,
  String? inputText,
  String? memoryId,
  PromptCreationConfigurations? promptCreationConfigurations,
  SessionState? sessionState,
  String? sourceArn,
  StreamingConfigurations? streamingConfigurations,
}) async {
  final headers = <String, String>{
    if (sourceArn != null) 'x-amz-source-arn': sourceArn.toString(),
  };
  final $payload = <String, dynamic>{
    if (bedrockModelConfigurations != null)
      'bedrockModelConfigurations': bedrockModelConfigurations,
    if (enableTrace != null) 'enableTrace': enableTrace,
    if (endSession != null) 'endSession': endSession,
    if (inputText != null) 'inputText': inputText,
    if (memoryId != null) 'memoryId': memoryId,
    if (promptCreationConfigurations != null)
      'promptCreationConfigurations': promptCreationConfigurations,
    if (sessionState != null) 'sessionState': sessionState,
    if (streamingConfigurations != null)
      'streamingConfigurations': streamingConfigurations,
  };
  final response = await _protocol.sendRaw(
    payload: $payload,
    method: 'POST',
    requestUri:
        '/agents/${Uri.encodeComponent(agentId)}/agentAliases/${Uri.encodeComponent(agentAliasId)}/sessions/${Uri.encodeComponent(sessionId)}/text',
    headers: headers,
    exceptionFnMap: _exceptionFns,
  );
  final $json = await _s.jsonFromResponse(response);
  return InvokeAgentResponse(
    completion: ResponseStream.fromJson($json),
    contentType: _s.extractHeaderStringValue(
        response.headers, 'x-amzn-bedrock-agent-content-type')!,
    sessionId: _s.extractHeaderStringValue(
        response.headers, 'x-amz-bedrock-agent-session-id')!,
    memoryId: _s.extractHeaderStringValue(
        response.headers, 'x-amz-bedrock-agent-memory-id'),
  );
}