Skip to content

Commit 58ff2d6

Browse files
authored
Merge pull request #3636 from diberry/diberry/0320-fix-agents-js
Agent quickstarts - JS - fix toolResources
2 parents f68864b + 76187af commit 58ff2d6

File tree

3 files changed

+74
-50
lines changed

3 files changed

+74
-50
lines changed

articles/ai-services/agents/includes/quickstart-javascript.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: aahill
44
ms.author: aahi
55
ms.service: azure-ai-agent-service
66
ms.topic: include
7-
ms.date: 02/03/2025
7+
ms.date: 03/21/2025
88
ms.custom: devx-track-js
99
---
1010

@@ -37,6 +37,12 @@ ms.custom: devx-track-js
3737
| Run | Activation of an agent to begin running based on the contents of Thread. The agent uses its configuration and Thread’s Messages to perform tasks by calling models and tools. As part of a Run, the agent appends Messages to the Thread. |
3838
| Run Step | A detailed list of steps the agent took as part of a Run. An agent can call tools or create Messages during its run. Examining Run Steps allows you to understand how the agent is getting to its results. |
3939

40+
Key objects in this code include:
41+
42+
* [AIProjectsClient](/javascript/api/@azure/ai-projects/aiprojectsclient)
43+
* [ToolUtility](/javascript/api/@azure/ai-projects/toolutility)
44+
* [Agent operations](/javascript/api/@azure/ai-projects/agentsoperations)
45+
4046
First, initialize a new project by running:
4147

