@sap-ai-sdk/langchain 1.0.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/LICENSE +201 -0
- package/README.md +121 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +2 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +2 -0
- package/dist/internal.js.map +1 -0
- package/dist/openai/chat.d.ts +28 -0
- package/dist/openai/chat.d.ts.map +1 -0
- package/dist/openai/chat.js +50 -0
- package/dist/openai/chat.js.map +1 -0
- package/dist/openai/embedding.d.ts +17 -0
- package/dist/openai/embedding.d.ts.map +1 -0
- package/dist/openai/embedding.js +28 -0
- package/dist/openai/embedding.js.map +1 -0
- package/dist/openai/index.d.ts +5 -0
- package/dist/openai/index.d.ts.map +1 -0
- package/dist/openai/index.js +5 -0
- package/dist/openai/index.js.map +1 -0
- package/dist/openai/types.d.ts +20 -0
- package/dist/openai/types.d.ts.map +1 -0
- package/dist/openai/types.js +2 -0
- package/dist/openai/types.js.map +1 -0
- package/dist/openai/util.d.ts +24 -0
- package/dist/openai/util.d.ts.map +1 -0
- package/dist/openai/util.js +170 -0
- package/dist/openai/util.js.map +1 -0
- package/internal.d.ts +3 -0
- package/internal.js +2 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# @sap-ai-sdk/langchain
|
|
2
|
+
|
|
3
|
+
This package provides LangChain model clients, built on top of the foundation model clients of the SAP Cloud SDK for AI.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
1. [Installation](#installation)
|
|
8
|
+
2. [Pre-requisites](#pre-requisites)
|
|
9
|
+
3. [Usage](#usage)
|
|
10
|
+
- [Client Initialization](#client-initialization)
|
|
11
|
+
- [Chat Clients](#chat-clients)
|
|
12
|
+
- [Embedding Clients](#embedding-clients)
|
|
13
|
+
4. [Support, Feedback, Contribution](#support-feedback-contribution)
|
|
14
|
+
5. [License](#license)
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
$ npm install @sap-ai-sdk/langchain
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Pre-requisites
|
|
23
|
+
|
|
24
|
+
- [Enable the AI Core service in SAP BTP](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/initial-setup).
|
|
25
|
+
- Bind the service to your application.
|
|
26
|
+
- Ensure the project is configured with Node.js v20 or higher, along with native ESM support.
|
|
27
|
+
- For testing your application locally:
|
|
28
|
+
- Download a service key for your AI Core service instance.
|
|
29
|
+
- Create a `.env` file in the root of your directory.
|
|
30
|
+
- Add an entry `AICORE_SERVICE_KEY='<content-of-service-key>'`.
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
This package provides both chat and embedding clients, currently supporting Azure OpenAI.
|
|
35
|
+
All clients comply with [LangChain's interface](https://js.langchain.com/docs/introduction).
|
|
36
|
+
|
|
37
|
+
### Client Initialization
|
|
38
|
+
|
|
39
|
+
To initialize a client, provide the model name:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import {
|
|
43
|
+
AzureOpenAiChatClient,
|
|
44
|
+
AzureOpenAiEmbeddingClient
|
|
45
|
+
} from '@sap-ai-sdk/langchain';
|
|
46
|
+
|
|
47
|
+
// For a chat client
|
|
48
|
+
const chatClient = new AzureOpenAiChatClient({ modelName: 'gpt-4o' });
|
|
49
|
+
// For an embedding client
|
|
50
|
+
const embeddingClient = new AzureOpenAiEmbeddingClient({ modelName: 'gpt-4o' });
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
In addition to the default parameters of the model vendor (e.g. OpenAI) and LangChain, there are additional parameters, which you can use to narrow down the search for the model you want to use:
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
const chatClient = new AzureOpenAiChatClient({
|
|
57
|
+
modelName: 'gpt-4o',
|
|
58
|
+
modelVersion: '24-07-2021',
|
|
59
|
+
resourceGroup: 'my-resource-group'
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Chat Client
|
|
64
|
+
|
|
65
|
+
The chat clients allow you to interact with Azure OpenAI chat models, accessible via the generative AI hub of SAP AI Core.
|
|
66
|
+
To invoke the client, you only have a to pass a prompt:
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
const response = await chatClient.invoke("What's the capital of France?");
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
#### Advanced Example with Templating and Output Parsing
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { AzureOpenAiChatClient } from '@sap-ai-sdk/langchain';
|
|
76
|
+
import { StringOutputParser } from '@langchain/core/output_parsers';
|
|
77
|
+
import { ChatPromptTemplate } from '@langchain/core/prompts';
|
|
78
|
+
|
|
79
|
+
const client = new AzureOpenAiChatClient({ modelName: 'gpt-35-turbo' });
|
|
80
|
+
const promptTemplate = ChatPromptTemplate.fromMessages([
|
|
81
|
+
['system', 'Answer the following in {language}:'],
|
|
82
|
+
['user', '{text}']
|
|
83
|
+
]);
|
|
84
|
+
const parser = new StringOutputParser();
|
|
85
|
+
const llmChain = promptTemplate.pipe(client).pipe(parser);
|
|
86
|
+
const response = await llmChain.invoke({
|
|
87
|
+
language: 'german',
|
|
88
|
+
text: 'What is the capital of France?'
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Embedding Client
|
|
93
|
+
|
|
94
|
+
Embedding clients allow embedding either text or documents (represented as arrays of strings).
|
|
95
|
+
|
|
96
|
+
#### Embed Text
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
const embeddedText = await embeddingClient.embedQuery(
|
|
100
|
+
'Paris is the capital of France.'
|
|
101
|
+
);
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
#### Embed Documents
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
const embeddedDocument = await embeddingClient.embedDocuments([
|
|
108
|
+
'Page 1: Paris is the capital of France.',
|
|
109
|
+
'Page 2: It is a beautiful city.'
|
|
110
|
+
]);
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Support, Feedback, Contribution
|
|
114
|
+
|
|
115
|
+
This project is open to feature requests/suggestions, bug reports etc. via [GitHub issues](https://github.com/SAP/ai-sdk-js/issues).
|
|
116
|
+
|
|
117
|
+
Contribution and feedback are encouraged and always welcome. For more information about how to contribute, the project structure, as well as additional contribution information, see our [Contribution Guidelines](https://github.com/SAP/ai-sdk-js/blob/main/CONTRIBUTING.md).
|
|
118
|
+
|
|
119
|
+
## License
|
|
120
|
+
|
|
121
|
+
The SAP Cloud SDK for AI is released under the [Apache License Version 2.0.](http://www.apache.org/licenses/).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +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"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,0BAA0B,EAC3B,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal.d.ts","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC"}
|
package/dist/internal.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal.js","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
2
|
+
import { BaseMessage } from '@langchain/core/messages';
|
|
3
|
+
import type { ChatResult } from '@langchain/core/outputs';
|
|
4
|
+
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
|
5
|
+
import { AzureOpenAiChatModel } from '@sap-ai-sdk/core';
|
|
6
|
+
import type { AzureOpenAiChatCallOptions, AzureOpenAiChatModelParams } from './types.js';
|
|
7
|
+
/**
|
|
8
|
+
* LangChain chat client for Azure OpenAI consumption on SAP BTP.
|
|
9
|
+
*/
|
|
10
|
+
export declare class AzureOpenAiChatClient extends BaseChatModel<AzureOpenAiChatCallOptions> implements AzureOpenAiChatModelParams {
|
|
11
|
+
modelName: AzureOpenAiChatModel;
|
|
12
|
+
modelVersion?: string;
|
|
13
|
+
resourceGroup?: string;
|
|
14
|
+
temperature?: number;
|
|
15
|
+
top_p?: number;
|
|
16
|
+
logit_bias?: Record<string, unknown>;
|
|
17
|
+
user?: string;
|
|
18
|
+
n?: number;
|
|
19
|
+
presence_penalty?: number;
|
|
20
|
+
frequency_penalty?: number;
|
|
21
|
+
stop?: string | string[];
|
|
22
|
+
max_tokens?: number;
|
|
23
|
+
private openAiChatClient;
|
|
24
|
+
constructor(fields: AzureOpenAiChatModelParams);
|
|
25
|
+
_llmType(): string;
|
|
26
|
+
_generate(messages: BaseMessage[], options: typeof this.ParsedCallOptions, runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=chat.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chat.d.ts","sourceRoot":"","sources":["../../src/openai/chat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,mCAAmC,CAAC;AAC7E,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAE1D,OAAO,EAAE,aAAa,EAAE,MAAM,6CAA6C,CAAC;AAC5E,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAExD,OAAO,KAAK,EACV,0BAA0B,EAC1B,0BAA0B,EAC3B,MAAM,YAAY,CAAC;AAEpB;;GAEG;AACH,qBAAa,qBACX,SAAQ,aAAa,CAAC,0BAA0B,CAChD,YAAW,0BAA0B;IAErC,SAAS,EAAE,oBAAoB,CAAC;IAChC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACzB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,gBAAgB,CAA4B;gBAExC,MAAM,EAAE,0BAA0B;IAiB9C,QAAQ,IAAI,MAAM;IAIH,SAAS,CACtB,QAAQ,EAAE,WAAW,EAAE,EACvB,OAAO,EAAE,OAAO,IAAI,CAAC,iBAAiB,EACtC,UAAU,CAAC,EAAE,wBAAwB,GACpC,OAAO,CAAC,UAAU,CAAC;CAqBvB"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { AzureOpenAiChatClient as AzureOpenAiChatClientBase } from '@sap-ai-sdk/foundation-models';
|
|
2
|
+
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
|
3
|
+
import { mapLangchainToAiClient, mapOutputToChatResult } from './util.js';
|
|
4
|
+
/**
|
|
5
|
+
* LangChain chat client for Azure OpenAI consumption on SAP BTP.
|
|
6
|
+
*/
|
|
7
|
+
export class AzureOpenAiChatClient extends BaseChatModel {
|
|
8
|
+
modelName;
|
|
9
|
+
modelVersion;
|
|
10
|
+
resourceGroup;
|
|
11
|
+
temperature;
|
|
12
|
+
top_p;
|
|
13
|
+
logit_bias;
|
|
14
|
+
user;
|
|
15
|
+
n;
|
|
16
|
+
presence_penalty;
|
|
17
|
+
frequency_penalty;
|
|
18
|
+
stop;
|
|
19
|
+
max_tokens;
|
|
20
|
+
openAiChatClient;
|
|
21
|
+
constructor(fields) {
|
|
22
|
+
super(fields);
|
|
23
|
+
this.openAiChatClient = new AzureOpenAiChatClientBase(fields);
|
|
24
|
+
this.modelName = fields.modelName;
|
|
25
|
+
this.modelVersion = fields.modelVersion;
|
|
26
|
+
this.resourceGroup = fields.resourceGroup;
|
|
27
|
+
this.temperature = fields.temperature;
|
|
28
|
+
this.top_p = fields.top_p;
|
|
29
|
+
this.logit_bias = fields.logit_bias;
|
|
30
|
+
this.user = fields.user;
|
|
31
|
+
this.n = fields.n;
|
|
32
|
+
this.stop = fields.stop;
|
|
33
|
+
this.presence_penalty = fields.presence_penalty;
|
|
34
|
+
this.frequency_penalty = fields.frequency_penalty;
|
|
35
|
+
this.max_tokens = fields.max_tokens;
|
|
36
|
+
}
|
|
37
|
+
_llmType() {
|
|
38
|
+
return 'azure_openai';
|
|
39
|
+
}
|
|
40
|
+
async _generate(messages, options, runManager) {
|
|
41
|
+
const res = await this.caller.callWithOptions({
|
|
42
|
+
signal: options.signal
|
|
43
|
+
}, () => this.openAiChatClient.run(mapLangchainToAiClient(this, options, messages), options.requestConfig));
|
|
44
|
+
const content = res.getContent();
|
|
45
|
+
// we currently do not support streaming
|
|
46
|
+
await runManager?.handleLLMNewToken(typeof content === 'string' ? content : '');
|
|
47
|
+
return mapOutputToChatResult(res.data);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=chat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chat.js","sourceRoot":"","sources":["../../src/openai/chat.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,qBAAqB,IAAI,yBAAyB,EAAE,MAAM,+BAA+B,CAAC;AACnG,OAAO,EAAE,aAAa,EAAE,MAAM,6CAA6C,CAAC;AAE5E,OAAO,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAM1E;;GAEG;AACH,MAAM,OAAO,qBACX,SAAQ,aAAyC;IAGjD,SAAS,CAAuB;IAChC,YAAY,CAAU;IACtB,aAAa,CAAU;IACvB,WAAW,CAAU;IACrB,KAAK,CAAU;IACf,UAAU,CAA2B;IACrC,IAAI,CAAU;IACd,CAAC,CAAU;IACX,gBAAgB,CAAU;IAC1B,iBAAiB,CAAU;IAC3B,IAAI,CAAqB;IACzB,UAAU,CAAU;IACZ,gBAAgB,CAA4B;IAEpD,YAAY,MAAkC;QAC5C,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,IAAI,CAAC,gBAAgB,GAAG,IAAI,yBAAyB,CAAC,MAAM,CAAC,CAAC;QAC9D,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC;QAClC,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC;QACxC,IAAI,CAAC,aAAa,GAAG,MAAM,CAAC,aAAa,CAAC;QAC1C,IAAI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enAiChatModel } from '@sap-ai-sdk/core';
|
|
3
|
+
import { AzureOpenAiEmbeddingModelParams } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* LangChain embedding client for Azure OpenAI consumption on SAP BTP.
|
|
6
|
+
*/
|
|
7
|
+
export declare class AzureOpenAiEmbeddingClient extends Embeddings implements AzureOpenAiEmbeddingModelParams {
|
|
8
|
+
modelName: AzureOpenAiChatModel;
|
|
9
|
+
modelVersion?: string;
|
|
10
|
+
resourceGroup?: string;
|
|
11
|
+
private openAiEmbeddingClient;
|
|
12
|
+
constructor(fields: AzureOpenAiEmbeddingModelParams);
|
|
13
|
+
embedDocuments(documents: string[]): Promise<number[][]>;
|
|
14
|
+
embedQuery(input: string): Promise<number[]>;
|
|
15
|
+
private createEmbedding;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=embedding.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"embedding.d.ts","sourceRoot":"","sources":["../../src/openai/embedding.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AACxD,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,+BAA+B,EAAE,MAAM,YAAY,CAAC;AAE7D;;GAEG;AACH,qBAAa,0BACX,SAAQ,UACR,YAAW,+BAA+B;IAE1C,SAAS,EAAE,oBAAoB,CAAC;IAChC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB,OAAO,CAAC,qBAAqB,CAAiC;gBAElD,MAAM,EAAE,+BAA+B;IAQpC,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC;IAMxD,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;YAI7C,eAAe;CAS9B"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { AzureOpenAiEmbeddingClient as AzureOpenAiEmbeddingClientBase } from '@sap-ai-sdk/foundation-models';
|
|
2
|
+
import { Embeddings } from '@langchain/core/embeddings';
|
|
3
|
+
/**
|
|
4
|
+
* LangChain embedding client for Azure OpenAI consumption on SAP BTP.
|
|
5
|
+
*/
|
|
6
|
+
export class AzureOpenAiEmbeddingClient extends Embeddings {
|
|
7
|
+
modelName;
|
|
8
|
+
modelVersion;
|
|
9
|
+
resourceGroup;
|
|
10
|
+
openAiEmbeddingClient;
|
|
11
|
+
constructor(fields) {
|
|
12
|
+
super(fields);
|
|
13
|
+
this.openAiEmbeddingClient = new AzureOpenAiEmbeddingClientBase(fields);
|
|
14
|
+
this.modelName = fields.modelName;
|
|
15
|
+
this.modelVersion = fields.modelVersion;
|
|
16
|
+
this.resourceGroup = fields.resourceGroup;
|
|
17
|
+
}
|
|
18
|
+
async embedDocuments(documents) {
|
|
19
|
+
return Promise.all(documents.map(document => this.createEmbedding({ input: document })));
|
|
20
|
+
}
|
|
21
|
+
async embedQuery(input) {
|
|
22
|
+
return this.createEmbedding({ input });
|
|
23
|
+
}
|
|
24
|
+
async createEmbedding(query) {
|
|
25
|
+
return this.caller.callWithOptions({}, async () => (await this.openAiEmbeddingClient.run(query)).getEmbedding() ?? []);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=embedding.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"embedding.js","sourceRoot":"","sources":["../../src/openai/embedding.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,0BAA0B,IAAI,8BAA8B,EAE7D,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AAIxD;;GAEG;AACH,MAAM,OAAO,0BACX,SAAQ,UAAU;IAGlB,SAAS,CAAuB;IAChC,YAAY,CAAU;IACtB,aAAa,CAAU;IAEf,qBAAqB,CAAiC;IAE9D,YAAY,MAAuC;QACjD,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,IAAI,CAAC,qBAAqB,GAAG,IAAI,8BAA8B,CAAC,MAAM,CAAC,CAAC;QACxE,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC;QAClC,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC;QACxC,IAAI,CAAC,aAAa,GAAG,MAAM,CAAC,aAAa,CAAC;IAC5C,CAAC;IAEQ,KAAK,CAAC,cAAc,CAAC,SAAmB;QAC/C,OAAO,OAAO,CAAC,GAAG,CAChB,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC,IAAI,CAAC,eAAe,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC,CACrE,CAAC;IACJ,CAAC;IAEQ,KAAK,CAAC,UAAU,CAAC,KAAa;QACrC,OAAO,IAAI,CAAC,eAAe,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;IACzC,CAAC;IAEO,KAAK,CAAC,eAAe,CAC3B,KAAqC;QAErC,OAAO,IAAI,CAAC,MAAM,CAAC,eAAe,CAChC,EAAE,EACF,KAAK,IAAI,EAAE,CACT,CAAC,MAAM,IAAI,CAAC,qBAAqB,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,YAAY,EAAE,IAAI,EAAE,CACrE,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/openai/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/openai/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { BaseChatModelCallOptions, BaseChatModelParams } from '@langchain/core/language_models/chat_models';
|
|
2
|
+
import { BaseLLMParams } from '@langchain/core/language_models/llms';
|
|
3
|
+
import type { AzureOpenAiCreateChatCompletionRequest } from '@sap-ai-sdk/foundation-models';
|
|
4
|
+
import type { AzureOpenAiChatModel, CustomRequestConfig } from '@sap-ai-sdk/core';
|
|
5
|
+
import type { ModelConfig, ResourceGroupConfig } from '@sap-ai-sdk/ai-api';
|
|
6
|
+
/**
|
|
7
|
+
* Input type for {@link AzureOpenAiChatClient} initialization.
|
|
8
|
+
*/
|
|
9
|
+
export type AzureOpenAiChatModelParams = Omit<AzureOpenAiCreateChatCompletionRequest, 'messages' | 'response_format' | 'seed' | 'functions' | 'tools' | 'tool_choice'> & BaseChatModelParams & ModelConfig<AzureOpenAiChatModel> & ResourceGroupConfig;
|
|
10
|
+
/**
|
|
11
|
+
* Call options for the {@link AzureOpenAiChatClient}.
|
|
12
|
+
*/
|
|
13
|
+
export type AzureOpenAiChatCallOptions = BaseChatModelCallOptions & Pick<AzureOpenAiCreateChatCompletionRequest, 'response_format' | 'seed' | 'functions' | 'tools' | 'tool_choice'> & {
|
|
14
|
+
requestConfig?: CustomRequestConfig;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Input type for {@link AzureOpenAiEmbeddingClient} initialization.
|
|
18
|
+
*/
|
|
19
|
+
export type AzureOpenAiEmbeddingModelParams = ModelConfig<AzureOpenAiChatModel> & ResourceGroupConfig & BaseLLMParams;
|
|
20
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/openai/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,mBAAmB,EACpB,MAAM,6CAA6C,CAAC;AACrD,OAAO,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AACrE,OAAO,KAAK,EAAE,sCAAsC,EAAE,MAAM,+BAA+B,CAAC;AAC5F,OAAO,KAAK,EACV,oBAAoB,EACpB,mBAAmB,EACpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAE3E;;GAEG;AACH,MAAM,MAAM,0BAA0B,GAAG,IAAI,CAC3C,sCAAsC,EACpC,UAAU,GACV,iBAAiB,GACjB,MAAM,GACN,WAAW,GACX,OAAO,GACP,aAAa,CAChB,GACC,mBAAmB,GACnB,WAAW,CAAC,oBAAoB,CAAC,GACjC,mBAAmB,CAAC;AAEtB;;GAEG;AACH,MAAM,MAAM,0BAA0B,GAAG,wBAAwB,GAC/D,IAAI,CACF,sCAAsC,EACtC,iBAAiB,GAAG,MAAM,GAAG,WAAW,GAAG,OAAO,GAAG,aAAa,CACnE,GAAG;IACF,aAAa,CAAC,EAAE,mBAAmB,CAAC;CACrC,CAAC;AAEJ;;GAEG;AACH,MAAM,MAAM,+BAA+B,GACzC,WAAW,CAAC,oBAAoB,CAAC,GAAG,mBAAmB,GAAG,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/openai/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { BaseMessage } from '@langchain/core/messages';
|
|
2
|
+
import { ChatResult } from '@langchain/core/outputs';
|
|
3
|
+
import { type AzureOpenAiCreateChatCompletionResponse, type AzureOpenAiCreateChatCompletionRequest } from '@sap-ai-sdk/foundation-models';
|
|
4
|
+
import { AzureOpenAiChatClient } from './chat.js';
|
|
5
|
+
import { AzureOpenAiChatCallOptions } from './types.js';
|
|
6
|
+
/**
|
|
7
|
+
* Maps {@link AzureOpenAiCreateChatCompletionResponse} to LangChain's {@link ChatResult}.
|
|
8
|
+
* @param completionResponse - The {@link AzureOpenAiCreateChatCompletionResponse} response.
|
|
9
|
+
* @returns The LangChain {@link ChatResult}
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
export declare function mapOutputToChatResult(completionResponse: AzureOpenAiCreateChatCompletionResponse): ChatResult;
|
|
13
|
+
/**
|
|
14
|
+
* Maps LangChain's input interface to the AI SDK client's input interface
|
|
15
|
+
* @param client The LangChain Azure OpenAI client
|
|
16
|
+
* @param options The {@link AzureOpenAiChatCallOptions}
|
|
17
|
+
* @param messages The messages to be send
|
|
18
|
+
* @returns An AI SDK compatibile request
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
export declare function mapLangchainToAiClient(client: AzureOpenAiChatClient, options: AzureOpenAiChatCallOptions & {
|
|
22
|
+
promptIndex?: number;
|
|
23
|
+
}, messages: BaseMessage[]): AzureOpenAiCreateChatCompletionRequest;
|
|
24
|
+
//# sourceMappingURL=util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/openai/util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,WAAW,EAAe,MAAM,0BAA0B,CAAC;AAC/E,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAErD,OAAO,EAGL,KAAK,uCAAuC,EAC5C,KAAK,sCAAsC,EAO5C,MAAM,+BAA+B,CAAC;AAEvC,OAAO,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAClD,OAAO,EAAE,0BAA0B,EAAE,MAAM,YAAY,CAAC;AAiFxD;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,kBAAkB,EAAE,uCAAuC,GAC1D,UAAU,CAmCZ;AAiFD;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,qBAAqB,EAC7B,OAAO,EAAE,0BAA0B,GAAG;IAAE,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,EAC9D,QAAQ,EAAE,WAAW,EAAE,GACtB,sCAAsC,CAmBxC"}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { AIMessage } from '@langchain/core/messages';
|
|
2
|
+
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
3
|
+
/**
|
|
4
|
+
* Maps a LangChain {@link StructuredTool} to {@link AzureOpenAiChatCompletionFunction}.
|
|
5
|
+
* @param tool - Base class for tools that accept input of any shape defined by a Zod schema.
|
|
6
|
+
* @returns The OpenAI chat completion function.
|
|
7
|
+
*/
|
|
8
|
+
function mapToolToOpenAiFunction(tool) {
|
|
9
|
+
return {
|
|
10
|
+
name: tool.name,
|
|
11
|
+
description: tool.description,
|
|
12
|
+
parameters: zodToJsonSchema(tool.schema)
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Maps a LangChain {@link StructuredTool} to {@link AzureOpenAiChatCompletionTool}.
|
|
17
|
+
* @param tool - Base class for tools that accept input of any shape defined by a Zod schema.
|
|
18
|
+
* @returns The OpenAI chat completion tool.
|
|
19
|
+
*/
|
|
20
|
+
function mapToolToOpenAiTool(tool) {
|
|
21
|
+
return {
|
|
22
|
+
type: 'function',
|
|
23
|
+
function: mapToolToOpenAiFunction(tool)
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Maps a {@link BaseMessage} to{@link AzureOpenAiChatMessage} message role.
|
|
28
|
+
* @param message - The {@link BaseMessage} to map.
|
|
29
|
+
* @returns The {@link AzureOpenAiChatMessage} message Role.
|
|
30
|
+
*/
|
|
31
|
+
function mapBaseMessageToRole(message) {
|
|
32
|
+
const messageTypeToRoleMap = new Map([
|
|
33
|
+
['human', 'user'],
|
|
34
|
+
['ai', 'assistant'],
|
|
35
|
+
['system', 'system'],
|
|
36
|
+
['function', 'function'],
|
|
37
|
+
['tool', 'tool']
|
|
38
|
+
]);
|
|
39
|
+
const messageType = message._getType();
|
|
40
|
+
const role = messageTypeToRoleMap.get(messageType);
|
|
41
|
+
if (!role) {
|
|
42
|
+
throw new Error(`Unsupported message type: ${messageType}`);
|
|
43
|
+
}
|
|
44
|
+
return role;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Maps {@link AzureOpenAiCreateChatCompletionResponse} to LangChain's {@link ChatResult}.
|
|
48
|
+
* @param completionResponse - The {@link AzureOpenAiCreateChatCompletionResponse} response.
|
|
49
|
+
* @returns The LangChain {@link ChatResult}
|
|
50
|
+
* @internal
|
|
51
|
+
*/
|
|
52
|
+
export function mapOutputToChatResult(completionResponse) {
|
|
53
|
+
return {
|
|
54
|
+
generations: completionResponse.choices.map((choice) => ({
|
|
55
|
+
text: choice.message?.content || '',
|
|
56
|
+
message: new AIMessage({
|
|
57
|
+
content: choice.message?.content || '',
|
|
58
|
+
additional_kwargs: {
|
|
59
|
+
finish_reason: choice.finish_reason,
|
|
60
|
+
index: choice.index,
|
|
61
|
+
function_call: choice.message?.function_call,
|
|
62
|
+
tool_calls: choice.message?.tool_calls,
|
|
63
|
+
tool_call_id: ''
|
|
64
|
+
}
|
|
65
|
+
}),
|
|
66
|
+
generationInfo: {
|
|
67
|
+
finish_reason: choice.finish_reason,
|
|
68
|
+
index: choice.index,
|
|
69
|
+
function_call: choice.message?.function_call,
|
|
70
|
+
tool_calls: choice.message?.tool_calls
|
|
71
|
+
}
|
|
72
|
+
})),
|
|
73
|
+
llmOutput: {
|
|
74
|
+
created: completionResponse.created,
|
|
75
|
+
id: completionResponse.id,
|
|
76
|
+
model: completionResponse.model,
|
|
77
|
+
object: completionResponse.object,
|
|
78
|
+
tokenUsage: {
|
|
79
|
+
completionTokens: completionResponse.usage?.completion_tokens || 0,
|
|
80
|
+
promptTokens: completionResponse.usage?.prompt_tokens || 0,
|
|
81
|
+
totalTokens: completionResponse.usage?.total_tokens || 0
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Maps {@link BaseMessage} to {@link AzureOpenAiChatMessage}.
|
|
88
|
+
* @param message - The message to map.
|
|
89
|
+
* @returns The {@link AzureOpenAiChatMessage}.
|
|
90
|
+
*/
|
|
91
|
+
function mapBaseMessageToAzureOpenAiChatMessage(message) {
|
|
92
|
+
return removeUndefinedProperties({
|
|
93
|
+
name: message.name,
|
|
94
|
+
...mapRoleAndContent(message),
|
|
95
|
+
function_call: message.additional_kwargs.function_call,
|
|
96
|
+
tool_calls: message.additional_kwargs.tool_calls,
|
|
97
|
+
tool_call_id: mapToolCallId(message)
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
function mapRoleAndContent(baseMessage) {
|
|
101
|
+
const role = mapBaseMessageToRole(baseMessage);
|
|
102
|
+
if (!['system', 'user', 'assistant', 'tool', 'function'].includes(role)) {
|
|
103
|
+
throw new Error(`Unsupported message role: ${role}`);
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
role,
|
|
107
|
+
content: baseMessage.content
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function isStructuredToolArray(tools) {
|
|
111
|
+
return !!tools?.every(tool => Array.isArray(tool.lc_namespace));
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Has to return an empty string to match one of the types of {@link AzureOpenAiChatCompletionRequestMessage}.
|
|
115
|
+
* @internal
|
|
116
|
+
*/
|
|
117
|
+
function mapToolCallId(message) {
|
|
118
|
+
return message._getType() === 'tool'
|
|
119
|
+
? message.tool_call_id
|
|
120
|
+
: '';
|
|
121
|
+
}
|
|
122
|
+
function mapToolChoice(toolChoice) {
|
|
123
|
+
if (toolChoice === 'auto' || toolChoice === 'none') {
|
|
124
|
+
return toolChoice;
|
|
125
|
+
}
|
|
126
|
+
if (typeof toolChoice === 'string') {
|
|
127
|
+
return {
|
|
128
|
+
type: 'function',
|
|
129
|
+
function: { name: toolChoice }
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Maps LangChain's input interface to the AI SDK client's input interface
|
|
135
|
+
* @param client The LangChain Azure OpenAI client
|
|
136
|
+
* @param options The {@link AzureOpenAiChatCallOptions}
|
|
137
|
+
* @param messages The messages to be send
|
|
138
|
+
* @returns An AI SDK compatibile request
|
|
139
|
+
* @internal
|
|
140
|
+
*/
|
|
141
|
+
export function mapLangchainToAiClient(client, options, messages) {
|
|
142
|
+
return removeUndefinedProperties({
|
|
143
|
+
messages: messages.map(mapBaseMessageToAzureOpenAiChatMessage),
|
|
144
|
+
max_tokens: client.max_tokens === -1 ? undefined : client.max_tokens,
|
|
145
|
+
temperature: client.temperature,
|
|
146
|
+
top_p: client.top_p,
|
|
147
|
+
logit_bias: client.logit_bias,
|
|
148
|
+
n: client.n,
|
|
149
|
+
stop: options?.stop ?? client.stop,
|
|
150
|
+
functions: isStructuredToolArray(options?.functions)
|
|
151
|
+
? options?.functions.map(mapToolToOpenAiFunction)
|
|
152
|
+
: options?.functions,
|
|
153
|
+
tools: isStructuredToolArray(options?.tools)
|
|
154
|
+
? options?.tools.map(mapToolToOpenAiTool)
|
|
155
|
+
: options?.tools,
|
|
156
|
+
tool_choice: mapToolChoice(options?.tool_choice),
|
|
157
|
+
response_format: options?.response_format,
|
|
158
|
+
seed: options?.seed
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
function removeUndefinedProperties(obj) {
|
|
162
|
+
const result = { ...obj };
|
|
163
|
+
for (const key in result) {
|
|
164
|
+
if (result[key] === undefined) {
|
|
165
|
+
delete result[key];
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return result;
|
|
169
|
+
}
|
|
170
|
+
//# sourceMappingURL=util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../../src/openai/util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAA4B,MAAM,0BAA0B,CAAC;AAe/E,OAAO,EAAE,eAAe,EAAE,MAAM,o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Chain clients based on the @sap-ai-sdk",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"sap-ai-sdk",
|
|
8
|
+
"langchain"
|
|
9
|
+
],
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist/**/*.js",
|
|
15
|
+
"dist/**/*.js.map",
|
|
16
|
+
"dist/**/*.d.ts",
|
|
17
|
+
"dist/**/*.d.ts.map",
|
|
18
|
+
"internal.js",
|
|
19
|
+
"internal.d.ts"
|
|
20
|
+
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@langchain/core": "0.3.1",
|
|
23
|
+
"zod-to-json-schema": "^3.23.2",
|
|
24
|
+
"@sap-ai-sdk/ai-api": "^1.0.0",
|
|
25
|
+
"@sap-ai-sdk/foundation-models": "^1.0.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"typescript": "^5.5.4"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"compile": "tsc",
|
|
32
|
+
"compile:cjs": "tsc -p tsconfig.cjs.json",
|
|
33
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
|
|
34
|
+
"lint": "eslint \"**/*.ts\" && prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -c",
|
|
35
|
+
"lint:fix": "eslint \"**/*.ts\" --fix && prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -w --log-level error",
|
|
36
|
+
"check:public-api": "node --loader ts-node/esm ../../scripts/check-public-api-cli.ts"
|
|
37
|
+
}
|
|
38
|
+
}
|