@@ -42,107 +42,6 @@ Use this article to find step-by-step instructions and code samples for using Op
42
42
43
43
::: zone-end
44
44
45
- :::zone-pivot="javascript"
46
-
47
-
48
- ## Create a project client
49
-
50
- Create a client object that contains the connection string for connecting to your AI project and other resources.
51
-
52
- ``` javascript
53
- const { AgentsClient , isOutputOfType , ToolUtility } = require (" @azure/ai-agents" );
54
- const { delay } = require (" @azure/core-util" );
55
- const { DefaultAzureCredential } = require (" @azure/identity" );
56
- const fs = require (" fs" );
57
- require (" dotenv/config" );
58
-
59
- const projectEndpoint = process .env [" PROJECT_ENDPOINT" ] || " <project connection string>" ;
60
-
61
- // Create an Azure AI Client
62
- const client = new AgentsClient (projectEndpoint, new DefaultAzureCredential ());
63
- ```
64
-
65
- ## Read in the OpenAPI spec
66
-
67
- ``` javascript
68
- // Read in OpenApi spec
69
- const filePath = " ./data/weatherOpenApi.json" ;
70
- const openApiSpec = JSON .parse (fs .readFileSync (filePath, " utf-8" ));
71
-
72
- // Define OpenApi function
73
- const openApiFunction = {
74
- name: " getWeather" ,
75
- spec: openApiSpec,
76
- description: " Retrieve weather information for a location" ,
77
- auth: {
78
- type: " anonymous" ,
79
- },
80
- default_params: [" format" ], // optional
81
- };
82
- ```
83
-
84
- ## Create an agent and enable the OpenAPI tool
85
-
86
- ``` javascript
87
- // Create OpenApi tool
88
- const openApiTool = ToolUtility .createOpenApiTool (openApiFunction);
89
-
90
- // Create agent with OpenApi tool
91
- const agent = await client .createAgent (modelDeploymentName, {
92
- name: " myAgent" ,
93
- instructions: " You are a helpful agent" ,
94
- tools: [openApiTool .definition ],
95
- });
96
- console .log (` Created agent, agent ID: ${ agent .id } ` );
97
- ```
98
-
99
- ## Create a thread
100
-
101
- ``` javascript
102
- // Create a thread
103
- const thread = await client .threads .create ();
104
- console .log (` Created thread, thread ID: ${ thread .id } ` );
105
-
106
- // Create a message
107
- const message = await client .messages .create (thread .id , " user" , " What's the weather in Seattle?" );
108
- console .log (` Created message, message ID: ${ message .id } ` );
109
- ```
110
-
111
- ## Create a run and check the output
112
-
113
- ``` javascript
114
- // Create and execute a run
115
- let run = await client .runs .create (thread .id , agent .id );
116
- while (run .status === " queued" || run .status === " in_progress" ) {
117
- await delay (1000 );
118
- run = await client .runs .get (thread .id , run .id );
119
- }
120
- if (run .status === " failed" ) {
121
- // Check if you got "Rate limit is exceeded.", then you want to get more quota
122
- console .log (` Run failed: ${ run .lastError } ` );
123
- }
124
- console .log (` Run finished with status: ${ run .status } ` );
125
-
126
- // Get most recent message from the assistant
127
- const messagesIterator = client .messages .list (thread .id );
128
- const messages = [];
129
- for await (const m of messagesIterator ) {
130
- messages .push (m);
131
- }
132
- const assistantMessage = messages .find ((msg ) => msg .role === " assistant" );
133
- if (assistantMessage) {
134
- const textContent = assistantMessage .content .find ((content ) => isOutputOfType (content, " text" ));
135
- if (textContent) {
136
- console .log (` Last message: ${ textContent .text .value } ` );
137
- }
138
- }
139
- // Delete the agent once done
140
- await client .deleteAgent (agent .id );
141
- console .log (` Deleted agent, agent ID: ${ agent .id } ` );
142
- ```
143
-
144
- ::: zone-end
145
-
146
45
:::zone pivot="python"
147
46
148
47
## Initialization
@@ -276,6 +175,108 @@ After the interaction is complete, the script performs cleanup by deleting the c
276
175
277
176
::: zone-end
278
177
178
+
179
+ :::zone-pivot="javascript"
180
+
181
+
182
+ ## Create a project client
183
+
184
+ Create a client object that contains the connection string for connecting to your AI project and other resources.
185
+
186
+ ``` javascript
187
+ const { AgentsClient , isOutputOfType , ToolUtility } = require (" @azure/ai-agents" );
188
+ const { delay } = require (" @azure/core-util" );
189
+ const { DefaultAzureCredential } = require (" @azure/identity" );
190
+ const fs = require (" fs" );
191
+ require (" dotenv/config" );
192
+
193
+ const projectEndpoint = process .env [" PROJECT_ENDPOINT" ] || " <project connection string>" ;
194
+
195
+ // Create an Azure AI Client
196
+ const client = new AgentsClient (projectEndpoint, new DefaultAzureCredential ());
197
+ ```
198
+
199
+ ## Read in the OpenAPI spec
200
+
201
+ ``` javascript
202
+ // Read in OpenApi spec
203
+ const filePath = " ./data/weatherOpenApi.json" ;
204
+ const openApiSpec = JSON .parse (fs .readFileSync (filePath, " utf-8" ));
205
+
206
+ // Define OpenApi function
207
+ const openApiFunction = {
208
+ name: " getWeather" ,
209
+ spec: openApiSpec,
210
+ description: " Retrieve weather information for a location" ,
211
+ auth: {
212
+ type: " anonymous" ,
213
+ },
214
+ default_params: [" format" ], // optional
215
+ };
216
+ ```
217
+
218
+ ## Create an agent and enable the OpenAPI tool
219
+
220
+ ``` javascript
221
+ // Create OpenApi tool
222
+ const openApiTool = ToolUtility .createOpenApiTool (openApiFunction);
223
+
224
+ // Create agent with OpenApi tool
225
+ const agent = await client .createAgent (modelDeploymentName, {
226
+ name: " myAgent" ,
227
+ instructions: " You are a helpful agent" ,
228
+ tools: [openApiTool .definition ],
229
+ });
230
+ console .log (` Created agent, agent ID: ${ agent .id } ` );
231
+ ```
232
+
233
+ ## Create a thread
234
+
235
+ ``` javascript
236
+ // Create a thread
237
+ const thread = await client .threads .create ();
238
+ console .log (` Created thread, thread ID: ${ thread .id } ` );
239
+
240
+ // Create a message
241
+ const message = await client .messages .create (thread .id , " user" , " What's the weather in Seattle?" );
242
+ console .log (` Created message, message ID: ${ message .id } ` );
243
+ ```
244
+
245
+ ## Create a run and check the output
246
+
247
+ ``` javascript
248
+ // Create and execute a run
249
+ let run = await client .runs .create (thread .id , agent .id );
250
+ while (run .status === " queued" || run .status === " in_progress" ) {
251
+ await delay (1000 );
252
+ run = await client .runs .get (thread .id , run .id );
253
+ }
254
+ if (run .status === " failed" ) {
255
+ // Check if you got "Rate limit is exceeded.", then you want to get more quota
256
+ console .log (` Run failed: ${ run .lastError } ` );
257
+ }
258
+ console .log (` Run finished with status: ${ run .status } ` );
259
+
260
+ // Get most recent message from the assistant
261
+ const messagesIterator = client .messages .list (thread .id );
262
+ const messages = [];
263
+ for await (const m of messagesIterator ) {
264
+ messages .push (m);
265
+ }
266
+ const assistantMessage = messages .find ((msg ) => msg .role === " assistant" );
267
+ if (assistantMessage) {
268
+ const textContent = assistantMessage .content .find ((content ) => isOutputOfType (content, " text" ));
269
+ if (textContent) {
270
+ console .log (` Last message: ${ textContent .text .value } ` );
271
+ }
272
+ }
273
+ // Delete the agent once done
274
+ await client .deleteAgent (agent .id );
275
+ console .log (` Deleted agent, agent ID: ${ agent .id } ` );
276
+ ```
277
+
278
+ ::: zone-end
279
+
279
280
:::zone pivot="csharp"
280
281
281
282
## Configure client and OpenAPI tool
0 commit comments