Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .changeset/new-tigers-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---

Check warning on line 1 in .changeset/new-tigers-guess.md

View workflow job for this annotation

GitHub Actions / grammar-check

[vale] reported by reviewdog 🐶 [SAP.Readability] The text is very complex! It has a grade score of >14. Raw Output: {"message": "[SAP.Readability] The text is very complex! It has a grade score of \u003e14.", "location": {"path": ".changeset/new-tigers-guess.md", "range": {"start": {"line": 1, "column": 1}}}, "severity": "WARNING"}
'@sap-ai-sdk/rpt': minor
---

[feat] Add generic HTTP request configuration support.
The `predictWithSchema()` and `predictWithoutSchema()` methods now accept an optional `customRequest` parameter of type `RptRequestOptions`, allowing configuration of custom HTTP request options such as headers, timeout, and middlewares.
7 changes: 7 additions & 0 deletions .changeset/true-dolls-say.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@sap-ai-sdk/rpt': minor
---

[feat] Add predict request compression support.
All requests with a body of 1024 bytes or larger will be automatically compressed with `gzip` by default, unless configured otherwise.
Compression configuration is available via the `requestCompression` property on the `RptClientConfig` object.
1 change: 1 addition & 0 deletions packages/rpt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@sap-ai-sdk/ai-api": "workspace:^",
"@sap-ai-sdk/core": "workspace:^",
"@sap-cloud-sdk/connectivity": "^4.4.0",
"@sap-cloud-sdk/http-client": "^4.4.0",
"@sap-cloud-sdk/util": "^4.4.0"
}
}
48 changes: 37 additions & 11 deletions packages/rpt/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import {
getFoundationModelDeploymentId,
getResourceGroup
} from '@sap-ai-sdk/ai-api/internal.js';
import { RptApi } from './internal.js';
import { type DataSchema, type PredictionData } from './types.js';
import { executeRequest } from '@sap-ai-sdk/core';
import { compressRequest } from './vendor/index.js';
import type { DataSchema, PredictionData, RptRequestOptions } from './types.js';
import type {
PredictRequestPayload,
PredictResponsePayload
Expand Down Expand Up @@ -32,36 +33,42 @@ export class RptClient {
* Prefer using this method when the data schema is known.
* @param dataSchema - Prediction data follows this schema. When using TypeScript, the data schema type is used to infer the types of the prediction data. In that case, the data schema must be provided as a constant (`as const`).
* @param predictionData - Data to base prediction on.
* @param customRequest - Custom request options.
* @returns Prediction response.
*/
async predictWithSchema<const T extends DataSchema>(
dataSchema: T,
predictionData: PredictionData<T>
predictionData: PredictionData<T>,
customRequest: RptRequestOptions = {}
): Promise<PredictResponsePayload> {
return this.executePrediction(predictionData, dataSchema);
return this.executePrediction(predictionData, dataSchema, customRequest);
}

/**
* Predict based on prediction data with data schema inferred.
* Prefer using `predictWithSchema` when the data schema is known.
* @param predictionData - Data to base prediction on.
* @param customRequest - Custom request options.
* @returns Prediction response.
*/
async predictWithoutSchema(
predictionData: PredictionData<DataSchema>
predictionData: PredictionData<DataSchema>,
customRequest: RptRequestOptions = {}
): Promise<PredictResponsePayload> {
return this.executePrediction(predictionData);
return this.executePrediction(predictionData, undefined, customRequest);
}

/**
* Predict based on data schema and prediction data.
* @param predictionData - Data to base prediction on.
* @param dataSchema - Prediction data follows this schema.
* @param customRequestAll - Custom request options.
* @returns Prediction response.
*/
private async executePrediction<const T extends DataSchema>(
predictionData: PredictionData<T>,
dataSchema?: T
dataSchema?: T,
customRequestAll: RptRequestOptions = {}
): Promise<PredictResponsePayload> {
const deploymentId = await getFoundationModelDeploymentId(
this.modelDeployment,
Expand All @@ -83,9 +90,28 @@ export class RptClient {
...predictionData
} satisfies PredictRequestPayload;

return RptApi.predict(body)
.setBasePath(`/inference/deployments/${deploymentId}`)
.addCustomHeaders({ 'ai-resource-group': resourceGroup || 'default' })
.execute(this.destination);
const { requestCompression, ...customRequest } = customRequestAll;

if (requestCompression?.mode !== false) {
customRequest.middleware = [
compressRequest(requestCompression),
...(customRequest.middleware || [])
];
}

const response = await executeRequest(
{
url: `/inference/deployments/${deploymentId}/predict`,
resourceGroup: resourceGroup || 'default'
},
body,
customRequest,
this.destination
);

if (response.data) {
return response.data;
}
throw new Error('No data received from RPT prediction request');
}
}
9 changes: 8 additions & 1 deletion packages/rpt/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
export * from './client.js';
export type { PredictionData, DateString } from './types.js';
export type {
PredictionData,
DateString,
RptRequestOptions,
RptRequestCompressionAlgorithm,
RptRequestCompressionMiddlewareOptions
} from './types.js';
export type {
PredictResponseMetadata,
PredictResponsePayload,
PredictResponseStatus
} from './client/rpt/schema/index.js';
export type { RequestCompressionMiddlewareOptions } from './vendor/index.js';
39 changes: 39 additions & 0 deletions packages/rpt/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import type { Xor } from '@sap-cloud-sdk/util';
import type { ColumnType, SchemaFieldConfig } from './client/rpt/index.js';
import type { CustomRequestConfig } from '@sap-cloud-sdk/http-client';
import type {
RequestCompressionMiddlewareOptions,
RequestCompressionAlgorithm
} from './vendor/index.js';

export type { RequestCompressionMiddlewareOptions } from './vendor/index.js';

/**
* Represents a string literal type that includes all column names from the data schema.
Expand Down Expand Up @@ -121,3 +128,35 @@ export type PredictionData<T extends DataSchema> = {
columns: ColType<T>;
}
>;

/**
* Compression algorithm to use for request Body.
*/
export type RptRequestCompressionAlgorithm = Extract<
'gzip',
RequestCompressionAlgorithm
>;

/**
* Compression middleware options for requests to the RPT service endpoint.
* @template C - The compression algorithm type.
*/
export type RptRequestCompressionMiddlewareOptions<
C extends RptRequestCompressionAlgorithm = 'gzip'
> = RequestCompressionMiddlewareOptions<C>;

/**
* Custom options for how requests to the RPT service endpoint are performed.
* @template C - The compression algorithm type.
*/
export interface RptRequestOptions<
C extends RptRequestCompressionAlgorithm = 'gzip'
> extends CustomRequestConfig {
/**
* Options to configure request compression.
* @remarks This option does not affect responses, only requests.
* Prediction requests with parquet will not be compressed even if the option is set, as they are already in a compressed binary format.
* Compression will be disabled if custom middlewares are provided in the destination or fetch options.
*/
requestCompression?: RptRequestCompressionMiddlewareOptions<C>;
}
Loading
Loading