Skip to content

Commit 034380f

Browse files
committed
javascript samples
1 parent be95b41 commit 034380f

File tree

8 files changed

+617
-45
lines changed

8 files changed

+617
-45
lines changed

articles/ai-services/agents/how-to/tools/azure-ai-search.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ Use an existing Azure AI Search index with the agent's Azure AI Search tool.
3434

3535
## Usage support
3636

37-
|Azure AI foundry support | Python SDK | C# SDK | Basic agent setup | Standard agent setup |
38-
|---------|---------|---------|---------|---------|
39-
| ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
37+
|Azure AI foundry support | Python SDK | C# SDK | JavaScript SDK | Basic agent setup | Standard agent setup |
38+
|---------|---------|---------|---------|---------|---------|
39+
| ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
4040

4141
[!INCLUDE [setup](../../includes/azure-search/setup.md)]
4242

articles/ai-services/agents/how-to/tools/bing-grounding.md

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ Developers and end users don't have access to raw content returned from Groundin
3030
3131
## Usage support
3232

33-
|Azure AI foundry support | Python SDK | C# SDK | Basic agent setup | Standard agent setup |
34-
|---------|---------|---------|---------|---------|
35-
| ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
33+
|Azure AI foundry support | Python SDK | C# SDK | JavaScript SDK |Basic agent setup | Standard agent setup |
34+
|---------|---------|---------|---------|---------|---------|
35+
| ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
3636

3737
## Setup
3838

