99using Azure . Core . TestFramework ;
1010using NUnit . Framework ;
1111using System . Collections . Generic ;
12+ using NUnit . Framework . Internal ;
1213
1314namespace Azure . AI . Projects . Tests ;
1415
@@ -18,13 +19,14 @@ public partial class Sample_Agents_Azure_AI_Search : SamplesBase<AIProjectsTestE
1819 public async Task AzureAISearchExample ( )
1920 {
2021 var connectionString = TestEnvironment . AzureAICONNECTIONSTRING ;
22+ var modelName = TestEnvironment . MODELDEPLOYMENTNAME ;
2123
2224 var clientOptions = new AIProjectClientOptions ( ) ;
2325
2426 // Adding the custom headers policy
2527 clientOptions . AddPolicy ( new CustomHeadersPolicy ( ) , HttpPipelinePosition . PerCall ) ;
2628 var projectClient = new AIProjectClient ( connectionString , new DefaultAzureCredential ( ) , clientOptions ) ;
27-
29+ #region Snippet:CreateAgentWithAzureAISearchTool
2830 ListConnectionsResponse connections = await projectClient . GetConnectionsClient ( ) . GetConnectionsAsync ( ConnectionType . AzureAISearch ) . ConfigureAwait ( false ) ;
2931
3032 if ( connections ? . Value == null || connections . Value . Count == 0 )
@@ -45,13 +47,13 @@ public async Task AzureAISearchExample()
4547 AgentsClient agentClient = projectClient . GetAgentsClient ( ) ;
4648
4749 Response < Agent > agentResponse = await agentClient . CreateAgentAsync (
48- model : "gpt-4" ,
50+ model : modelName ,
4951 name : "my-assistant" ,
5052 instructions : "You are a helpful assistant." ,
5153 tools : new List < ToolDefinition > { new AzureAISearchToolDefinition ( ) } ,
5254 toolResources : searchResource ) ;
5355 Agent agent = agentResponse . Value ;
54-
56+ #endregion
5557 // Create thread for communication
5658 Response < AgentThread > threadResponse = await agentClient . CreateThreadAsync ( ) ;
5759 AgentThread thread = threadResponse . Value ;
@@ -60,7 +62,7 @@ public async Task AzureAISearchExample()
6062 Response < ThreadMessage > messageResponse = await agentClient . CreateMessageAsync (
6163 thread . Id ,
6264 MessageRole . User ,
63- "Hello, send an email with the datetime and weather information in New York ?" ) ;
65+ "What is the temperature rating of the cozynights sleeping bag ?" ) ;
6466 ThreadMessage message = messageResponse . Value ;
6567
6668 // Run the agent
@@ -74,9 +76,15 @@ public async Task AzureAISearchExample()
7476 while ( runResponse . Value . Status == RunStatus . Queued
7577 || runResponse . Value . Status == RunStatus . InProgress ) ;
7678
77- Response < PageableList < ThreadMessage > > afterRunMessagesResponse
78- = await agentClient . GetMessagesAsync ( thread . Id ) ;
79- IReadOnlyList < ThreadMessage > messages = afterRunMessagesResponse . Value . Data ;
79+ Assert . AreEqual (
80+ RunStatus . Completed ,
81+ runResponse . Value . Status ,
82+ runResponse . Value . LastError ? . Message ) ;
83+ #region Snippet:PopulateReferencesAgentWithAzureAISearchTool
84+ PageableList < ThreadMessage > messages = await agentClient . GetMessagesAsync (
85+ threadId : thread . Id ,
86+ order : ListSortOrder . Ascending
87+ ) ;
8088
8189 // Note: messages iterate from newest to oldest, with the messages[0] being the most recent
8290 foreach ( ThreadMessage threadMessage in messages )
@@ -86,7 +94,25 @@ Response<PageableList<ThreadMessage>> afterRunMessagesResponse
8694 {
8795 if ( contentItem is MessageTextContent textItem )
8896 {
89- Console . Write ( textItem . Text ) ;
97+ // We need to annotate only Agent messages.
98+ if ( threadMessage . Role == MessageRole . Agent && textItem . Annotations . Count > 0 )
99+ {
100+ string annotatedText = textItem . Text ;
101+ foreach ( MessageTextAnnotation annotation in textItem . Annotations )
102+ {
103+ if ( annotation is MessageTextUrlCitationAnnotation urlAnnotation )
104+ {
105+ annotatedText = annotatedText . Replace (
106+ urlAnnotation . Text ,
107+ $ " [see { urlAnnotation . UrlCitation . Title } ] ({ urlAnnotation . UrlCitation . Url } )") ;
108+ }
109+ }
110+ Console . Write ( annotatedText ) ;
111+ }
112+ else
113+ {
114+ Console . Write ( textItem . Text ) ;
115+ }
90116 }
91117 else if ( contentItem is MessageImageFileContent imageFileItem )
92118 {
@@ -95,5 +121,6 @@ Response<PageableList<ThreadMessage>> afterRunMessagesResponse
95121 Console . WriteLine ( ) ;
96122 }
97123 }
124+ #endregion
98125 }
99126}
0 commit comments