Skip to content

Commit 9f57119

Browse files
committed
AI Agents
1 parent a4f6b37 commit 9f57119

File tree

8 files changed

+948
-0
lines changed

8 files changed

+948
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
I need to solve the equation `3x + 11 = 14`. Can you help me?
2+
---------------------------------
3+
Sure! I can help you solve the equation \(3x + 11 = 14\).
4+
5+
To solve this equation, we need to isolate the variable \(x\). Let's go ahead and solve it.
6+
---------------------------------
7+
The solution to the equation \(3x + 11 = 14\) is \(x = 1\).
8+
9+
Therefore, the value of \(x\) that satisfies the equation is 1.
10+
11+
Let me know if you need help with anything else!
12+
---------------------------------
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "azureaiagents-js",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"type": "module",
6+
"scripts": {
7+
"start": "node -r dotenv/config src/index.js",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"description": "",
13+
"dependencies": {
14+
"@azure/ai-projects": "^1.0.0-beta.2",
15+
"@azure/core-util": "^1.11.0",
16+
"@azure/identity": "^4.6.0",
17+
"dotenv": "^16.4.7"
18+
},
19+
"devDependencies": {
20+
"@types/node": "^22.9.1",
21+
"prettier": "3.3.3",
22+
"typescript": "^5.6.3"
23+
}
24+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// index.js
2+
3+
import {
4+
AIProjectsClient,
5+
DoneEvent,
6+
ErrorEvent,
7+
isOutputOfType,
8+
MessageStreamEvent,
9+
RunStreamEvent,
10+
ToolUtility,
11+
} from "@azure/ai-projects";
12+
import { DefaultAzureCredential } from "@azure/identity";
13+
import dotenv from 'dotenv';
14+
15+
dotenv.config();
16+
17+
// Set the connection string from the environment variable
18+
const connectionString = process.env.PROJECT_CONNECTION_STRING;
19+
20+
// Throw an error if the connection string is not set
21+
if (!connectionString) {
22+
throw new Error("Please set the PROJECT_CONNECTION_STRING environment variable.");
23+
}
24+
25+
export async function main() {
26+
const client = AIProjectsClient.fromConnectionString(
27+
connectionString || "",
28+
new DefaultAzureCredential(),
29+
);
30+
31+
// Step 1 code interpreter tool
32+
const codeInterpreterTool = ToolUtility.createCodeInterpreterTool();
33+
34+
// Step 2 an agent
35+
const agent = await client.agents.createAgent("gpt-4o-mini", {
36+
name: "my-agent",
37+
instructions: "You are a helpful agent",
38+
tools: [codeInterpreterTool.definition],
39+
toolResources: codeInterpreterTool.file_ids,
40+
});
41+
42+
// Step 3 a thread
43+
const thread = await client.agents.createThread();
44+
45+
// Step 4 a message to thread
46+
await client.agents.createMessage(
47+
thread.id, {
48+
role: "user",
49+
content: "I need to solve the equation `3x + 11 = 14`. Can you help me?",
50+
});
51+
52+
// Intermission is now correlated with thread
53+
// Intermission messages will retrieve the message just added
54+
55+
// Step 5 the agent
56+
const streamEventMessages = await client.agents.createRun(thread.id, agent.id).stream();
57+
58+
for await (const eventMessage of streamEventMessages) {
59+
switch (eventMessage.event) {
60+
case RunStreamEvent.ThreadRunCreated:
61+
break;
62+
case MessageStreamEvent.ThreadMessageDelta:
63+
{
64+
const messageDelta = eventMessage.data;
65+
messageDelta.delta.content.forEach((contentPart) => {
66+
if (contentPart.type === "text") {
67+
const textContent = contentPart;
68+
const textValue = textContent.text?.value || "No text";
69+
}
70+
});
71+
}
72+
break;
73+
74+
case RunStreamEvent.ThreadRunCompleted:
75+
break;
76+
case ErrorEvent.Error:
77+
console.log(`An error occurred. Data ${eventMessage.data}`);
78+
break;
79+
case DoneEvent.Done:
80+
break;
81+
}
82+
}
83+
84+
// 6. Print the messages from the agent
85+
const messages = await client.agents.listMessages(thread.id);
86+
87+
// Messages iterate from oldest to newest
88+
// messages[0] is the most recent
89+
for (let i = messages.data.length - 1; i >= 0; i--) {
90+
const m = messages.data[i];
91+
if (m.content && m.content.length > 0 && isOutputOfType(m.content[0], "text")) {
92+
const textContent = m.content[0];
93+
console.log(`${textContent.text.value}`);
94+
console.log(`---------------------------------`);
95+
}
96+
}
97+
98+
// 7. Delete the agent once done
99+
await client.agents.deleteAgent(agent.id);
100+
}
101+
102+
main().catch((err) => {
103+
console.error("The sample encountered an error:", err);
104+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
I need to solve the equation `3x + 11 = 14`. Can you help me?
2+
---------------------------------
3+
Sure! I can help you solve the equation \(3x + 11 = 14\).
4+
5+
To solve this equation, we need to isolate the variable \(x\). Let's go ahead and solve it.
6+
---------------------------------
7+
The solution to the equation \(3x + 11 = 14\) is \(x = 1\).
8+
9+
Therefore, the value of \(x\) that satisfies the equation is 1.
10+
11+
Let me know if you need help with anything else!
12+
---------------------------------

0 commit comments

Comments
 (0)