@@ -4,7 +4,7 @@ author: aahill
44ms.author : aahi
55ms.service : azure-ai-agent-service
66ms.topic : include
7- ms.date : 03/21 /2025
7+ ms.date : 03/28 /2025
88ms.custom : devx-track-ts
99---
1010
@@ -72,118 +72,6 @@ For example, your connection string may look something like:
7272
7373Set 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
18977Build the TypeScript code then run the code using ` node index.js ` and observe.
0 commit comments