Skip to content

retrieve_and_generate async

retrieve_and_generate(
    input: RetrieveAndGenerateInput,
    *,
    config_overrides: Optional[AsyncBedrockAgentRuntimeClientConfig] = None,
    session_id: Optional[SessionId] = None,
    retrieve_and_generate_configuration: Optional[
        RetrieveAndGenerateConfiguration
    ] = None,
    session_configuration: Optional[RetrieveAndGenerateSessionConfiguration] = None,
    user_context: Optional[UserContext] = None,
) -> RetrieveAndGenerateResponse

Queries a knowledge base and generates responses based on the retrieved results and using the specified foundation model or inference profile. The response only cites sources that are relevant to the query.

This API cannot be used with managed knowledge bases. Use AgenticRetrieveStream or Retrieve with managed knowledge bases.

Parameters:

  • session_id (Optional[SessionId], default: None ) –

    The unique identifier of the session. When you first make a RetrieveAndGenerate request, Amazon Bedrock automatically generates this value. You must reuse this value for all subsequent requests in the same conversational session. This value allows Amazon Bedrock to maintain context and knowledge from previous interactions. You can't explicitly set the sessionId yourself.

  • input (RetrieveAndGenerateInput) –

    Contains the query to be made to the knowledge base.

  • retrieve_and_generate_configuration (Optional[RetrieveAndGenerateConfiguration], default: None ) –

    Contains configurations for the knowledge base query and retrieval process. For more information, see Query configurations.

  • session_configuration (Optional[RetrieveAndGenerateSessionConfiguration], default: None ) –

    Contains details about the session with the knowledge base.

  • user_context (Optional[UserContext], default: None ) –

    Contains information about the user making the request. This is used for access control filtering to ensure that retrieval results only include documents the user is authorized to access.

Raises:

  • AccessDeniedException –

    The request is denied because of missing access permissions. Check your permissions and retry your request.

  • BadGatewayException –

    There was an issue with a dependency due to a server issue. Retry your request.

  • ConflictException –

    There was a conflict performing an operation. Resolve the conflict and retry your request.

  • DependencyFailedException –

    There was an issue with a dependency. Check the resource configurations and retry the request.

  • InternalServerException –

    An internal server error occurred. Retry your request.

  • ResourceNotFoundException –

    The specified resource Amazon Resource Name (ARN) was not found. Check the Amazon Resource Name (ARN) and try your request again.

  • ServiceQuotaExceededException –

    The number of requests exceeds the service quota. Resubmit your request later.

  • ThrottlingException –

    The number of requests exceeds the limit. Resubmit your request later.

  • ValidationException –

    Input validation failed. Check your request parameters and retry the request.

  • UnknownServiceError –

    The service returned an error code this client does not model.

