@sap-ai-sdk/langchain 1.8.1-20250302013118.0 → 1.8.1-20250304013124.0
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/README.md +13 -164
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/orchestration/client.d.ts +33 -0
- package/dist/orchestration/client.d.ts.map +1 -0
- package/dist/orchestration/client.js +80 -0
- package/dist/orchestration/client.js.map +1 -0
- package/dist/orchestration/index.d.ts +6 -0
- package/dist/orchestration/index.d.ts.map +1 -0
- package/dist/orchestration/index.js +6 -0
- package/dist/orchestration/index.js.map +1 -0
- package/dist/orchestration/orchestration-message-chunk.d.ts +13 -0
- package/dist/orchestration/orchestration-message-chunk.d.ts.map +1 -0
- package/dist/orchestration/orchestration-message-chunk.js +15 -0
- package/dist/orchestration/orchestration-message-chunk.js.map +1 -0
- package/dist/orchestration/orchestration-message.d.ts +13 -0
- package/dist/orchestration/orchestration-message.d.ts.map +1 -0
- package/dist/orchestration/orchestration-message.js +15 -0
- package/dist/orchestration/orchestration-message.js.map +1 -0
- package/dist/orchestration/types.d.ts +12 -0
- package/dist/orchestration/types.d.ts.map +1 -0
- package/dist/orchestration/types.js +2 -0
- package/dist/orchestration/types.js.map +1 -0
- package/dist/orchestration/util.d.ts +25 -0
- package/dist/orchestration/util.d.ts.map +1 -0
- package/dist/orchestration/util.js +136 -0
- package/dist/orchestration/util.js.map +1 -0
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -2,17 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
SAP Cloud SDK for AI is the official Software Development Kit (SDK) for **SAP AI Core**, **SAP Generative AI Hub**, and **Orchestration Service**.
|
|
4
4
|
|
|
5
|
-
This package provides LangChain
|
|
5
|
+
This package provides LangChain clients built on top of the foundation model and orchestration clients of the SAP Cloud SDK for AI.
|
|
6
6
|
|
|
7
7
|
### Table of Contents
|
|
8
8
|
|
|
9
9
|
- [Installation](#installation)
|
|
10
10
|
- [Prerequisites](#prerequisites)
|
|
11
|
-
- [Relationship between Models and Deployment ID](#relationship-between-models-and-deployment-id)
|
|
12
11
|
- [Usage](#usage)
|
|
13
|
-
- [Client
|
|
14
|
-
- [
|
|
15
|
-
- [Embedding Client](#embedding-client)
|
|
12
|
+
- [Orchestration Client](#orchestration-client)
|
|
13
|
+
- [Azure OpenAI Client](#azure-openai-client)
|
|
16
14
|
- [Local Testing](#local-testing)
|
|
17
15
|
- [Support, Feedback, Contribution](#support-feedback-contribution)
|
|
18
16
|
- [License](#license)
|
|
@@ -28,10 +26,11 @@ $ npm install @sap-ai-sdk/langchain
|
|
|
28
26
|
- [Enable the AI Core service in SAP BTP](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/initial-setup).
|
|
29
27
|
- Use the same `@langchain/core` version as the `@sap-ai-sdk/langchain` package, to see which langchain version this package is currently using, check our [package.json](./package.json).
|
|
30
28
|
- Configure the project with **Node.js v20 or higher** and **native ESM** support.
|
|
31
|
-
- Ensure a
|
|
32
|
-
- Use the [`DeploymentApi`](https://github.com/SAP/ai-sdk-js/blob/main/packages/ai-api/README.md#create-a-deployment) from `@sap-ai-sdk/ai-api` [
|
|
33
|
-
|
|
34
|
-
|
|
29
|
+
- Ensure that a relevant deployment is available in the SAP Generative AI Hub:
|
|
30
|
+
- Use the [`DeploymentApi`](https://github.com/SAP/ai-sdk-js/blob/main/packages/ai-api/README.md#create-a-deployment) from `@sap-ai-sdk/ai-api` or the [SAP AI Launchpad](https://help.sap.com/docs/sap-ai-core/generative-ai-hub/activate-generative-ai-hub-for-sap-ai-launchpad?locale=en-US&q=launchpad) to create a deployment.
|
|
31
|
+
- For **OpenAI model**, follow [this guide](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/create-deployment-for-generative-ai-model-in-sap-ai-core).
|
|
32
|
+
- For **orchestration service**, follow [this guide](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/create-deployment-for-orchestration).
|
|
33
|
+
- Once deployed, access the service via the `deploymentUrl`.
|
|
35
34
|
|
|
36
35
|
> **Accessing the AI Core Service via the SDK**
|
|
37
36
|
>
|
|
@@ -40,168 +39,18 @@ $ npm install @sap-ai-sdk/langchain
|
|
|
40
39
|
> - In Cloud Foundry, it's accessed from the `VCAP_SERVICES` environment variable.
|
|
41
40
|
> - In Kubernetes / Kyma environments, you have to mount the service binding as a secret instead, for more information refer to [this documentation](https://www.npmjs.com/package/@sap/xsenv#usage-in-kubernetes).
|
|
42
41
|
|
|
43
|
-
## Relationship between Models and Deployment ID
|
|
44
|
-
|
|
45
|
-
SAP AI Core manages access to generative AI models through the global AI scenario `foundation-models`.
|
|
46
|
-
Creating a deployment for a model requires access to this scenario.
|
|
47
|
-
|
|
48
|
-
Each model, model version, and resource group allows for a one-time deployment.
|
|
49
|
-
After deployment completion, the response includes a `deploymentUrl` and an `id`, which is the deployment ID.
|
|
50
|
-
For more information, see [here](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/create-deployment-for-generative-ai-model-in-sap-ai-core).
|
|
51
|
-
|
|
52
|
-
[Resource groups](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/resource-groups?q=resource+group) represent a virtual collection of related resources within the scope of one SAP AI Core tenant.
|
|
53
|
-
|
|
54
|
-
Consequently, each deployment ID and resource group uniquely map to a combination of model and model version within the `foundation-models` scenario.
|
|
55
|
-
|
|
56
42
|
## Usage
|
|
57
43
|
|
|
58
|
-
This package offers
|
|
44
|
+
This package offers LangChain clients for Azure OpenAI and SAP Orchestration service.
|
|
59
45
|
All clients comply with [LangChain's interface](https://js.langchain.com/docs/introduction).
|
|
60
46
|
|
|
61
|
-
### Client
|
|
62
|
-
|
|
63
|
-
To initialize a client, provide the model name:
|
|
64
|
-
|
|
65
|
-
```ts
|
|
66
|
-
import {
|
|
67
|
-
AzureOpenAiChatClient,
|
|
68
|
-
AzureOpenAiEmbeddingClient
|
|
69
|
-
} from '@sap-ai-sdk/langchain';
|
|
70
|
-
|
|
71
|
-
// For a chat client
|
|
72
|
-
const chatClient = new AzureOpenAiChatClient({ modelName: 'gpt-4o' });
|
|
73
|
-
// For an embedding client
|
|
74
|
-
const embeddingClient = new AzureOpenAiEmbeddingClient({ modelName: 'gpt-4o' });
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
In addition to the default parameters of the model vendor (e.g., OpenAI) and LangChain, additional parameters can be used to help narrow down the search for the desired model:
|
|
78
|
-
|
|
79
|
-
```ts
|
|
80
|
-
const chatClient = new AzureOpenAiChatClient({
|
|
81
|
-
modelName: 'gpt-4o',
|
|
82
|
-
modelVersion: '24-07-2021',
|
|
83
|
-
resourceGroup: 'my-resource-group'
|
|
84
|
-
});
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
**Do not pass a `deployment ID` to initialize the client.**
|
|
88
|
-
For the LangChain model clients, initialization is done using the model name, model version and resource group.
|
|
89
|
-
|
|
90
|
-
An important note is that LangChain clients by default attempt 6 retries with exponential backoff in case of a failure.
|
|
91
|
-
Especially in testing environments you might want to reduce this number to speed up the process:
|
|
92
|
-
|
|
93
|
-
```ts
|
|
94
|
-
const embeddingClient = new AzureOpenAiEmbeddingClient({
|
|
95
|
-
modelName: 'gpt-4o',
|
|
96
|
-
maxRetries: 0
|
|
97
|
-
});
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
#### Custom Destination
|
|
101
|
-
|
|
102
|
-
When initializing the `AzureOpenAiChatClient` and `AzureOpenAiEmbeddingClient` clients, it is possible to provide a custom destination.
|
|
103
|
-
For example, when targeting a destination with the name `my-destination`, the following code can be used:
|
|
104
|
-
|
|
105
|
-
```ts
|
|
106
|
-
const chatClient = new AzureOpenAiChatClient(
|
|
107
|
-
{
|
|
108
|
-
modelName: 'gpt-4o',
|
|
109
|
-
modelVersion: '24-07-2021',
|
|
110
|
-
resourceGroup: 'my-resource-group'
|
|
111
|
-
},
|
|
112
|
-
{
|
|
113
|
-
destinationName: 'my-destination'
|
|
114
|
-
}
|
|
115
|
-
);
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
By default, the fetched destination is cached.
|
|
119
|
-
To disable caching, set the `useCache` parameter to `false` together with the `destinationName` parameter.
|
|
47
|
+
### Orchestration Client
|
|
120
48
|
|
|
121
|
-
|
|
49
|
+
For more information about the Orchestration client, refer to the [documentation](https://github.com/SAP/ai-sdk-js/tree/main/packages/langchain/src/orchestration/README.md).
|
|
122
50
|
|
|
123
|
-
|
|
124
|
-
To invoke the client, pass a prompt:
|
|
51
|
+
### Azure OpenAI Client
|
|
125
52
|
|
|
126
|
-
|
|
127
|
-
const response = await chatClient.invoke("What's the capital of France?");
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
#### Advanced Example with Templating and Output Parsing
|
|
131
|
-
|
|
132
|
-
```ts
|
|
133
|
-
import { AzureOpenAiChatClient } from '@sap-ai-sdk/langchain';
|
|
134
|
-
import { StringOutputParser } from '@langchain/core/output_parsers';
|
|
135
|
-
import { ChatPromptTemplate } from '@langchain/core/prompts';
|
|
136
|
-
|
|
137
|
-
// initialize the client
|
|
138
|
-
const client = new AzureOpenAiChatClient({ modelName: 'gpt-35-turbo' });
|
|
139
|
-
|
|
140
|
-
// create a prompt template
|
|
141
|
-
const promptTemplate = ChatPromptTemplate.fromMessages([
|
|
142
|
-
['system', 'Answer the following in {language}:'],
|
|
143
|
-
['user', '{text}']
|
|
144
|
-
]);
|
|
145
|
-
// create an output parser
|
|
146
|
-
const parser = new StringOutputParser();
|
|
147
|
-
|
|
148
|
-
// chain together template, client, and parser
|
|
149
|
-
const llmChain = promptTemplate.pipe(client).pipe(parser);
|
|
150
|
-
|
|
151
|
-
// invoke the chain
|
|
152
|
-
return llmChain.invoke({
|
|
153
|
-
language: 'german',
|
|
154
|
-
text: 'What is the capital of France?'
|
|
155
|
-
});
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
### Embedding Client
|
|
159
|
-
|
|
160
|
-
Embedding clients allow embedding either text or document chunks (represented as arrays of strings).
|
|
161
|
-
While you can use them standalone, they are usually used in combination with other LangChain utilities, like a text splitter for preprocessing and a vector store for storage and retrieval of the relevant embeddings.
|
|
162
|
-
For a complete example how to implement RAG with our LangChain client, take a look at our [sample code](https://github.com/SAP/ai-sdk-js/blob/main/sample-code/src/langchain-azure-openai.ts).
|
|
163
|
-
|
|
164
|
-
#### Embed Text
|
|
165
|
-
|
|
166
|
-
```ts
|
|
167
|
-
const embeddedText = await embeddingClient.embedQuery(
|
|
168
|
-
'Paris is the capital of France.'
|
|
169
|
-
);
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
#### Embed Document Chunks
|
|
173
|
-
|
|
174
|
-
```ts
|
|
175
|
-
const embeddedDocuments = await embeddingClient.embedDocuments([
|
|
176
|
-
'Page 1: Paris is the capital of France.',
|
|
177
|
-
'Page 2: It is a beautiful city.'
|
|
178
|
-
]);
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
#### Preprocess, embed, and store documents
|
|
182
|
-
|
|
183
|
-
```ts
|
|
184
|
-
// Create a text splitter and split the document
|
|
185
|
-
const textSplitter = new RecursiveCharacterTextSplitter({
|
|
186
|
-
chunkSize: 2000,
|
|
187
|
-
chunkOverlap: 200
|
|
188
|
-
});
|
|
189
|
-
const splits = await textSplitter.splitDocuments(docs);
|
|
190
|
-
|
|
191
|
-
// Initialize the embedding client
|
|
192
|
-
const embeddingClient = new AzureOpenAiEmbeddingClient({
|
|
193
|
-
modelName: 'text-embedding-ada-002'
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
// Create a vector store from the document
|
|
197
|
-
const vectorStore = await MemoryVectorStore.fromDocuments(
|
|
198
|
-
splits,
|
|
199
|
-
embeddingClient
|
|
200
|
-
);
|
|
201
|
-
|
|
202
|
-
// Create a retriever for the vector store
|
|
203
|
-
const retriever = vectorStore.asRetriever();
|
|
204
|
-
```
|
|
53
|
+
For more information about Azure OpenAI client, refer to the [documentation](https://github.com/SAP/ai-sdk-js/tree/main/packages/langchain/src/openai/README.md).
|
|
205
54
|
|
|
206
55
|
## Local Testing
|
|
207
56
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export { AzureOpenAiChatClient, AzureOpenAiEmbeddingClient } from './openai/index.js';
|
|
2
2
|
export type { AzureOpenAiChatModelParams, AzureOpenAiEmbeddingModelParams, AzureOpenAiChatCallOptions } from './openai/index.js';
|
|
3
|
+
export { OrchestrationClient } from './orchestration/index.js';
|
|
4
|
+
export type { OrchestrationCallOptions } from './orchestration/index.js';
|
|
3
5
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,0BAA0B,EAC3B,MAAM,mBAAmB,CAAC;AAC3B,YAAY,EACV,0BAA0B,EAC1B,+BAA+B,EAC/B,0BAA0B,EAC3B,MAAM,mBAAmB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,0BAA0B,EAC3B,MAAM,mBAAmB,CAAC;AAC3B,YAAY,EACV,0BAA0B,EAC1B,+BAA+B,EAC/B,0BAA0B,EAC3B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,YAAY,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,0BAA0B,EAC3B,MAAM,mBAAmB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,0BAA0B,EAC3B,MAAM,mBAAmB,CAAC;AAM3B,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
|
2
|
+
import type { BaseLanguageModelInput } from '@langchain/core/language_models/base';
|
|
3
|
+
import type { Runnable, RunnableLike } from '@langchain/core/runnables';
|
|
4
|
+
import type { OrchestrationMessageChunk } from './orchestration-message-chunk.js';
|
|
5
|
+
import type { ChatResult } from '@langchain/core/outputs';
|
|
6
|
+
import type { OrchestrationModuleConfig } from '@sap-ai-sdk/orchestration';
|
|
7
|
+
import type { BaseChatModelParams } from '@langchain/core/language_models/chat_models';
|
|
8
|
+
import type { ResourceGroupConfig } from '@sap-ai-sdk/ai-api';
|
|
9
|
+
import type { BaseMessage } from '@langchain/core/messages';
|
|
10
|
+
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
11
|
+
import type { OrchestrationCallOptions } from './types.js';
|
|
12
|
+
import type { HttpDestinationOrFetchOptions } from '@sap-cloud-sdk/connectivity';
|
|
13
|
+
/**
|
|
14
|
+
* The Orchestration client.
|
|
15
|
+
*/
|
|
16
|
+
export declare class OrchestrationClient extends BaseChatModel<OrchestrationCallOptions, OrchestrationMessageChunk> {
|
|
17
|
+
orchestrationConfig: Omit<OrchestrationModuleConfig, 'streaming'>;
|
|
18
|
+
langchainOptions: BaseChatModelParams;
|
|
19
|
+
deploymentConfig?: ResourceGroupConfig | undefined;
|
|
20
|
+
destination?: HttpDestinationOrFetchOptions | undefined;
|
|
21
|
+
constructor(orchestrationConfig: Omit<OrchestrationModuleConfig, 'streaming'>, langchainOptions?: BaseChatModelParams, deploymentConfig?: ResourceGroupConfig | undefined, destination?: HttpDestinationOrFetchOptions | undefined);
|
|
22
|
+
_llmType(): string;
|
|
23
|
+
/**
|
|
24
|
+
* Create a new runnable sequence that runs each individual runnable in series,
|
|
25
|
+
* piping the output of one runnable into another runnable or runnable-like.
|
|
26
|
+
* @param coerceable - A runnable, function, or object whose values are functions or runnables.
|
|
27
|
+
* @returns A new runnable sequence.
|
|
28
|
+
*/
|
|
29
|
+
pipe<NewRunOutput>(coerceable: RunnableLike<OrchestrationMessageChunk, NewRunOutput>): Runnable<BaseLanguageModelInput, Exclude<NewRunOutput, Error>, OrchestrationCallOptions>;
|
|
30
|
+
_generate(messages: BaseMessage[], options: typeof this.ParsedCallOptions, runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
|
|
31
|
+
private mergeOrchestrationConfig;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/orchestration/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,6CAA6C,CAAC;AAO5E,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,sCAAsC,CAAC;AACnF,OAAO,KAAK,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACxE,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,kCAAkC,CAAC;AAClF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AAC3E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,6CAA6C,CAAC;AACvF,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,mCAAmC,CAAC;AAClF,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAC;AAC3D,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,6BAA6B,CAAC;AAEjF;;GAEG;AACH,qBAAa,mBAAoB,SAAQ,aAAa,CACpD,wBAAwB,EACxB,yBAAyB,CAC1B;IAGU,mBAAmB,EAAE,IAAI,CAAC,yBAAyB,EAAE,WAAW,CAAC;IACjE,gBAAgB,EAAE,mBAAmB;IACrC,gBAAgB,CAAC,EAAE,mBAAmB;IACtC,WAAW,CAAC,EAAE,6BAA6B;gBAH3C,mBAAmB,EAAE,IAAI,CAAC,yBAAyB,EAAE,WAAW,CAAC,EACjE,gBAAgB,GAAE,mBAAwB,EAC1C,gBAAgB,CAAC,EAAE,mBAAmB,YAAA,EACtC,WAAW,CAAC,EAAE,6BAA6B,YAAA;IAKpD,QAAQ,IAAI,MAAM;IAIlB;;;;;OAKG;IACM,IAAI,CAAC,YAAY,EACxB,UAAU,EAAE,YAAY,CAAC,yBAAyB,EAAE,YAAY,CAAC,GAChE,QAAQ,CACT,sBAAsB,EACtB,OAAO,CAAC,YAAY,EAAE,KAAK,CAAC,EAC5B,wBAAwB,CACzB;IAQc,SAAS,CACtB,QAAQ,EAAE,WAAW,EAAE,EACvB,OAAO,EAAE,OAAO,IAAI,CAAC,iBAAiB,EACtC,UAAU,CAAC,EAAE,wBAAwB,GACpC,OAAO,CAAC,UAAU,CAAC;IAoCtB,OAAO,CAAC,wBAAwB;CA8BjC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
|
2
|
+
import { OrchestrationClient as OrchestrationClientBase } from '@sap-ai-sdk/orchestration';
|
|
3
|
+
import { isTemplate, mapLangchainMessagesToOrchestrationMessages, mapOutputToChatResult } from './util.js';
|
|
4
|
+
/**
|
|
5
|
+
* The Orchestration client.
|
|
6
|
+
*/
|
|
7
|
+
export class OrchestrationClient extends BaseChatModel {
|
|
8
|
+
orchestrationConfig;
|
|
9
|
+
langchainOptions;
|
|
10
|
+
deploymentConfig;
|
|
11
|
+
destination;
|
|
12
|
+
constructor(
|
|
13
|
+
// TODO: Omit streaming until supported
|
|
14
|
+
orchestrationConfig, langchainOptions = {}, deploymentConfig, destination) {
|
|
15
|
+
super(langchainOptions);
|
|
16
|
+
this.orchestrationConfig = orchestrationConfig;
|
|
17
|
+
this.langchainOptions = langchainOptions;
|
|
18
|
+
this.deploymentConfig = deploymentConfig;
|
|
19
|
+
this.destination = destination;
|
|
20
|
+
}
|
|
21
|
+
_llmType() {
|
|
22
|
+
return 'orchestration';
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Create a new runnable sequence that runs each individual runnable in series,
|
|
26
|
+
* piping the output of one runnable into another runnable or runnable-like.
|
|
27
|
+
* @param coerceable - A runnable, function, or object whose values are functions or runnables.
|
|
28
|
+
* @returns A new runnable sequence.
|
|
29
|
+
*/
|
|
30
|
+
pipe(coerceable) {
|
|
31
|
+
return super.pipe(coerceable);
|
|
32
|
+
}
|
|
33
|
+
async _generate(messages, options, runManager) {
|
|
34
|
+
const res = await this.caller.callWithOptions({
|
|
35
|
+
signal: options.signal
|
|
36
|
+
}, () => {
|
|
37
|
+
const { inputParams, customRequestConfig } = options;
|
|
38
|
+
const mergedOrchestrationConfig = this.mergeOrchestrationConfig(options);
|
|
39
|
+
const orchestrationClient = new OrchestrationClientBase(mergedOrchestrationConfig, this.deploymentConfig, this.destination);
|
|
40
|
+
const messagesHistory = mapLangchainMessagesToOrchestrationMessages(messages);
|
|
41
|
+
return orchestrationClient.chatCompletion({
|
|
42
|
+
messagesHistory,
|
|
43
|
+
inputParams
|
|
44
|
+
}, customRequestConfig);
|
|
45
|
+
});
|
|
46
|
+
const content = res.getContent();
|
|
47
|
+
// TODO: Add streaming as soon as we support it
|
|
48
|
+
await runManager?.handleLLMNewToken(typeof content === 'string' ? content : '');
|
|
49
|
+
return mapOutputToChatResult(res.data);
|
|
50
|
+
}
|
|
51
|
+
mergeOrchestrationConfig(options) {
|
|
52
|
+
const { tools = [], stop = [] } = options;
|
|
53
|
+
return {
|
|
54
|
+
...this.orchestrationConfig,
|
|
55
|
+
llm: {
|
|
56
|
+
...this.orchestrationConfig.llm,
|
|
57
|
+
model_params: {
|
|
58
|
+
...this.orchestrationConfig.llm.model_params,
|
|
59
|
+
...(stop.length && {
|
|
60
|
+
stop: [
|
|
61
|
+
...(this.orchestrationConfig.llm.model_params?.stop || []),
|
|
62
|
+
...stop
|
|
63
|
+
]
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
templating: {
|
|
68
|
+
...this.orchestrationConfig.templating,
|
|
69
|
+
...(isTemplate(this.orchestrationConfig.templating) &&
|
|
70
|
+
tools.length && {
|
|
71
|
+
tools: [
|
|
72
|
+
...(this.orchestrationConfig.templating.tools || []),
|
|
73
|
+
...tools
|
|
74
|
+
]
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/orchestration/client.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/orchestration/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,kCAAkC,CAAC;AACjD,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/orchestration/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,kCAAkC,CAAC;AACjD,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AIMessageChunk } from '@langchain/core/messages';
|
|
2
|
+
import type { AIMessageChunkFields } from '@langchain/core/messages';
|
|
3
|
+
import type { ModuleResults } from '@sap-ai-sdk/orchestration';
|
|
4
|
+
/**
|
|
5
|
+
* An AI Message Chunk containing module results and request ID.
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
export declare class OrchestrationMessageChunk extends AIMessageChunk {
|
|
9
|
+
module_results: ModuleResults;
|
|
10
|
+
request_id: string;
|
|
11
|
+
constructor(fields: string | AIMessageChunkFields, module_results: ModuleResults, request_id: string);
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=orchestration-message-chunk.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"orchestration-message-chunk.d.ts","sourceRoot":"","sources":["../../src/orchestration/orchestration-message-chunk.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AACrE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAE/D;;;GAGG;AACH,qBAAa,yBAA0B,SAAQ,cAAc;IAC3D,cAAc,EAAE,aAAa,CAAC;IAC9B,UAAU,EAAE,MAAM,CAAC;gBAEjB,MAAM,EAAE,MAAM,GAAG,oBAAoB,EACrC,cAAc,EAAE,aAAa,EAC7B,UAAU,EAAE,MAAM;CAMrB"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AIMessageChunk } from '@langchain/core/messages';
|
|
2
|
+
/**
|
|
3
|
+
* An AI Message Chunk containing module results and request ID.
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export class OrchestrationMessageChunk extends AIMessageChunk {
|
|
7
|
+
module_results;
|
|
8
|
+
request_id;
|
|
9
|
+
constructor(fields, module_results, request_id) {
|
|
10
|
+
super(fields);
|
|
11
|
+
this.module_results = module_results;
|
|
12
|
+
this.request_id = request_id;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=orchestration-message-chunk.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"orchestration-message-chunk.js","sourceRoot":"","sources":["../../src/orchestration/orchestration-message-chunk.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAI1D;;;GAGG;AACH,MAAM,OAAO,yBAA0B,SAAQ,cAAc;IAC3D,cAAc,CAAgB;IAC9B,UAAU,CAAS;IACnB,YACE,MAAqC,EACrC,cAA6B,EAC7B,UAAkB;QAElB,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;QACrC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;CACF"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AIMessage } from '@langchain/core/messages';
|
|
2
|
+
import type { AIMessageFields } from '@langchain/core/messages';
|
|
3
|
+
import type { ModuleResults } from '@sap-ai-sdk/orchestration';
|
|
4
|
+
/**
|
|
5
|
+
* An AI Message containing module results and request ID.
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
export declare class OrchestrationMessage extends AIMessage {
|
|
9
|
+
module_results: ModuleResults;
|
|
10
|
+
request_id: string;
|
|
11
|
+
constructor(fields: string | AIMessageFields, module_results: ModuleResults, request_id: string);
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=orchestration-message.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"orchestration-message.d.ts","sourceRoot":"","sources":["../../src/orchestration/orchestration-message.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AACrD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAE/D;;;GAGG;AACH,qBAAa,oBAAqB,SAAQ,SAAS;IACjD,cAAc,EAAE,aAAa,CAAC;IAC9B,UAAU,EAAE,MAAM,CAAC;gBAEjB,MAAM,EAAE,MAAM,GAAG,eAAe,EAChC,cAAc,EAAE,aAAa,EAC7B,UAAU,EAAE,MAAM;CAMrB"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AIMessage } from '@langchain/core/messages';
|
|
2
|
+
/**
|
|
3
|
+
* An AI Message containing module results and request ID.
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export class OrchestrationMessage extends AIMessage {
|
|
7
|
+
module_results;
|
|
8
|
+
request_id;
|
|
9
|
+
constructor(fields, module_results, request_id) {
|
|
10
|
+
super(fields);
|
|
11
|
+
this.module_results = module_results;
|
|
12
|
+
this.request_id = request_id;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=orchestration-message.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"orchestration-message.js","sourceRoot":"","sources":["../../src/orchestration/orchestration-message.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAIrD;;;GAGG;AACH,MAAM,OAAO,oBAAqB,SAAQ,SAAS;IACjD,cAAc,CAAgB;IAC9B,UAAU,CAAS;IACnB,YACE,MAAgC,EAChC,cAA6B,EAC7B,UAAkB;QAElB,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;QACrC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;CACF"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Prompt, Template } from '@sap-ai-sdk/orchestration';
|
|
2
|
+
import type { BaseChatModelCallOptions } from '@langchain/core/language_models/chat_models';
|
|
3
|
+
import type { CustomRequestConfig } from '@sap-ai-sdk/core';
|
|
4
|
+
/**
|
|
5
|
+
* Options for an orchestration call.
|
|
6
|
+
*/
|
|
7
|
+
export type OrchestrationCallOptions = Pick<BaseChatModelCallOptions, 'stop' | 'signal' | 'timeout' | 'callbacks' | 'metadata' | 'runId' | 'runName' | 'tags'> & {
|
|
8
|
+
customRequestConfig?: CustomRequestConfig;
|
|
9
|
+
tools?: Template['tools'];
|
|
10
|
+
inputParams?: Prompt['inputParams'];
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/orchestration/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAClE,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,6CAA6C,CAAC;AAC5F,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAE5D;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAAG,IAAI,CACzC,wBAAwB,EACtB,MAAM,GACN,QAAQ,GACR,SAAS,GACT,WAAW,GACX,UAAU,GACV,OAAO,GACP,SAAS,GACT,MAAM,CACT,GAAG;IACF,mBAAmB,CAAC,EAAE,mBAAmB,CAAC;IAC1C,KAAK,CAAC,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAC;IAC1B,WAAW,CAAC,EAAE,MAAM,CAAC,aAAa,CAAC,CAAC;CACrC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/orchestration/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { ChatResult } from '@langchain/core/outputs';
|
|
2
|
+
import type { ChatMessage, CompletionPostResponse, Template } from '@sap-ai-sdk/orchestration';
|
|
3
|
+
import type { BaseMessage } from '@langchain/core/messages';
|
|
4
|
+
/**
|
|
5
|
+
* Checks if the object is a {@link Template}.
|
|
6
|
+
* @param object - The object to check.
|
|
7
|
+
* @returns True if the object is a {@link Template}.
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export declare function isTemplate(object: Record<string, any>): object is Template;
|
|
11
|
+
/**
|
|
12
|
+
* Maps LangChain messages to orchestration messages.
|
|
13
|
+
* @param messages - The LangChain messages to map.
|
|
14
|
+
* @returns The orchestration messages mapped from LangChain messages.
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export declare function mapLangchainMessagesToOrchestrationMessages(messages: BaseMessage[]): ChatMessage[];
|
|
18
|
+
/**
|
|
19
|
+
* Maps the completion response to a {@link ChatResult}.
|
|
20
|
+
* @param completionResponse - The completion response to map.
|
|
21
|
+
* @returns The mapped {@link ChatResult}.
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
export declare function mapOutputToChatResult(completionResponse: CompletionPostResponse): ChatResult;
|
|
25
|
+
//# sourceMappingURL=util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/orchestration/util.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,KAAK,EACV,WAAW,EACX,sBAAsB,EACtB,QAAQ,EACT,MAAM,2BAA2B,CAAC;AAGnC,OAAO,KAAK,EAEV,WAAW,EAGZ,MAAM,0BAA0B,CAAC;AAElC;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,IAAI,QAAQ,CAE1E;AAwED;;;;;GAKG;AACH,wBAAgB,2CAA2C,CACzD,QAAQ,EAAE,WAAW,EAAE,GACtB,WAAW,EAAE,CAEf;AAoBD;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,kBAAkB,EAAE,sBAAsB,GACzC,UAAU,CA4CZ"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { OrchestrationMessage } from './orchestration-message.js';
|
|
2
|
+
/**
|
|
3
|
+
* Checks if the object is a {@link Template}.
|
|
4
|
+
* @param object - The object to check.
|
|
5
|
+
* @returns True if the object is a {@link Template}.
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
export function isTemplate(object) {
|
|
9
|
+
return 'template' in object;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Maps {@link BaseMessage} to {@link ChatMessage}.
|
|
13
|
+
* @param message - The message to map.
|
|
14
|
+
* @returns The {@link ChatMessage}.
|
|
15
|
+
*/
|
|
16
|
+
// TODO: Add mapping of refusal property, once LangChain base class supports it natively.
|
|
17
|
+
function mapBaseMessageToChatMessage(message) {
|
|
18
|
+
switch (message.getType()) {
|
|
19
|
+
case 'ai':
|
|
20
|
+
return mapAiMessageToAzureOpenAiAssistantMessage(message);
|
|
21
|
+
case 'human':
|
|
22
|
+
return mapHumanMessageToChatMessage(message);
|
|
23
|
+
case 'system':
|
|
24
|
+
return mapSystemMessageToAzureOpenAiSystemMessage(message);
|
|
25
|
+
// TODO: As soon as tool messages are supported by orchestration, create mapping function similar to our azure mapping function.
|
|
26
|
+
case 'function':
|
|
27
|
+
case 'tool':
|
|
28
|
+
default:
|
|
29
|
+
throw new Error(`Unsupported message type: ${message.getType()}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Maps LangChain's {@link AIMessage} to Azure OpenAI's {@link AzureOpenAiChatCompletionRequestAssistantMessage}.
|
|
34
|
+
* @param message - The {@link AIMessage} to map.
|
|
35
|
+
* @returns The Azure OpenAI {@link AzureOpenAiChatCompletionRequestAssistantMessage}.
|
|
36
|
+
*/
|
|
37
|
+
function mapAiMessageToAzureOpenAiAssistantMessage(message) {
|
|
38
|
+
/* TODO: Tool calls are currently bugged in orchestration, pass these fields as soon as orchestration supports it.
|
|
39
|
+
const tool_calls =
|
|
40
|
+
mapLangchainToolCallToAzureOpenAiToolCall(message.tool_calls) ??
|
|
41
|
+
message.additional_kwargs.tool_calls;
|
|
42
|
+
*/
|
|
43
|
+
return {
|
|
44
|
+
/* TODO: Tool calls are currently bugged in orchestration, pass these fields as soon as orchestration supports it.
|
|
45
|
+
...(tool_calls?.length ? { tool_calls } : {}),
|
|
46
|
+
function_call: message.additional_kwargs.function_call,
|
|
47
|
+
*/
|
|
48
|
+
content: message.content,
|
|
49
|
+
role: 'assistant'
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function mapHumanMessageToChatMessage(message) {
|
|
53
|
+
return {
|
|
54
|
+
role: 'user',
|
|
55
|
+
content: message.content
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function mapSystemMessageToAzureOpenAiSystemMessage(message) {
|
|
59
|
+
// TODO: Remove as soon as image_url is a supported input for system messages in orchestration.
|
|
60
|
+
if (typeof message.content !== 'string' &&
|
|
61
|
+
message.content.some(content => content.type === 'image_url')) {
|
|
62
|
+
throw new Error('System messages with image URLs are not supported by the Orchestration Client.');
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
role: 'system',
|
|
66
|
+
content: message.content
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Maps LangChain messages to orchestration messages.
|
|
71
|
+
* @param messages - The LangChain messages to map.
|
|
72
|
+
* @returns The orchestration messages mapped from LangChain messages.
|
|
73
|
+
* @internal
|
|
74
|
+
*/
|
|
75
|
+
export function mapLangchainMessagesToOrchestrationMessages(messages) {
|
|
76
|
+
return messages.map(mapBaseMessageToChatMessage);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Maps {@link AzureOpenAiChatCompletionMessageToolCalls} to LangChain's {@link ToolCall}.
|
|
80
|
+
* @param toolCalls - The {@link AzureOpenAiChatCompletionMessageToolCalls} response.
|
|
81
|
+
* @returns The LangChain {@link ToolCall}.
|
|
82
|
+
*/
|
|
83
|
+
function mapAzureOpenAiToLangchainToolCall(toolCalls) {
|
|
84
|
+
if (toolCalls) {
|
|
85
|
+
return toolCalls.map(toolCall => ({
|
|
86
|
+
id: toolCall.id,
|
|
87
|
+
name: toolCall.function.name,
|
|
88
|
+
args: JSON.parse(toolCall.function.arguments),
|
|
89
|
+
type: 'tool_call'
|
|
90
|
+
}));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Maps the completion response to a {@link ChatResult}.
|
|
95
|
+
* @param completionResponse - The completion response to map.
|
|
96
|
+
* @returns The mapped {@link ChatResult}.
|
|
97
|
+
* @internal
|
|
98
|
+
*/
|
|
99
|
+
export function mapOutputToChatResult(completionResponse) {
|
|
100
|
+
const { orchestration_result, module_results, request_id } = completionResponse;
|
|
101
|
+
const { choices, created, id, model, object, usage, system_fingerprint } = orchestration_result;
|
|
102
|
+
return {
|
|
103
|
+
generations: choices.map(choice => ({
|
|
104
|
+
text: choice.message.content ?? '',
|
|
105
|
+
message: new OrchestrationMessage({
|
|
106
|
+
content: choice.message.content ?? '',
|
|
107
|
+
tool_calls: mapAzureOpenAiToLangchainToolCall(choice.message.tool_calls),
|
|
108
|
+
additional_kwargs: {
|
|
109
|
+
finish_reason: choice.finish_reason,
|
|
110
|
+
index: choice.index,
|
|
111
|
+
function_call: choice.message.function_call,
|
|
112
|
+
tool_calls: choice.message.tool_calls
|
|
113
|
+
}
|
|
114
|
+
}, module_results, request_id),
|
|
115
|
+
generationInfo: {
|
|
116
|
+
finish_reason: choice.finish_reason,
|
|
117
|
+
index: choice.index,
|
|
118
|
+
function_call: choice.message.function_call,
|
|
119
|
+
tool_calls: choice.message.tool_calls
|
|
120
|
+
}
|
|
121
|
+
})),
|
|
122
|
+
llmOutput: {
|
|
123
|
+
created,
|
|
124
|
+
id,
|
|
125
|
+
model,
|
|
126
|
+
object,
|
|
127
|
+
system_fingerprint,
|
|
128
|
+
tokenUsage: {
|
|
129
|
+
completionTokens: usage?.completion_tokens ?? 0,
|
|
130
|
+
promptTokens: usage?.prompt_tokens ?? 0,
|
|
131
|
+
totalTokens: usage?.total_tokens ?? 0
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../../src/orchestration/util.ts"],"names":[],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sap-ai-sdk/langchain",
|
|
3
|
-
"version": "1.8.1-
|
|
3
|
+
"version": "1.8.1-20250304013124.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"keywords": [
|
|
@@ -23,9 +23,10 @@
|
|
|
23
23
|
"uuid": "^11.1.0",
|
|
24
24
|
"@langchain/core": "0.3.40",
|
|
25
25
|
"zod-to-json-schema": "^3.24.3",
|
|
26
|
-
"@sap-ai-sdk/
|
|
27
|
-
"@sap-ai-sdk/core": "^1.8.1-
|
|
28
|
-
"@sap-ai-sdk/
|
|
26
|
+
"@sap-ai-sdk/ai-api": "^1.8.1-20250304013124.0",
|
|
27
|
+
"@sap-ai-sdk/core": "^1.8.1-20250304013124.0",
|
|
28
|
+
"@sap-ai-sdk/foundation-models": "^1.8.1-20250304013124.0",
|
|
29
|
+
"@sap-ai-sdk/orchestration": "^1.8.1-20250304013124.0"
|
|
29
30
|
},
|
|
30
31
|
"scripts": {
|
|
31
32
|
"compile": "tsc",
|