@azure/ai-agents 1.0.0-alpha.20250509.4 → 1.0.0-alpha.20250515.1
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 +63 -115
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
# Azure AI Agents client library for JavaScript
|
|
2
2
|
|
|
3
|
-
Use the AI Agents client library
|
|
3
|
+
Use the AI Agents client library to:
|
|
4
4
|
|
|
5
|
-
- **
|
|
6
|
-
For example, get the inference endpoint URL and credentials associated with your Azure OpenAI connection.
|
|
7
|
-
- **Develop Agents using the Azure AI Agent Service**, leveraging an extensive ecosystem of models, tools, and capabilities from OpenAI, Microsoft, and other LLM providers. The Azure AI Agent Service enables the building of Agents for a wide range of generative AI use cases. The package is currently in private preview.
|
|
8
|
-
- **Enable OpenTelemetry tracing**.
|
|
5
|
+
- **Develop Agents using the Azure AI Agent Service**, leveraging an extensive ecosystem of models, tools, and capabilities from OpenAI, Microsoft, and other LLM providers. The Azure AI Agent Service enables the building of Agents for a wide range of generative AI use cases.
|
|
9
6
|
|
|
10
7
|
[Product documentation](https://aka.ms/azsdk/azure-ai-projects/product-doc)
|
|
11
|
-
|
|
12
|
-
| [Samples](https://github.com/Azure/azure-sdk-for-js/tree/feature/azure-ai-agents/sdk/ai/ai-agents/samples)
|
|
8
|
+
| [Samples](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/ai/ai-agents/samples/)
|
|
13
9
|
| [Package (npm)](https://www.npmjs.com/package/@azure/ai-agents)
|
|
14
10
|
| [API reference documentation](https://learn.microsoft.com/javascript/api/overview/azure/ai-projects-readme?view=azure-node-preview)
|
|
15
11
|
|
|
@@ -17,16 +13,12 @@ Use the AI Agents client library (in preview) to:
|
|
|
17
13
|
|
|
18
14
|
- [Getting started](#getting-started)
|
|
19
15
|
- [Prerequisite](#prerequisite)
|
|
16
|
+
- [Authorization](#authorization)
|
|
20
17
|
- [Install the package](#install-the-package)
|
|
21
18
|
- [Key concepts](#key-concepts)
|
|
22
19
|
- [Create and authenticate the client](#create-and-authenticate-the-client)
|
|
23
20
|
- [Examples](#examples)
|
|
24
|
-
- [
|
|
25
|
-
- [Get properties of all connections](#get-properties-of-all-connections)
|
|
26
|
-
- [Get properties of all connections of a particular type](#get-properties-of-all-connections-of-a-particular-type)
|
|
27
|
-
- [Get properties of a default connection](#get-properties-of-a-default-connection)
|
|
28
|
-
- [Get properties of a connection by its connection name](#get-properties-of-a-connection-by-its-connection-name)
|
|
29
|
-
- [Agents (Preview)](#agents-private-preview)
|
|
21
|
+
- [Agents](#agents)
|
|
30
22
|
- [Create an Agent](#create-agent) with:
|
|
31
23
|
- [File Search](#create-agent-with-file-search)
|
|
32
24
|
- [Code interpreter](#create-agent-with-code-interpreter)
|
|
@@ -39,14 +31,11 @@ Use the AI Agents client library (in preview) to:
|
|
|
39
31
|
- [Create message](#create-message) with:
|
|
40
32
|
- [File search attachment](#create-message-with-file-search-attachment)
|
|
41
33
|
- [Code interpreter attachment](#create-message-with-code-interpreter-attachment)
|
|
34
|
+
- [Image input](#create-message-with-image-inputs)
|
|
42
35
|
- [Execute Run, Create Thread and Run, or Stream](#create-run-run_and_process-or-stream)
|
|
43
36
|
- [Retrieve message](#retrieve-message)
|
|
44
37
|
- [Retrieve file](#retrieve-file)
|
|
45
38
|
- [Tear down by deleting resource](#teardown)
|
|
46
|
-
- [Tracing](#tracing)
|
|
47
|
-
- [Tracing](#tracing)
|
|
48
|
-
- [Installation](#installation)
|
|
49
|
-
- [Tracing example](#tracing-example)
|
|
50
39
|
- [Troubleshooting](#troubleshooting)
|
|
51
40
|
- [Exceptions](#exceptions)
|
|
52
41
|
- [Reporting issues](#reporting-issues)
|
|
@@ -60,9 +49,11 @@ Use the AI Agents client library (in preview) to:
|
|
|
60
49
|
- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
|
|
61
50
|
- An [Azure subscription][azure_sub].
|
|
62
51
|
- A [project in Azure AI Foundry](https://learn.microsoft.com/azure/ai-studio/how-to/create-projects?tabs=ai-studio).
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
52
|
+
|
|
53
|
+
### Authorization
|
|
54
|
+
|
|
55
|
+
- [Entra ID][entra_id] is needed to authenticate the client. Your application needs an object that implements the [TokenCredential](https://learn.microsoft.com/javascript/api/@azure/core-auth/tokencredential) interface. Code samples here use [DefaultAzureCredential](https://learn.microsoft.com/javascript/api/@azure/identity/defaultazurecredential?view=azure-node-latest). To get that working, you will need:
|
|
56
|
+
- The `Contributor` role. Role assigned can be done via the "Access Control (IAM)" tab of your Azure AI Project resource in the Azure portal. Learn more about role assignments [here](https://learn.microsoft.com/azure/role-based-access-control/role-assignments-portal).
|
|
66
57
|
- [Azure CLI](https://learn.microsoft.com/cli/azure/install-azure-cli) installed.
|
|
67
58
|
- You are logged into your Azure account by running `az login`.
|
|
68
59
|
- Note that if you have multiple Azure subscriptions, the subscription that contains your Azure AI Project resource must be your default subscription. Run `az account list --output table` to list all your subscription and see which one is the default. Run `az account set --subscription "Your Subscription ID or Name"` to change your default subscription.
|
|
@@ -77,7 +68,9 @@ npm install @azure/ai-agents @azure/identity
|
|
|
77
68
|
|
|
78
69
|
### Create and authenticate the client
|
|
79
70
|
|
|
80
|
-
The
|
|
71
|
+
The `AgentsClient` is used to construct the client. Currently, we recommend that you use the AgentsClient through the [Azure AI Projects Client Library](https://www.npmjs.com/package/@azure/ai-projects) using `client.agents`.
|
|
72
|
+
|
|
73
|
+
To construct a client:
|
|
81
74
|
|
|
82
75
|
```ts snippet:setup
|
|
83
76
|
import { AgentsClient } from "@azure/ai-agents";
|
|
@@ -90,11 +83,9 @@ const client = new AgentsClient(projectEndpoint, new DefaultAzureCredential());
|
|
|
90
83
|
|
|
91
84
|
## Examples
|
|
92
85
|
|
|
93
|
-
### Agents
|
|
86
|
+
### Agents
|
|
94
87
|
|
|
95
|
-
Agents in the Azure AI Projects client library are designed to facilitate various interactions and operations within your AI projects. They serve as the core components that manage and execute tasks, leveraging different tools and resources to achieve specific goals. The following steps outline the typical sequence for interacting with Agents. See the
|
|
96
|
-
|
|
97
|
-
Agents are actively being developed. A sign-up form for private preview is coming soon.
|
|
88
|
+
Agents in the Azure AI Projects client library are designed to facilitate various interactions and operations within your AI projects. They serve as the core components that manage and execute tasks, leveraging different tools and resources to achieve specific goals. The following steps outline the typical sequence for interacting with Agents. See the [package samples](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/ai/ai-agents/samples/) for additional Agent samples.
|
|
98
89
|
|
|
99
90
|
#### Create Agent
|
|
100
91
|
|
|
@@ -114,19 +105,30 @@ You can use `ToolSet` to do this:
|
|
|
114
105
|
```ts snippet:toolSet
|
|
115
106
|
import { ToolSet } from "@azure/ai-agents";
|
|
116
107
|
|
|
108
|
+
// Upload file for code interpreter tool
|
|
109
|
+
const filePath1 = "./data/nifty500QuarterlyResults.csv";
|
|
110
|
+
const fileStream1 = fs.createReadStream(filePath1);
|
|
111
|
+
const codeInterpreterFile = await client.files.upload(fileStream1, "assistants", {
|
|
112
|
+
fileName: "myLocalFile",
|
|
113
|
+
});
|
|
114
|
+
console.log(`Uploaded local file, file ID : ${codeInterpreterFile.id}`);
|
|
115
|
+
// Upload file for file search tool
|
|
116
|
+
const filePath2 = "./data/sampleFileForUpload.txt";
|
|
117
|
+
const fileStream2 = fs.createReadStream(filePath2);
|
|
118
|
+
const fileSearchFile = await client.files.upload(fileStream2, "assistants", {
|
|
119
|
+
fileName: "sampleFileForUpload.txt",
|
|
120
|
+
});
|
|
121
|
+
console.log(`Uploaded file, file ID: ${fileSearchFile.id}`);
|
|
122
|
+
// Create vector store for file search tool
|
|
123
|
+
const vectorStore = await client.vectorStores
|
|
124
|
+
.createAndPoll({
|
|
125
|
+
fileIds: [fileSearchFile.id],
|
|
126
|
+
})
|
|
127
|
+
.pollUntilDone();
|
|
117
128
|
// Create tool set
|
|
118
129
|
const toolSet = new ToolSet();
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
// Create agent with tool set
|
|
123
|
-
const agent = await client.createAgent("gpt-4o", {
|
|
124
|
-
name: "my-agent",
|
|
125
|
-
instructions: "You are a helpful agent",
|
|
126
|
-
tools: toolSet.toolDefinitions,
|
|
127
|
-
toolResources: toolSet.toolResources,
|
|
128
|
-
});
|
|
129
|
-
console.log(`Created agent, agent ID: ${agent.id}`);
|
|
130
|
+
toolSet.addFileSearchTool([vectorStore.id]);
|
|
131
|
+
toolSet.addCodeInterpreterTool([codeInterpreterFile.id]);
|
|
130
132
|
```
|
|
131
133
|
|
|
132
134
|
#### Create Agent with File Search
|
|
@@ -152,7 +154,7 @@ console.log(`Created vector store, vector store ID: ${vectorStore.id}`);
|
|
|
152
154
|
const fileSearchTool = ToolUtility.createFileSearchTool([vectorStore.id]);
|
|
153
155
|
|
|
154
156
|
const agent = await client.createAgent("gpt-4o", {
|
|
155
|
-
name: "
|
|
157
|
+
name: "File Search Agent",
|
|
156
158
|
instructions: "You are helpful agent that can help fetch data from files you know about.",
|
|
157
159
|
tools: [fileSearchTool.definition],
|
|
158
160
|
toolResources: fileSearchTool.resources,
|
|
@@ -189,7 +191,7 @@ console.log(`Created agent, agent ID: ${agent.id}`);
|
|
|
189
191
|
|
|
190
192
|
#### Create Agent with Bing Grounding
|
|
191
193
|
|
|
192
|
-
To enable your Agent to perform search through Bing search API, you use `ToolUtility.
|
|
194
|
+
To enable your Agent to perform search through Bing search API, you use `ToolUtility.createBingGroundingTool()` along with a connection. See [here](https://learn.microsoft.com/azure/ai-services/agents/how-to/tools/bing-grounding?tabs=python&pivots=overview) to learn more about Grounding with Bing Search.
|
|
193
195
|
|
|
194
196
|
Here is an example:
|
|
195
197
|
|
|
@@ -219,15 +221,15 @@ Here is an example to integrate Azure AI Search:
|
|
|
219
221
|
```ts snippet:AISearch
|
|
220
222
|
import { ToolUtility } from "@azure/ai-agents";
|
|
221
223
|
|
|
222
|
-
const
|
|
224
|
+
const connectionName = process.env["AZURE_AI_SEARCH_CONNECTION_NAME"] || "<connection-name>";
|
|
223
225
|
|
|
224
226
|
// Initialize Azure AI Search tool
|
|
225
|
-
const azureAISearchTool = ToolUtility.createAzureAISearchTool(
|
|
227
|
+
const azureAISearchTool = ToolUtility.createAzureAISearchTool(connectionName, "search-index", {
|
|
226
228
|
queryType: "simple",
|
|
227
229
|
topK: 3,
|
|
228
|
-
filter: "",
|
|
229
|
-
indexConnectionId:
|
|
230
|
-
indexName: "",
|
|
230
|
+
filter: "", // Add string here to filter results
|
|
231
|
+
indexConnectionId: connectionName,
|
|
232
|
+
indexName: "search-index",
|
|
231
233
|
});
|
|
232
234
|
|
|
233
235
|
// Create agent with the Azure AI search tool
|
|
@@ -314,7 +316,7 @@ class FunctionToolExecutor {
|
|
|
314
316
|
|
|
315
317
|
public invokeTool(toolCall: RequiredToolCall & FunctionToolDefinition): ToolOutput | undefined {
|
|
316
318
|
console.log(`Function tool call - ${toolCall.function.name}`);
|
|
317
|
-
const args = [];
|
|
319
|
+
const args: any[] = [];
|
|
318
320
|
if (toolCall.function.parameters) {
|
|
319
321
|
try {
|
|
320
322
|
const params = JSON.parse(toolCall.function.parameters);
|
|
@@ -329,14 +331,21 @@ class FunctionToolExecutor {
|
|
|
329
331
|
}
|
|
330
332
|
}
|
|
331
333
|
const result = this.functionTools
|
|
332
|
-
.
|
|
333
|
-
|
|
334
|
+
.map((tool) =>
|
|
335
|
+
tool.definition.function.name === toolCall.function.name ? tool.func(...args) : undefined,
|
|
336
|
+
)
|
|
337
|
+
.find((r) => r !== undefined);
|
|
334
338
|
return result
|
|
335
339
|
? {
|
|
336
340
|
toolCallId: toolCall.id,
|
|
337
341
|
output: JSON.stringify(result),
|
|
338
342
|
}
|
|
339
|
-
:
|
|
343
|
+
: {
|
|
344
|
+
toolCallId: toolCall.id,
|
|
345
|
+
output: JSON.stringify({
|
|
346
|
+
error: `No matching tool found for function: ${toolCall.function.name}`,
|
|
347
|
+
}),
|
|
348
|
+
};
|
|
340
349
|
}
|
|
341
350
|
|
|
342
351
|
public getFunctionDefinitions(): FunctionToolDefinition[] {
|
|
@@ -425,7 +434,7 @@ console.log(`Created thread, thread ID: ${thread.id}`);
|
|
|
425
434
|
|
|
426
435
|
#### Create Thread with Tool Resource
|
|
427
436
|
|
|
428
|
-
In some scenarios, you might need to assign specific resources to individual threads. To achieve this, you provide the `toolResources` argument to `
|
|
437
|
+
In some scenarios, you might need to assign specific resources to individual threads. To achieve this, you provide the `toolResources` argument to `threads.create`. In the following example, you create a vector store and upload a file, enable an Agent for file search using the `tools` argument, and then associate the file with the thread using the `toolResources` argument.
|
|
429
438
|
|
|
430
439
|
```ts snippet:threadWithTool
|
|
431
440
|
import { ToolUtility } from "@azure/ai-agents";
|
|
@@ -458,7 +467,7 @@ const thread = await client.threads.create({ toolResources: fileSearchTool.resou
|
|
|
458
467
|
|
|
459
468
|
#### List Threads
|
|
460
469
|
|
|
461
|
-
To list all threads attached to a given agent, use
|
|
470
|
+
To list all threads attached to a given agent, use `threads.list`:
|
|
462
471
|
|
|
463
472
|
```ts snippet:listThreads
|
|
464
473
|
const threads = client.threads.list();
|
|
@@ -549,65 +558,7 @@ You can send messages to Azure agents with image inputs in following ways:
|
|
|
549
558
|
- **Using a public image accessible via URL**
|
|
550
559
|
- **Using a base64 encoded image string**
|
|
551
560
|
|
|
552
|
-
The following
|
|
553
|
-
|
|
554
|
-
##### Create message using uploaded image file
|
|
555
|
-
|
|
556
|
-
```ts snippet:imageInputWithFile
|
|
557
|
-
const imagePath = "./data/image_file.png";
|
|
558
|
-
// Upload the local image file
|
|
559
|
-
const fileStream = fs.createReadStream(imagePath);
|
|
560
|
-
const imageFile = await client.files.upload(fileStream, "assistants", {
|
|
561
|
-
fileName: "image_file.png",
|
|
562
|
-
});
|
|
563
|
-
console.log(`Uploaded file, file ID: ${imageFile.id}`);
|
|
564
|
-
|
|
565
|
-
// Create a message with both text and image content
|
|
566
|
-
console.log("Creating message with image content...");
|
|
567
|
-
const inputMessage = "Hello, what is in the image?";
|
|
568
|
-
const content = [
|
|
569
|
-
{
|
|
570
|
-
type: "text",
|
|
571
|
-
text: inputMessage,
|
|
572
|
-
},
|
|
573
|
-
{
|
|
574
|
-
type: "image_file",
|
|
575
|
-
image_file: {
|
|
576
|
-
file_id: imageFile.id,
|
|
577
|
-
detail: "high",
|
|
578
|
-
},
|
|
579
|
-
},
|
|
580
|
-
];
|
|
581
|
-
const message = await client.messages.create(thread.id, "user", content);
|
|
582
|
-
console.log(`Created message, message ID: ${message.id}`);
|
|
583
|
-
```
|
|
584
|
-
|
|
585
|
-
##### Create message with an image URL input
|
|
586
|
-
|
|
587
|
-
```ts snippet:imageInputWithUrl
|
|
588
|
-
const imageUrl =
|
|
589
|
-
"https://github.com/Azure/azure-sdk-for-js/blob/0aa88ceb18d865726d423f73b8393134e783aea6/sdk/ai/ai-projects/data/image_file.png?raw=true";
|
|
590
|
-
|
|
591
|
-
// Create a message with both text and image content
|
|
592
|
-
console.log("Creating message with image content...");
|
|
593
|
-
const inputMessage = "Hello, what is in the image?";
|
|
594
|
-
const content = [
|
|
595
|
-
{
|
|
596
|
-
type: "text",
|
|
597
|
-
text: inputMessage,
|
|
598
|
-
},
|
|
599
|
-
{
|
|
600
|
-
type: "image_url",
|
|
601
|
-
image_url: {
|
|
602
|
-
url: imageUrl,
|
|
603
|
-
detail: "high",
|
|
604
|
-
},
|
|
605
|
-
},
|
|
606
|
-
];
|
|
607
|
-
|
|
608
|
-
const message = await client.messages.create(thread.id, "user", content);
|
|
609
|
-
console.log(`Created message, message ID: ${message.id}`);
|
|
610
|
-
```
|
|
561
|
+
The following example demonstrates base64 method:
|
|
611
562
|
|
|
612
563
|
##### Create message with base64-encoded image input
|
|
613
564
|
|
|
@@ -653,7 +604,7 @@ console.log(`Created message, message ID: ${message.id}`);
|
|
|
653
604
|
|
|
654
605
|
#### Create Run, Run_and_Process, or Stream
|
|
655
606
|
|
|
656
|
-
Here is an example of `
|
|
607
|
+
Here is an example of `runs.create` and poll until the run is completed:
|
|
657
608
|
|
|
658
609
|
```ts snippet:createRun
|
|
659
610
|
import { delay } from "@azure/core-util";
|
|
@@ -768,7 +719,7 @@ for await (const dataPoint of messages) {
|
|
|
768
719
|
|
|
769
720
|
### Retrieve File
|
|
770
721
|
|
|
771
|
-
Files uploaded by Agents cannot be retrieved back. If your use case needs to access the file content uploaded by the Agents, you are advised to keep an additional copy accessible by your application. However, files generated by Agents are retrievable by `
|
|
722
|
+
Files uploaded by Agents cannot be retrieved back. If your use case needs to access the file content uploaded by the Agents, you are advised to keep an additional copy accessible by your application. However, files generated by Agents are retrievable by `files.getContent`.
|
|
772
723
|
|
|
773
724
|
Here is an example retrieving file ids from messages:
|
|
774
725
|
|
|
@@ -835,7 +786,7 @@ Client methods that make service calls raise an [RestError](https://learn.micros
|
|
|
835
786
|
import { RestError } from "@azure/core-rest-pipeline";
|
|
836
787
|
|
|
837
788
|
try {
|
|
838
|
-
const
|
|
789
|
+
const thread = await client.threads.create();
|
|
839
790
|
} catch (e) {
|
|
840
791
|
if (e instanceof RestError) {
|
|
841
792
|
console.log(`Status code: ${e.code}`);
|
|
@@ -859,7 +810,7 @@ To report issues with the client library, or request additional features, please
|
|
|
859
810
|
|
|
860
811
|
## Next steps
|
|
861
812
|
|
|
862
|
-
Have a look at the [package samples](https://github.com/Azure/azure-sdk-for-js/tree/
|
|
813
|
+
Have a look at the [package samples](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/ai/ai-agents/samples) folder, containing fully runnable code.
|
|
863
814
|
|
|
864
815
|
## Contributing
|
|
865
816
|
|
|
@@ -883,7 +834,4 @@ additional questions or comments.
|
|
|
883
834
|
[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/
|
|
884
835
|
[entra_id]: https://learn.microsoft.com/azure/ai-services/authentication?tabs=powershell#authenticate-with-microsoft-entra-id
|
|
885
836
|
[azure_identity_npm]: https://www.npmjs.com/package/@azure/identity
|
|
886
|
-
[default_azure_credential]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential
|
|
887
837
|
[azure_sub]: https://azure.microsoft.com/free/
|
|
888
|
-
[evaluators]: https://learn.microsoft.com/azure/ai-studio/how-to/develop/evaluate-sdk
|
|
889
|
-
[evaluator_library]: https://learn.microsoft.com/azure/ai-studio/how-to/evaluate-generative-ai-app#view-and-manage-the-evaluators-in-the-evaluator-library
|
package/package.json
CHANGED