@azure-rest/ai-inference 1.0.0-beta.4 → 1.0.0-beta.6

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 (172) hide show
  1. package/LICENSE +3 -3
  2. package/README.md +323 -248
  3. package/dist/browser/clientDefinitions.d.ts +14 -5
  4. package/dist/browser/clientDefinitions.d.ts.map +1 -1
  5. package/dist/browser/clientDefinitions.js.map +1 -1
  6. package/dist/browser/constants.d.ts +7 -0
  7. package/dist/browser/constants.d.ts.map +1 -1
  8. package/dist/browser/constants.js +7 -0
  9. package/dist/browser/constants.js.map +1 -1
  10. package/dist/browser/index.d.ts +11 -3
  11. package/dist/browser/index.d.ts.map +1 -1
  12. package/dist/browser/index.js +10 -3
  13. package/dist/browser/index.js.map +1 -1
  14. package/dist/browser/isUnexpected.d.ts +8 -1
  15. package/dist/browser/isUnexpected.d.ts.map +1 -1
  16. package/dist/browser/isUnexpected.js +0 -4
  17. package/dist/browser/isUnexpected.js.map +1 -1
  18. package/dist/browser/logger.d.ts.map +1 -1
  19. package/dist/browser/logger.js +7 -0
  20. package/dist/browser/logger.js.map +1 -1
  21. package/dist/browser/modelClient.d.ts +7 -0
  22. package/dist/browser/modelClient.d.ts.map +1 -1
  23. package/dist/browser/modelClient.js +1 -1
  24. package/dist/browser/modelClient.js.map +1 -1
  25. package/dist/browser/models.d.ts +94 -12
  26. package/dist/browser/models.d.ts.map +1 -1
  27. package/dist/browser/models.js.map +1 -1
  28. package/dist/browser/outputModels.d.ts +26 -17
  29. package/dist/browser/outputModels.d.ts.map +1 -1
  30. package/dist/browser/outputModels.js.map +1 -1
  31. package/dist/browser/parameters.d.ts +15 -8
  32. package/dist/browser/parameters.d.ts.map +1 -1
  33. package/dist/browser/parameters.js.map +1 -1
  34. package/dist/browser/responses.d.ts +9 -2
  35. package/dist/browser/responses.d.ts.map +1 -1
  36. package/dist/browser/responses.js.map +1 -1
  37. package/dist/browser/tracingHelper.d.ts +8 -1
  38. package/dist/browser/tracingHelper.d.ts.map +1 -1
  39. package/dist/browser/tracingHelper.js +26 -26
  40. package/dist/browser/tracingHelper.js.map +1 -1
  41. package/dist/browser/tracingPolicy.d.ts +7 -0
  42. package/dist/browser/tracingPolicy.d.ts.map +1 -1
  43. package/dist/browser/tracingPolicy.js +2 -2
  44. package/dist/browser/tracingPolicy.js.map +1 -1
  45. package/dist/commonjs/clientDefinitions.d.ts +14 -5
  46. package/dist/commonjs/clientDefinitions.d.ts.map +1 -1
  47. package/dist/commonjs/clientDefinitions.js.map +1 -1
  48. package/dist/commonjs/constants.d.ts +7 -0
  49. package/dist/commonjs/constants.d.ts.map +1 -1
  50. package/dist/commonjs/constants.js +9 -2
  51. package/dist/commonjs/constants.js.map +1 -1
  52. package/dist/commonjs/index.d.ts +11 -3
  53. package/dist/commonjs/index.d.ts.map +1 -1
  54. package/dist/commonjs/index.js +10 -3
  55. package/dist/commonjs/index.js.map +1 -1
  56. package/dist/commonjs/isUnexpected.d.ts +8 -1
  57. package/dist/commonjs/isUnexpected.d.ts.map +1 -1
  58. package/dist/commonjs/isUnexpected.js +0 -4
  59. package/dist/commonjs/isUnexpected.js.map +1 -1
  60. package/dist/commonjs/logger.d.ts.map +1 -1
  61. package/dist/commonjs/logger.js +7 -0
  62. package/dist/commonjs/logger.js.map +1 -1
  63. package/dist/commonjs/modelClient.d.ts +7 -0
  64. package/dist/commonjs/modelClient.d.ts.map +1 -1
  65. package/dist/commonjs/modelClient.js +1 -1
  66. package/dist/commonjs/modelClient.js.map +1 -1
  67. package/dist/commonjs/models.d.ts +94 -12
  68. package/dist/commonjs/models.d.ts.map +1 -1
  69. package/dist/commonjs/models.js.map +1 -1
  70. package/dist/commonjs/outputModels.d.ts +26 -17
  71. package/dist/commonjs/outputModels.d.ts.map +1 -1
  72. package/dist/commonjs/outputModels.js.map +1 -1
  73. package/dist/commonjs/parameters.d.ts +15 -8
  74. package/dist/commonjs/parameters.d.ts.map +1 -1
  75. package/dist/commonjs/parameters.js.map +1 -1
  76. package/dist/commonjs/responses.d.ts +9 -2
  77. package/dist/commonjs/responses.d.ts.map +1 -1
  78. package/dist/commonjs/responses.js.map +1 -1
  79. package/dist/commonjs/tracingHelper.d.ts +8 -1
  80. package/dist/commonjs/tracingHelper.d.ts.map +1 -1
  81. package/dist/commonjs/tracingHelper.js +26 -26
  82. package/dist/commonjs/tracingHelper.js.map +1 -1
  83. package/dist/commonjs/tracingPolicy.d.ts +7 -0
  84. package/dist/commonjs/tracingPolicy.d.ts.map +1 -1
  85. package/dist/commonjs/tracingPolicy.js +2 -2
  86. package/dist/commonjs/tracingPolicy.js.map +1 -1
  87. package/dist/commonjs/tsdoc-metadata.json +1 -1
  88. package/dist/esm/clientDefinitions.d.ts +14 -5
  89. package/dist/esm/clientDefinitions.d.ts.map +1 -1
  90. package/dist/esm/clientDefinitions.js.map +1 -1
  91. package/dist/esm/constants.d.ts +7 -0
  92. package/dist/esm/constants.d.ts.map +1 -1
  93. package/dist/esm/constants.js +7 -0
  94. package/dist/esm/constants.js.map +1 -1
  95. package/dist/esm/index.d.ts +11 -3
  96. package/dist/esm/index.d.ts.map +1 -1
  97. package/dist/esm/index.js +10 -3
  98. package/dist/esm/index.js.map +1 -1
  99. package/dist/esm/isUnexpected.d.ts +8 -1
  100. package/dist/esm/isUnexpected.d.ts.map +1 -1
  101. package/dist/esm/isUnexpected.js +0 -4
  102. package/dist/esm/isUnexpected.js.map +1 -1
  103. package/dist/esm/logger.d.ts.map +1 -1
  104. package/dist/esm/logger.js +7 -0
  105. package/dist/esm/logger.js.map +1 -1
  106. package/dist/esm/modelClient.d.ts +7 -0
  107. package/dist/esm/modelClient.d.ts.map +1 -1
  108. package/dist/esm/modelClient.js +1 -1
  109. package/dist/esm/modelClient.js.map +1 -1
  110. package/dist/esm/models.d.ts +94 -12
  111. package/dist/esm/models.d.ts.map +1 -1
  112. package/dist/esm/models.js.map +1 -1
  113. package/dist/esm/outputModels.d.ts +26 -17
  114. package/dist/esm/outputModels.d.ts.map +1 -1
  115. package/dist/esm/outputModels.js.map +1 -1
  116. package/dist/esm/parameters.d.ts +15 -8
  117. package/dist/esm/parameters.d.ts.map +1 -1
  118. package/dist/esm/parameters.js.map +1 -1
  119. package/dist/esm/responses.d.ts +9 -2
  120. package/dist/esm/responses.d.ts.map +1 -1
  121. package/dist/esm/responses.js.map +1 -1
  122. package/dist/esm/tracingHelper.d.ts +8 -1
  123. package/dist/esm/tracingHelper.d.ts.map +1 -1
  124. package/dist/esm/tracingHelper.js +26 -26
  125. package/dist/esm/tracingHelper.js.map +1 -1
  126. package/dist/esm/tracingPolicy.d.ts +7 -0
  127. package/dist/esm/tracingPolicy.d.ts.map +1 -1
  128. package/dist/esm/tracingPolicy.js +2 -2
  129. package/dist/esm/tracingPolicy.js.map +1 -1
  130. package/dist/react-native/clientDefinitions.d.ts +14 -5
  131. package/dist/react-native/clientDefinitions.d.ts.map +1 -1
  132. package/dist/react-native/clientDefinitions.js.map +1 -1
  133. package/dist/react-native/constants.d.ts +7 -0
  134. package/dist/react-native/constants.d.ts.map +1 -1
  135. package/dist/react-native/constants.js +7 -0
  136. package/dist/react-native/constants.js.map +1 -1
  137. package/dist/react-native/index.d.ts +11 -3
  138. package/dist/react-native/index.d.ts.map +1 -1
  139. package/dist/react-native/index.js +10 -3
  140. package/dist/react-native/index.js.map +1 -1
  141. package/dist/react-native/isUnexpected.d.ts +8 -1
  142. package/dist/react-native/isUnexpected.d.ts.map +1 -1
  143. package/dist/react-native/isUnexpected.js +0 -4
  144. package/dist/react-native/isUnexpected.js.map +1 -1
  145. package/dist/react-native/logger.d.ts.map +1 -1
  146. package/dist/react-native/logger.js +7 -0
  147. package/dist/react-native/logger.js.map +1 -1
  148. package/dist/react-native/modelClient.d.ts +7 -0
  149. package/dist/react-native/modelClient.d.ts.map +1 -1
  150. package/dist/react-native/modelClient.js +1 -1
  151. package/dist/react-native/modelClient.js.map +1 -1
  152. package/dist/react-native/models.d.ts +94 -12
  153. package/dist/react-native/models.d.ts.map +1 -1
  154. package/dist/react-native/models.js.map +1 -1
  155. package/dist/react-native/outputModels.d.ts +26 -17
  156. package/dist/react-native/outputModels.d.ts.map +1 -1
  157. package/dist/react-native/outputModels.js.map +1 -1
  158. package/dist/react-native/parameters.d.ts +15 -8
  159. package/dist/react-native/parameters.d.ts.map +1 -1
  160. package/dist/react-native/parameters.js.map +1 -1
  161. package/dist/react-native/responses.d.ts +9 -2
  162. package/dist/react-native/responses.d.ts.map +1 -1
  163. package/dist/react-native/responses.js.map +1 -1
  164. package/dist/react-native/tracingHelper.d.ts +8 -1
  165. package/dist/react-native/tracingHelper.d.ts.map +1 -1
  166. package/dist/react-native/tracingHelper.js +26 -26
  167. package/dist/react-native/tracingHelper.js.map +1 -1
  168. package/dist/react-native/tracingPolicy.d.ts +7 -0
  169. package/dist/react-native/tracingPolicy.d.ts.map +1 -1
  170. package/dist/react-native/tracingPolicy.js +2 -2
  171. package/dist/react-native/tracingPolicy.js.map +1 -1
  172. package/package.json +30 -22
