|
| 1 | +# Sample file search on agent with message attachment and code interpreter in Azure.AI.Agents |
| 2 | + |
| 3 | +In this example we demonstrate, how to use file search with `MessageAttachment`. |
| 4 | + |
| 5 | +1. First we need to create agent client and read the environment variables, which will be used in the next steps. |
| 6 | +```C# Snippet:AgentsCodeInterpreterFileAttachment_CreateClient |
| 7 | +var projectEndpoint = configuration["ProjectEndpoint"]; |
| 8 | +var modelDeploymentName = configuration["ModelDeploymentName"]; |
| 9 | + |
| 10 | +PersistentAgentsClient client = new(new Uri(projectEndpoint), new DefaultAzureCredential()); |
| 11 | + |
| 12 | +CodeInterpreterToolDefinition codeInterpreterTool = new(); |
| 13 | + |
| 14 | +string fileName = "sample_file_for_upload.txt"; |
| 15 | +string fullPath = Path.Combine(AppContext.BaseDirectory, fileName); |
| 16 | + |
| 17 | +File.WriteAllText( |
| 18 | + path: fullPath, |
| 19 | + contents: "The word 'apple' uses the code 442345, while the word 'banana' uses the code 673457."); |
| 20 | +``` |
| 21 | + |
| 22 | +2. We need to create an agent, create and upload file and `ThreadMessage` with the uploaded file ID in the `MessageAttachment`. |
| 23 | + |
| 24 | +Synchronous sample: |
| 25 | +```C# Snippet:AgentsCreateAgentWithInterpreterToolSync |
| 26 | +PersistentAgent agent = client.CreateAgent( |
| 27 | + model: modelDeploymentName, |
| 28 | + name: "my-agent", |
| 29 | + instructions: "You are a helpful agent that can help fetch data from files you know about.", |
| 30 | + tools: [codeInterpreterTool]); |
| 31 | + |
| 32 | +PersistentAgentFile uploadedAgentFile = client.UploadFile( |
| 33 | + filePath: fullPath, |
| 34 | + purpose: PersistentAgentFilePurpose.Agents); |
| 35 | + |
| 36 | +var fileId = uploadedAgentFile.Id; |
| 37 | + |
| 38 | +var attachment = new MessageAttachment( |
| 39 | + fileId: fileId, |
| 40 | + tools: [codeInterpreterTool] |
| 41 | +); |
| 42 | + |
| 43 | +PersistentAgentThread thread = client.CreateThread(); |
| 44 | + |
| 45 | +client.CreateMessage( |
| 46 | + threadId: thread.Id, |
| 47 | + role: MessageRole.User, |
| 48 | + content: "Can you give me the documented codes for 'banana' and 'orange'?", |
| 49 | + attachments: [attachment]); |
| 50 | +``` |
| 51 | + |
| 52 | +Asynchronous sample: |
| 53 | +```C# Snippet:AgentsCreateAgentWithInterpreterTool |
| 54 | +PersistentAgent agent = await client.CreateAgentAsync( |
| 55 | + model: modelDeploymentName, |
| 56 | + name: "my-agent", |
| 57 | + instructions: "You are a helpful agent that can help fetch data from files you know about.", |
| 58 | + tools: [codeInterpreterTool]); |
| 59 | + |
| 60 | +PersistentAgentFile uploadedAgentFile = await client.UploadFileAsync( |
| 61 | + filePath: "sample_file_for_upload.txt", |
| 62 | + purpose: PersistentAgentFilePurpose.Agents); |
| 63 | + |
| 64 | +var fileId = uploadedAgentFile.Id; |
| 65 | + |
| 66 | +var attachment = new MessageAttachment( |
| 67 | + fileId: fileId, |
| 68 | + tools: [codeInterpreterTool]); |
| 69 | + |
| 70 | +PersistentAgentThread thread = await client.CreateThreadAsync(); |
| 71 | + |
| 72 | +await client.CreateMessageAsync( |
| 73 | + threadId: thread.Id, |
| 74 | + role: MessageRole.User, |
| 75 | + content: "Can you give me the documented codes for 'banana' and 'orange'?", |
| 76 | + attachments: [attachment]); |
| 77 | +``` |
| 78 | + |
| 79 | +3. Next we will create a `ThreadRun` and wait until the run is completed. If the run was not successful we will print the last error message. |
| 80 | + |
| 81 | +Synchronous sample: |
| 82 | +```C# Snippet:AgentsCodeInterpreterFileAttachmentSync_CreateRun |
| 83 | +ThreadRun run = client.CreateRun( |
| 84 | + thread.Id, |
| 85 | + agent.Id); |
| 86 | + |
| 87 | +do |
| 88 | +{ |
| 89 | + Thread.Sleep(TimeSpan.FromMilliseconds(500)); |
| 90 | + run = client.GetRun(thread.Id, run.Id); |
| 91 | +} |
| 92 | +while (run.Status == RunStatus.Queued |
| 93 | + || run.Status == RunStatus.InProgress |
| 94 | + || run.Status == RunStatus.RequiresAction); |
| 95 | +``` |
| 96 | + |
| 97 | +Asynchronous sample: |
| 98 | +```C# Snippet:AgentsCodeInterpreterFileAttachmentSync_CreateRun |
| 99 | +ThreadRun run = await client.CreateRunAsync( |
| 100 | + thread.Id, |
| 101 | + agent.Id); |
| 102 | + |
| 103 | +do |
| 104 | +{ |
| 105 | + Thread.Sleep(TimeSpan.FromMilliseconds(500)); |
| 106 | + run = await client.GetRunAsync(thread.Id, run.Id); |
| 107 | +} |
| 108 | +while (run.Status == RunStatus.Queued |
| 109 | + || run.Status == RunStatus.InProgress |
| 110 | + || run.Status == RunStatus.RequiresAction); |
| 111 | +``` |
| 112 | + |
| 113 | +4. Print the messages to the console in chronological order. |
| 114 | + |
| 115 | +Synchronous sample: |
| 116 | +```C# Snippet:AgentsCodeInterpreterFileAttachmentSync_PrintMessages |
| 117 | +PageableList<ThreadMessage> messages = client.GetMessages( |
| 118 | + threadId: thread.Id, |
| 119 | + order: ListSortOrder.Ascending); |
| 120 | + |
| 121 | +foreach (ThreadMessage threadMessage in messages) |
| 122 | +{ |
| 123 | + foreach (MessageContent contentItem in threadMessage.ContentItems) |
| 124 | + { |
| 125 | + if (contentItem is MessageTextContent textItem) |
| 126 | + { |
| 127 | + Console.Write($"{threadMessage.Role}: {textItem.Text}"); |
| 128 | + } |
| 129 | + Console.WriteLine(); |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +Asynchronous sample: |
| 135 | +```C# Snippet:AgentsCodeInterpreterFileAttachment_PrintMessages |
| 136 | +PageableList<ThreadMessage> messages = await client.GetMessagesAsync( |
| 137 | + threadId: thread.Id, |
| 138 | + order: ListSortOrder.Ascending); |
| 139 | + |
| 140 | +foreach (ThreadMessage threadMessage in messages) |
| 141 | +{ |
| 142 | + foreach (MessageContent contentItem in threadMessage.ContentItems) |
| 143 | + { |
| 144 | + if (contentItem is MessageTextContent textItem) |
| 145 | + { |
| 146 | + Console.Write($"{threadMessage.Role}: {textItem.Text}"); |
| 147 | + } |
| 148 | + Console.WriteLine(); |
| 149 | + } |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +5. Finally, we delete all the resources, we have created in this sample. |
| 154 | + |
| 155 | +Synchronous sample: |
| 156 | +```C# Snippet:AgentsCodeInterpreterFileAttachmentSync_Cleanup |
| 157 | +client.DeleteFile(fileId: fileId); |
| 158 | +client.DeleteThread(thread.Id); |
| 159 | +client.DeleteAgent(agent.Id); |
| 160 | +``` |
| 161 | + |
| 162 | +Asynchronous sample: |
| 163 | +```C# Snippet:AgentsCodeInterpreterFileAttachment_Cleanup |
| 164 | +await client.DeleteFileAsync(fileId); |
| 165 | +await client.DeleteThreadAsync(thread.Id); |
| 166 | +await client.DeleteAgentAsync(agent.Id); |
| 167 | +``` |
0 commit comments