@@ -4,28 +4,31 @@ In this example we will create the local file, upload it to the newly created `V
44
551 . First we need to create agent client and read the environment variables that will be used in the next steps.
66``` C# Snippet:AgentsFileSearch_CreateClient
7- // Get Connection information from Environment Variables
8- // To use App Config instead: https://learn.microsoft.com/en-us/visualstudio/ide/managing-application-settings-dotnet
9- var projectEndpoint = System .Environment .GetEnvironmentVariable (" PROJECT_ENDPOINT" );
10- var modelDeploymentName = System .Environment .GetEnvironmentVariable (" MODEL_DEPLOYMENT_NAME" );
7+ // Get Connection information from app configuration
8+ var projectEndpoint = configuration [" ProjectEndpoint" ];
9+ var modelDeploymentName = configuration [" ModelDeploymentName" ];
1110
1211// Create the Agent Client
13- PersistentAgentsClient client = new (projectEndpoint , new DefaultAzureCredential ());
12+ PersistentAgentsClient agentClient = new (
13+ projectEndpoint ,
14+ new DefaultAzureCredential (),
15+ new PersistentAgentsAdministrationClientOptions (
16+ PersistentAgentsAdministrationClientOptions .ServiceVersion .V2025_05_01
17+ ));
1418```
1519
16202 . Now we will create a local file and upload it to the data store.
1721
1822Synchronous sample:
1923``` C# Snippet:AgentsFileSearch_UploadAgentFiles
20- // Upload a file and wait for it to be processed
2124// Create a local sample file
2225System .IO .File .WriteAllText (
2326 path : " sample_file_for_upload.txt" ,
2427 contents : " The word 'apple' uses the code 442345, while the word 'banana' uses the code 673457."
2528);
2629
2730// Upload local sample file to the agent
28- PersistentAgentFile uploadedAgentFile = agentClient .UploadFile (
31+ PersistentAgentFileInfo uploadedAgentFile = agentClient . Files .UploadFile (
2932 filePath : " sample_file_for_upload.txt" ,
3033 purpose : PersistentAgentFilePurpose .Agents
3134);
@@ -46,7 +49,7 @@ await System.IO.File.WriteAllTextAsync(
4649);
4750
4851// Upload local sample file to the agent
49- PersistentAgentFile uploadedAgentFile = await agentClient .UploadFileAsync (
52+ PersistentAgentFileInfo uploadedAgentFile = await agentClient . Files .UploadFileAsync (
5053 filePath : " sample_file_for_upload.txt" ,
5154 purpose : PersistentAgentFilePurpose .Agents
5255);
@@ -65,7 +68,7 @@ Synchronous sample:
6568// Create a vector store with the file and wait for it to be processed.
6669// If you do not specify a vector store, CreateMessage will create a vector
6770// store with a default expiration policy of seven days after they were last active
68- VectorStore vectorStore = agentClient .CreateVectorStore (
71+ VectorStore vectorStore = agentClient .VectorStores . CreateVectorStore (
6972 fileIds : new List <string > { uploadedAgentFile .Id },
7073 name : " my_vector_store" );
7174
@@ -76,7 +79,7 @@ Asynchronous sample:
7679// Create a vector store with the file and wait for it to be processed.
7780// If you do not specify a vector store, CreateMessage will create a vector
7881// store with a default expiration policy of seven days after they were last active
79- VectorStore vectorStore = await agentClient .CreateVectorStoreAsync (
82+ VectorStore vectorStore = await agentClient .VectorStores . CreateVectorStoreAsync (
8083 fileIds : new List <string > { uploadedAgentFile .Id },
8184 name : " my_vector_store" );
8285
@@ -91,7 +94,7 @@ FileSearchToolResource fileSearchToolResource = new FileSearchToolResource();
9194fileSearchToolResource .VectorStoreIds .Add (vectorStore .Id );
9295
9396// Create an agent with Tools and Tool Resources
94- PersistentAgent agent = agentClient .CreateAgent (
97+ PersistentAgent agent = agentClient .Administration . CreateAgent (
9598 model : modelDeploymentName ,
9699 name : " SDK Test Agent - Retrieval" ,
97100 instructions : " You are a helpful agent that can help fetch data from files you know about." ,
@@ -106,7 +109,7 @@ FileSearchToolResource fileSearchToolResource = new FileSearchToolResource();
106109fileSearchToolResource .VectorStoreIds .Add (vectorStore .Id );
107110
108111// Create an agent with Tools and Tool Resources
109- PersistentAgent agent = await agentClient .CreateAgentAsync (
112+ PersistentAgent agent = await agentClient .Administration . CreateAgentAsync (
110113 model : modelDeploymentName ,
111114 name : " SDK Test Agent - Retrieval" ,
112115 instructions : " You are a helpful agent that can help fetch data from files you know about." ,
@@ -119,21 +122,21 @@ PersistentAgent agent = await agentClient.CreateAgentAsync(
119122Synchronous sample:
120123``` C# Snippet:AgentsFileSearch_CreateThreadAndRun
121124// Create the agent thread for communication
122- PersistentAgentThread thread = agentClient .CreateThread ();
125+ PersistentAgentThread thread = agentClient .Threads . CreateThread ();
123126
124127// Create message and run the agent
125- ThreadMessage messageResponse = agentClient .CreateMessage (
128+ ThreadMessage messageResponse = agentClient .Messages . CreateMessage (
126129 thread .Id ,
127130 MessageRole .User ,
128131 " Can you give me the documented codes for 'banana' and 'orange'?" );
129132
130- ThreadRun run = agentClient .CreateRun (thread , agent );
133+ ThreadRun run = agentClient .Runs . CreateRun (thread , agent );
131134
132135// Wait for the agent to finish running
133136do
134137{
135138 Thread .Sleep (TimeSpan .FromMilliseconds (500 ));
136- run = agentClient .GetRun (thread .Id , run .Id );
139+ run = agentClient .Runs . GetRun (thread .Id , run .Id );
137140}
138141while (run .Status == RunStatus .Queued
139142 || run .Status == RunStatus .InProgress );
@@ -149,21 +152,21 @@ if (run.Status != RunStatus.Completed)
149152Asynchronous sample:
150153``` C# Snippet:AgentsFileSearchAsync_CreateThreadAndRun
151154// Create the agent thread for communication
152- PersistentAgentThread thread = await agentClient .CreateThreadAsync ();
155+ PersistentAgentThread thread = await agentClient .Threads . CreateThreadAsync ();
153156
154157// Create message and run the agent
155- ThreadMessage messageResponse = await agentClient .CreateMessageAsync (
158+ ThreadMessage messageResponse = await agentClient .Messages . CreateMessageAsync (
156159 thread .Id ,
157160 MessageRole .User ,
158161 " Can you give me the documented codes for 'banana' and 'orange'?" );
159162
160- ThreadRun run = await agentClient .CreateRunAsync (thread , agent );
163+ ThreadRun run = await agentClient .Runs . CreateRunAsync (thread , agent );
161164
162165// Wait for the agent to finish running
163166do
164167{
165168 await Task .Delay (TimeSpan .FromMilliseconds (500 ));
166- run = await agentClient .GetRunAsync (thread .Id , run .Id );
169+ run = await agentClient .Runs . GetRunAsync (thread .Id , run .Id );
167170}
168171while (run .Status == RunStatus .Queued
169172 || run .Status == RunStatus .InProgress );
@@ -177,10 +180,11 @@ if (run.Status != RunStatus.Completed)
177180```
178181
1791826 . Print the agent messages to console in chronological order (including formatting file citations).
180- ``` C# Snippet:AgentsFileSearch_Print
181183
184+ Synchronous sample:
185+ ``` C# Snippet:AgentsFileSearch_Print
182186// Retrieve all messages from the agent client
183- PageableList < ThreadMessage > messages = agentClient .GetMessages (
187+ Pageable < ThreadMessage > messages = agentClient . Messages .GetMessages (
184188 threadId : thread .Id ,
185189 order : ListSortOrder .Ascending
186190);
@@ -233,21 +237,83 @@ foreach (ThreadMessage threadMessage in messages)
233237 Console .WriteLine ();
234238 }
235239}
240+
241+ ```
242+
243+ Asynchronous sample:
244+ ``` C# Snippet:AgentsFileSearchAsync_Print
245+ // Retrieve all messages from the agent client
246+ AsyncPageable < ThreadMessage > messages = agentClient .Messages .GetMessagesAsync (
247+ threadId : thread .Id ,
248+ order : ListSortOrder .Ascending
249+ );
250+
251+ // Helper method for replacing references
252+ static string replaceReferences (Dictionary < string , string > fileIds , string fileID , string placeholder , string text )
253+ {
254+ if (fileIds .TryGetValue (fileID , out string replacement ))
255+ return text .Replace (placeholder , $" [{replacement }]" );
256+ else
257+ return text .Replace (placeholder , $" [{fileID }]" );
258+ }
259+
260+ // Process messages in order
261+ await foreach (ThreadMessage threadMessage in messages )
262+ {
263+ Console .Write ($" {threadMessage .CreatedAt : yyyy - MM - dd HH : mm : ss } - {threadMessage .Role ,10 }: " );
264+
265+ foreach (MessageContent contentItem in threadMessage .ContentItems )
266+ {
267+ if (contentItem is MessageTextContent textItem )
268+ {
269+ if (threadMessage .Role == MessageRole .Agent && textItem .Annotations .Count > 0 )
270+ {
271+ string strMessage = textItem .Text ;
272+
273+ // If we file path or file citation annotations - rewrite the 'source' FileId with the file name
274+ foreach (MessageTextAnnotation annotation in textItem .Annotations )
275+ {
276+ if (annotation is MessageTextFilePathAnnotation pathAnnotation )
277+ {
278+ strMessage = replaceReferences (fileIds , pathAnnotation .FileId , pathAnnotation .Text , strMessage );
279+ }
280+ else if (annotation is MessageTextFileCitationAnnotation citationAnnotation )
281+ {
282+ strMessage = replaceReferences (fileIds , citationAnnotation .FileId , citationAnnotation .Text , strMessage );
283+ }
284+ }
285+ Console .Write (strMessage );
286+ }
287+ else
288+ {
289+ Console .Write (textItem .Text );
290+ }
291+ }
292+ else if (contentItem is MessageImageFileContent imageFileItem )
293+ {
294+ Console .Write ($" <image from ID: {imageFileItem .FileId }" );
295+ }
296+ Console .WriteLine ();
297+ }
298+ }
236299```
300+
2373017 . Finally, we delete all the resources created in this sample.
238302
239303Synchronous sample:
240304``` C# Snippet:AgentsFileSearch_Cleanup
241- client .DeleteVectorStore (vectorStore .Id );
242- client .DeleteFile (uploadedAgentFile .Id );
243- client .DeleteThread (thread .Id );
244- client .DeleteAgent (agent .Id );
305+ // Clean up resources
306+ agentClient .VectorStores .DeleteVectorStore (vectorStore .Id );
307+ agentClient .Files .DeleteFile (uploadedAgentFile .Id );
308+ agentClient .Threads .DeleteThread (thread .Id );
309+ agentClient .Administration .DeleteAgent (agent .Id );
245310```
246311
247312Asynchronous sample:
248313``` C# Snippet:AgentsFileSearchAsync_Cleanup
249- await client .DeleteVectorStoreAsync (vectorStore .Id );
250- await client .DeleteFileAsync (uploadedAgentFile .Id );
251- await client .DeleteThreadAsync (thread .Id );
252- await client .DeleteAgentAsync (agent .Id );
314+ // Clean up resources
315+ await agentClient .VectorStores .DeleteVectorStoreAsync (vectorStore .Id );
316+ await agentClient .Files .DeleteFileAsync (uploadedAgentFile .Id );
317+ await agentClient .Threads .DeleteThreadAsync (thread .Id );
318+ await agentClient .Administration .DeleteAgentAsync (agent .Id );
253319```
0 commit comments