@@ -291,53 +291,37 @@ agentClient.Administration.DeleteAgent(agent.Id);
291291First, create an Azure AI Client using the connection string of your project.
292292
293293``` javascript
294- const connectionString =
295- process .env [" AZURE_AI_PROJECTS_CONNECTION_STRING" ] || " <project connection string>" ;
294+ const projectEndpoint = process .env [" PROJECT_ENDPOINT" ] || " <project connection string>" ;
296295
297- if (! connectionString ) {
296+ if (! projectString ) {
298297 throw new Error (" AZURE_AI_PROJECTS_CONNECTION_STRING must be set in the environment variables" );
299298}
300299
301- const client = AIProjectsClient .fromConnectionString (
302- connectionString || " " ,
303- new DefaultAzureCredential (),
304- );
305- ```
306-
307- ## Get the connection ID for the Azure AI Search resource
308- Get the connection ID of the Azure AI Search connection in the project.
309-
310- ``` javascript
311- const cognitiveServicesConnectionName = " <cognitiveServicesConnectionName>" ;
312- const cognitiveServicesConnection = await client .connections .getConnection (
313- cognitiveServicesConnectionName,
314- );
300+ const client = new AgentsClient (projectEndpoint, new DefaultAzureCredential ());
315301```
316302
317303## Configure the Azure AI Search tool
318- Using the connection ID you got in the previous step, you can now configure the Azure AI Search tool to use your Azure AI Search index.
304+ Using the connection ID of the Azure AI Search resource, configure the Azure AI Search tool to use your Azure AI Search index.
319305
320306``` javascript
321- const azureAISearchTool = ToolUtility .createAzureAISearchTool (
322- cognitiveServicesConnection .id ,
323- cognitiveServicesConnection .name ,
324- );
307+ const connectionId = process .env [" AZURE_AI_CONNECTION_ID" ] || " <connection-name>" ;
308+
309+ const azureAISearchTool = ToolUtility .createAzureAISearchTool (connectionId, " ai-search-sample" , {
310+ queryType: " simple" ,
311+ topK: 3 ,
312+ filter: " " ,
313+ indexConnectionId: " " ,
314+ indexName: " " ,
315+ });
325316
326- // Create agent with the Azure AI search tool
327- const agent = await client .agents .createAgent (" gpt-4o-mini" , {
328- name: " my-agent" ,
329- instructions: " You are a helpful agent" ,
330- tools: [azureAISearchTool .definition ],
331- toolResources: azureAISearchTool .resources ,
332- });
333- console .log (` Created agent, agent ID : ${ agent .id } ` );
334317```
335318
336319## Create an agent with the Azure AI Search tool enabled
337320
338321Change the model to the one deployed in your project. You can find the model name in the Azure AI Foundry under the ** Models** tab. You can also change the name and instructions of the agent to suit your needs.
339322
340323``` javascript
324+
341325const agent = await client .agents .createAgent (" gpt-4o-mini" , {
342326 name: " my-agent" ,
343327 instructions: " You are a helpful agent" ,
@@ -352,60 +336,69 @@ console.log(`Created agent, agent ID : ${agent.id}`);
352336Now that the agent is created, ask it questions about the data in your Azure AI Search index.
353337
354338``` javascript
355- // create a thread
356- const thread = await client .agents .createThread ();
357-
358- // add a message to thread
359- await client .agents .createMessage (
360- thread .id , {
361- role: " user" ,
362- content: " what are my health insurance plan coverage types?" ,
363- });
339+ // Create thread for communication
340+ const thread = await client .threads .create ();
341+ console .log (` Created thread, thread ID: ${ thread .id } ` );
342+
343+ // Create message to thread
344+ const message = await client .messages .create (
345+ thread .id ,
346+ " user" ,
347+ " What is the temperature rating of the cozynights sleeping bag?" ,
348+ );
349+ console .log (` Created message, message ID : ${ message .id } ` );
364350
365- // Intermission is now correlated with thread
366- // Intermission messages will retrieve the message just added
367-
368- // create a run
369- const streamEventMessages = await client .agents .createRun (thread .id , agent .id ).stream ();
370-
371- for await (const eventMessage of streamEventMessages ) {
372- switch (eventMessage .event ) {
373- case RunStreamEvent .ThreadRunCreated :
374- break ;
375- case MessageStreamEvent .ThreadMessageDelta :
376- {
377- const messageDelta = eventMessage .data ;
378- messageDelta .delta .content .forEach ((contentPart ) => {
379- if (contentPart .type === " text" ) {
380- const textContent = contentPart;
381- const textValue = textContent .text ? .value || " No text" ;
351+ // Create and process agent run in thread with tools
352+ let run = await client .runs .create (thread .id , agent .id );
353+ while (run .status === " queued" || run .status === " in_progress" ) {
354+ await delay (1000 );
355+ run = await client .runs .get (thread .id , run .id );
356+ }
357+ if (run .status === " failed" ) {
358+ console .log (` Run failed:` , JSON .stringify (run, null , 2 ));
359+ }
360+ console .log (` Run finished with status: ${ run .status } ` );
361+
362+ // Fetch run steps to get the details of agent run
363+ const runSteps = await client .runSteps .list (thread .id , run .id );
364+
365+ for await (const step of runSteps ) {
366+ console .log (` Step ID: ${ step .id } , Status: ${ step .status } ` );
367+ const stepDetails = step .stepDetails ;
368+ if (isOutputOfType (stepDetails, " tool_calls" )) {
369+ const toolCalls = stepDetails .toolCalls ;
370+ for (const toolCall of toolCalls) {
371+ console .log (` Tool Call ID: ${ toolCall .id } , Tool type: ${ toolCall .type } ` );
372+ if (isOutputOfType (toolCall, " azure_ai_search" )) {
373+ {
374+ const azureAISearch = toolCall .azureAISearch ;
375+ if (azureAISearch) {
376+ console .log (` Azure AI Search Tool Call input: ${ azureAISearch .input } ` );
377+ console .log (` Azure AI Search Tool Call output: ${ azureAISearch .output } ` );
382378 }
383- });
379+ }
384380 }
385- break ;
386-
387- case RunStreamEvent .ThreadRunCompleted :
388- break ;
389- case ErrorEvent .Error :
390- console .log (` An error occurred. Data ${ eventMessage .data } ` );
391- break ;
392- case DoneEvent .Done :
393- break ;
381+ }
394382 }
395383}
396-
397- // Print the messages from the agent
398- const messages = await client .agents .listMessages (thread .id );
399-
400- // Messages iterate from oldest to newest
401- // messages[0] is the most recent
402- for (let i = messages .data .length - 1 ; i >= 0 ; i-- ) {
403- const m = messages .data [i];
404- if (isOutputOfType< MessageTextContentOutput> (m .content [0 ], " text" )) {
405- const textContent = m .content [0 ];
406- console .log (` ${ textContent .text .value } ` );
407- console .log (` ---------------------------------` );
384+ // Delete the assistant when done
385+ await client .deleteAgent (agent .id );
386+ console .log (` Deleted agent, agent ID: ${ agent .id } ` );
387+
388+ // Fetch and log all messages
389+ const messagesIterator = client .messages .list (thread .id );
390+ console .log (` Messages:` );
391+
392+ // Get the first message
393+ for await (const m of messagesIterator ) {
394+ if (m .content .length > 0 ) {
395+ const agentMessage = m .content [0 ];
396+ if (isOutputOfType (agentMessage, " text" )) {
397+ const textContent = agentMessage;
398+ console .log (` Text Message Content - ${ textContent .text .value } ` );
399+ }
408400 }
401+ break ; // Just process the first message
409402}
410403```
411404
0 commit comments