Skip to content

execute_sampling_operation

execute_sampling_operation(
    training_session_name: TrainingSessionName,
    operation_config: SampleOperationConfig,
    *,
    config_overrides: Optional[SageMakerTrainingSessionRuntimeClientConfig] = None,
) -> ExecuteSamplingOperationOutput

Submits an asynchronous sampling operation to generate text or compute log-probabilities from the model associated with the specified training session. The session must be in Running or Blocked state. Use GetSamplingOperationResult to poll for the result.

Parameters:

  • training_session_name (TrainingSessionName) –

    The name of the training session to sample from.

  • operation_config (SampleOperationConfig) –

    The configuration for the sample operation. Specify either a sample configuration to generate text or a computeLogprobs configuration to compute token log-probabilities.

Raises:

  • ConflictException –

    The training session is in a state that cannot accept new operations. The session must be in Running or Blocked state to accept requests.

  • InternalServerException –

    An internal service error occurred. Retry the request using exponential backoff.

  • ResourceNotFoundException –

    The specified training session or operation does not exist, or the operation does not belong to the specified training session.

  • ThrottlingException –

    The request was throttled. Retry the request after a brief wait using exponential backoff.

  • ValidationException –

    The request is not valid. Check the request syntax and parameters. Examples include invalid sequence numbers, malformed client tokens, missing required fields, or invalid operation configuration.

  • UnknownServiceError –

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

Examples:

Generate samples from a prompt Generates completions for a tokenized prompt using the session's current weights. Generation behavior is controlled by samplingParams, and the operation is dispatched asynchronously.

>>> client.execute_sampling_operation(training_session_name='my-training-session', operation_config={'sample': {'prompt': {'chunks': [{'tokens': [791, 6864, 315, 9822, 374], 'type': 'encoded_text'}]}, 'numSamples': 4, 'samplingParams': {'maxTokens': 1024, 'temperature': 0.7, 'topP': 0.9}}})
Source code in docs_src/capo_sagemakertrainingsessionruntime/_services/sage_maker_training_session_runtime.py
def execute_sampling_operation(
    self,
    training_session_name: "capo_sagemakertrainingsessionruntime.types.training_session_name.TrainingSessionName",
    operation_config: "capo_sagemakertrainingsessionruntime.types.sample_operation_config.SampleOperationConfig",
    *,
    config_overrides: Optional[SageMakerTrainingSessionRuntimeClientConfig] = None,
) -> "capo_sagemakertrainingsessionruntime.types.execute_sampling_operation_output.ExecuteSamplingOperationOutput":
    """<p>Submits an asynchronous sampling operation to generate text or compute log-probabilities from the model associated with the specified training session. The session must be in <code>Running</code> or <code>Blocked</code> state. Use <code>GetSamplingOperationResult</code> to poll for the result.</p>

    Args:
        training_session_name: <p>The name of the training session to sample from.</p>
        operation_config: <p>The configuration for the sample operation. Specify either a sample configuration to generate text or a computeLogprobs configuration to compute token log-probabilities.</p>

    Raises:
        capo_sagemakertrainingsessionruntime.errors.conflict_exception.ConflictException: <p>The training session is in a state that cannot accept new operations. The session must be in <code>Running</code> or <code>Blocked</code> state to accept requests.</p>
        capo_sagemakertrainingsessionruntime.errors.internal_server_exception.InternalServerException: <p>An internal service error occurred. Retry the request using exponential backoff.</p>
        capo_sagemakertrainingsessionruntime.errors.resource_not_found_exception.ResourceNotFoundException: <p>The specified training session or operation does not exist, or the operation does not belong to the specified training session.</p>
        capo_sagemakertrainingsessionruntime.errors.throttling_exception.ThrottlingException: <p>The request was throttled. Retry the request after a brief wait using exponential backoff.</p>
        capo_sagemakertrainingsessionruntime.errors.validation_exception.ValidationException: <p>The request is not valid. Check the request syntax and parameters. Examples include invalid sequence numbers, malformed client tokens, missing required fields, or invalid operation configuration.</p>
        capo_sagemakertrainingsessionruntime.errors.UnknownServiceError: The service returned an error code this client does not model.

    Examples:
        Generate samples from a prompt
        Generates completions for a tokenized prompt using the session's current weights. Generation behavior is controlled by `samplingParams`, and the operation is dispatched asynchronously.

        >>> client.execute_sampling_operation(training_session_name='my-training-session', operation_config={'sample': {'prompt': {'chunks': [{'tokens': [791, 6864, 315, 9822, 374], 'type': 'encoded_text'}]}, 'numSamples': 4, 'samplingParams': {'maxTokens': 1024, 'temperature': 0.7, 'topP': 0.9}}})
    """

    def _handler(
        req: "OperationRequest[capo_sagemakertrainingsessionruntime.types.execute_sampling_operation_input.ExecuteSamplingOperationInput]",
    ) -> OperationResponse[
        "capo_sagemakertrainingsessionruntime.types.execute_sampling_operation_output.ExecuteSamplingOperationOutput"
    ]:
        import capo_sagemakertrainingsessionruntime._operations.lego_router_service.execute_sampling_operation

        output, http_response = (
            capo_sagemakertrainingsessionruntime._operations.lego_router_service.execute_sampling_operation.execute_sampling_operation(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    def _redact_input(
        input_: "capo_sagemakertrainingsessionruntime.types.execute_sampling_operation_input.ExecuteSamplingOperationInput",
    ) -> dict[str, Any]:
        import capo_sagemakertrainingsessionruntime.types.execute_sampling_operation_input

        return capo_sagemakertrainingsessionruntime.types.execute_sampling_operation_input.redact_sensitive(
            input_
        )

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging("ExecuteSamplingOperation", redact_input=_redact_input),
    )
    input_: capo_sagemakertrainingsessionruntime.types.execute_sampling_operation_input.ExecuteSamplingOperationInput = {
        "training_session_name": training_session_name,
        "operation_config": operation_config,
    }

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output