@@ -32,9 +32,9 @@ We recommend using Agents with the latest models to take advantage of the new fe
32
32
33
33
## Usage support
34
34
35
- | Azure AI foundry support | Python SDK | C# SDK | Basic agent setup | Standard agent setup |
36
- | ---------| ---------| ---------| ---------| ---------|
37
- | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
35
+ | Azure AI foundry support | Python SDK | C# SDK | JavaScript SDK | Basic agent setup | Standard agent setup |
36
+ | ---------| ---------| ---------| ---------| ---------| --------- |
37
+ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
38
38
39
39
## Using the function calling tool with an agent
40
40
@@ -57,7 +57,7 @@ You can add the function calling tool to an agent programatically using the code
57
57
::: zone pivot="code-example"
58
58
59
59
60
- ## Define imports and create a project client
60
+ ## Create a project client
61
61
62
62
# [ Python] ( #tab/python )
63
63
@@ -89,6 +89,23 @@ var connectionString = Environment.GetEnvironmentVariable("PROJECT_CONNECTION_ST
89
89
AgentsClient client = new AgentsClient (connectionString , new DefaultAzureCredential ());
90
90
```
91
91
92
+ # [ JavaScript] ( #tab/javascript )
93
+
94
+ To use code interpreter, first you need to create a project client, which will contain a connection string to your AI project, and will be used to authenticate API calls.
95
+
96
+ ``` javascript
97
+ const connectionString =
98
+ process .env [" AZURE_AI_PROJECTS_CONNECTION_STRING" ] || " <project connection string>" ;
99
+
100
+ if (! connectionString) {
101
+ throw new Error (" AZURE_AI_PROJECTS_CONNECTION_STRING must be set." );
102
+ }
103
+ const client = AIProjectsClient .fromConnectionString (
104
+ connectionString || " " ,
105
+ new DefaultAzureCredential (),
106
+ );
107
+ ```
108
+
92
109
---
93
110
94
111
## Upload a file
@@ -129,6 +146,21 @@ VectorStore vectorStore = await client.CreateVectorStoreAsync(
129
146
CodeInterpreterToolResource codeInterpreterToolResource = new CodeInterpreterToolResource ();
130
147
CodeInterpreterToolResource .VectorStoreIds .Add (vectorStore .Id );
131
148
```
149
+
150
+ # [ JavaScript] ( #tab/javascript )
151
+
152
+ Files can be uploaded and then referenced by agents or messages. Once it's uploaded it can be added to the tool utility for referencing.
153
+
154
+ ``` javascript
155
+ const fileStream = fs .createReadStream (" nifty_500_quarterly_results.csv" );
156
+ const fFile = await client .agents .uploadFile (fileStream, " assistants" , {
157
+ fileName: " nifty_500_quarterly_results.csv" ,
158
+ });
159
+ console .log (` Uploaded local file, file ID : ${ file .id } ` );
160
+
161
+ const codeInterpreterTool = ToolUtility .createCodeInterpreterTool ([file .id ]);
162
+ ```
163
+
132
164
---
133
165
134
166
## Create an agent with the code interpreter tool
@@ -163,6 +195,18 @@ Response<Agent> agentResponse = await client.CreateAgentAsync(
163
195
Agent agent = agentResponse .Value ;
164
196
```
165
197
198
+ # [ JavaScript] ( #tab/javascript )
199
+
200
+ ``` javascript
201
+ // Notice that CodeInterpreter must be enabled in the agent creation, otherwise the agent will not be able to see the file attachment
202
+ const agent = await client .agents .createAgent (" gpt-4o-mini" , {
203
+ name: " my-agent" ,
204
+ instructions: " You are a helpful agent" ,
205
+ tools: [codeInterpreterTool .definition ],
206
+ toolResources: codeInterpreterTool .resources ,
207
+ });
208
+ console .log (` Created agent, agent ID: ${ agent .id } ` );
209
+ ```
166
210
---
167
211
168
212
## Create a thread, message, and get the agent response
@@ -262,6 +306,63 @@ foreach (ThreadMessage threadMessage in messages)
262
306
}
263
307
}
264
308
```
309
+
310
+ # [ JavaScript] ( #tab/javascript )
311
+
312
+ ``` javascript
313
+ // create a thread
314
+ const thread = await client .agents .createThread ();
315
+
316
+ // add a message to thread
317
+ await client .agents .createMessage (
318
+ thread .id , {
319
+ role: " user" ,
320
+ content: " I need to solve the equation `3x + 11 = 14`. Can you help me?" ,
321
+ });
322
+ // create a run
323
+ const streamEventMessages = await client .agents .createRun (thread .id , agent .id ).stream ();
324
+
325
+ for await (const eventMessage of streamEventMessages ) {
326
+ switch (eventMessage .event ) {
327
+ case RunStreamEvent .ThreadRunCreated :
328
+ break ;
329
+ case MessageStreamEvent .ThreadMessageDelta :
330
+ {
331
+ const messageDelta = eventMessage .data ;
332
+ messageDelta .delta .content .forEach ((contentPart ) => {
333
+ if (contentPart .type === " text" ) {
334
+ const textContent = contentPart;
335
+ const textValue = textContent .text ? .value || " No text" ;
336
+ }
337
+ });
338
+ }
339
+ break ;
340
+
341
+ case RunStreamEvent .ThreadRunCompleted :
342
+ break ;
343
+ case ErrorEvent .Error :
344
+ console .log (` An error occurred. Data ${ eventMessage .data } ` );
345
+ break ;
346
+ case DoneEvent .Done :
347
+ break ;
348
+ }
349
+ }
350
+
351
+ // Print the messages from the agent
352
+ const messages = await client .agents .listMessages (thread .id );
353
+
354
+ // Messages iterate from oldest to newest
355
+ // messages[0] is the most recent
356
+ for (let i = messages .data .length - 1 ; i >= 0 ; i-- ) {
357
+ const m = messages .data [i];
358
+ if (isOutputOfType< MessageTextContentOutput> (m .content [0 ], " text" )) {
359
+ const textContent = m .content [0 ];
360
+ console .log (` ${ textContent .text .value } ` );
361
+ console .log (` ---------------------------------` );
362
+ }
363
+ }
364
+ ` ` `
365
+
265
366
---
266
367
267
368
## Download files generated by code interpreter
@@ -297,6 +398,29 @@ foreach (MessageContent contentItem in message.Content)
297
398
}
298
399
}
299
400
` ` `
401
+ # [JavaScript](#tab/javascript)
402
+
403
+ 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 ` getFileContent` .
404
+
405
+ ` ` ` javascript
406
+ const messages = await client .agents .listMessages (thread .id );
407
+ const imageFile = (messages .data [0 ].content [0 ] as MessageImageFileContentOutput).imageFile ;
408
+ const imageFileName = (await client .agents .getFile (imageFile .fileId )).filename ;
409
+
410
+ const fileContent = await (await client .agents .getFileContent (imageFile .fileId ).asNodeStream ()).body ;
411
+ if (fileContent) {
412
+ const chunks: Buffer [] = [];
413
+ for await (const chunk of fileContent ) {
414
+ chunks .push (Buffer .from (chunk));
415
+ }
416
+ const buffer = Buffer .concat (chunks);
417
+ fs .writeFileSync (imageFileName, buffer);
418
+ } else {
419
+ console .error (" Failed to retrieve file content: fileContent is undefined" );
420
+ }
421
+ console .log (` Saved image file to: ${ imageFileName} ` );
422
+ ` ` `
423
+
300
424
301
425
---
302
426
0 commit comments