@@ -2,8 +2,8 @@ import fs from 'fs';
22import path from 'path' ;
33import { fileURLToPath } from 'url' ;
44import { DefaultAzureCredential } from '@azure/identity' ;
5+ import { ToolUtility , DoneEvent , ErrorEvent , RunStreamEvent , MessageStreamEvent } from '@azure/ai-agents' ;
56import { AIProjectClient } from '@azure/ai-projects' ;
6- import { AgentsClient , ToolUtility , DoneEvent , ErrorEvent , RunStreamEvent , MessageStreamEvent } from '@azure/ai-agents' ;
77import { config } from 'dotenv' ;
88config ( ) ;
99
@@ -36,46 +36,52 @@ async function chatCompletion() {
3636async function runAgents ( ) {
3737 // <create_and_run_agent>
3838
39- // Create an Azure AI Client
39+ // Create an Azure AI Foundry Client
4040 const endpoint = process . env . PROJECT_ENDPOINT ;
4141 const deployment = process . env . MODEL_DEPLOYMENT_NAME || 'gpt-4o' ;
42- const client = new AgentsClient ( endpoint , new DefaultAzureCredential ( ) ) ;
42+ const client = new AIProjectClient ( endpoint , new DefaultAzureCredential ( ) ) ;
4343
4444 // Create an Agent
45- const agent = await client . createAgent ( deployment , {
45+ const agent = await client . agents . createAgent ( deployment , {
4646 name : 'my-agent' ,
47- instructions : 'You are a helpful agent' ,
47+ instructions : 'You are a helpful agent'
4848 } ) ;
4949 console . log ( `\n==================== 🕵️ POEM AGENT ====================` ) ;
5050
5151 // Create a thread and message
52- const thread = await client . createThread ( ) ;
52+ const thread = await client . agents . threads . create ( ) ;
5353 const prompt = 'Write me a poem about flowers' ;
5454 console . log ( `\n---------------- 📝 User Prompt ---------------- \n${ prompt } ` ) ;
55- await client . createMessage ( thread . id , 'user' , prompt ) ;
55+ await client . agents . messages . create ( thread . id , 'user' , prompt ) ;
5656
5757 // Create run
58- let run = await client . createRun ( thread . id , agent . id ) ;
58+ let run = await client . agents . runs . create ( thread . id , agent . id ) ;
5959
6060 // Wait for run to complete
6161 console . log ( `\n---------------- 🚦 Run Status ----------------` ) ;
6262 while ( [ 'queued' , 'in_progress' , 'requires_action' ] . includes ( run . status ) ) {
6363 // Avoid adding a lot of messages to the console
6464 await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
65- run = await client . getRun ( thread . id , run . id ) ;
65+ run = await client . agents . runs . get ( thread . id , run . id ) ;
6666 console . log ( `Run status: ${ run . status } ` ) ;
6767 }
6868
6969 console . log ( '\n---------------- 📊 Token Usage ----------------' ) ;
7070 console . table ( [ run . usage ] ) ;
7171
72- const messages = await client . listMessages ( thread . id ) ;
73- const assistantMessage = messages . data . find ( m => m . role === 'assistant' ) ;
72+ const messagesIterator = await client . agents . messages . list ( thread . id ) ;
73+ let assistantMessage = null ;
74+ for await ( const m of messagesIterator ) {
75+ if ( m . role === 'assistant' ) {
76+ assistantMessage = m ;
77+ break ;
78+ }
79+ }
7480 console . log ( '\n---------------- 💬 Response ----------------' ) ;
7581 printAssistantMessage ( assistantMessage ) ;
7682
7783 // Delete the Agent
78- await client . deleteAgent ( agent . id ) ;
84+ await client . agents . deleteAgent ( agent . id ) ;
7985 console . log ( `Deleted Agent, Agent ID: ${ agent . id } ` ) ;
8086
8187 // </create_and_run_agent>
@@ -86,11 +92,11 @@ async function runAgents() {
8692 console . log ( `\n==================== 🕵️ FILE AGENT ====================` ) ;
8793 const filePath = path . join ( __dirname , '../../../../data/product_info_1.md' ) ;
8894 const fileStream = fs . createReadStream ( filePath ) ;
89- const file = await client . uploadFile ( fileStream , 'assistants' , {
95+ const file = await client . agents . files . upload ( fileStream , 'assistants' , {
9096 fileName : 'product_info_1.md'
9197 } ) ;
9298 console . log ( `Uploaded file, ID: ${ file . id } ` ) ;
93- const vectorStore = await client . createVectorStore ( {
99+ const vectorStore = await client . agents . vectorStores . create ( {
94100 fileIds : [ file . id ] ,
95101 name : 'my_vectorstore'
96102 } ) ;
@@ -105,21 +111,21 @@ async function runAgents() {
105111
106112 // Create an Agent and a FileSearch tool
107113 const fileSearchTool = ToolUtility . createFileSearchTool ( [ vectorStore . id ] ) ;
108- const fileAgent = await client . createAgent ( deployment , {
114+ const fileAgent = await client . agents . createAgent ( deployment , {
109115 name : 'my-file-agent' ,
110116 instructions : 'You are a helpful assistant and can search information from uploaded files' ,
111117 tools : [ fileSearchTool . definition ] ,
112118 toolResources : fileSearchTool . resources ,
113119 } ) ;
114120
115121 // Create a thread and message
116- const fileSearchThread = await client . createThread ( { toolResources : fileSearchTool . resources } ) ;
122+ const fileSearchThread = await client . agents . threads . create ( { toolResources : fileSearchTool . resources } ) ;
117123 const filePrompt = 'What are the steps to setup the TrailMaster X4 Tent?' ;
118124 console . log ( `\n---------------- 📝 User Prompt ---------------- \n${ filePrompt } ` ) ;
119- await client . createMessage ( fileSearchThread . id , 'user' , filePrompt ) ;
125+ await client . agents . messages . create ( fileSearchThread . id , 'user' , filePrompt ) ;
120126
121127 // Create run
122- let fileSearchRun = await client . createRun ( fileSearchThread . id , fileAgent . id ) . stream ( ) ;
128+ let fileSearchRun = await client . agents . runs . create ( fileSearchThread . id , fileAgent . id ) . stream ( ) ;
123129
124130 for await ( const eventMessage of fileSearchRun ) {
125131 switch ( eventMessage . event ) {
@@ -147,14 +153,20 @@ async function runAgents() {
147153 }
148154 }
149155
150- const fileSearchMessages = await client . listMessages ( fileSearchThread . id ) ;
151- const fileAssistantMessage = fileSearchMessages . data . find ( m => m . role === 'assistant' ) ;
156+ const fileSearchMessagesIterator = await client . agents . messages . list ( fileSearchThread . id ) ;
157+ let fileAssistantMessage = null ;
158+ for await ( const m of fileSearchMessagesIterator ) {
159+ if ( m . role === 'assistant' ) {
160+ fileAssistantMessage = m ;
161+ break ;
162+ }
163+ }
152164 console . log ( `\n---------------- 💬 Response ---------------- \n` ) ;
153165 printAssistantMessage ( fileAssistantMessage ) ;
154166
155- client . deleteVectorStore ( vectorStore . id ) ;
156- client . deleteFile ( file . id ) ;
157- client . deleteAgent ( fileAgent . id ) ;
167+ client . agents . vectorStores . delete ( vectorStore . id ) ;
168+ client . agents . files . delete ( file . id ) ;
169+ client . agents . deleteAgent ( fileAgent . id ) ;
158170 console . log ( `\n🧹 Deleted VectorStore, File, and FileAgent. FileAgent ID: ${ fileAgent . id } ` ) ;
159171
160172 // </create_filesearch_agent>
0 commit comments