Source code in docs_src/capo_bedrock_agent_runtime/_services/async_bedrock_agent_runtime.py
async def retrieve_and_generate(
    self,
    input: "capo_bedrock_agent_runtime.types.retrieve_and_generate_input.RetrieveAndGenerateInput",
    *,
    config_overrides: Optional[AsyncBedrockAgentRuntimeClientConfig] = None,
    session_id: Optional[
        "capo_bedrock_agent_runtime.types.session_id.SessionId"
    ] = None,
    retrieve_and_generate_configuration: Optional[
        "capo_bedrock_agent_runtime.types.retrieve_and_generate_configuration.RetrieveAndGenerateConfiguration"
    ] = None,
    session_configuration: Optional[
        "capo_bedrock_agent_runtime.types.retrieve_and_generate_session_configuration.RetrieveAndGenerateSessionConfiguration"
    ] = None,
    user_context: Optional[
        "capo_bedrock_agent_runtime.types.user_context.UserContext"
    ] = None,
) -> "capo_bedrock_agent_runtime.types.retrieve_and_generate_response.RetrieveAndGenerateResponse":
    """<p>Queries a knowledge base and generates responses based on the retrieved results and using the specified foundation model or <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/cross-region-inference.html">inference profile</a>. The response only cites sources that are relevant to the query.</p> <note> <p>This API cannot be used with managed knowledge bases. Use <a href="https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_AgenticRetrieveStream.html">AgenticRetrieveStream</a> or <a href="https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Retrieve.html">Retrieve</a> with managed knowledge bases.</p> </note>

    Args:
        session_id: <p>The unique identifier of the session. When you first make a <code>RetrieveAndGenerate</code> request, Amazon Bedrock automatically generates this value. You must reuse this value for all subsequent requests in the same conversational session. This value allows Amazon Bedrock to maintain context and knowledge from previous interactions. You can't explicitly set the <code>sessionId</code> yourself.</p>
        input: <p>Contains the query to be made to the knowledge base.</p>
        retrieve_and_generate_configuration: <p>Contains configurations for the knowledge base query and retrieval process. For more information, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/kb-test-config.html">Query configurations</a>.</p>
        session_configuration: <p>Contains details about the session with the knowledge base.</p>
        user_context: <p>Contains information about the user making the request. This is used for access control filtering to ensure that retrieval results only include documents the user is authorized to access.</p>

    Raises:
        capo_bedrock_agent_runtime.errors.access_denied_exception.AccessDeniedException: <p>The request is denied because of missing access permissions. Check your permissions and retry your request.</p>
        capo_bedrock_agent_runtime.errors.bad_gateway_exception.BadGatewayException: <p>There was an issue with a dependency due to a server issue. Retry your request.</p>
        capo_bedrock_agent_runtime.errors.conflict_exception.ConflictException: <p>There was a conflict performing an operation. Resolve the conflict and retry your request.</p>
        capo_bedrock_agent_runtime.errors.dependency_failed_exception.DependencyFailedException: <p>There was an issue with a dependency. Check the resource configurations and retry the request.</p>
        capo_bedrock_agent_runtime.errors.internal_server_exception.InternalServerException: <p>An internal server error occurred. Retry your request.</p>
        capo_bedrock_agent_runtime.errors.resource_not_found_exception.ResourceNotFoundException: <p>The specified resource Amazon Resource Name (ARN) was not found. Check the Amazon Resource Name (ARN) and try your request again.</p>
        capo_bedrock_agent_runtime.errors.service_quota_exceeded_exception.ServiceQuotaExceededException: <p>The number of requests exceeds the service quota. Resubmit your request later.</p>
        capo_bedrock_agent_runtime.errors.throttling_exception.ThrottlingException: <p>The number of requests exceeds the limit. Resubmit your request later.</p>
        capo_bedrock_agent_runtime.errors.validation_exception.ValidationException: <p>Input validation failed. Check your request parameters and retry the request.</p>
        capo_bedrock_agent_runtime.errors.UnknownServiceError: The service returned an error code this client does not model.
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_bedrock_agent_runtime.types.retrieve_and_generate_request.RetrieveAndGenerateRequest]",
    ) -> AsyncOperationResponse[
        "capo_bedrock_agent_runtime.types.retrieve_and_generate_response.RetrieveAndGenerateResponse"
    ]:
        import capo_bedrock_agent_runtime._operations.amazon_bedrock_agent_run_time_service.retrieve_and_generate

        (
            output,
            http_response,
        ) = await capo_bedrock_agent_runtime._operations.amazon_bedrock_agent_run_time_service.retrieve_and_generate.async_retrieve_and_generate(
            req.options, req.input
        )
        return AsyncOperationResponse(output=output, response=http_response)

    def _redact_input(
        input_: "capo_bedrock_agent_runtime.types.retrieve_and_generate_request.RetrieveAndGenerateRequest",
    ) -> dict[str, Any]:
        import capo_bedrock_agent_runtime.types.retrieve_and_generate_request

        return capo_bedrock_agent_runtime.types.retrieve_and_generate_request.redact_sensitive(
            input_
        )

    def _redact_output(
        output: "capo_bedrock_agent_runtime.types.retrieve_and_generate_response.RetrieveAndGenerateResponse",
    ) -> dict[str, Any]:
        import capo_bedrock_agent_runtime.types.retrieve_and_generate_response

        return capo_bedrock_agent_runtime.types.retrieve_and_generate_response.redact_sensitive(
            output
        )

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging(
            "RetrieveAndGenerate",
            redact_input=_redact_input,
            redact_output=_redact_output,
        ),
    )
    input_: capo_bedrock_agent_runtime.types.retrieve_and_generate_request.RetrieveAndGenerateRequest = {
        "input": input
    }
    if session_id is not None:
        input_["session_id"] = session_id
    if retrieve_and_generate_configuration is not None:
        input_["retrieve_and_generate_configuration"] = (
            retrieve_and_generate_configuration
        )
    if session_configuration is not None:
        input_["session_configuration"] = session_configuration
    if user_context is not None:
        input_["user_context"] = user_context

    response = await aexecute_pipeline(
        AsyncOperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    await response.response.aclose()
    return response.output