@@ -2,8 +2,10 @@ import {
22 getFoundationModelDeploymentId ,
33 getResourceGroup
44} from '@sap-ai-sdk/ai-api/internal.js' ;
5- import { RptApi } from './internal.js' ;
6- import { type DataSchema , type PredictionData } from './types.js' ;
5+ import { createLogger } from '@sap-cloud-sdk/util' ;
6+ import { executeRequest } from '@sap-ai-sdk/core' ;
7+ import { compressRequest } from './vendor/index.js' ;
8+ import type { DataSchema , PredictionData , RptRequestOptions } from './types.js' ;
79import type {
810 PredictRequestPayload ,
911 PredictResponsePayload
@@ -12,6 +14,11 @@ import type { SapRptModel } from '@sap-ai-sdk/core/internal.js';
1214import type { ModelDeployment } from '@sap-ai-sdk/ai-api' ;
1315import type { HttpDestinationOrFetchOptions } from '@sap-cloud-sdk/connectivity' ;
1416
17+ const logger = createLogger ( {
18+ package : 'rpt' ,
19+ messageContext : 'RptClient'
20+ } ) ;
21+
1522/**
1623 * Representation of an RPT client to make predictions.
1724 * @experimental This class is experimental and may change at any time without prior notice.
@@ -32,36 +39,42 @@ export class RptClient {
3239 * Prefer using this method when the data schema is known.
3340 * @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`).
3441 * @param predictionData - Data to base prediction on.
42+ * @param customRequest - Custom request options.
3543 * @returns Prediction response.
3644 */
3745 async predictWithSchema < const T extends DataSchema > (
3846 dataSchema : T ,
39- predictionData : PredictionData < T >
47+ predictionData : PredictionData < T > ,
48+ customRequest : RptRequestOptions = { }
4049 ) : Promise < PredictResponsePayload > {
41- return this . executePrediction ( predictionData , dataSchema ) ;
50+ return this . executePrediction ( predictionData , dataSchema , customRequest ) ;
4251 }
4352
4453 /**
4554 * Predict based on prediction data with data schema inferred.
4655 * Prefer using `predictWithSchema` when the data schema is known.
4756 * @param predictionData - Data to base prediction on.
57+ * @param customRequest - Custom request options.
4858 * @returns Prediction response.
4959 */
5060 async predictWithoutSchema (
51- predictionData : PredictionData < DataSchema >
61+ predictionData : PredictionData < DataSchema > ,
62+ customRequest : RptRequestOptions = { }
5263 ) : Promise < PredictResponsePayload > {
53- return this . executePrediction ( predictionData ) ;
64+ return this . executePrediction ( predictionData , undefined , customRequest ) ;
5465 }
5566
5667 /**
5768 * Predict based on data schema and prediction data.
5869 * @param predictionData - Data to base prediction on.
5970 * @param dataSchema - Prediction data follows this schema.
71+ * @param customRequestAll - Custom request options.
6072 * @returns Prediction response.
6173 */
6274 private async executePrediction < const T extends DataSchema > (
6375 predictionData : PredictionData < T > ,
64- dataSchema ?: T
76+ dataSchema ?: T ,
77+ customRequestAll : RptRequestOptions = { }
6578 ) : Promise < PredictResponsePayload > {
6679 const deploymentId = await getFoundationModelDeploymentId (
6780 this . modelDeployment ,
@@ -83,9 +96,32 @@ export class RptClient {
8396 ...predictionData
8497 } satisfies PredictRequestPayload ;
8598
86- return RptApi . predict ( body )
87- . setBasePath ( `/inference/deployments/${ deploymentId } ` )
88- . addCustomHeaders ( { 'ai-resource-group' : resourceGroup || 'default' } )
89- . execute ( this . destination ) ;
99+ const { requestCompression, ...customRequest } = customRequestAll ;
100+
101+ if ( requestCompression ?. mode !== false ) {
102+ if ( customRequest . middlewares ) {
103+ logger . warn (
104+ 'Custom middlewares are not supported when request compression is enabled. ' +
105+ 'The compression middleware will not be applied. ' +
106+ 'Please disable request compression in RptRequestOptions to avoid this warning (`requestCompression.mode = false`).'
107+ ) ;
108+ }
109+ customRequest . middlewares = [ compressRequest ( requestCompression ) ] ;
110+ }
111+
112+ const response = await executeRequest (
113+ {
114+ url : `/inference/deployments/${ deploymentId } /predict` ,
115+ resourceGroup : resourceGroup || 'default'
116+ } ,
117+ body ,
118+ customRequest ,
119+ this . destination
120+ ) ;
121+
122+ if ( response . data ) {
123+ return response . data ;
124+ }
125+ throw new Error ( 'No data received from RPT prediction request' ) ;
90126 }
91127}
0 commit comments