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+ } ) ;
0 commit comments