@sap-ai-sdk/foundation-models 1.3.1-20241119013140.0 → 1.3.1-20241122013124.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.
Files changed (2) hide show
  1. package/README.md +79 -1
  2. package/package.json +5 -5
package/README.md CHANGED
@@ -85,6 +85,8 @@ const chatClient = new AzureOpenAiChatClient({
85
85
 
86
86
  ### Azure OpenAI Chat Client
87
87
 
88
+ #### Making Requests
89
+
88
90
  Use the `AzureOpenAiChatClient` to send chat completion requests to an OpenAI model deployed in SAP generative AI hub.
89
91
 
90
92
  The client sends request with Azure OpenAI API version `2024-06-01`.
@@ -136,7 +138,7 @@ const response = await chatClient.run({
136
138
  const responseContent = response.getContent();
137
139
  const tokenUsage = response.getTokenUsage();
138
140
 
139
- logger.info(
141
+ console.log(
140
142
  `Total tokens consumed by the request: ${tokenUsage.total_tokens}\n` +
141
143
  `Input prompt tokens consumed: ${tokenUsage.prompt_tokens}\n` +
142
144
  `Output text completion tokens consumed: ${tokenUsage.completion_tokens}\n`
@@ -145,6 +147,82 @@ logger.info(
145
147
 
146
148
  Refer to `AzureOpenAiChatCompletionParameters` interface for other parameters that can be passed to the chat completion request.
147
149
 
150
+ #### Streaming
151
+
152
+ The `AzureOpenAiChatClient` supports streaming response for chat completion requests based on the [Server-sent events](https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events) standard.
153
+
154
+ Use the `stream()` method to receive a stream of chunk responses from the model.
155
+ After consuming the stream, call the helper methods to get the finish reason and token usage information respectively.
156
+
157
+ ```ts
158
+ const chatClient = new AzureOpenAiChatClient('gpt-4o');
159
+ const response = await chatClient.stream({
160
+ messages: [
161
+ {
162
+ role: 'user',
163
+ content: 'Give me a very long introduction of SAP Cloud SDK.'
164
+ }
165
+ ]
166
+ });
167
+
168
+ for await (const chunk of response.stream) {
169
+ console.log(JSON.stringify(chunk));
170
+ }
171
+
172
+ const finishReason = response.getFinishReason();
173
+ const tokenUsage = response.getTokenUsage();
174
+
175
+ console.log(`Finish reason: ${finishReason}\n`);
176
+ console.log(`Token usage: ${JSON.stringify(tokenUsage)}\n`);
177
+ ```
178
+
179
+ ##### Streaming the Delta Content
180
+
181
+ The client provides a helper method to extract delta content and stream string directly.
182
+
183
+ ```ts
184
+ for await (const chunk of response.stream.toContentStream()) {
185
+ console.log(chunk); // will log the delta content
186
+ }
187
+ ```
188
+
189
+ Each chunk will be a defined string containing the delta content.
190
+ Set `choiceIndex` parameter for `toContentStream()` method to stream a specific choice.
191
+
192
+ ##### Streaming with Abort Controller
193
+
194
+ Streaming request can be aborted using the `AbortController` API.
195
+ In case of an error, the SAP Cloud SDK for AI will automatically close the stream.
196
+ Additionally, it can be aborted manually by calling the `stream()` method with an `AbortController` object.
197
+
198
+ ```ts
199
+ const chatClient = new AzureOpenAiChatClient('gpt-4o');
200
+ const controller = new AbortController();
201
+ const response = await new AzureOpenAiChatClient('gpt-35-turbo').stream(
202
+ {
203
+ messages: [
204
+ {
205
+ role: 'user',
206
+ content: 'Give me a very long introduction of SAP Cloud SDK.'
207
+ }
208
+ ]
209
+ },
210
+ controller
211
+ );
212
+
213
+ // Abort the streaming request after one second
214
+ setTimeout(() => {
215
+ controller.abort();
216
+ }, 1000);
217
+
218
+ for await (const chunk of response.stream) {
219
+ console.log(JSON.stringify(chunk));
220
+ }
221
+ ```
222
+
223
+ In this example, streaming request will be aborted after one second.
224
+ Abort controller can be useful, e.g., when end-user wants to stop the stream or refreshes the page.
225
+
148
226
  ### Azure OpenAI Embedding Client
149
227
 
150
228
  Use the `AzureOpenAiEmbeddingClient` to send embedding requests to an OpenAI model deployed in SAP generative AI hub.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sap-ai-sdk/foundation-models",
3
- "version": "1.3.1-20241119013140.0",
3
+ "version": "1.3.1-20241122013124.0",
4
4
  "description": "",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [
@@ -20,10 +20,10 @@
20
20
  "internal.d.ts"
21
21
  ],
22
22
  "dependencies": {
23
- "@sap-cloud-sdk/http-client": "^3.22.2",
24
- "@sap-cloud-sdk/util": "^3.22.2",
25
- "@sap-ai-sdk/ai-api": "^1.3.1-20241119013140.0",
26
- "@sap-ai-sdk/core": "^1.3.1-20241119013140.0"
23
+ "@sap-cloud-sdk/http-client": "^3.23.0",
24
+ "@sap-cloud-sdk/util": "^3.23.0",
25
+ "@sap-ai-sdk/ai-api": "^1.3.1-20241122013124.0",
26
+ "@sap-ai-sdk/core": "^1.3.1-20241122013124.0"
27
27
  },
28
28
  "scripts": {
29
29
  "compile": "tsc",