@jerome-benoit/sap-ai-provider-v2 4.2.3 → 4.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +244 -234
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +245 -235
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -29752,7 +29752,6 @@ __export(index_exports, {
|
|
|
29752
29752
|
buildTranslationConfig: () => import_orchestration3.buildTranslationConfig,
|
|
29753
29753
|
createSAPAIProvider: () => createSAPAIProvider,
|
|
29754
29754
|
getProviderName: () => getProviderName,
|
|
29755
|
-
isConfigReference: () => import_orchestration3.isConfigReference,
|
|
29756
29755
|
sapAIEmbeddingProviderOptions: () => sapAIEmbeddingProviderOptions,
|
|
29757
29756
|
sapAILanguageModelProviderOptions: () => sapAILanguageModelProviderOptions,
|
|
29758
29757
|
sapai: () => sapai
|
|
@@ -30004,46 +30003,45 @@ function convertToAISDKError(error, context) {
|
|
|
30004
30003
|
});
|
|
30005
30004
|
}
|
|
30006
30005
|
}
|
|
30007
|
-
const responseHeaders = context?.responseHeaders ?? getAxiosResponseHeaders(error);
|
|
30008
30006
|
if (rootError instanceof Error) {
|
|
30009
30007
|
const errorMsg = rootError.message.toLowerCase();
|
|
30010
|
-
const
|
|
30008
|
+
const originalErrorMsg = rootError.message;
|
|
30011
30009
|
if (errorMsg.includes("authentication") || errorMsg.includes("unauthorized") || errorMsg.includes("aicore_service_key") || errorMsg.includes("invalid credentials") || errorMsg.includes("service credentials") || errorMsg.includes("service binding")) {
|
|
30012
30010
|
return new import_provider.LoadAPIKeyError({
|
|
30013
|
-
message: `SAP AI Core authentication failed: ${
|
|
30011
|
+
message: `SAP AI Core authentication failed: ${originalErrorMsg}
|
|
30014
30012
|
|
|
30015
30013
|
Make sure your AICORE_SERVICE_KEY environment variable is set correctly.
|
|
30016
30014
|
See: https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/create-service-key`
|
|
30017
30015
|
});
|
|
30018
30016
|
}
|
|
30019
30017
|
if (errorMsg.includes("econnrefused") || errorMsg.includes("enotfound") || errorMsg.includes("network") || errorMsg.includes("timeout")) {
|
|
30020
|
-
return
|
|
30021
|
-
|
|
30022
|
-
|
|
30023
|
-
|
|
30024
|
-
|
|
30025
|
-
|
|
30026
|
-
|
|
30027
|
-
|
|
30028
|
-
|
|
30018
|
+
return createAPICallError(
|
|
30019
|
+
error,
|
|
30020
|
+
{
|
|
30021
|
+
isRetryable: true,
|
|
30022
|
+
message: `Network error connecting to SAP AI Core: ${originalErrorMsg}`,
|
|
30023
|
+
statusCode: HTTP_STATUS.SERVICE_UNAVAILABLE
|
|
30024
|
+
},
|
|
30025
|
+
context
|
|
30026
|
+
);
|
|
30029
30027
|
}
|
|
30030
30028
|
if (errorMsg.includes("could not resolve destination")) {
|
|
30031
|
-
return
|
|
30032
|
-
|
|
30033
|
-
|
|
30034
|
-
|
|
30029
|
+
return createAPICallError(
|
|
30030
|
+
error,
|
|
30031
|
+
{
|
|
30032
|
+
isRetryable: false,
|
|
30033
|
+
message: `SAP AI Core destination error: ${originalErrorMsg}
|
|
30035
30034
|
|
|
30036
30035
|
Check your destination configuration or provide a valid destinationName.`,
|
|
30037
|
-
|
|
30038
|
-
|
|
30039
|
-
|
|
30040
|
-
|
|
30041
|
-
});
|
|
30036
|
+
statusCode: HTTP_STATUS.BAD_REQUEST
|
|
30037
|
+
},
|
|
30038
|
+
context
|
|
30039
|
+
);
|
|
30042
30040
|
}
|
|
30043
30041
|
if (errorMsg.includes("failed to resolve deployment") || errorMsg.includes("no deployment matched")) {
|
|
30044
|
-
const modelId = extractModelIdentifier(
|
|
30042
|
+
const modelId = extractModelIdentifier(originalErrorMsg);
|
|
30045
30043
|
return new import_provider.NoSuchModelError({
|
|
30046
|
-
message: `SAP AI Core deployment error: ${
|
|
30044
|
+
message: `SAP AI Core deployment error: ${originalErrorMsg}
|
|
30047
30045
|
|
|
30048
30046
|
Make sure you have a running orchestration deployment in your AI Core instance.
|
|
30049
30047
|
See: https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/create-deployment-for-orchestration`,
|
|
@@ -30052,129 +30050,165 @@ See: https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/create-depl
|
|
|
30052
30050
|
});
|
|
30053
30051
|
}
|
|
30054
30052
|
if (errorMsg.includes("filtered by the output filter")) {
|
|
30055
|
-
return
|
|
30056
|
-
|
|
30057
|
-
|
|
30058
|
-
|
|
30053
|
+
return createAPICallError(
|
|
30054
|
+
error,
|
|
30055
|
+
{
|
|
30056
|
+
isRetryable: false,
|
|
30057
|
+
message: `Content was filtered: ${originalErrorMsg}
|
|
30059
30058
|
|
|
30060
30059
|
The model's response was blocked by content safety filters. Try a different prompt.`,
|
|
30061
|
-
|
|
30062
|
-
|
|
30063
|
-
|
|
30064
|
-
|
|
30065
|
-
});
|
|
30060
|
+
statusCode: HTTP_STATUS.BAD_REQUEST
|
|
30061
|
+
},
|
|
30062
|
+
context
|
|
30063
|
+
);
|
|
30066
30064
|
}
|
|
30067
|
-
const statusMatch = /status code (\d+)/i.exec(
|
|
30065
|
+
const statusMatch = /status code (\d+)/i.exec(originalErrorMsg);
|
|
30068
30066
|
if (statusMatch) {
|
|
30069
30067
|
const extractedStatus = Number.parseInt(statusMatch[1], 10);
|
|
30070
|
-
return
|
|
30071
|
-
|
|
30072
|
-
|
|
30073
|
-
|
|
30074
|
-
|
|
30075
|
-
|
|
30076
|
-
|
|
30077
|
-
|
|
30078
|
-
|
|
30068
|
+
return createAPICallError(
|
|
30069
|
+
error,
|
|
30070
|
+
{
|
|
30071
|
+
isRetryable: isRetryable(extractedStatus),
|
|
30072
|
+
message: `SAP AI Core request failed: ${originalErrorMsg}`,
|
|
30073
|
+
statusCode: extractedStatus
|
|
30074
|
+
},
|
|
30075
|
+
context
|
|
30076
|
+
);
|
|
30079
30077
|
}
|
|
30080
30078
|
if (errorMsg.includes("consumed stream")) {
|
|
30081
|
-
return
|
|
30082
|
-
|
|
30083
|
-
|
|
30084
|
-
|
|
30085
|
-
|
|
30086
|
-
|
|
30087
|
-
|
|
30088
|
-
|
|
30089
|
-
|
|
30079
|
+
return createAPICallError(
|
|
30080
|
+
error,
|
|
30081
|
+
{
|
|
30082
|
+
isRetryable: false,
|
|
30083
|
+
message: `SAP AI Core stream consumption error: ${originalErrorMsg}`,
|
|
30084
|
+
statusCode: HTTP_STATUS.INTERNAL_ERROR
|
|
30085
|
+
},
|
|
30086
|
+
context
|
|
30087
|
+
);
|
|
30090
30088
|
}
|
|
30091
30089
|
if (errorMsg.includes("iterating over") || errorMsg.includes("parse message into json") || errorMsg.includes("received from") || errorMsg.includes("no body") || errorMsg.includes("invalid sse payload")) {
|
|
30092
|
-
return
|
|
30093
|
-
|
|
30094
|
-
|
|
30095
|
-
|
|
30096
|
-
|
|
30097
|
-
|
|
30098
|
-
|
|
30099
|
-
|
|
30100
|
-
|
|
30090
|
+
return createAPICallError(
|
|
30091
|
+
error,
|
|
30092
|
+
{
|
|
30093
|
+
isRetryable: true,
|
|
30094
|
+
message: `SAP AI Core streaming error: ${originalErrorMsg}`,
|
|
30095
|
+
statusCode: HTTP_STATUS.INTERNAL_ERROR
|
|
30096
|
+
},
|
|
30097
|
+
context
|
|
30098
|
+
);
|
|
30101
30099
|
}
|
|
30102
30100
|
if (errorMsg.includes("prompt template or messages must be defined") || errorMsg.includes("filtering parameters cannot be empty") || errorMsg.includes("templating yaml string must be non-empty") || errorMsg.includes("could not access response data") || errorMsg.includes("could not parse json") || errorMsg.includes("error parsing yaml") || errorMsg.includes("yaml does not conform") || errorMsg.includes("validation errors")) {
|
|
30103
|
-
return
|
|
30104
|
-
|
|
30105
|
-
|
|
30106
|
-
|
|
30107
|
-
|
|
30108
|
-
|
|
30109
|
-
|
|
30110
|
-
|
|
30111
|
-
|
|
30101
|
+
return createAPICallError(
|
|
30102
|
+
error,
|
|
30103
|
+
{
|
|
30104
|
+
isRetryable: false,
|
|
30105
|
+
message: `SAP AI Core configuration error: ${originalErrorMsg}`,
|
|
30106
|
+
statusCode: HTTP_STATUS.BAD_REQUEST
|
|
30107
|
+
},
|
|
30108
|
+
context
|
|
30109
|
+
);
|
|
30112
30110
|
}
|
|
30113
30111
|
if (errorMsg.includes("buffer is not available as globals")) {
|
|
30114
|
-
return
|
|
30115
|
-
|
|
30116
|
-
|
|
30117
|
-
|
|
30118
|
-
|
|
30119
|
-
|
|
30120
|
-
|
|
30121
|
-
|
|
30122
|
-
|
|
30112
|
+
return createAPICallError(
|
|
30113
|
+
error,
|
|
30114
|
+
{
|
|
30115
|
+
isRetryable: false,
|
|
30116
|
+
message: `SAP AI Core environment error: ${originalErrorMsg}`,
|
|
30117
|
+
statusCode: HTTP_STATUS.INTERNAL_ERROR
|
|
30118
|
+
},
|
|
30119
|
+
context
|
|
30120
|
+
);
|
|
30123
30121
|
}
|
|
30124
30122
|
if (errorMsg.includes("response stream is undefined")) {
|
|
30125
|
-
return
|
|
30126
|
-
|
|
30127
|
-
|
|
30128
|
-
|
|
30129
|
-
|
|
30130
|
-
|
|
30131
|
-
|
|
30132
|
-
|
|
30133
|
-
|
|
30123
|
+
return createAPICallError(
|
|
30124
|
+
error,
|
|
30125
|
+
{
|
|
30126
|
+
isRetryable: false,
|
|
30127
|
+
message: `SAP AI Core response stream error: ${originalErrorMsg}`,
|
|
30128
|
+
statusCode: HTTP_STATUS.INTERNAL_ERROR
|
|
30129
|
+
},
|
|
30130
|
+
context
|
|
30131
|
+
);
|
|
30134
30132
|
}
|
|
30135
30133
|
if (errorMsg.includes("response is required to process") || errorMsg.includes("stream is still open") || errorMsg.includes("data is not available yet")) {
|
|
30136
|
-
return
|
|
30137
|
-
|
|
30138
|
-
|
|
30139
|
-
|
|
30140
|
-
|
|
30141
|
-
|
|
30142
|
-
|
|
30143
|
-
|
|
30144
|
-
|
|
30134
|
+
return createAPICallError(
|
|
30135
|
+
error,
|
|
30136
|
+
{
|
|
30137
|
+
isRetryable: true,
|
|
30138
|
+
message: `SAP AI Core response processing error: ${originalErrorMsg}`,
|
|
30139
|
+
statusCode: HTTP_STATUS.INTERNAL_ERROR
|
|
30140
|
+
},
|
|
30141
|
+
context
|
|
30142
|
+
);
|
|
30145
30143
|
}
|
|
30146
30144
|
if (errorMsg.includes("failed to fetch the list of deployments")) {
|
|
30147
|
-
return
|
|
30148
|
-
|
|
30149
|
-
|
|
30150
|
-
|
|
30151
|
-
|
|
30152
|
-
|
|
30153
|
-
|
|
30154
|
-
|
|
30155
|
-
|
|
30145
|
+
return createAPICallError(
|
|
30146
|
+
error,
|
|
30147
|
+
{
|
|
30148
|
+
isRetryable: true,
|
|
30149
|
+
message: `SAP AI Core deployment retrieval error: ${originalErrorMsg}`,
|
|
30150
|
+
statusCode: HTTP_STATUS.SERVICE_UNAVAILABLE
|
|
30151
|
+
},
|
|
30152
|
+
context
|
|
30153
|
+
);
|
|
30156
30154
|
}
|
|
30157
30155
|
if (errorMsg.includes("received non-uint8array")) {
|
|
30158
|
-
return
|
|
30159
|
-
|
|
30160
|
-
|
|
30161
|
-
|
|
30162
|
-
|
|
30163
|
-
|
|
30164
|
-
|
|
30165
|
-
|
|
30166
|
-
|
|
30156
|
+
return createAPICallError(
|
|
30157
|
+
error,
|
|
30158
|
+
{
|
|
30159
|
+
isRetryable: false,
|
|
30160
|
+
message: `SAP AI Core stream buffer error: ${originalErrorMsg}`,
|
|
30161
|
+
statusCode: HTTP_STATUS.INTERNAL_ERROR
|
|
30162
|
+
},
|
|
30163
|
+
context
|
|
30164
|
+
);
|
|
30167
30165
|
}
|
|
30168
30166
|
}
|
|
30169
30167
|
const message = rootError instanceof Error ? rootError.message : typeof rootError === "string" ? rootError : "Unknown error occurred";
|
|
30170
30168
|
const fullMessage = context?.operation ? `SAP AI Core ${context.operation} failed: ${message}` : `SAP AI Core error: ${message}`;
|
|
30169
|
+
return createAPICallError(
|
|
30170
|
+
error,
|
|
30171
|
+
{
|
|
30172
|
+
isRetryable: false,
|
|
30173
|
+
message: fullMessage,
|
|
30174
|
+
statusCode: HTTP_STATUS.INTERNAL_ERROR
|
|
30175
|
+
},
|
|
30176
|
+
context
|
|
30177
|
+
);
|
|
30178
|
+
}
|
|
30179
|
+
function normalizeHeaders(headers) {
|
|
30180
|
+
if (!headers || typeof headers !== "object") return void 0;
|
|
30181
|
+
const record = headers;
|
|
30182
|
+
const entries = Object.entries(record).flatMap(([key, value]) => {
|
|
30183
|
+
if (typeof value === "string") return [[key, value]];
|
|
30184
|
+
if (Array.isArray(value)) {
|
|
30185
|
+
const strings = value.filter((item) => typeof item === "string").join("; ");
|
|
30186
|
+
return strings.length > 0 ? [[key, strings]] : [];
|
|
30187
|
+
}
|
|
30188
|
+
if (typeof value === "number" || typeof value === "boolean") {
|
|
30189
|
+
return [[key, String(value)]];
|
|
30190
|
+
}
|
|
30191
|
+
return [];
|
|
30192
|
+
});
|
|
30193
|
+
if (entries.length === 0) return void 0;
|
|
30194
|
+
return Object.fromEntries(entries);
|
|
30195
|
+
}
|
|
30196
|
+
function createAPICallError(error, options, context) {
|
|
30197
|
+
const responseBody = getAxiosResponseBody(error);
|
|
30198
|
+
const responseHeaders = context?.responseHeaders ?? getAxiosResponseHeaders(error);
|
|
30199
|
+
const enrichMessage = options.enrichMessage ?? true;
|
|
30200
|
+
const message = enrichMessage && responseBody ? `${options.message}
|
|
30201
|
+
|
|
30202
|
+
SAP AI Core Error Response:
|
|
30203
|
+
${responseBody}` : options.message;
|
|
30171
30204
|
return new import_provider.APICallError({
|
|
30172
30205
|
cause: error,
|
|
30173
|
-
isRetryable:
|
|
30174
|
-
message
|
|
30206
|
+
isRetryable: options.isRetryable,
|
|
30207
|
+
message,
|
|
30175
30208
|
requestBodyValues: context?.requestBody,
|
|
30209
|
+
responseBody,
|
|
30176
30210
|
responseHeaders,
|
|
30177
|
-
statusCode:
|
|
30211
|
+
statusCode: options.statusCode,
|
|
30178
30212
|
url: context?.url ?? ""
|
|
30179
30213
|
});
|
|
30180
30214
|
}
|
|
@@ -30198,13 +30232,23 @@ function extractModelIdentifier(message, location) {
|
|
|
30198
30232
|
}
|
|
30199
30233
|
return void 0;
|
|
30200
30234
|
}
|
|
30201
|
-
function
|
|
30235
|
+
function getAxiosError(error) {
|
|
30202
30236
|
if (!(error instanceof Error)) return void 0;
|
|
30203
30237
|
const rootCause = (0, import_util.isErrorWithCause)(error) ? error.rootCause : error;
|
|
30204
|
-
if (typeof rootCause !== "object") return void 0;
|
|
30238
|
+
if (typeof rootCause !== "object" || rootCause === null) return void 0;
|
|
30205
30239
|
const maybeAxios = rootCause;
|
|
30206
30240
|
if (maybeAxios.isAxiosError !== true) return void 0;
|
|
30207
|
-
return
|
|
30241
|
+
return maybeAxios;
|
|
30242
|
+
}
|
|
30243
|
+
function getAxiosResponseBody(error) {
|
|
30244
|
+
const axiosError = getAxiosError(error);
|
|
30245
|
+
if (!axiosError?.response?.data) return void 0;
|
|
30246
|
+
return serializeAxiosResponseData(axiosError.response.data);
|
|
30247
|
+
}
|
|
30248
|
+
function getAxiosResponseHeaders(error) {
|
|
30249
|
+
const axiosError = getAxiosError(error);
|
|
30250
|
+
if (!axiosError) return void 0;
|
|
30251
|
+
return normalizeHeaders(axiosError.response?.headers);
|
|
30208
30252
|
}
|
|
30209
30253
|
function getStatusCodeFromSAPError(code) {
|
|
30210
30254
|
if (!code) return HTTP_STATUS.INTERNAL_ERROR;
|
|
@@ -30250,22 +30294,22 @@ function isOrchestrationErrorResponse(error) {
|
|
|
30250
30294
|
function isRetryable(statusCode) {
|
|
30251
30295
|
return statusCode === HTTP_STATUS.REQUEST_TIMEOUT || statusCode === HTTP_STATUS.CONFLICT || statusCode === HTTP_STATUS.RATE_LIMIT || statusCode >= HTTP_STATUS.INTERNAL_ERROR && statusCode < 600;
|
|
30252
30296
|
}
|
|
30253
|
-
function
|
|
30254
|
-
if (
|
|
30255
|
-
|
|
30256
|
-
|
|
30257
|
-
if (typeof
|
|
30258
|
-
|
|
30259
|
-
|
|
30260
|
-
|
|
30261
|
-
}
|
|
30262
|
-
if (typeof value === "number" || typeof value === "boolean") {
|
|
30263
|
-
return [[key, String(value)]];
|
|
30297
|
+
function serializeAxiosResponseData(data, maxLength = 2e3) {
|
|
30298
|
+
if (data === void 0) return void 0;
|
|
30299
|
+
let serialized;
|
|
30300
|
+
try {
|
|
30301
|
+
if (typeof data === "string") {
|
|
30302
|
+
serialized = data;
|
|
30303
|
+
} else {
|
|
30304
|
+
serialized = JSON.stringify(data, null, 2);
|
|
30264
30305
|
}
|
|
30265
|
-
|
|
30266
|
-
|
|
30267
|
-
|
|
30268
|
-
|
|
30306
|
+
} catch {
|
|
30307
|
+
serialized = `[Unable to serialize: ${typeof data}]`;
|
|
30308
|
+
}
|
|
30309
|
+
if (serialized.length > maxLength) {
|
|
30310
|
+
return serialized.slice(0, maxLength) + "...[truncated]";
|
|
30311
|
+
}
|
|
30312
|
+
return serialized;
|
|
30269
30313
|
}
|
|
30270
30314
|
function tryExtractSAPErrorFromMessage(message) {
|
|
30271
30315
|
const jsonMatch = /\{[\s\S]*\}/.exec(message);
|
|
@@ -30376,7 +30420,7 @@ var SAPAIEmbeddingModel = class {
|
|
|
30376
30420
|
*
|
|
30377
30421
|
* This is the main implementation that handles all SAP AI Core embedding logic.
|
|
30378
30422
|
* @param modelId - The model identifier (e.g., 'text-embedding-ada-002', 'text-embedding-3-small').
|
|
30379
|
-
* @param settings - Model configuration settings (embedding type, model parameters, etc.).
|
|
30423
|
+
* @param settings - Model configuration settings (embedding type, model parameters, etc.).
|
|
30380
30424
|
* @param config - SAP AI Core deployment and destination configuration.
|
|
30381
30425
|
*/
|
|
30382
30426
|
constructor(modelId, settings = {}, config) {
|
|
@@ -30736,6 +30780,20 @@ function convertToSAPMessages(prompt, options = {}) {
|
|
|
30736
30780
|
}
|
|
30737
30781
|
|
|
30738
30782
|
// src/sap-ai-language-model.ts
|
|
30783
|
+
var PARAM_MAPPINGS = [
|
|
30784
|
+
{ camelCaseKey: "maxTokens", optionKey: "maxOutputTokens", outputKey: "max_tokens" },
|
|
30785
|
+
{ camelCaseKey: "temperature", optionKey: "temperature", outputKey: "temperature" },
|
|
30786
|
+
{ camelCaseKey: "topP", optionKey: "topP", outputKey: "top_p" },
|
|
30787
|
+
{ camelCaseKey: "topK", optionKey: "topK", outputKey: "top_k" },
|
|
30788
|
+
{
|
|
30789
|
+
camelCaseKey: "frequencyPenalty",
|
|
30790
|
+
optionKey: "frequencyPenalty",
|
|
30791
|
+
outputKey: "frequency_penalty"
|
|
30792
|
+
},
|
|
30793
|
+
{ camelCaseKey: "presencePenalty", optionKey: "presencePenalty", outputKey: "presence_penalty" },
|
|
30794
|
+
{ camelCaseKey: "seed", optionKey: "seed", outputKey: "seed" },
|
|
30795
|
+
{ camelCaseKey: "parallel_tool_calls", outputKey: "parallel_tool_calls" }
|
|
30796
|
+
];
|
|
30739
30797
|
var StreamIdGenerator = class {
|
|
30740
30798
|
generateResponseId() {
|
|
30741
30799
|
return crypto.randomUUID();
|
|
@@ -30811,49 +30869,12 @@ var SAPAILanguageModel = class {
|
|
|
30811
30869
|
try {
|
|
30812
30870
|
const { messages, orchestrationConfig, warnings } = await this.buildOrchestrationConfig(options);
|
|
30813
30871
|
const client = this.createClient(orchestrationConfig);
|
|
30814
|
-
const
|
|
30815
|
-
const requestBody = {
|
|
30816
|
-
messages,
|
|
30817
|
-
model: {
|
|
30818
|
-
...orchestrationConfig.promptTemplating.model
|
|
30819
|
-
},
|
|
30820
|
-
...promptTemplating.prompt.tools ? { tools: promptTemplating.prompt.tools } : {},
|
|
30821
|
-
...promptTemplating.prompt.response_format ? { response_format: promptTemplating.prompt.response_format } : {},
|
|
30822
|
-
...(() => {
|
|
30823
|
-
const masking = orchestrationConfig.masking;
|
|
30824
|
-
return masking && Object.keys(masking).length > 0 ? { masking } : {};
|
|
30825
|
-
})(),
|
|
30826
|
-
...(() => {
|
|
30827
|
-
const filtering = orchestrationConfig.filtering;
|
|
30828
|
-
return filtering && Object.keys(filtering).length > 0 ? { filtering } : {};
|
|
30829
|
-
})(),
|
|
30830
|
-
...(() => {
|
|
30831
|
-
const grounding = orchestrationConfig.grounding;
|
|
30832
|
-
return grounding && Object.keys(grounding).length > 0 ? { grounding } : {};
|
|
30833
|
-
})(),
|
|
30834
|
-
...(() => {
|
|
30835
|
-
const translation = orchestrationConfig.translation;
|
|
30836
|
-
return translation && Object.keys(translation).length > 0 ? { translation } : {};
|
|
30837
|
-
})()
|
|
30838
|
-
};
|
|
30872
|
+
const requestBody = this.buildRequestBody(messages, orchestrationConfig);
|
|
30839
30873
|
const response = await client.chatCompletion(
|
|
30840
30874
|
requestBody,
|
|
30841
30875
|
options.abortSignal ? { signal: options.abortSignal } : void 0
|
|
30842
30876
|
);
|
|
30843
|
-
const
|
|
30844
|
-
const responseHeaders = responseHeadersRaw ? Object.fromEntries(
|
|
30845
|
-
Object.entries(responseHeadersRaw).flatMap(([key, value]) => {
|
|
30846
|
-
if (typeof value === "string") return [[key, value]];
|
|
30847
|
-
if (Array.isArray(value)) {
|
|
30848
|
-
const strings = value.filter((item) => typeof item === "string").join("; ");
|
|
30849
|
-
return strings.length > 0 ? [[key, strings]] : [];
|
|
30850
|
-
}
|
|
30851
|
-
if (typeof value === "number" || typeof value === "boolean") {
|
|
30852
|
-
return [[key, String(value)]];
|
|
30853
|
-
}
|
|
30854
|
-
return [];
|
|
30855
|
-
})
|
|
30856
|
-
) : void 0;
|
|
30877
|
+
const responseHeaders = normalizeHeaders(response.rawResponse.headers);
|
|
30857
30878
|
const content = [];
|
|
30858
30879
|
const textContent = response.getContent();
|
|
30859
30880
|
if (textContent) {
|
|
@@ -30938,23 +30959,7 @@ var SAPAILanguageModel = class {
|
|
|
30938
30959
|
try {
|
|
30939
30960
|
const { messages, orchestrationConfig, warnings } = await this.buildOrchestrationConfig(options);
|
|
30940
30961
|
const client = this.createClient(orchestrationConfig);
|
|
30941
|
-
const
|
|
30942
|
-
const requestBody = {
|
|
30943
|
-
messages,
|
|
30944
|
-
model: {
|
|
30945
|
-
...orchestrationConfig.promptTemplating.model
|
|
30946
|
-
},
|
|
30947
|
-
...promptTemplating.prompt.tools ? { tools: promptTemplating.prompt.tools } : {},
|
|
30948
|
-
...promptTemplating.prompt.response_format ? { response_format: promptTemplating.prompt.response_format } : {},
|
|
30949
|
-
...(() => {
|
|
30950
|
-
const masking = orchestrationConfig.masking;
|
|
30951
|
-
return masking && Object.keys(masking).length > 0 ? { masking } : {};
|
|
30952
|
-
})(),
|
|
30953
|
-
...(() => {
|
|
30954
|
-
const filtering = orchestrationConfig.filtering;
|
|
30955
|
-
return filtering && Object.keys(filtering).length > 0 ? { filtering } : {};
|
|
30956
|
-
})()
|
|
30957
|
-
};
|
|
30962
|
+
const requestBody = this.buildRequestBody(messages, orchestrationConfig);
|
|
30958
30963
|
const streamResponse = await client.stream(requestBody, options.abortSignal, {
|
|
30959
30964
|
promptTemplating: { include_usage: true }
|
|
30960
30965
|
});
|
|
@@ -31309,20 +31314,14 @@ var SAPAILanguageModel = class {
|
|
|
31309
31314
|
this.settings.modelParams ?? {},
|
|
31310
31315
|
sapOptions?.modelParams ?? {}
|
|
31311
31316
|
);
|
|
31312
|
-
|
|
31313
|
-
|
|
31314
|
-
|
|
31315
|
-
|
|
31316
|
-
|
|
31317
|
-
|
|
31318
|
-
if (options.
|
|
31319
|
-
|
|
31320
|
-
if (frequencyPenalty !== void 0) {
|
|
31321
|
-
modelParams.frequency_penalty = frequencyPenalty;
|
|
31322
|
-
}
|
|
31323
|
-
const presencePenalty = options.presencePenalty ?? sapOptions?.modelParams?.presencePenalty ?? this.settings.modelParams?.presencePenalty;
|
|
31324
|
-
if (presencePenalty !== void 0) {
|
|
31325
|
-
modelParams.presence_penalty = presencePenalty;
|
|
31317
|
+
applyParameterOverrides(
|
|
31318
|
+
modelParams,
|
|
31319
|
+
options,
|
|
31320
|
+
sapOptions?.modelParams,
|
|
31321
|
+
this.settings.modelParams
|
|
31322
|
+
);
|
|
31323
|
+
if (options.stopSequences && options.stopSequences.length > 0) {
|
|
31324
|
+
modelParams.stop = options.stopSequences;
|
|
31326
31325
|
}
|
|
31327
31326
|
if (supportsN) {
|
|
31328
31327
|
const nValue = sapOptions?.modelParams?.n ?? this.settings.modelParams?.n;
|
|
@@ -31332,16 +31331,6 @@ var SAPAILanguageModel = class {
|
|
|
31332
31331
|
} else {
|
|
31333
31332
|
delete modelParams.n;
|
|
31334
31333
|
}
|
|
31335
|
-
const parallelToolCalls = sapOptions?.modelParams?.parallel_tool_calls ?? this.settings.modelParams?.parallel_tool_calls;
|
|
31336
|
-
if (parallelToolCalls !== void 0) {
|
|
31337
|
-
modelParams.parallel_tool_calls = parallelToolCalls;
|
|
31338
|
-
}
|
|
31339
|
-
if (options.stopSequences && options.stopSequences.length > 0) {
|
|
31340
|
-
modelParams.stop = options.stopSequences;
|
|
31341
|
-
}
|
|
31342
|
-
if (options.seed !== void 0) {
|
|
31343
|
-
modelParams.seed = options.seed;
|
|
31344
|
-
}
|
|
31345
31334
|
validateModelParamsWithWarnings(
|
|
31346
31335
|
{
|
|
31347
31336
|
frequencyPenalty: options.frequencyPenalty,
|
|
@@ -31392,25 +31381,35 @@ var SAPAILanguageModel = class {
|
|
|
31392
31381
|
...responseFormat ? { response_format: responseFormat } : {}
|
|
31393
31382
|
}
|
|
31394
31383
|
},
|
|
31395
|
-
...(
|
|
31396
|
-
|
|
31397
|
-
|
|
31398
|
-
|
|
31399
|
-
...(() => {
|
|
31400
|
-
const filtering = this.settings.filtering;
|
|
31401
|
-
return filtering && Object.keys(filtering).length > 0 ? { filtering } : {};
|
|
31402
|
-
})(),
|
|
31403
|
-
...(() => {
|
|
31404
|
-
const grounding = this.settings.grounding;
|
|
31405
|
-
return grounding && Object.keys(grounding).length > 0 ? { grounding } : {};
|
|
31406
|
-
})(),
|
|
31407
|
-
...(() => {
|
|
31408
|
-
const translation = this.settings.translation;
|
|
31409
|
-
return translation && Object.keys(translation).length > 0 ? { translation } : {};
|
|
31410
|
-
})()
|
|
31384
|
+
...this.settings.masking && Object.keys(this.settings.masking).length > 0 ? { masking: this.settings.masking } : {},
|
|
31385
|
+
...this.settings.filtering && Object.keys(this.settings.filtering).length > 0 ? { filtering: this.settings.filtering } : {},
|
|
31386
|
+
...this.settings.grounding && Object.keys(this.settings.grounding).length > 0 ? { grounding: this.settings.grounding } : {},
|
|
31387
|
+
...this.settings.translation && Object.keys(this.settings.translation).length > 0 ? { translation: this.settings.translation } : {}
|
|
31411
31388
|
};
|
|
31412
31389
|
return { messages, orchestrationConfig, warnings };
|
|
31413
31390
|
}
|
|
31391
|
+
/**
|
|
31392
|
+
* Builds the request body for SAP AI SDK chat completion or streaming.
|
|
31393
|
+
* @param messages - The chat messages to send.
|
|
31394
|
+
* @param orchestrationConfig - The orchestration configuration.
|
|
31395
|
+
* @returns The request body object.
|
|
31396
|
+
* @internal
|
|
31397
|
+
*/
|
|
31398
|
+
buildRequestBody(messages, orchestrationConfig) {
|
|
31399
|
+
const promptTemplating = orchestrationConfig.promptTemplating;
|
|
31400
|
+
return {
|
|
31401
|
+
messages,
|
|
31402
|
+
model: {
|
|
31403
|
+
...orchestrationConfig.promptTemplating.model
|
|
31404
|
+
},
|
|
31405
|
+
...promptTemplating.prompt.tools ? { tools: promptTemplating.prompt.tools } : {},
|
|
31406
|
+
...promptTemplating.prompt.response_format ? { response_format: promptTemplating.prompt.response_format } : {},
|
|
31407
|
+
...orchestrationConfig.masking && Object.keys(orchestrationConfig.masking).length > 0 ? { masking: orchestrationConfig.masking } : {},
|
|
31408
|
+
...orchestrationConfig.filtering && Object.keys(orchestrationConfig.filtering).length > 0 ? { filtering: orchestrationConfig.filtering } : {},
|
|
31409
|
+
...orchestrationConfig.grounding && Object.keys(orchestrationConfig.grounding).length > 0 ? { grounding: orchestrationConfig.grounding } : {},
|
|
31410
|
+
...orchestrationConfig.translation && Object.keys(orchestrationConfig.translation).length > 0 ? { translation: orchestrationConfig.translation } : {}
|
|
31411
|
+
};
|
|
31412
|
+
}
|
|
31414
31413
|
/**
|
|
31415
31414
|
* Creates an SAP AI SDK OrchestrationClient with the given configuration.
|
|
31416
31415
|
* @param config - The SAP AI SDK orchestration module configuration.
|
|
@@ -31421,6 +31420,18 @@ var SAPAILanguageModel = class {
|
|
|
31421
31420
|
return new import_orchestration2.OrchestrationClient(config, this.config.deploymentConfig, this.config.destination);
|
|
31422
31421
|
}
|
|
31423
31422
|
};
|
|
31423
|
+
function applyParameterOverrides(modelParams, options, sapModelParams, settingsModelParams) {
|
|
31424
|
+
const params = modelParams;
|
|
31425
|
+
for (const mapping of PARAM_MAPPINGS) {
|
|
31426
|
+
const value = (mapping.optionKey ? options[mapping.optionKey] : void 0) ?? (mapping.camelCaseKey ? sapModelParams?.[mapping.camelCaseKey] : void 0) ?? (mapping.camelCaseKey ? settingsModelParams?.[mapping.camelCaseKey] : void 0);
|
|
31427
|
+
if (value !== void 0) {
|
|
31428
|
+
params[mapping.outputKey] = value;
|
|
31429
|
+
}
|
|
31430
|
+
if (mapping.camelCaseKey && mapping.camelCaseKey !== mapping.outputKey) {
|
|
31431
|
+
Reflect.deleteProperty(params, mapping.camelCaseKey);
|
|
31432
|
+
}
|
|
31433
|
+
}
|
|
31434
|
+
}
|
|
31424
31435
|
function buildSAPToolParameters(schema) {
|
|
31425
31436
|
const schemaType = schema.type;
|
|
31426
31437
|
if (schemaType !== void 0 && schemaType !== "object") {
|
|
@@ -31679,7 +31690,6 @@ var import_orchestration5 = require("@sap-ai-sdk/orchestration");
|
|
|
31679
31690
|
buildTranslationConfig,
|
|
31680
31691
|
createSAPAIProvider,
|
|
31681
31692
|
getProviderName,
|
|
31682
|
-
isConfigReference,
|
|
31683
31693
|
sapAIEmbeddingProviderOptions,
|
|
31684
31694
|
sapAILanguageModelProviderOptions,
|
|
31685
31695
|
sapai
|