requestStream method

Stream<StreamingPromptResponse> requestStream(
  1. Map<String, String>? templateVariables
)

Requests an AI streaming prompt. The prompt is streamed from the API gateway in chunks.

Param templateVariables is a map of template variables to use for the prompt request. throws MissingPromptVariableException if a template variable is missing. throws an APIGatewayException if the API gateway returns an error.

Implementation

Stream<StreamingPromptResponse> requestStream(
  Map<String, String>? templateVariables,
) {
  if (_isDebug) {
    _logger.fine("requesting streaming prompt");
  }

  final stream = _stub.requestStreamingPrompt(
    _createPromptRequest(templateVariables),
    options: CallOptions(
      metadata: {
        "authorization": "Bearer $_apiToken",
      },
    ),
  );

  return stream.handleError((e) {
    if (_isDebug) {
      _logger.fine("exception streaming prompt: $e");
    }
    if (e.code == StatusCode.invalidArgument) {
      throw MissingPromptVariableException(
          e.message ?? _missingTemplateVariableMessage);
    }
    throw APIGatewayException(e.message ?? _apiGatewayErrorMessage);
  });
}