@jerome-benoit/sap-ai-provider 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.d.cts +9 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.js +245 -235
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
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
|
|
@@ -29933,46 +29932,45 @@ function convertToAISDKError(error, context) {
|
|
|
29933
29932
|
});
|
|
29934
29933
|
}
|
|
29935
29934
|
}
|
|
29936
|
-
const responseHeaders = context?.responseHeaders ?? getAxiosResponseHeaders(error);
|
|
29937
29935
|
if (rootError instanceof Error) {
|
|
29938
29936
|
const errorMsg = rootError.message.toLowerCase();
|
|
29939
|
-
const
|
|
29937
|
+
const originalErrorMsg = rootError.message;
|
|
29940
29938
|
if (errorMsg.includes("authentication") || errorMsg.includes("unauthorized") || errorMsg.includes("aicore_service_key") || errorMsg.includes("invalid credentials") || errorMsg.includes("service credentials") || errorMsg.includes("service binding")) {
|
|
29941
29939
|
return new import_provider.LoadAPIKeyError({
|
|
29942
|
-
message: `SAP AI Core authentication failed: ${
|
|
29940
|
+
message: `SAP AI Core authentication failed: ${originalErrorMsg}
|
|
29943
29941
|
|
|
29944
29942
|
Make sure your AICORE_SERVICE_KEY environment variable is set correctly.
|
|
29945
29943
|
See: https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/create-service-key`
|
|
29946
29944
|
});
|
|
29947
29945
|
}
|
|
29948
29946
|
if (errorMsg.includes("econnrefused") || errorMsg.includes("enotfound") || errorMsg.includes("network") || errorMsg.includes("timeout")) {
|
|
29949
|
-
return
|
|
29950
|
-
|
|
29951
|
-
|
|
29952
|
-
|
|
29953
|
-
|
|
29954
|
-
|
|
29955
|
-
|
|
29956
|
-
|
|
29957
|
-
|
|
29947
|
+
return createAPICallError(
|
|
29948
|
+
error,
|
|
29949
|
+
{
|
|
29950
|
+
isRetryable: true,
|
|
29951
|
+
message: `Network error connecting to SAP AI Core: ${originalErrorMsg}`,
|
|
29952
|
+
statusCode: HTTP_STATUS.SERVICE_UNAVAILABLE
|
|
29953
|
+
},
|
|
29954
|
+
context
|
|
29955
|
+
);
|
|
29958
29956
|
}
|
|
29959
29957
|
if (errorMsg.includes("could not resolve destination")) {
|
|
29960
|
-
return
|
|
29961
|
-
|
|
29962
|
-
|
|
29963
|
-
|
|
29958
|
+
return createAPICallError(
|
|
29959
|
+
error,
|
|
29960
|
+
{
|
|
29961
|
+
isRetryable: false,
|
|
29962
|
+
message: `SAP AI Core destination error: ${originalErrorMsg}
|
|
29964
29963
|
|
|
29965
29964
|
Check your destination configuration or provide a valid destinationName.`,
|
|
29966
|
-
|
|
29967
|
-
|
|
29968
|
-
|
|
29969
|
-
|
|
29970
|
-
});
|
|
29965
|
+
statusCode: HTTP_STATUS.BAD_REQUEST
|
|
29966
|
+
},
|
|
29967
|
+
context
|
|
29968
|
+
);
|
|
29971
29969
|
}
|
|
29972
29970
|
if (errorMsg.includes("failed to resolve deployment") || errorMsg.includes("no deployment matched")) {
|
|
29973
|
-
const modelId = extractModelIdentifier(
|
|
29971
|
+
const modelId = extractModelIdentifier(originalErrorMsg);
|
|
29974
29972
|
return new import_provider.NoSuchModelError({
|
|
29975
|
-
message: `SAP AI Core deployment error: ${
|
|
29973
|
+
message: `SAP AI Core deployment error: ${originalErrorMsg}
|
|
29976
29974
|
|
|
29977
29975
|
Make sure you have a running orchestration deployment in your AI Core instance.
|
|
29978
29976
|
See: https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/create-deployment-for-orchestration`,
|
|
@@ -29981,129 +29979,165 @@ See: https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/create-depl
|
|
|
29981
29979
|
});
|
|
29982
29980
|
}
|
|
29983
29981
|
if (errorMsg.includes("filtered by the output filter")) {
|
|
29984
|
-
return
|
|
29985
|
-
|
|
29986
|
-
|
|
29987
|
-
|
|
29982
|
+
return createAPICallError(
|
|
29983
|
+
error,
|
|
29984
|
+
{
|
|
29985
|
+
isRetryable: false,
|
|
29986
|
+
message: `Content was filtered: ${originalErrorMsg}
|
|
29988
29987
|
|
|
29989
29988
|
The model's response was blocked by content safety filters. Try a different prompt.`,
|
|
29990
|
-
|
|
29991
|
-
|
|
29992
|
-
|
|
29993
|
-
|
|
29994
|
-
});
|
|
29989
|
+
statusCode: HTTP_STATUS.BAD_REQUEST
|
|
29990
|
+
},
|
|
29991
|
+
context
|
|
29992
|
+
);
|
|
29995
29993
|
}
|
|
29996
|
-
const statusMatch = /status code (\d+)/i.exec(
|
|
29994
|
+
const statusMatch = /status code (\d+)/i.exec(originalErrorMsg);
|
|
29997
29995
|
if (statusMatch) {
|
|
29998
29996
|
const extractedStatus = Number.parseInt(statusMatch[1], 10);
|
|
29999
|
-
return
|
|
30000
|
-
|
|
30001
|
-
|
|
30002
|
-
|
|
30003
|
-
|
|
30004
|
-
|
|
30005
|
-
|
|
30006
|
-
|
|
30007
|
-
|
|
29997
|
+
return createAPICallError(
|
|
29998
|
+
error,
|
|
29999
|
+
{
|
|
30000
|
+
isRetryable: isRetryable(extractedStatus),
|
|
30001
|
+
message: `SAP AI Core request failed: ${originalErrorMsg}`,
|
|
30002
|
+
statusCode: extractedStatus
|
|
30003
|
+
},
|
|
30004
|
+
context
|
|
30005
|
+
);
|
|
30008
30006
|
}
|
|
30009
30007
|
if (errorMsg.includes("consumed stream")) {
|
|
30010
|
-
return
|
|
30011
|
-
|
|
30012
|
-
|
|
30013
|
-
|
|
30014
|
-
|
|
30015
|
-
|
|
30016
|
-
|
|
30017
|
-
|
|
30018
|
-
|
|
30008
|
+
return createAPICallError(
|
|
30009
|
+
error,
|
|
30010
|
+
{
|
|
30011
|
+
isRetryable: false,
|
|
30012
|
+
message: `SAP AI Core stream consumption error: ${originalErrorMsg}`,
|
|
30013
|
+
statusCode: HTTP_STATUS.INTERNAL_ERROR
|
|
30014
|
+
},
|
|
30015
|
+
context
|
|
30016
|
+
);
|
|
30019
30017
|
}
|
|
30020
30018
|
if (errorMsg.includes("iterating over") || errorMsg.includes("parse message into json") || errorMsg.includes("received from") || errorMsg.includes("no body") || errorMsg.includes("invalid sse payload")) {
|
|
30021
|
-
return
|
|
30022
|
-
|
|
30023
|
-
|
|
30024
|
-
|
|
30025
|
-
|
|
30026
|
-
|
|
30027
|
-
|
|
30028
|
-
|
|
30029
|
-
|
|
30019
|
+
return createAPICallError(
|
|
30020
|
+
error,
|
|
30021
|
+
{
|
|
30022
|
+
isRetryable: true,
|
|
30023
|
+
message: `SAP AI Core streaming error: ${originalErrorMsg}`,
|
|
30024
|
+
statusCode: HTTP_STATUS.INTERNAL_ERROR
|
|
30025
|
+
},
|
|
30026
|
+
context
|
|
30027
|
+
);
|
|
30030
30028
|
}
|
|
30031
30029
|
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")) {
|
|
30032
|
-
return
|
|
30033
|
-
|
|
30034
|
-
|
|
30035
|
-
|
|
30036
|
-
|
|
30037
|
-
|
|
30038
|
-
|
|
30039
|
-
|
|
30040
|
-
|
|
30030
|
+
return createAPICallError(
|
|
30031
|
+
error,
|
|
30032
|
+
{
|
|
30033
|
+
isRetryable: false,
|
|
30034
|
+
message: `SAP AI Core configuration error: ${originalErrorMsg}`,
|
|
30035
|
+
statusCode: HTTP_STATUS.BAD_REQUEST
|
|
30036
|
+
},
|
|
30037
|
+
context
|
|
30038
|
+
);
|
|
30041
30039
|
}
|
|
30042
30040
|
if (errorMsg.includes("buffer is not available as globals")) {
|
|
30043
|
-
return
|
|
30044
|
-
|
|
30045
|
-
|
|
30046
|
-
|
|
30047
|
-
|
|
30048
|
-
|
|
30049
|
-
|
|
30050
|
-
|
|
30051
|
-
|
|
30041
|
+
return createAPICallError(
|
|
30042
|
+
error,
|
|
30043
|
+
{
|
|
30044
|
+
isRetryable: false,
|
|
30045
|
+
message: `SAP AI Core environment error: ${originalErrorMsg}`,
|
|
30046
|
+
statusCode: HTTP_STATUS.INTERNAL_ERROR
|
|
30047
|
+
},
|
|
30048
|
+
context
|
|
30049
|
+
);
|
|
30052
30050
|
}
|
|
30053
30051
|
if (errorMsg.includes("response stream is undefined")) {
|
|
30054
|
-
return
|
|
30055
|
-
|
|
30056
|
-
|
|
30057
|
-
|
|
30058
|
-
|
|
30059
|
-
|
|
30060
|
-
|
|
30061
|
-
|
|
30062
|
-
|
|
30052
|
+
return createAPICallError(
|
|
30053
|
+
error,
|
|
30054
|
+
{
|
|
30055
|
+
isRetryable: false,
|
|
30056
|
+
message: `SAP AI Core response stream error: ${originalErrorMsg}`,
|
|
30057
|
+
statusCode: HTTP_STATUS.INTERNAL_ERROR
|
|
30058
|
+
},
|
|
30059
|
+
context
|
|
30060
|
+
);
|
|
30063
30061
|
}
|
|
30064
30062
|
if (errorMsg.includes("response is required to process") || errorMsg.includes("stream is still open") || errorMsg.includes("data is not available yet")) {
|
|
30065
|
-
return
|
|
30066
|
-
|
|
30067
|
-
|
|
30068
|
-
|
|
30069
|
-
|
|
30070
|
-
|
|
30071
|
-
|
|
30072
|
-
|
|
30073
|
-
|
|
30063
|
+
return createAPICallError(
|
|
30064
|
+
error,
|
|
30065
|
+
{
|
|
30066
|
+
isRetryable: true,
|
|
30067
|
+
message: `SAP AI Core response processing error: ${originalErrorMsg}`,
|
|
30068
|
+
statusCode: HTTP_STATUS.INTERNAL_ERROR
|
|
30069
|
+
},
|
|
30070
|
+
context
|
|
30071
|
+
);
|
|
30074
30072
|
}
|
|
30075
30073
|
if (errorMsg.includes("failed to fetch the list of deployments")) {
|
|
30076
|
-
return
|
|
30077
|
-
|
|
30078
|
-
|
|
30079
|
-
|
|
30080
|
-
|
|
30081
|
-
|
|
30082
|
-
|
|
30083
|
-
|
|
30084
|
-
|
|
30074
|
+
return createAPICallError(
|
|
30075
|
+
error,
|
|
30076
|
+
{
|
|
30077
|
+
isRetryable: true,
|
|
30078
|
+
message: `SAP AI Core deployment retrieval error: ${originalErrorMsg}`,
|
|
30079
|
+
statusCode: HTTP_STATUS.SERVICE_UNAVAILABLE
|
|
30080
|
+
},
|
|
30081
|
+
context
|
|
30082
|
+
);
|
|
30085
30083
|
}
|
|
30086
30084
|
if (errorMsg.includes("received non-uint8array")) {
|
|
30087
|
-
return
|
|
30088
|
-
|
|
30089
|
-
|
|
30090
|
-
|
|
30091
|
-
|
|
30092
|
-
|
|
30093
|
-
|
|
30094
|
-
|
|
30095
|
-
|
|
30085
|
+
return createAPICallError(
|
|
30086
|
+
error,
|
|
30087
|
+
{
|
|
30088
|
+
isRetryable: false,
|
|
30089
|
+
message: `SAP AI Core stream buffer error: ${originalErrorMsg}`,
|
|
30090
|
+
statusCode: HTTP_STATUS.INTERNAL_ERROR
|
|
30091
|
+
},
|
|
30092
|
+
context
|
|
30093
|
+
);
|
|
30096
30094
|
}
|
|
30097
30095
|
}
|
|
30098
30096
|
const message = rootError instanceof Error ? rootError.message : typeof rootError === "string" ? rootError : "Unknown error occurred";
|
|
30099
30097
|
const fullMessage = context?.operation ? `SAP AI Core ${context.operation} failed: ${message}` : `SAP AI Core error: ${message}`;
|
|
30098
|
+
return createAPICallError(
|
|
30099
|
+
error,
|
|
30100
|
+
{
|
|
30101
|
+
isRetryable: false,
|
|
30102
|
+
message: fullMessage,
|
|
30103
|
+
statusCode: HTTP_STATUS.INTERNAL_ERROR
|
|
30104
|
+
},
|
|
30105
|
+
context
|
|
30106
|
+
);
|
|
30107
|
+
}
|
|
30108
|
+
function normalizeHeaders(headers) {
|
|
30109
|
+
if (!headers || typeof headers !== "object") return void 0;
|
|
30110
|
+
const record = headers;
|
|
30111
|
+
const entries = Object.entries(record).flatMap(([key, value]) => {
|
|
30112
|
+
if (typeof value === "string") return [[key, value]];
|
|
30113
|
+
if (Array.isArray(value)) {
|
|
30114
|
+
const strings = value.filter((item) => typeof item === "string").join("; ");
|
|
30115
|
+
return strings.length > 0 ? [[key, strings]] : [];
|
|
30116
|
+
}
|
|
30117
|
+
if (typeof value === "number" || typeof value === "boolean") {
|
|
30118
|
+
return [[key, String(value)]];
|
|
30119
|
+
}
|
|
30120
|
+
return [];
|
|
30121
|
+
});
|
|
30122
|
+
if (entries.length === 0) return void 0;
|
|
30123
|
+
return Object.fromEntries(entries);
|
|
30124
|
+
}
|
|
30125
|
+
function createAPICallError(error, options, context) {
|
|
30126
|
+
const responseBody = getAxiosResponseBody(error);
|
|
30127
|
+
const responseHeaders = context?.responseHeaders ?? getAxiosResponseHeaders(error);
|
|
30128
|
+
const enrichMessage = options.enrichMessage ?? true;
|
|
30129
|
+
const message = enrichMessage && responseBody ? `${options.message}
|
|
30130
|
+
|
|
30131
|
+
SAP AI Core Error Response:
|
|
30132
|
+
${responseBody}` : options.message;
|
|
30100
30133
|
return new import_provider.APICallError({
|
|
30101
30134
|
cause: error,
|
|
30102
|
-
isRetryable:
|
|
30103
|
-
message
|
|
30135
|
+
isRetryable: options.isRetryable,
|
|
30136
|
+
message,
|
|
30104
30137
|
requestBodyValues: context?.requestBody,
|
|
30138
|
+
responseBody,
|
|
30105
30139
|
responseHeaders,
|
|
30106
|
-
statusCode:
|
|
30140
|
+
statusCode: options.statusCode,
|
|
30107
30141
|
url: context?.url ?? ""
|
|
30108
30142
|
});
|
|
30109
30143
|
}
|
|
@@ -30127,13 +30161,23 @@ function extractModelIdentifier(message, location) {
|
|
|
30127
30161
|
}
|
|
30128
30162
|
return void 0;
|
|
30129
30163
|
}
|
|
30130
|
-
function
|
|
30164
|
+
function getAxiosError(error) {
|
|
30131
30165
|
if (!(error instanceof Error)) return void 0;
|
|
30132
30166
|
const rootCause = (0, import_util.isErrorWithCause)(error) ? error.rootCause : error;
|
|
30133
|
-
if (typeof rootCause !== "object") return void 0;
|
|
30167
|
+
if (typeof rootCause !== "object" || rootCause === null) return void 0;
|
|
30134
30168
|
const maybeAxios = rootCause;
|
|
30135
30169
|
if (maybeAxios.isAxiosError !== true) return void 0;
|
|
30136
|
-
return
|
|
30170
|
+
return maybeAxios;
|
|
30171
|
+
}
|
|
30172
|
+
function getAxiosResponseBody(error) {
|
|
30173
|
+
const axiosError = getAxiosError(error);
|
|
30174
|
+
if (!axiosError?.response?.data) return void 0;
|
|
30175
|
+
return serializeAxiosResponseData(axiosError.response.data);
|
|
30176
|
+
}
|
|
30177
|
+
function getAxiosResponseHeaders(error) {
|
|
30178
|
+
const axiosError = getAxiosError(error);
|
|
30179
|
+
if (!axiosError) return void 0;
|
|
30180
|
+
return normalizeHeaders(axiosError.response?.headers);
|
|
30137
30181
|
}
|
|
30138
30182
|
function getStatusCodeFromSAPError(code) {
|
|
30139
30183
|
if (!code) return HTTP_STATUS.INTERNAL_ERROR;
|
|
@@ -30179,22 +30223,22 @@ function isOrchestrationErrorResponse(error) {
|
|
|
30179
30223
|
function isRetryable(statusCode) {
|
|
30180
30224
|
return statusCode === HTTP_STATUS.REQUEST_TIMEOUT || statusCode === HTTP_STATUS.CONFLICT || statusCode === HTTP_STATUS.RATE_LIMIT || statusCode >= HTTP_STATUS.INTERNAL_ERROR && statusCode < 600;
|
|
30181
30225
|
}
|
|
30182
|
-
function
|
|
30183
|
-
if (
|
|
30184
|
-
|
|
30185
|
-
|
|
30186
|
-
if (typeof
|
|
30187
|
-
|
|
30188
|
-
|
|
30189
|
-
|
|
30190
|
-
}
|
|
30191
|
-
if (typeof value === "number" || typeof value === "boolean") {
|
|
30192
|
-
return [[key, String(value)]];
|
|
30226
|
+
function serializeAxiosResponseData(data, maxLength = 2e3) {
|
|
30227
|
+
if (data === void 0) return void 0;
|
|
30228
|
+
let serialized;
|
|
30229
|
+
try {
|
|
30230
|
+
if (typeof data === "string") {
|
|
30231
|
+
serialized = data;
|
|
30232
|
+
} else {
|
|
30233
|
+
serialized = JSON.stringify(data, null, 2);
|
|
30193
30234
|
}
|
|
30194
|
-
|
|
30195
|
-
|
|
30196
|
-
|
|
30197
|
-
|
|
30235
|
+
} catch {
|
|
30236
|
+
serialized = `[Unable to serialize: ${typeof data}]`;
|
|
30237
|
+
}
|
|
30238
|
+
if (serialized.length > maxLength) {
|
|
30239
|
+
return serialized.slice(0, maxLength) + "...[truncated]";
|
|
30240
|
+
}
|
|
30241
|
+
return serialized;
|
|
30198
30242
|
}
|
|
30199
30243
|
function tryExtractSAPErrorFromMessage(message) {
|
|
30200
30244
|
const jsonMatch = /\{[\s\S]*\}/.exec(message);
|
|
@@ -30305,7 +30349,7 @@ var SAPAIEmbeddingModel = class {
|
|
|
30305
30349
|
*
|
|
30306
30350
|
* This is the main implementation that handles all SAP AI Core embedding logic.
|
|
30307
30351
|
* @param modelId - The model identifier (e.g., 'text-embedding-ada-002', 'text-embedding-3-small').
|
|
30308
|
-
* @param settings - Model configuration settings (embedding type, model parameters, etc.).
|
|
30352
|
+
* @param settings - Model configuration settings (embedding type, model parameters, etc.).
|
|
30309
30353
|
* @param config - SAP AI Core deployment and destination configuration.
|
|
30310
30354
|
*/
|
|
30311
30355
|
constructor(modelId, settings = {}, config) {
|
|
@@ -30584,6 +30628,20 @@ function convertToSAPMessages(prompt, options = {}) {
|
|
|
30584
30628
|
}
|
|
30585
30629
|
|
|
30586
30630
|
// src/sap-ai-language-model.ts
|
|
30631
|
+
var PARAM_MAPPINGS = [
|
|
30632
|
+
{ camelCaseKey: "maxTokens", optionKey: "maxOutputTokens", outputKey: "max_tokens" },
|
|
30633
|
+
{ camelCaseKey: "temperature", optionKey: "temperature", outputKey: "temperature" },
|
|
30634
|
+
{ camelCaseKey: "topP", optionKey: "topP", outputKey: "top_p" },
|
|
30635
|
+
{ camelCaseKey: "topK", optionKey: "topK", outputKey: "top_k" },
|
|
30636
|
+
{
|
|
30637
|
+
camelCaseKey: "frequencyPenalty",
|
|
30638
|
+
optionKey: "frequencyPenalty",
|
|
30639
|
+
outputKey: "frequency_penalty"
|
|
30640
|
+
},
|
|
30641
|
+
{ camelCaseKey: "presencePenalty", optionKey: "presencePenalty", outputKey: "presence_penalty" },
|
|
30642
|
+
{ camelCaseKey: "seed", optionKey: "seed", outputKey: "seed" },
|
|
30643
|
+
{ camelCaseKey: "parallel_tool_calls", outputKey: "parallel_tool_calls" }
|
|
30644
|
+
];
|
|
30587
30645
|
var StreamIdGenerator = class {
|
|
30588
30646
|
generateResponseId() {
|
|
30589
30647
|
return crypto.randomUUID();
|
|
@@ -30659,49 +30717,12 @@ var SAPAILanguageModel = class {
|
|
|
30659
30717
|
try {
|
|
30660
30718
|
const { messages, orchestrationConfig, warnings } = await this.buildOrchestrationConfig(options);
|
|
30661
30719
|
const client = this.createClient(orchestrationConfig);
|
|
30662
|
-
const
|
|
30663
|
-
const requestBody = {
|
|
30664
|
-
messages,
|
|
30665
|
-
model: {
|
|
30666
|
-
...orchestrationConfig.promptTemplating.model
|
|
30667
|
-
},
|
|
30668
|
-
...promptTemplating.prompt.tools ? { tools: promptTemplating.prompt.tools } : {},
|
|
30669
|
-
...promptTemplating.prompt.response_format ? { response_format: promptTemplating.prompt.response_format } : {},
|
|
30670
|
-
...(() => {
|
|
30671
|
-
const masking = orchestrationConfig.masking;
|
|
30672
|
-
return masking && Object.keys(masking).length > 0 ? { masking } : {};
|
|
30673
|
-
})(),
|
|
30674
|
-
...(() => {
|
|
30675
|
-
const filtering = orchestrationConfig.filtering;
|
|
30676
|
-
return filtering && Object.keys(filtering).length > 0 ? { filtering } : {};
|
|
30677
|
-
})(),
|
|
30678
|
-
...(() => {
|
|
30679
|
-
const grounding = orchestrationConfig.grounding;
|
|
30680
|
-
return grounding && Object.keys(grounding).length > 0 ? { grounding } : {};
|
|
30681
|
-
})(),
|
|
30682
|
-
...(() => {
|
|
30683
|
-
const translation = orchestrationConfig.translation;
|
|
30684
|
-
return translation && Object.keys(translation).length > 0 ? { translation } : {};
|
|
30685
|
-
})()
|
|
30686
|
-
};
|
|
30720
|
+
const requestBody = this.buildRequestBody(messages, orchestrationConfig);
|
|
30687
30721
|
const response = await client.chatCompletion(
|
|
30688
30722
|
requestBody,
|
|
30689
30723
|
options.abortSignal ? { signal: options.abortSignal } : void 0
|
|
30690
30724
|
);
|
|
30691
|
-
const
|
|
30692
|
-
const responseHeaders = responseHeadersRaw ? Object.fromEntries(
|
|
30693
|
-
Object.entries(responseHeadersRaw).flatMap(([key, value]) => {
|
|
30694
|
-
if (typeof value === "string") return [[key, value]];
|
|
30695
|
-
if (Array.isArray(value)) {
|
|
30696
|
-
const strings = value.filter((item) => typeof item === "string").join("; ");
|
|
30697
|
-
return strings.length > 0 ? [[key, strings]] : [];
|
|
30698
|
-
}
|
|
30699
|
-
if (typeof value === "number" || typeof value === "boolean") {
|
|
30700
|
-
return [[key, String(value)]];
|
|
30701
|
-
}
|
|
30702
|
-
return [];
|
|
30703
|
-
})
|
|
30704
|
-
) : void 0;
|
|
30725
|
+
const responseHeaders = normalizeHeaders(response.rawResponse.headers);
|
|
30705
30726
|
const content = [];
|
|
30706
30727
|
const textContent = response.getContent();
|
|
30707
30728
|
if (textContent) {
|
|
@@ -30786,23 +30807,7 @@ var SAPAILanguageModel = class {
|
|
|
30786
30807
|
try {
|
|
30787
30808
|
const { messages, orchestrationConfig, warnings } = await this.buildOrchestrationConfig(options);
|
|
30788
30809
|
const client = this.createClient(orchestrationConfig);
|
|
30789
|
-
const
|
|
30790
|
-
const requestBody = {
|
|
30791
|
-
messages,
|
|
30792
|
-
model: {
|
|
30793
|
-
...orchestrationConfig.promptTemplating.model
|
|
30794
|
-
},
|
|
30795
|
-
...promptTemplating.prompt.tools ? { tools: promptTemplating.prompt.tools } : {},
|
|
30796
|
-
...promptTemplating.prompt.response_format ? { response_format: promptTemplating.prompt.response_format } : {},
|
|
30797
|
-
...(() => {
|
|
30798
|
-
const masking = orchestrationConfig.masking;
|
|
30799
|
-
return masking && Object.keys(masking).length > 0 ? { masking } : {};
|
|
30800
|
-
})(),
|
|
30801
|
-
...(() => {
|
|
30802
|
-
const filtering = orchestrationConfig.filtering;
|
|
30803
|
-
return filtering && Object.keys(filtering).length > 0 ? { filtering } : {};
|
|
30804
|
-
})()
|
|
30805
|
-
};
|
|
30810
|
+
const requestBody = this.buildRequestBody(messages, orchestrationConfig);
|
|
30806
30811
|
const streamResponse = await client.stream(requestBody, options.abortSignal, {
|
|
30807
30812
|
promptTemplating: { include_usage: true }
|
|
30808
30813
|
});
|
|
@@ -31157,20 +31162,14 @@ var SAPAILanguageModel = class {
|
|
|
31157
31162
|
this.settings.modelParams ?? {},
|
|
31158
31163
|
sapOptions?.modelParams ?? {}
|
|
31159
31164
|
);
|
|
31160
|
-
|
|
31161
|
-
|
|
31162
|
-
|
|
31163
|
-
|
|
31164
|
-
|
|
31165
|
-
|
|
31166
|
-
if (options.
|
|
31167
|
-
|
|
31168
|
-
if (frequencyPenalty !== void 0) {
|
|
31169
|
-
modelParams.frequency_penalty = frequencyPenalty;
|
|
31170
|
-
}
|
|
31171
|
-
const presencePenalty = options.presencePenalty ?? sapOptions?.modelParams?.presencePenalty ?? this.settings.modelParams?.presencePenalty;
|
|
31172
|
-
if (presencePenalty !== void 0) {
|
|
31173
|
-
modelParams.presence_penalty = presencePenalty;
|
|
31165
|
+
applyParameterOverrides(
|
|
31166
|
+
modelParams,
|
|
31167
|
+
options,
|
|
31168
|
+
sapOptions?.modelParams,
|
|
31169
|
+
this.settings.modelParams
|
|
31170
|
+
);
|
|
31171
|
+
if (options.stopSequences && options.stopSequences.length > 0) {
|
|
31172
|
+
modelParams.stop = options.stopSequences;
|
|
31174
31173
|
}
|
|
31175
31174
|
if (supportsN) {
|
|
31176
31175
|
const nValue = sapOptions?.modelParams?.n ?? this.settings.modelParams?.n;
|
|
@@ -31180,16 +31179,6 @@ var SAPAILanguageModel = class {
|
|
|
31180
31179
|
} else {
|
|
31181
31180
|
delete modelParams.n;
|
|
31182
31181
|
}
|
|
31183
|
-
const parallelToolCalls = sapOptions?.modelParams?.parallel_tool_calls ?? this.settings.modelParams?.parallel_tool_calls;
|
|
31184
|
-
if (parallelToolCalls !== void 0) {
|
|
31185
|
-
modelParams.parallel_tool_calls = parallelToolCalls;
|
|
31186
|
-
}
|
|
31187
|
-
if (options.stopSequences && options.stopSequences.length > 0) {
|
|
31188
|
-
modelParams.stop = options.stopSequences;
|
|
31189
|
-
}
|
|
31190
|
-
if (options.seed !== void 0) {
|
|
31191
|
-
modelParams.seed = options.seed;
|
|
31192
|
-
}
|
|
31193
31182
|
validateModelParamsWithWarnings(
|
|
31194
31183
|
{
|
|
31195
31184
|
frequencyPenalty: options.frequencyPenalty,
|
|
@@ -31240,25 +31229,35 @@ var SAPAILanguageModel = class {
|
|
|
31240
31229
|
...responseFormat ? { response_format: responseFormat } : {}
|
|
31241
31230
|
}
|
|
31242
31231
|
},
|
|
31243
|
-
...(
|
|
31244
|
-
|
|
31245
|
-
|
|
31246
|
-
|
|
31247
|
-
...(() => {
|
|
31248
|
-
const filtering = this.settings.filtering;
|
|
31249
|
-
return filtering && Object.keys(filtering).length > 0 ? { filtering } : {};
|
|
31250
|
-
})(),
|
|
31251
|
-
...(() => {
|
|
31252
|
-
const grounding = this.settings.grounding;
|
|
31253
|
-
return grounding && Object.keys(grounding).length > 0 ? { grounding } : {};
|
|
31254
|
-
})(),
|
|
31255
|
-
...(() => {
|
|
31256
|
-
const translation = this.settings.translation;
|
|
31257
|
-
return translation && Object.keys(translation).length > 0 ? { translation } : {};
|
|
31258
|
-
})()
|
|
31232
|
+
...this.settings.masking && Object.keys(this.settings.masking).length > 0 ? { masking: this.settings.masking } : {},
|
|
31233
|
+
...this.settings.filtering && Object.keys(this.settings.filtering).length > 0 ? { filtering: this.settings.filtering } : {},
|
|
31234
|
+
...this.settings.grounding && Object.keys(this.settings.grounding).length > 0 ? { grounding: this.settings.grounding } : {},
|
|
31235
|
+
...this.settings.translation && Object.keys(this.settings.translation).length > 0 ? { translation: this.settings.translation } : {}
|
|
31259
31236
|
};
|
|
31260
31237
|
return { messages, orchestrationConfig, warnings };
|
|
31261
31238
|
}
|
|
31239
|
+
/**
|
|
31240
|
+
* Builds the request body for SAP AI SDK chat completion or streaming.
|
|
31241
|
+
* @param messages - The chat messages to send.
|
|
31242
|
+
* @param orchestrationConfig - The orchestration configuration.
|
|
31243
|
+
* @returns The request body object.
|
|
31244
|
+
* @internal
|
|
31245
|
+
*/
|
|
31246
|
+
buildRequestBody(messages, orchestrationConfig) {
|
|
31247
|
+
const promptTemplating = orchestrationConfig.promptTemplating;
|
|
31248
|
+
return {
|
|
31249
|
+
messages,
|
|
31250
|
+
model: {
|
|
31251
|
+
...orchestrationConfig.promptTemplating.model
|
|
31252
|
+
},
|
|
31253
|
+
...promptTemplating.prompt.tools ? { tools: promptTemplating.prompt.tools } : {},
|
|
31254
|
+
...promptTemplating.prompt.response_format ? { response_format: promptTemplating.prompt.response_format } : {},
|
|
31255
|
+
...orchestrationConfig.masking && Object.keys(orchestrationConfig.masking).length > 0 ? { masking: orchestrationConfig.masking } : {},
|
|
31256
|
+
...orchestrationConfig.filtering && Object.keys(orchestrationConfig.filtering).length > 0 ? { filtering: orchestrationConfig.filtering } : {},
|
|
31257
|
+
...orchestrationConfig.grounding && Object.keys(orchestrationConfig.grounding).length > 0 ? { grounding: orchestrationConfig.grounding } : {},
|
|
31258
|
+
...orchestrationConfig.translation && Object.keys(orchestrationConfig.translation).length > 0 ? { translation: orchestrationConfig.translation } : {}
|
|
31259
|
+
};
|
|
31260
|
+
}
|
|
31262
31261
|
/**
|
|
31263
31262
|
* Creates an SAP AI SDK OrchestrationClient with the given configuration.
|
|
31264
31263
|
* @param config - The SAP AI SDK orchestration module configuration.
|
|
@@ -31269,6 +31268,18 @@ var SAPAILanguageModel = class {
|
|
|
31269
31268
|
return new import_orchestration2.OrchestrationClient(config, this.config.deploymentConfig, this.config.destination);
|
|
31270
31269
|
}
|
|
31271
31270
|
};
|
|
31271
|
+
function applyParameterOverrides(modelParams, options, sapModelParams, settingsModelParams) {
|
|
31272
|
+
const params = modelParams;
|
|
31273
|
+
for (const mapping of PARAM_MAPPINGS) {
|
|
31274
|
+
const value = (mapping.optionKey ? options[mapping.optionKey] : void 0) ?? (mapping.camelCaseKey ? sapModelParams?.[mapping.camelCaseKey] : void 0) ?? (mapping.camelCaseKey ? settingsModelParams?.[mapping.camelCaseKey] : void 0);
|
|
31275
|
+
if (value !== void 0) {
|
|
31276
|
+
params[mapping.outputKey] = value;
|
|
31277
|
+
}
|
|
31278
|
+
if (mapping.camelCaseKey && mapping.camelCaseKey !== mapping.outputKey) {
|
|
31279
|
+
Reflect.deleteProperty(params, mapping.camelCaseKey);
|
|
31280
|
+
}
|
|
31281
|
+
}
|
|
31282
|
+
}
|
|
31272
31283
|
function buildSAPToolParameters(schema) {
|
|
31273
31284
|
const schemaType = schema.type;
|
|
31274
31285
|
if (schemaType !== void 0 && schemaType !== "object") {
|
|
@@ -31436,7 +31447,6 @@ var import_orchestration5 = require("@sap-ai-sdk/orchestration");
|
|
|
31436
31447
|
buildTranslationConfig,
|
|
31437
31448
|
createSAPAIProvider,
|
|
31438
31449
|
getProviderName,
|
|
31439
|
-
isConfigReference,
|
|
31440
31450
|
sapAIEmbeddingProviderOptions,
|
|
31441
31451
|
sapAILanguageModelProviderOptions,
|
|
31442
31452
|
sapai
|