package/README.md CHANGED
@@ -8,28 +8,27 @@ Key links:
8
8
 
9
9
  - [Source code](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/ai/ai-inference-rest)
10
10
  - [Package (NPM)](https://aka.ms/npm-azure-rest-ai-inference)
11
- - [API reference documentation](https://aka.ms/AAp1kxa)
11
+ - [API reference documentation](https://learn.microsoft.com/javascript/api/@azure-rest/ai-inference/)
12
12
  - [Samples](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/ai/ai-inference-rest/samples)
13
13
 
14
14
  ## Getting started
15
15
 
16
- ```javascript
16
+ ```ts snippet:ReadmeSample_Node
17
17
  import ModelClient, { isUnexpected } from "@azure-rest/ai-inference";
18
18
  import { AzureKeyCredential } from "@azure/core-auth";
19
- const client = new ModelClient(
19
+
20
+ const client = ModelClient(
20
21
  "https://<Azure Model endpoint>",
21
- new AzureKeyCredential("<Azure API key>")
22
+ new AzureKeyCredential("<Azure API key>"),
22
23
  );
23
24
 
24
25
  const response = await client.path("/chat/completions").post({
25
26
  body: {
26
- messages: [
27
- {role: "user", content: "How many feet are in a mile?"},
28
- ],
29
- }
27
+ messages: [{ role: "user", content: "How many feet are in a mile?" }],
28
+ },
30
29
  });
31
30
 
32
- if(isUnexpected(response)) {
31
+ if (isUnexpected(response)) {
33
32
  throw response.body.error;
34
33
  }
35
34
  console.log(response.body.choices[0].message.content);
@@ -45,13 +44,14 @@ console.log(response.body.choices[0].message.content);
45
44
 
46
45
  ### Install the `@azure-rest/ai-inference` package
47
46
 
48
- Install the Azure ModelClient REST client REST client library for JavaScript with `npm`:
47
+ Install the Azure Inference REST client library for JavaScript with `npm`:
49
48
 
50
49
  ```bash
51
50
  npm install @azure-rest/ai-inference
52
51
  ```
53
52
 
54
- ### Create and authenticate a `ModelClient`
53
+ ### Create and authenticate the Inference client
54
+
55
55
  #### Using an API Key from Azure
56
56
 
57
57
  You can authenticate with an Azure API key using the [Azure Core Auth library][azure_core_auth]. To use the AzureKeyCredential provider shown below, please install the `@azure/core-auth` package:
@@ -66,11 +66,11 @@ Use the [Azure Portal][azure_portal] to browse to your Model deployment and retr
66
66
 
67
67
  Once you have an API key and endpoint, you can use the `AzureKeyCredential` class to authenticate the client as follows:
68
68
 
69
- ```javascript
69
+ ```ts snippet:ReadmeSample_KeyCredential
70
70
  import ModelClient from "@azure-rest/ai-inference";
71
71
  import { AzureKeyCredential } from "@azure/core-auth";
72
72
 
73
- const client = new ModelClient("<endpoint>", new AzureKeyCredential("<API key>"));
73
+ const client = ModelClient("<endpoint>", new AzureKeyCredential("<API key>"));
74
74
  ```
75
75
 
76
76
  #### Using an Azure Active Directory Credential
@@ -84,44 +84,37 @@ npm install @azure/identity
84
84
 
85
85
  Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`.
86
86
 
87
- ```javascript
87
+ ```ts snippet:ReadmeSample_TokenCredential
88
88
  import ModelClient from "@azure-rest/ai-inference";
89
- import { DefaultAzureCredential } from "@azure/identity";
89
+ import { DefaultAzureCredential } from "@azure/identity";
90
90
 
91
- const client = new ModelClient("<endpoint>", new DefaultAzureCredential());
91
+ const client = ModelClient("<endpoint>", new DefaultAzureCredential());
92
92
  ```
93
93
 
94
94
  ## Key concepts
95
95
 
96
96
  The main concept to understand is [Completions][azure_openai_completions_docs]. Briefly explained, completions provides its functionality in the form of a text prompt, which by using a specific [model](https://learn.microsoft.com/azure/cognitive-services/openai/concepts/models), will then attempt to match the context and patterns, providing an output text. The following code snippet provides a rough overview:
97
97
 
98
- ```javascript
98
+ ```ts snippet:ReadmeSample_Completions
99
99
  import ModelClient, { isUnexpected } from "@azure-rest/ai-inference";
100
100
  import { AzureKeyCredential } from "@azure/core-auth";
101
101
 
102
- async function main(){
103
- const client = new ModelClient(
102
+ const client = ModelClient(
104
103
  "https://your-model-endpoint/",
105
- new AzureKeyCredential("your-model-api-key"));
106
-
107
- const response = await client.path("/chat/completions").post({
108
- body: {
109
- messages: [
110
- {role: "user", content: "Hello, world!"},
111
- ],
112
- }
113
- });
104
+ new AzureKeyCredential("your-model-api-key"),
105
+ );
114
106
 
115
- if(isUnexpected(response)) {
116
- throw response.body.error;
117
- }
107
+ const response = await client.path("/chat/completions").post({
108
+ body: {
109
+ messages: [{ role: "user", content: "Hello, world!" }],
110
+ },
111
+ });
118
112
 
119
- console.log(response.body.choices[0].message.content);
113
+ if (isUnexpected(response)) {
114
+ throw response.body.error;
120
115
  }
121
116
 
122
- main().catch((err) => {
123
- console.error("The sample encountered an error:", err);
124
- });
117
+ console.log(response.body.choices[0].message.content);
125
118
  ```
126
119
 
127
120
  ## Examples
@@ -136,122 +129,117 @@ npm install @azure/core-sse
136
129
 
137
130
  This example authenticates using a DefaultAzureCredential, then generates chat responses to input chat question and messages.
138
131
 
139
- ```javascript
132
+ ```ts snippet:ReadmeSample_ChatbotResponse
140
133
  import ModelClient from "@azure-rest/ai-inference";
141
134
  import { DefaultAzureCredential } from "@azure/identity";
142
135
  import { createSseStream } from "@azure/core-sse";
136
+ import { IncomingMessage } from "node:http";
143
137
 
144
- async function main(){
145
- const endpoint = "https://myaccount.openai.azure.com/";
146
- const client = new ModelClient(endpoint, new DefaultAzureCredential());
138
+ const endpoint = "https://myaccount.openai.azure.com/";
139
+ const client = ModelClient(endpoint, new DefaultAzureCredential());
147
140
 
148
- const messages = [
149
- // NOTE: "system" role is not supported on all Azure Models
150
- { role: "system", content: "You are a helpful assistant. You will talk like a pirate." },
151
- { role: "user", content: "Can you help me?" },
152
- { role: "assistant", content: "Arrrr! Of course, me hearty! What can I do for ye?" },
153
- { role: "user", content: "What's the best way to train a parrot?" },
154
- ];
141
+ const messages = [
142
+ // NOTE: "system" role is not supported on all Azure Models
143
+ { role: "system", content: "You are a helpful assistant. You will talk like a pirate." },
144
+ { role: "user", content: "Can you help me?" },
145
+ { role: "assistant", content: "Arrrr! Of course, me hearty! What can I do for ye?" },
146
+ { role: "user", content: "What's the best way to train a parrot?" },
147
+ ];
155
148
 
156
- console.log(`Messages: ${messages.map((m) => m.content).join("\n")}`);
149
+ console.log(`Messages: ${messages.map((m) => m.content).join("\n")}`);
157
150
 
158
- const response = await client.path("/chat/completions").post({
151
+ const response = await client
152
+ .path("/chat/completions")
153
+ .post({
159
154
  body: {
160
155
  messages,
161
156
  stream: true,
162
- max_tokens: 128
163
- }
164
- }).asNodeStream();
157
+ max_tokens: 128,
158
+ },
159
+ })
160
+ .asNodeStream();
165
161
 
166
- const stream = response.body;
167
- if (!stream) {
168
- throw new Error("The response stream is undefined");
169
- }
162
+ const stream = response.body;
163
+ if (!stream) {
164
+ throw new Error("The response stream is undefined");
165
+ }
170
166
 
171
- if (response.status !== "200") {
172
- throw new Error(`Failed to get chat completions: ${response.body.error}`);
173
- }
167
+ if (response.status !== "200") {
168
+ throw new Error("Failed to get chat completions");
169
+ }
174
170
 
175
- const sses = createSseStream(stream);
171
+ const sses = createSseStream(stream as IncomingMessage);
176
172
 
177
- for await (const event of sses) {
178
- if (event.data === "[DONE]") {
179
- return;
180
- }
181
- for (const choice of (JSON.parse(event.data)).choices) {
182
- console.log(choice.delta?.content ?? "");
183
- }
173
+ for await (const event of sses) {
174
+ if (event.data === "[DONE]") {
175
+ return;
176
+ }
177
+ for (const choice of JSON.parse(event.data).choices) {
178
+ console.log(choice.delta?.content ?? "");
184
179
  }
185
180
  }
186
-
187
- main().catch((err) => {
188
- console.error("The sample encountered an error:", err);
189
- });
190
181
  ```
191
182
 
192
183
  ### Generate Multiple Completions With Subscription Key
193
184
 
194
185
  This example generates text responses to input prompts using an Azure subscription key
195
186
 
196
- ```javascript
197
- import ModelClient from "@azure-rest/ai-inference";
187
+ ```ts snippet:ReadmeSample_MultipleCompletions
188
+ import ModelClient, { isUnexpected } from "@azure-rest/ai-inference";
198
189
  import { AzureKeyCredential } from "@azure/core-auth";
199
190
 
200
- async function main(){
201
- // Replace with your Model API key
202
- const key = "YOUR_MODEL_API_KEY";
203
- const endpoint = "https://your-model-endpoint/";
204
- const client = new ModelClient(endpoint, new AzureKeyCredential(key));
205
-
206
- const messages = [
207
- { role: "user", content: "How are you today?" },
208
- { role: "user", content: "What is inference in the context of AI?" },
209
- { role: "user", content: "Why do children love dinosaurs?" },
210
- { role: "user", content: "Generate a proof of Euler's identity" },
211
- { role: "user", content: "Describe in single words only the good things that come into your mind about your mother." },
212
- ];
213
-
214
- let promptIndex = 0;
215
- const response = await client.path("/chat/completions").post({
216
- body: {
217
- messages
218
- }
219
- });
191
+ // Replace with your Model API key
192
+ const key = "YOUR_MODEL_API_KEY";
193
+ const endpoint = "https://your-model-endpoint/";
194
+ const client = ModelClient(endpoint, new AzureKeyCredential(key));
220
195
 
221
- if(response.status !== "200") {
222
- throw response.body.error;
223
- }
224
- for (const choice of response.body.choices) {
225
- const completion = choice.message.content;
226
- console.log(`Input: ${messages[promptIndex++].content}`);
227
- console.log(`Chatbot: ${completion}`);
228
- }
229
- }
196
+ const messages = [
197
+ { role: "user", content: "How are you today?" },
198
+ { role: "user", content: "What is inference in the context of AI?" },
199
+ { role: "user", content: "Why do children love dinosaurs?" },
200
+ { role: "user", content: "Generate a proof of Euler's identity" },
201
+ {
202
+ role: "user",
203
+ content:
204
+ "Describe in single words only the good things that come into your mind about your mother.",
205
+ },
206
+ ];
230
207
 
231
- main().catch((err) => {
232
- console.error("The sample encountered an error:", err);
208
+ let promptIndex = 0;
209
+ const response = await client.path("/chat/completions").post({
210
+ body: {
211
+ messages,
212
+ },
233
213
  });
214
+
215
+ if (isUnexpected(response)) {
216
+ throw response.body.error;
217
+ }
218
+ for (const choice of response.body.choices) {
219
+ const completion = choice.message.content;
220
+ console.log(`Input: ${messages[promptIndex++].content}`);
221
+ console.log(`Chatbot: ${completion}`);
222
+ }
234
223
  ```
235
224
 
236
225
  ### Summarize Text with Completion
237
226
 
238
227
  This example generates a summarization of the given input prompt.
239
228
 
240
- ```javascript
241
- import ModelClient from "@azure-rest/ai-inference";
229
+ ```ts snippet:ReadmeSample_SummarizeText
230
+ import ModelClient, { isUnexpected } from "@azure-rest/ai-inference";
242
231
  import { DefaultAzureCredential } from "@azure/identity";
243
232
 
244
- async function main(){
245
- const endpoint = "https://your-model-endpoint/";
246
- const client = new ModelClient(endpoint, new DefaultAzureCredential());
233
+ const endpoint = "https://myaccount.openai.azure.com/";
234
+ const client = ModelClient(endpoint, new DefaultAzureCredential());
247
235
 
248
- const textToSummarize = `
236
+ const textToSummarize = `
249
237
  Two independent experiments reported their results this morning at CERN, Europe's high-energy physics laboratory near Geneva in Switzerland. Both show convincing evidence of a new boson particle weighing around 125 gigaelectronvolts, which so far fits predictions of the Higgs previously made by theoretical physicists.
250
238
 
251
239
  ""As a layman I would say: 'I think we have it'. Would you agree?"" Rolf-Dieter Heuer, CERN's director-general, asked the packed auditorium. The physicists assembled there burst into applause.
252
240
  :`;
253
241
 
254
- const summarizationPrompt = `
242
+ const summarizationPrompt = `
255
243
  Summarize the following text.
256
244
 
257
245
  Text:
@@ -262,70 +250,64 @@ async function main(){
262
250
  Summary:
263
251
  `;
264
252
 
265
- console.log(`Input: ${summarizationPrompt}`);
253
+ console.log(`Input: ${summarizationPrompt}`);
266
254
 
267
- const response = await client.path("/chat/completions").post({
268
- body: {
269
- messages: [
270
- { role: "user", content: summarizationPrompt }
271
- ],
272
- max_tokens: 64
273
- }
274
- });
255
+ const response = await client.path("/chat/completions").post({
256
+ body: {
257
+ messages: [{ role: "user", content: summarizationPrompt }],
258
+ max_tokens: 64,
259
+ },
260
+ });
275
261
 
276
- if(response.status !== "200") {
277
- throw response.body.error;
278
- }
279
- const completion = response.body.choices[0].message.content;
280
- console.log(`Summarization: ${completion}`);
262
+ if (isUnexpected(response)) {
263
+ throw response.body.error;
281
264
  }
282
-
283
- main().catch((err) => {
284
- console.error("The sample encountered an error:", err);
285
- });
265
+ const completion = response.body.choices[0].message.content;
266
+ console.log(`Summarization: ${completion}`);
286
267
  ```
287
268
 
288
269
  ### Use chat tools
289
270
 
290
271
  **Tools** extend chat completions by allowing an assistant to invoke defined functions and other capabilities in the
291
- process of fulfilling a chat completions request. To use chat tools, start by defining a function tool:
272
+ process of fulfilling a chat completions request. To use chat tools, start by defining a function tool named `getCurrentWeather`. With the tool defined, include that new definition in the options for a chat completions request:
273
+
274
+ ```ts snippet:ReadmeSample_ChatTools
275
+ import ModelClient from "@azure-rest/ai-inference";
276
+ import { DefaultAzureCredential } from "@azure/identity";
277
+
278
+ const endpoint = "https://myaccount.openai.azure.com/";
279
+ const client = ModelClient(endpoint, new DefaultAzureCredential());
292
280
 
293
- ```js
294
281
  const getCurrentWeather = {
295
- name: "get_current_weather",
296
- description: "Get the current weather in a given location",
297
- parameters: {
298
- type: "object",
299
- properties: {
300
- location: {
301
- type: "string",
302
- description: "The city and state, e.g. San Francisco, CA",
303
- },
304
- unit: {
305
- type: "string",
306
- enum: ["celsius", "fahrenheit"],
307
- },
282
+ name: "get_current_weather",
283
+ description: "Get the current weather in a given location",
284
+ parameters: {
285
+ type: "object",
286
+ properties: {
287
+ location: {
288
+ type: "string",
289
+ description: "The city and state, e.g. San Francisco, CA",
290
+ },
291
+ unit: {
292
+ type: "string",
293
+ enum: ["celsius", "fahrenheit"],
308
294
  },
309
- required: ["location"],
310
295
  },
311
- };
312
- ```
313
-
314
- With the tool defined, include that new definition in the options for a chat completions request:
296
+ required: ["location"],
297
+ },
298
+ };
315
299
 
316
- ```js
317
300
  const messages = [{ role: "user", content: "What is the weather like in Boston?" }];
318
- const tools = [
319
- {
320
- type: "function",
321
- function: getCurrentWeather,
322
- },
323
- ];
324
301
  const result = await client.path("/chat/completions").post({
325
302
  body: {
326
303
  messages,
327
- tools
328
- }
304
+ tools: [
305
+ {
306
+ type: "function",
307
+ function: getCurrentWeather,
308
+ },
309
+ ],
310
+ },
329
311
  });
330
312
  ```
331
313
 
@@ -333,19 +315,19 @@ When the assistant decides that one or more tools should be used, the response m
333
315
  calls" that must all be resolved via "tool messages" on the subsequent request. This resolution of tool calls into
334
316
  new request messages can be thought of as a sort of "callback" for chat completions.
335
317
 
336
- ```js
318
+ ```ts snippet:ReadmeSample_ChatToolsResponse
337
319
  // Purely for convenience and clarity, this function handles tool call responses.
338
320
  function applyToolCall({ function: call, id }) {
339
- if (call.name === "get_current_weather") {
340
- const { location, unit } = JSON.parse(call.arguments);
341
- // In a real application, this would be a call to a weather API with location and unit parameters
342
- return {
343
- role: "tool",
344
- content: `The weather in ${location} is 72 degrees ${unit} and sunny.`,
345
- toolCallId: id,
346
- }
347
- }
348
- throw new Error(`Unknown tool call: ${call.name}`);
321
+ if (call.name === "get_current_weather") {
322
+ const { location, unit } = JSON.parse(call.arguments);
323
+ // In a real application, this would be a call to a weather API with location and unit parameters
324
+ return {
325
+ role: "tool",
326
+ content: `The weather in ${location} is 72 degrees ${unit} and sunny.`,
327
+ toolCallId: id,
328
+ };
329
+ }
330
+ throw new Error(`Unknown tool call: ${call.name}`);
349
331
  }
350
332
  ```
351
333
 
@@ -353,87 +335,164 @@ To provide tool call resolutions to the assistant to allow the request to contin
353
335
  context -- including the original system and user messages, the response from the assistant that included the tool
354
336
  calls, and the tool messages that resolved each of those tools -- when making a subsequent request.
355
337
 
356
- ```js
357
- const choice = result.body.choices[0];
358
- const responseMessage = choice.message;
359
- if (responseMessage?.role === "assistant") {
360
- const requestedToolCalls = responseMessage?.toolCalls;
361
- if (requestedToolCalls?.length) {
362
- const toolCallResolutionMessages = [
363
- ...messages,
364
- responseMessage,
365
- ...requestedToolCalls.map(applyToolCall),
366
- ];
367
- const toolCallResolutionResult = await client.path("/chat/completions").post({
368
- body: {
369
- messages: toolCallResolutionMessages
370
- }
371
- });
372
- // continue handling the response as normal
338
+ ```ts snippet:ReadmeSample_ChatToolsResolution
339
+ import ModelClient from "@azure-rest/ai-inference";
340
+ import { DefaultAzureCredential } from "@azure/identity";
341
+
342
+ const endpoint = "https://myaccount.openai.azure.com/";
343
+ const client = ModelClient(endpoint, new DefaultAzureCredential());
344
+
345
+ // From previous snippets
346
+ const messages = [{ role: "user", content: "What is the weather like in Boston?" }];
347
+
348
+ function applyToolCall({ function: call, id }) {
349
+ // from previous snippet
350
+ }
351
+
352
+ // Handle result from previous snippet
353
+ async function handleResponse(result) {
354
+ const choice = result.body.choices[0];
355
+ const responseMessage = choice.message;
356
+ if (responseMessage?.role === "assistant") {
357
+ const requestedToolCalls = responseMessage?.toolCalls;
358
+ if (requestedToolCalls?.length) {
359
+ const toolCallResolutionMessages = [
360
+ ...messages,
361
+ responseMessage,
362
+ ...requestedToolCalls.map(applyToolCall),
363
+ ];
364
+ const toolCallResolutionResult = await client.path("/chat/completions").post({
365
+ body: {
366
+ messages: toolCallResolutionMessages,
367
+ },
368
+ });
369
+ // continue handling the response as normal
370
+ }
373
371
  }
374
372
  }
375
373
  ```
376
374
 
377
- ### Chat with images (using models supporting image chat, such as gpt-4o)
375
+ ### Chat with images (using models supporting image chat, such as gpt-4o)
378
376
 
379
377
  Some Azure models allow you to use images as input components into chat completions.
380
378
 
381
- To do this, provide distinct content items on the user message(s) for the chat completions request:
379
+ To do this, provide distinct content items on the user message(s) for the chat completions request. Chat Completions will then proceed as usual, though the model may report the more informative `finish_details` in lieu
380
+ of `finish_reason`.
382
381
 
383
- ```js
384
- const url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
385
- const messages = [{
386
- role: "user", content: [{
387
- type: "image_url",
388
- image_url: {
389
- url,
390
- detail: "auto"
391
- }
392
- }]},
393
- {role: "user", content: "describe the image"}];
394
- ```
382
+ ```ts snippet:ReadmeSample_ChatWithImages
383
+ import ModelClient, { isUnexpected } from "@azure-rest/ai-inference";
384
+ import { DefaultAzureCredential } from "@azure/identity";
395
385
 
396
- Chat Completions will then proceed as usual, though the model may report the more informative `finish_details` in lieu
397
- of `finish_reason`:
386
+ const endpoint = "https://myaccount.openai.azure.com/";
387
+ const client = ModelClient(endpoint, new DefaultAzureCredential());
388
+
389
+ const url =
390
+ "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg";
391
+ const messages = [
392
+ {
393
+ role: "user",
394
+ content: [
395
+ {
396
+ type: "image_url",
397
+ image_url: {
398
+ url,
399
+ detail: "auto",
400
+ },
401
+ },
402
+ ],
403
+ },
404
+ { role: "user", content: "describe the image" },
405
+ ];
398
406
 
399
- ```js
400
407
  const response = await client.path("/chat/completions").post({
401
408
  body: {
402
- messages
409
+ messages,
410
+ },
403
411
  });
404
- console.log(`Chatbot: ${response.choices[0].message?.content}`);
412
+
413
+ if (isUnexpected(response)) {
414
+ throw response.body.error;
415
+ }
416
+ console.log(`Chatbot: ${response.body.choices[0].message?.content}`);
405
417
  ```
406
418
 
407
419
  ### Text Embeddings example
408
420
 
409
- This example demonstrates how to get text embeddings with Entra ID authentication.
421
+ This example demonstrates how to get text embeddings with Entra ID authentication.
410
422
 
411
- ```javascript
423
+ ```ts snippet:ReadmeSample_TextEmbeddings
412
424
  import ModelClient, { isUnexpected } from "@azure-rest/ai-inference";
413
425
  import { DefaultAzureCredential } from "@azure/identity";
414
426
 
415
- const endpoint = "<your_model_endpoint>";
416
- const credential = new DefaultAzureCredential();
427
+ const endpoint = "https://myaccount.openai.azure.com/";
428
+ const client = ModelClient(endpoint, new DefaultAzureCredential());
417
429
 
418
- async function main(){
419
- const client = ModelClient(endpoint, credential);
420
- const response = await client.path("/embeddings").post({
421
- body: {
422
- input: ["first phrase", "second phrase", "third phrase"]
423
- }
424
- });
430
+ const response = await client.path("/embeddings").post({
431
+ body: {
432
+ input: ["first phrase", "second phrase", "third phrase"],
433
+ },
434
+ });
425
435
 
426
- if (isUnexpected(response)) {
427
- throw response.body.error;
428
- }
429
- for (const data of response.body.data) {
430
- console.log(`data length: ${data.length}, [${data[0]}, ${data[1]}, ..., ${data[data.length - 2]}, ${data[data.length - 1]}]`);
436
+ if (isUnexpected(response)) {
437
+ throw response.body.error;
438
+ }
439
+ for (const data of response.body.data) {
440
+ console.log(
441
+ `data length: ${data.embedding.length}, [${data[0]}, ${data[1]}, ..., ${data[data.embedding.length - 2]}, ${data[data.embedding.length - 1]}]`,
442
+ );
443
+ }
444
+ ```
445
+
446
+ The length of the embedding vector depends on the model, but you should see something like this:
447
+
448
+ ```text
449
+ data: length=1024, [0.0013399124, -0.01576233, ..., 0.007843018, 0.000238657]
450
+ data: length=1024, [0.036590576, -0.0059547424, ..., 0.011405945, 0.004863739]
451
+ data: length=1024, [0.04196167, 0.029083252, ..., -0.0027484894, 0.0073127747]
452
+ ```
453
+
454
+ To generate embeddings for additional phrases, simply call `client.path("/embeddings").post` multiple times using the same `client`.
455
+
456
+ ### Image Embeddings example
457
+
458
+ This example demonstrates how to get image embeddings with Entra ID authentication.
459
+
460
+ ```ts snippet:ReadmeSample_ImageEmbeddings
461
+ import { DefaultAzureCredential } from "@azure/identity";
462
+ import { readFileSync } from "node:fs";
463
+ import ModelClient, { isUnexpected } from "@azure-rest/ai-inference";
464
+
465
+ const endpoint = "https://myaccount.openai.azure.com/";
466
+ const credential = new DefaultAzureCredential();
467
+
468
+ function getImageDataUrl(imageFile, imageFormat) {
469
+ try {
470
+ const imageBuffer = readFileSync(imageFile);
471
+ const imageBase64 = imageBuffer.toString("base64");
472
+ return `data:image/${imageFormat};base64,${imageBase64}`;
473
+ } catch (error) {
474
+ console.error(`Could not read '${imageFile}'.`);
475
+ console.error("Set the correct path to the image file before running this sample.");
476
+ process.exit(1);
431
477
  }
432
478
  }
433
479
 
434
- main().catch((err) => {
435
- console.error("The sample encountered an error:", err);
480
+ const client = ModelClient(endpoint, credential);
481
+ const image = getImageDataUrl("<image_file>", "<image_format>");
482
+ const response = await client.path("/images/embeddings").post({
483
+ body: {
484
+ input: [{ image }],
485
+ },
436
486
  });
487
+
488
+ if (isUnexpected(response)) {
489
+ throw response.body.error;
490
+ }
491
+ for (const data of response.body.data) {
492
+ console.log(
493
+ `data length: ${data.embedding.length}, [${data[0]}, ${data[1]}, ..., ${data[data.embedding.length - 2]}, ${data[data.embedding.length - 1]}]`,
494
+ );
495
+ }
437
496
  ```
438
497
 
439
498
  The length of the embedding vector depends on the model, but you should see something like this:
@@ -444,25 +503,28 @@ data: length=1024, [0.036590576, -0.0059547424, ..., 0.011405945, 0.004863739]
444
503
  data: length=1024, [0.04196167, 0.029083252, ..., -0.0027484894, 0.0073127747]
445
504
  ```
446
505
 
447
- To generate embeddings for additional phrases, simply call `client.path("/embeddings").post` multiple times using the same `client`.
506
+ ### Instrumentation
448
507
 
449
- ### Instrumentation
450
508
  Currently instrumentation is only supported for `Chat Completion without streaming`.
451
509
  To enable instrumentation, it is required to register exporter(s).
452
510
 
453
511
  Here is an example to add console as a exporter:
454
- ```js
455
- import { ConsoleSpanExporter, NodeTracerProvider, SimpleSpanProcessor } from "@opentelemetry/sdk-trace-node";
512
+
513
+ ```ts snippet:ReadmeSample_Instrumentation
514
+ import {
515
+ NodeTracerProvider,
516
+ SimpleSpanProcessor,
517
+ ConsoleSpanExporter,
518
+ } from "@opentelemetry/sdk-trace-node";
456
519
 
457
520
  const provider = new NodeTracerProvider();
458
521
  provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
459
522
  provider.register();
460
-
461
523
  ```
462
524
 
463
525
  Here is an example to add application insight to be a exporter:
464
526
 
465
- ```js
527
+ ```ts snippet:ReadmeSample_InstrumentationAppInsights
466
528
  import { NodeTracerProvider, SimpleSpanProcessor } from "@opentelemetry/sdk-trace-node";
467
529
  import { AzureMonitorTraceExporter } from "@azure/monitor-opentelemetry-exporter";
468
530
 
@@ -479,40 +541,53 @@ provider.register();
479
541
 
480
542
  To use instrumentation for Azure SDK, you need to register it before importing any dependencies from `@azure/core-tracing`, such as `@azure-rest/ai-inference`.
481
543
 
482
- ```js
544
+ ```ts snippet:ReadmeSample_InstrumentationImport
483
545
  import { registerInstrumentations } from "@opentelemetry/instrumentation";
484
546
  import { createAzureSdkInstrumentation } from "@azure/opentelemetry-instrumentation-azure-sdk";
485
547
 
486
548
  registerInstrumentations({
487
549
  instrumentations: [createAzureSdkInstrumentation()],
488
550
  });
489
-
490
- import ModelClient from "@azure-rest/ai-inference";
491
551
  ```
492
552
 
493
- Finally when you are making a call for chat completion, you need to include
494
- ```js
495
- tracingOptions: { tracingContext: context.active() }
496
- ```
497
- Here is an example:
553
+ Finally when you are making a call for chat completion, you need to include the `tracingOptions` in the request. Here is an example:
498
554
 
499
- ```js
555
+ ```ts snippet:ReadmeSample_InstrumentationRequest
556
+ import { DefaultAzureCredential } from "@azure/identity";
557
+ import ModelClient from "@azure-rest/ai-inference";
500
558
  import { context } from "@opentelemetry/api";
559
+
560
+ const endpoint = "https://myaccount.openai.azure.com/";
561
+ const credential = new DefaultAzureCredential();
562
+ const client = ModelClient(endpoint, credential);
563
+
564
+ const messages = [
565
+ // NOTE: "system" role is not supported on all Azure Models
566
+ { role: "system", content: "You are a helpful assistant. You will talk like a pirate." },
567
+ { role: "user", content: "Can you help me?" },
568
+ { role: "assistant", content: "Arrrr! Of course, me hearty! What can I do for ye?" },
569
+ { role: "user", content: "What's the best way to train a parrot?" },
570
+ ];
571
+
501
572
  client.path("/chat/completions").post({
502
- body: {...},
503
- tracingOptions: { tracingContext: context.active() }
573
+ body: {
574
+ messages,
575
+ },
576
+ tracingOptions: { tracingContext: context.active() },
504
577
  });
505
578
  ```
506
579
 
507
580
  ### Tracing Your Own Functions
508
- Open Telemetry provides `startActiveSpan` to instrument you own code. Here is an example:
509
581
 
510
- ```js
582
+ Open Telemetry provides `startActiveSpan` to instrument you own code. Here is an example:
583
+
584
+ ```ts snippet:ReadmeSample_TracingOwnFunction
511
585
  import { trace } from "@opentelemetry/api";
586
+
512
587
  const tracer = trace.getTracer("sample", "0.1.0");
513
588
 
514
589
  const getWeatherFunc = (location: string, unit: string): string => {
515
- return tracer.startActiveSpan("getWeatherFunc", span => {
590
+ return tracer.startActiveSpan("getWeatherFunc", (span) => {
516
591
  if (unit !== "celsius") {
517
592
  unit = "fahrenheit";
518
593
  }
@@ -521,18 +596,17 @@ const getWeatherFunc = (location: string, unit: string): string => {
521
596
  span.end();
522
597
  return result;
523
598
  });
524
- }
599
+ };
525
600
  ```
526
601
 
527
-
528
602
  ## Troubleshooting
529
603
 
530
604
  ### Logging
531
605
 
532
606
  Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`. Alternatively, logging can be enabled at runtime by calling `setLogLevel` in the `@azure/logger`:
533
607
 
534
- ```javascript
535
- const { setLogLevel } = require("@azure/logger");
608
+ ```ts snippet:SetLogLevel
609
+ import { setLogLevel } from "@azure/logger";
536
610
 
537
611
  setLogLevel("info");
538
612
  ```
@@ -540,11 +614,12 @@ setLogLevel("info");
540
614
  For more detailed instructions on how to enable logs, you can look at the [@azure/logger package docs](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/core/logger).
541
615
 
542
616
  <!-- LINKS -->
617
+
543
618
  [stream_chat_completion_sample]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/streamChatCompletions.ts
544
619
  [azure_openai_completions_docs]: https://learn.microsoft.com/azure/cognitive-services/openai/how-to/completions
545
620
  [defaultazurecredential]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential
546
621
  [azure_identity]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity
547
622
  [azure_core_auth]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/core/core-auth
548
- [register_aad_app]: https://docs.microsoft.com/azure/cognitive-services/authentication#assign-a-role-to-a-service-principal
549
- [azure_cli]: https://docs.microsoft.com/cli/azure
623
+ [register_aad_app]: https://learn.microsoft.com/azure/cognitive-services/authentication#assign-a-role-to-a-service-principal
624
+ [azure_cli]: https://learn.microsoft.com/cli/azure
550
625
  [azure_portal]: https://portal.azure.com