Skip to content
Draft
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions docs-js/rpt.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Use the `predictWithSchema()` method to make predictions with a schema explicitl
Pass the data schema, and the prediction data including the prediction configuration and input data to the method.
Prefer providing the data schema if it is known instead of relying on the auto-inference solely based on the input data.

For information about custom request options such as compression, custom headers, or timeout, refer to the [Advanced Usage](#advanced-usage) section.

```typescript
const prediction = await client.predictWithSchema(
// Data schema
Expand Down Expand Up @@ -146,6 +148,8 @@ To make predictions without providing a schema, use the `predictWithoutSchema()`
Pass only the prediction data as a parameter.
The data schema will be inferred from the input data.

For information about custom request options such as compression, custom headers, or timeout, refer to the [Advanced Usage](#advanced-usage) section.

```typescript
const prediction = await client.predictWithoutSchema(
// Prediction data
Expand Down Expand Up @@ -180,3 +184,117 @@ const prediction = await client.predictWithoutSchema(
}
);
```

## Advanced Usage

The RPT client supports advanced features for specialized use cases.

### Request Compression

The RPT client automatically compresses prediction requests to improve performance for large datasets.
By default, requests with a body of 1024 bytes or larger are automatically compressed using `gzip`.

To customize compression settings, pass a `customRequest` parameter with a `requestCompression` configuration to the `predictWithSchema()` or `predictWithoutSchema()` methods:

<!-- vale off -->

```typescript
import * as zlib from 'zlib';

const prediction = await client.predictWithSchema(schema, predictionData, {
requestCompression: {
algorithm: 'gzip', // Only 'gzip' is currently supported for RPT
mode: true, // Force compression regardless of payload size (default 'auto')
autoCompressMinSize: 2048 // Only compress payloads >= 2048 bytes in 'auto' mode (default 1024)
compressOptions: {
level: zlib.constants.Z_BEST_SPEED // Custom compression level
}
}
});
```

<!-- vale on -->

### Custom Request Options

The `customRequest` parameter also supports additional HTTP request configuration options from the SAP Cloud SDK for JavaScript, such as:

- `headers`: Custom HTTP headers.
- `timeout`: Request timeout in milliseconds.
- `middleware`: Custom HTTP middleware functions.

:::warning

The compression middleware is automatically prepended to the middleware chain, unless disabled by setting `mode: false`.

:::

Example with custom headers:

```typescript
const prediction = await client.predictWithSchema(schema, predictionData, {
headers: {
'x-custom-header': 'value'
},
timeout: 30000, // 30 seconds
requestCompression: {
mode: true
}
});
```

## Resilience

The `@sap-cloud-sdk/resilience` package provides built-in resilience features to handle transient failures and improve reliability.
For advanced resilience patterns, refer to the [SAP Cloud SDK documentation on resilience](https://sap.github.io/cloud-sdk/docs/js/guides/resilience).

### Retry

The HTTP client automatically retries failed requests on transient errors such as network issues or temporary server errors.
Customize retry behavior using the resilience library.

Set a custom retry count:

```typescript
import { retry } from '@sap-cloud-sdk/resilience';

const prediction = await client.predictWithSchema(schema, predictionData, {
middleware: [
retry({ maxRetries: 3, backoffType: 'exponential', initialDelay: 1000 })
],
requestCompression: {
mode: false // Disable automatic compression, compression middleware will not be prepended
}
});
```

### Timeout

Configure timeout for requests to prevent hanging connections.
The default timeout is 10 seconds.

```typescript
const prediction = await client.predictWithSchema(schema, predictionData, {
timeout: 30000 // 30 seconds
});
```

### Combining Resilience Patterns

Combine multiple resilience patterns for comprehensive protection:

<!-- vale off -->

```typescript
import { retry, timeout, circuitBreaker } from '@sap-cloud-sdk/resilience';

const prediction = await client.predictWithSchema(schema, predictionData, {
middleware: [
retry({ maxRetries: 3, backoffType: 'exponential', initialDelay: 1000 }),
circuitBreaker({ failureThreshold: 5, recoveryTimeout: 60000 }),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it could be useful to explain why both the timeouts are needed and what is their precedence and role.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reorganised the section a bit to also also show resilience(), I've replace the timeout: property with the middleware - this was a copy-paste error.

timeout(30000)

});
```

<!-- vale on -->