Skip to content

Commit 35e6ef8

Browse files
committed
linked code
1 parent c639c8e commit 35e6ef8

File tree

2 files changed

+5
-222
lines changed

2 files changed

+5
-222
lines changed

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

Lines changed: 3 additions & 108 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: 03/21/2025
7+
ms.date: 03/28/2025
88
ms.custom: devx-track-js
99
---
1010

@@ -88,112 +88,7 @@ Set this connection string as an environment variable named `PROJECT_CONNECTION_
8888

8989
Next, create an `index.js` file and paste in the code below:
9090

91-
```javascript
92-
// index.js
93-
import {
94-
AIProjectsClient,
95-
DoneEvent,
96-
ErrorEvent,
97-
isOutputOfType,
98-
MessageStreamEvent,
99-
RunStreamEvent,
100-
ToolUtility,
101-
} from "@azure/ai-projects";
102-
import { DefaultAzureCredential } from "@azure/identity";
103-
import dotenv from 'dotenv';
104-
105-
dotenv.config();
106-
107-
// Set the connection string from the environment variable
108-
const connectionString = process.env.PROJECT_CONNECTION_STRING;
109-
const model = "gpt-4o";
110-
111-
// Throw an error if the connection string is not set
112-
if (!connectionString) {
113-
throw new Error("Please set the PROJECT_CONNECTION_STRING environment variable.");
114-
}
115-
116-
export async function main() {
117-
const client = AIProjectsClient.fromConnectionString(
118-
connectionString || "",
119-
new DefaultAzureCredential(),
120-
);
121-
122-
// Step 1 code interpreter tool
123-
const codeInterpreterTool = ToolUtility.createCodeInterpreterTool([]);
124-
125-
// Step 2 an agent
126-
const agent = await client.agents.createAgent(model, {
127-
name: "my-agent",
128-
instructions: "You are a helpful agent",
129-
tools: [codeInterpreterTool.definition],
130-
toolResources: codeInterpreterTool.resources,
131-
});
132-
133-
// Step 3 a thread
134-
const thread = await client.agents.createThread();
135-
136-
// Step 4 a message to thread
137-
await client.agents.createMessage(
138-
thread.id, {
139-
role: "user",
140-
content: "I need to solve the equation `3x + 11 = 14`. Can you help me?",
141-
});
142-
143-
// Intermission is now correlated with thread
144-
// Intermission messages will retrieve the message just added
145-
146-
// Step 5 the agent
147-
const streamEventMessages = await client.agents.createRun(thread.id, agent.id).stream();
148-
149-
for await (const eventMessage of streamEventMessages) {
150-
switch (eventMessage.event) {
151-
case RunStreamEvent.ThreadRunCreated:
152-
break;
153-
case MessageStreamEvent.ThreadMessageDelta:
154-
{
155-
const messageDelta = eventMessage.data;
156-
messageDelta.delta.content.forEach((contentPart) => {
157-
if (contentPart.type === "text") {
158-
const textContent = contentPart;
159-
const textValue = textContent.text?.value || "No text";
160-
}
161-
});
162-
}
163-
break;
164-
165-
case RunStreamEvent.ThreadRunCompleted:
166-
break;
167-
case ErrorEvent.Error:
168-
console.log(`An error occurred. Data ${eventMessage.data}`);
169-
break;
170-
case DoneEvent.Done:
171-
break;
172-
}
173-
}
174-
175-
// 6. Print the messages from the agent
176-
const messages = await client.agents.listMessages(thread.id);
177-
178-
// Messages iterate from oldest to newest
179-
// messages[0] is the most recent
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-
}
188-
}
189-
190-
// 7. Delete the agent once done
191-
await client.agents.deleteAgent(agent.id);
192-
}
193-
194-
main().catch((err) => {
195-
console.error("The sample encountered an error:", err);
196-
});
197-
```
91+
:::code language="JavaScript" source="~/azure-typescript-e2e-apps/quickstarts/ai-agents/js/src/index.js":::
92+
19893

19994
Run the code using `node index.js` and observe.

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

Lines changed: 2 additions & 114 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: 03/27/2025
7+
ms.date: 03/28/2025
88
ms.custom: devx-track-ts
99
---
1010

@@ -72,118 +72,6 @@ For example, your connection string may look something like:
7272

7373
Set this connection string as an environment variable named `PROJECT_CONNECTION_STRING`.
7474

75-
```typescript
76-
// index.ts
77-
import {
78-
AIProjectsClient,
79-
DoneEvent,
80-
ErrorEvent,
81-
isOutputOfType,
82-
MessageStreamEvent,
83-
RunStreamEvent,
84-
ToolUtility
85-
} from "@azure/ai-projects";
86-
import type {
87-
MessageDeltaChunk,
88-
MessageDeltaTextContent,
89-
MessageTextContentOutput
90-
} from "@azure/ai-projects";
91-
import { DefaultAzureCredential } from "@azure/identity";
92-
import dotenv from 'dotenv';
93-
94-
dotenv.config();
95-
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
101-
if (!connectionString) {
102-
throw new Error("Please set the PROJECT_CONNECTION_STRING environment variable.");
103-
}
104-
105-
export async function main() {
106-
const client = AIProjectsClient.fromConnectionString(
107-
connectionString || "",
108-
new DefaultAzureCredential(),
109-
);
110-
111-
// Step 1 code interpreter tool
112-
const codeInterpreterTool = ToolUtility.createCodeInterpreterTool([]);
113-
114-
// Step 2 an agent
115-
const agent = await client.agents.createAgent(model, {
116-
name: "my-agent",
117-
instructions: "You are a helpful agent",
118-
tools: [codeInterpreterTool.definition],
119-
toolResources: codeInterpreterTool.resources,
120-
});
121-
122-
// Step 3 a thread
123-
const thread = await client.agents.createThread();
124-
125-
// Step 4 a message to thread
126-
await client.agents.createMessage(
127-
thread.id, {
128-
role: "user",
129-
content: "I need to solve the equation `3x + 11 = 14`. Can you help me?",
130-
});
131-
132-
// Intermission is now correlated with thread
133-
// Intermission messages will retrieve the message just added
134-
135-
// Step 5 the agent
136-
const streamEventMessages = await client.agents.createRun(thread.id, agent.id).stream();
137-
138-
for await (const eventMessage of streamEventMessages) {
139-
switch (eventMessage.event) {
140-
case RunStreamEvent.ThreadRunCreated:
141-
break;
142-
case MessageStreamEvent.ThreadMessageDelta:
143-
{
144-
const messageDelta = eventMessage.data as MessageDeltaChunk;
145-
messageDelta.delta.content.forEach((contentPart) => {
146-
if (contentPart.type === "text") {
147-
const textContent = contentPart as MessageDeltaTextContent;
148-
const textValue = textContent.text?.value || "No text";
149-
}
150-
});
151-
}
152-
break;
153-
154-
case RunStreamEvent.ThreadRunCompleted:
155-
break;
156-
case ErrorEvent.Error:
157-
console.log(`An error occurred. Data ${eventMessage.data}`);
158-
break;
159-
case DoneEvent.Done:
160-
break;
161-
}
162-
}
163-
164-
// 6. Print the messages from the agent
165-
const messages = await client.agents.listMessages(thread.id);
166-
167-
// Messages iterate from oldest to newest
168-
// messages[0] is the most recent
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-
}
177-
}
178-
179-
// 7. Delete the agent once done
180-
await client.agents.deleteAgent(agent.id);
181-
}
182-
183-
main().catch((err) => {
184-
console.error("The sample encountered an error:", err);
185-
});
186-
```
187-
75+
:::code language="TypeScript" source="~/azure-typescript-e2e-apps/quickstarts/ai-agents/ts/src/index.ts":::
18876

18977
Build the TypeScript code then run the code using `node index.js` and observe.

0 commit comments

Comments
 (0)