4248
```console
@@ -84,7 +90,6 @@ Next, create an `index.js` file and paste in the code below:
8490

8591
```javascript
8692
// index.js
87-
8893
import {
8994
AIProjectsClient,
9095
DoneEvent,
@@ -101,6 +106,7 @@ dotenv.config();
101106

102107
// Set the connection string from the environment variable
103108
const connectionString = process.env.PROJECT_CONNECTION_STRING;
109+
const model = "gpt-4o";
104110

105111
// Throw an error if the connection string is not set
106112
if (!connectionString) {
@@ -114,10 +120,10 @@ export async function main() {
114120
);
115121

116122
// Step 1 code interpreter tool
117-
const codeInterpreterTool = ToolUtility.createCodeInterpreterTool();
123+
const codeInterpreterTool = ToolUtility.createCodeInterpreterTool([]);
118124

119125
// Step 2 an agent
120-
const agent = await client.agents.createAgent("gpt-4o-mini", {
126+
const agent = await client.agents.createAgent(model, {
121127
name: "my-agent",
122128
instructions: "You are a helpful agent",
123129
tools: [codeInterpreterTool.definition],
@@ -171,13 +177,14 @@ export async function main() {
171177

172178
// Messages iterate from oldest to newest
173179
// messages[0] is the most recent
174-
for (let i = messages.data.length - 1; i >= 0; i--) {
175-
const m = messages.data[i];
176-
if (m.content && m.content.length > 0 && isOutputOfType(m.content[0], "text")) {
177-
const textContent = m.content[0];
178-
console.log(`${textContent.text.value}`);
179-
console.log(`---------------------------------`);
180-
}
180+
const messagesArray = messages.data;
181+
for (let i = messagesArray.length - 1; i >= 0; i--) {
182+
const m = messagesArray[i];
183+
console.log(`Type: ${m.content[0].type}`);
184+
if (isOutputOfType(m.content[0], "text")) {
185+
const textContent = m.content[0];
186+
console.log(`Text: ${textContent.text.value}`);
187+
}
181188
}
182189

183190
// 7. Delete the agent once done
@@ -189,4 +196,4 @@ main().catch((err) => {
189196
});
190197
```
191198
192-
Run the code using `node index.js` and observe.
199+
Run the code using `node index.js` and observe.

articles/ai-services/agents/includes/quickstart-output.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@ author: aahill
44
ms.author: aahi
55
ms.service: azure-ai-agent-service
66
ms.topic: include
7-
ms.date: 01/28/2025
7+
ms.date: 03/21/2025
88
ms.custom: devx-track-js
99
---
1010

1111
The output contains the prompt, and answers.
1212

1313
```
14-
I need to solve the equation `3x + 11 = 14`. Can you help me?
15-
---------------------------------
16-
Sure! I can help you solve the equation \(3x + 11 = 14\).
14+
Type: text
15+
Text: Sure, I can help you solve the equation \(3x + 11 = 14\).
1716
18-
To solve this equation, we need to isolate the variable \(x\). Let's go ahead and solve it.
19-
---------------------------------
20-
The solution to the equation \(3x + 11 = 14\) is \(x = 1\).
17+
To solve for \(x\), we need to isolate \(x\) on one side of the equation. Here are the steps:
2118
22-
Therefore, the value of \(x\) that satisfies the equation is 1.
19+
1. Subtract 11 from both sides of the equation:
20+
\[3x + 11 - 11 = 14 - 11\]
21+
\[3x = 3\]
2322
24-
Let me know if you need help with anything else!
25-
---------------------------------
23+
2. Divide both sides of the equation by 3:
24+
\[\frac{3x}{3} = \frac{3}{3}\]
25+
\[x = 1\]
26+
27+
So the solution to the equation \(3x + 11 = 14\) is \(x = 1\).
28+
Type: text
29+
Text: I need to solve the equation `3x + 11 = 14`. Can you help me?
2630
```

articles/ai-services/agents/includes/quickstart-typescript.md

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: aahill
44
ms.author: aahi
55
ms.service: azure-ai-agent-service
66
ms.topic: include
7-
ms.date: 02/03/2025
7+
ms.date: 03/21/2025
88
ms.custom: devx-track-ts
99
---
1010

@@ -35,6 +35,12 @@ ms.custom: devx-track-ts
3535
| Run | Activation of an agent to begin running based on the contents of Thread. The agent uses its configuration and Thread’s Messages to perform tasks by calling models and tools. As part of a Run, the agent appends Messages to the Thread. |
3636
| Run Step | A detailed list of steps the agent took as part of a Run. An agent can call tools or create Messages during its run. Examining Run Steps allows you to understand how the agent is getting to its results. |
3737

38+
Key objects in this code include:
39+
40+
* [AIProjectsClient](/javascript/api/@azure/ai-projects/aiprojectsclient)
41+
* [ToolUtility](/javascript/api/@azure/ai-projects/toolutility)
42+
* [Agent operations](/javascript/api/@azure/ai-projects/agentsoperations)
43+
3844
Run the following commands to install the npm packages.
3945

4046
```console
@@ -68,61 +74,65 @@ Set this connection string as an environment variable named `PROJECT_CONNECTION_
6874

6975
```typescript
7076
// index.ts
71-
72-
import type {
73-
MessageDeltaChunk,
74-
MessageDeltaTextContent,
75-
MessageTextContentOutput,
76-
} from "@azure/ai-projects";
7777
import {
7878
AIProjectsClient,
7979
DoneEvent,
8080
ErrorEvent,
8181
isOutputOfType,
8282
MessageStreamEvent,
8383
RunStreamEvent,
84-
ToolUtility,
84+
ToolUtility
85+
} from "@azure/ai-projects";
86+
import type {
87+
MessageDeltaChunk,
88+
MessageDeltaTextContent,
89+
MessageTextContentOutput
8590
} from "@azure/ai-projects";
8691
import { DefaultAzureCredential } from "@azure/identity";
92+
import dotenv from 'dotenv';
8793

88-
const connectionString =
89-
process.env["AZURE_AI_PROJECTS_CONNECTION_STRING"] || "<project connection string>";
94+
dotenv.config();
9095

96+
// Set the connection string from the environment variable
97+
const connectionString = process.env.PROJECT_CONNECTION_STRING;
98+
const model = "gpt-4o";
99+
100+
// Throw an error if the connection string is not set
91101
if (!connectionString) {
92-
throw new Error("AZURE_AI_PROJECTS_CONNECTION_STRING must be set in the environment variables");
102+
throw new Error("Please set the PROJECT_CONNECTION_STRING environment variable.");
93103
}
94104

95-
export async function main(): Promise<void> {
105+
export async function main() {
96106
const client = AIProjectsClient.fromConnectionString(
97107
connectionString || "",
98108
new DefaultAzureCredential(),
99109
);
100110

101-
// Step 1: Create code interpreter tool
102-
const codeInterpreterTool = ToolUtility.createCodeInterpreterTool();
111+
// Step 1 code interpreter tool
112+
const codeInterpreterTool = ToolUtility.createCodeInterpreterTool([]);
103113

104-
// Step 2: Create an agent
105-
const agent = await client.agents.createAgent("gpt-4o-mini", {
114+
// Step 2 an agent
115+
const agent = await client.agents.createAgent(model, {
106116
name: "my-agent",
107117
instructions: "You are a helpful agent",
108118
tools: [codeInterpreterTool.definition],
109119
toolResources: codeInterpreterTool.resources,
110120
});
111121

112-
// Step 3: Create a thread
122+
// Step 3 a thread
113123
const thread = await client.agents.createThread();
114124

115-
// Step 4: Add a message to thread
125+
// Step 4 a message to thread
116126
await client.agents.createMessage(
117127
thread.id, {
118128
role: "user",
119129
content: "I need to solve the equation `3x + 11 = 14`. Can you help me?",
120130
});
121131

122-
// Intermission: message is now correlated with thread
123-
// Intermission: listing messages will retrieve the message just added
132+
// Intermission is now correlated with thread
133+
// Intermission messages will retrieve the message just added
124134

125-
// Step 5: Run the agent
135+
// Step 5 the agent
126136
const streamEventMessages = await client.agents.createRun(thread.id, agent.id).stream();
127137

128138
for await (const eventMessage of streamEventMessages) {
@@ -156,13 +166,14 @@ export async function main(): Promise<void> {
156166

157167
// Messages iterate from oldest to newest
158168
// messages[0] is the most recent
159-
for (let i = messages.data.length - 1; i >= 0; i--) {
160-
const m = messages.data[i];
161-
if (isOutputOfType<MessageTextContentOutput>(m.content[0], "text")) {
162-
const textContent = m.content[0] as MessageTextContentOutput;
163-
console.log(`${textContent.text.value}`);
164-
console.log(`---------------------------------`);
165-
}
169+
const messagesArray = messages.data;
170+
for (let i = messagesArray.length - 1; i >= 0; i--) {
171+
const m = messagesArray[i];
172+
console.log(`Type: ${m.content[0].type}`);
173+
if (isOutputOfType<MessageTextContentOutput>(m.content[0], "text")) {
174+
const textContent = m.content[0] as MessageTextContentOutput;
175+
console.log(`Text: ${textContent.text.value}`);
176+
}
166177
}
167178

168179
// 7. Delete the agent once done
@@ -174,3 +185,5 @@ main().catch((err) => {
174185
});
175186
```
176187

188+
189+
Build the TypeScript code then run the code using `node index.js` and observe.

0 commit comments

Comments
 (0)