@@ -138,6 +138,21 @@ var projectClient = new AIProjectClient(connectionString, new DefaultAzureCreden
138138

139139
```
140140

141+
# [JavaScript](#tab/javascript)
142+
143+
```javascript
144+
const connectionString =
145+
process.env["AZURE_AI_PROJECTS_CONNECTION_STRING"] || "<project connection string>";
146+
147+
if (!connectionString) {
148+
throw new Error("AZURE_AI_PROJECTS_CONNECTION_STRING must be set.");
149+
}
150+
const client = AIProjectsClient.fromConnectionString(
151+
connectionString || "",
152+
new DefaultAzureCredential(),
153+
);
154+
```
155+
141156
---
142157

143158
## Step 2: Enable the Grounding with Bing search tool
@@ -190,6 +205,23 @@ Response<Agent> agentResponse = await agentClient.CreateAgentAsync(
190205
tools: new List<ToolDefinition> { bingGroundingTool });
191206
Agent agent = agentResponse.Value;
192207
```
208+
209+
# [JavaScript](#tab/javascript)
210+
211+
```javascript
212+
const bingGroundingConnectionId = "<bingGroundingConnectionId>";
213+
const bingTool = ToolUtility.createConnectionTool(connectionToolType.BingGrounding, [
214+
bingGroundingConnectionId,
215+
]);
216+
217+
const agent = await client.agents.createAgent("gpt-4o", {
218+
name: "my-agent",
219+
instructions: "You are a helpful agent",
220+
tools: [bingTool.definition],
221+
});
222+
console.log(`Created agent, agent ID : ${agent.id}`);
223+
```
224+
193225
---
194226

195227

@@ -226,6 +258,20 @@ Response<ThreadMessage> messageResponse = await agentClient.CreateMessageAsync(
226258
ThreadMessage message = messageResponse.Value;
227259
```
228260

261+
# [JavaScript](#tab/javascript)
262+
263+
```javascript
264+
// create a thread
265+
const thread = await client.agents.createThread();
266+
267+
// add a message to thread
268+
await client.agents.createMessage(
269+
thread.id, {
270+
role: "user",
271+
content: "What is the weather in Seattle?",
272+
});
273+
```
274+
229275
---
230276

231277
## Step 4: Create a run and check the output
@@ -293,6 +339,55 @@ foreach (ThreadMessage threadMessage in messages)
293339
}
294340
}
295341
```
342+
343+
# [JavaScript](#tab/javascript)
344+
345+
```javascript
346+
347+
// create a run
348+
const streamEventMessages = await client.agents.createRun(thread.id, agent.id).stream();
349+
350+
for await (const eventMessage of streamEventMessages) {
351+
switch (eventMessage.event) {
352+
case RunStreamEvent.ThreadRunCreated:
353+
break;
354+
case MessageStreamEvent.ThreadMessageDelta:
355+
{
356+
const messageDelta = eventMessage.data;
357+
messageDelta.delta.content.forEach((contentPart) => {
358+
if (contentPart.type === "text") {
359+
const textContent = contentPart;
360+
const textValue = textContent.text?.value || "No text";
361+
}
362+
});
363+
}
364+
break;
365+
366+
case RunStreamEvent.ThreadRunCompleted:
367+
break;
368+
case ErrorEvent.Error:
369+
console.log(`An error occurred. Data ${eventMessage.data}`);
370+
break;
371+
case DoneEvent.Done:
372+
break;
373+
}
374+
}
375+
376+
// Print the messages from the agent
377+
const messages = await client.agents.listMessages(thread.id);
378+
379+
// Messages iterate from oldest to newest
380+
// messages[0] is the most recent
381+
for (let i = messages.data.length - 1; i >= 0; i--) {
382+
const m = messages.data[i];
383+
if (isOutputOfType<MessageTextContentOutput>(m.content[0], "text")) {
384+
const textContent = m.content[0];
385+
console.log(`${textContent.text.value}`);
386+
console.log(`---------------------------------`);
387+
}
388+
}
389+
```
390+
296391
---
297392
298393
::: zone-end

articles/ai-services/agents/how-to/tools/code-interpreter.md

Lines changed: 128 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ We recommend using Agents with the latest models to take advantage of the new fe
3232

3333
## Usage support
3434

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+
| ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
3838

3939
## Using the function calling tool with an agent
4040

@@ -57,7 +57,7 @@ You can add the function calling tool to an agent programatically using the code
5757
::: zone pivot="code-example"
5858

5959

60-
## Define imports and create a project client
60+
## Create a project client
6161

6262
# [Python](#tab/python)
6363

@@ -89,6 +89,23 @@ var connectionString = Environment.GetEnvironmentVariable("PROJECT_CONNECTION_ST
8989
AgentsClient client = new AgentsClient(connectionString, new DefaultAzureCredential());
9090
```
9191

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+
92109
---
93110

94111
## Upload a file
@@ -129,6 +146,21 @@ VectorStore vectorStore = await client.CreateVectorStoreAsync(
129146
CodeInterpreterToolResource codeInterpreterToolResource = new CodeInterpreterToolResource();
130147
CodeInterpreterToolResource.VectorStoreIds.Add(vectorStore.Id);
131148
```
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+
132164
---
133165

134166
## Create an agent with the code interpreter tool
@@ -163,6 +195,18 @@ Response<Agent> agentResponse = await client.CreateAgentAsync(
163195
Agent agent = agentResponse.Value;
164196
```
165197

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+
```
166210
---
167211

168212
## Create a thread, message, and get the agent response
@@ -262,6 +306,63 @@ foreach (ThreadMessage threadMessage in messages)
262306
}
263307
}
264308
```
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+
265366
---
266367
267368
## Download files generated by code interpreter
@@ -297,6 +398,29 @@ foreach (MessageContent contentItem in message.Content)
297398
}
298399
}
299400
```
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+
300424
301425
---
302426

articles/ai-services/agents/how-to/tools/file-search.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ File search augments agents with knowledge from outside its model, such as propr
3333

3434
### Usage support
3535

36-
|Azure AI foundry support | Python SDK | C# SDK | Basic agent setup | Standard agent setup |
36+
|Azure AI foundry support | Python SDK | C# SDK | JavaScript SDK | Basic agent setup | Standard agent setup |
3737
|---------|---------|---------|---------|---------|
38-
| ✔️ | ✔️ | ✔️ | File upload only | File upload and using blob storage |
38+
| ✔️ | ✔️ | ✔️ | ✔️ | File upload only | File upload and using blob storage |
3939

4040
## Dependency on agent setup
4141

0 commit comments

Comments
 (0)