@@ -85,253 +85,16 @@ As a fallback, there's a 60-second maximum wait in the run object when the threa
85
85
86
86
::: zone pivot="upload-files-code-examples"
87
87
88
- ## Quickstart – Upload Local Files with file search
89
-
90
- In this example, we’ll use Azure AI Agent Service to create an agent that can help answer questions on information you upload from local files.
91
-
92
- ## Prerequisites
93
- Complete the [ agent setup] ( ../../quickstart.md ) .
94
-
95
- ## Step 1: Create a project client
96
-
97
- Create a client object, which will contain the connection string for connecting to your AI project and other resources.
98
-
99
- # [ Python] ( #tab/python )
100
-
101
- ``` python
102
- import os
103
- from azure.ai.projects import AIProjectClient
104
- from azure.ai.projects.models import FileSearchTool, VectorStoreDataSource, VectorStoreDataSourceAssetType
105
- from azure.identity import DefaultAzureCredential
106
-
107
-
108
- # Create an Azure AI Client from a connection string, copied from your AI Studio project.
109
- # At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<ProjectName>"
110
- # Customer needs to login to Azure subscription via Azure CLI and set the environment variables
111
-
112
- credential = DefaultAzureCredential()
113
- project_client = AIProjectClient.from_connection_string(
114
- credential = credential, conn_str = os.environ[" PROJECT_CONNECTION_STRING" ]
115
- )
116
- ```
117
-
118
- # [ C#] ( #tab/csharp )
119
-
120
- ``` csharp
121
- using System ;
122
- using System .Collections .Generic ;
123
- using System .IO ;
124
- using System .Threading .Tasks ;
125
- using Azure .Core .TestFramework ;
126
- using NUnit .Framework ;
127
-
128
- // Create an Azure AI Client from a connection string, copied from your AI Studio project.
129
- // At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<ProjectName>"
130
- // Customer needs to login to Azure subscription via Azure CLI and set the environment variables
131
- var connectionString = TestEnvironment .AzureAICONNECTIONSTRING ;
132
- AgentsClient client = new AgentsClient (connectionString , new DefaultAzureCredential ());
133
- ```
134
-
135
- ---
136
-
137
- ## Step 2: Upload files and add them to a Vector Store
138
-
139
- To access your files, the file search tool uses the vector store object. Upload your files and create a vector store to contain them. Once the vector store is created, you should poll its status until all files are out of the ` in_progress ` state to ensure that all content has finished processing. The SDK provides helpers for uploading and polling.
140
-
141
- Vector stores are created using message attachments that have a default expiration policy of seven days after they were last active (defined as the last time the vector store was part of a run). This default exists to help you manage your vector storage costs. You can override these expiration policies at any time.
142
-
143
- # [ Python] ( #tab/python )
144
-
145
- ``` python
146
- # We will upload the local file to Azure and will use it for vector store creation.
147
- _, asset_uri = project_client.upload_file(" ./data/product_info_1.md" )
148
- print (f " Uploaded file, asset URI: { asset_uri} " )
149
- # create a vector store with no file and wait for it to be processed
150
- ds = VectorStoreDataSource(asset_identifier = asset_uri, asset_type = VectorStoreDataSourceAssetType.URI_ASSET )
151
- vector_store = project_client.agents.create_vector_store_and_poll(data_sources = [ds], name = " sample_vector_store" )
152
- print (f " Created vector store, vector store ID: { vector_store.id} " )
153
- ```
154
-
155
- # [ C#] ( #tab/csharp )
156
-
157
- ``` csharp
158
- // Upload a file and wait for it to be processed
159
- File .WriteAllText (
160
- path : " sample_file_for_upload.txt" ,
161
- contents : " The word 'apple' uses the code 442345, while the word 'banana' uses the code 673457." );
162
- Response < AgentFile > uploadAgentFileResponse = await client .UploadFileAsync (
163
- filePath : " sample_file_for_upload.txt" ,
164
- purpose : AgentFilePurpose .Agents );
165
-
166
- AgentFile uploadedAgentFile = uploadAgentFileResponse .Value ;
167
-
168
- // Create a vector store with the file and wait for it to be processed.
169
- // If you do not specify a vector store, create_message will create a vector store with a default expiration policy of seven days after they were last active
170
- VectorStore vectorStore = await client .CreateVectorStoreAsync (
171
- fileIds : new List <string > { uploadedAgentFile .Id },
172
- name : " my_vector_store" );
173
- ```
174
- ---
175
-
176
- ## Step 3: Enable file search
177
-
178
- To make the files accessible to your agent, create a ` FileSearchTool ` object with the ` vector_store ` ID, and attach ` tools ` and ` tool_resources ` to the agent.
179
-
180
- # [ Python] ( #tab/python )
181
-
182
- ``` python
183
-
184
- # create a file search tool
185
- file_search_tool = FileSearchTool(vector_store_ids = [vector_store.id])
186
-
187
- # notices that FileSearchTool as tool and tool_resources must be added or the agent will be unable to search the file
188
- agent = project_client.agents.create_agent(
189
- model = " gpt-4o-mini" ,
190
- name = " my-agent" ,
191
- instructions = " You are a helpful agent" ,
192
- tools = file_search_tool.definitions,
193
- tool_resources = file_search_tool.resources,
194
- )
195
- print (f " Created agent, agent ID: { agent.id} " )
196
- ```
197
-
198
- # [ C#] ( #tab/csharp )
199
-
200
- ``` csharp
201
- FileSearchToolResource fileSearchToolResource = new FileSearchToolResource ();
202
- fileSearchToolResource .VectorStoreIds .Add (vectorStore .Id );
203
-
204
- // Create an agent with toolResources and process assistant run
205
- Response < Agent > agentResponse = await client .CreateAgentAsync (
206
- model : " gpt-4o-mini" ,
207
- name : " SDK Test Agent - Retrieval" ,
208
- instructions : " You are a helpful agent that can help fetch data from files you know about." ,
209
- tools : new List <ToolDefinition > { new FileSearchToolDefinition () },
210
- toolResources : new ToolResources () { FileSearch = fileSearchToolResource });
211
- Agent agent = agentResponse .Value ;
212
- ```
213
-
214
- ---
215
-
216
- ## Step 4: Create a thread
217
-
218
- # [ Python] ( #tab/python )
219
-
220
- ``` python
221
- thread = project_client.agents.create_thread()
222
- print (f " Created thread, thread ID: { thread.id} " )
223
-
224
- message = project_client.agents.create_message(
225
- thread_id = thread.id, role = " user" , content = " What feature does Smart Eyewear offer?"
226
- )
227
- print (f " Created message, message ID: { message.id} " )
228
- ```
229
-
230
- # [ C#] ( #tab/csharp )
231
-
232
- ``` csharp
233
- // Create thread for communication
234
- Response < AgentThread > threadResponse = await client .CreateThreadAsync ();
235
- AgentThread thread = threadResponse .Value ;
236
-
237
- // Create message to thread
238
- Response < ThreadMessage > messageResponse = await client .CreateMessageAsync (
239
- thread .Id ,
240
- MessageRole .User ,
241
- " Can you give me the documented codes for 'banana' and 'orange'?" );
242
- ThreadMessage message = messageResponse .Value ;
243
- ```
244
- ---
245
-
246
- ## Step 5: Create a run and check the output
247
- Create a run and observe that the model uses the file search tool to provide a response to the user's question.
248
- # [ Python] ( #tab/python )
249
- ``` python
250
- run = project_client.agents.create_and_process_run(thread_id = thread.id, assistant_id = agent.id)
251
- print (f " Created run, run ID: { run.id} " )
252
-
253
- project_client.agents.delete_vector_store(vector_store.id)
254
- print (" Deleted vector store" )
255
-
256
- project_client.agents.delete_agent(agent.id)
257
- print (" Deleted agent" )
258
-
259
- messages = project_client.agents.list_messages(thread_id = thread.id)
260
- print (f " Messages: { messages} " )
261
- ```
262
-
263
- # [ C#] ( #tab/csharp )
264
-
265
- ``` csharp
266
- // Run the agent
267
- Response < ThreadRun > runResponse = await client .CreateRunAsync (thread , agent );
268
-
269
- do
270
- {
271
- await Task .Delay (TimeSpan .FromMilliseconds (500 ));
272
- runResponse = await client .GetRunAsync (thread .Id , runResponse .Value .Id );
273
- }
274
- while (runResponse .Value .Status == RunStatus .Queued
275
- || runResponse .Value .Status == RunStatus .InProgress );
276
-
277
- Response < PageableList < ThreadMessage >> afterRunMessagesResponse
278
- = await client .GetMessagesAsync (thread .Id );
279
- IReadOnlyList < ThreadMessage > messages = afterRunMessagesResponse .Value .Data ;
280
-
281
- // Note: messages iterate from newest to oldest, with the messages[0] being the most recent
282
- foreach (ThreadMessage threadMessage in messages )
283
- {
284
- Console .Write ($" {threadMessage .CreatedAt : yyyy - MM - dd HH : mm : ss } - {threadMessage .Role ,10 }: " );
285
- foreach (MessageContent contentItem in threadMessage .ContentItems )
286
- {
287
- if (contentItem is MessageTextContent textItem )
288
- {
289
- Console .Write (textItem .Text );
290
- }
291
- else if (contentItem is MessageImageFileContent imageFileItem )
292
- {
293
- Console .Write ($" <image from ID: {imageFileItem .FileId }" );
294
- }
295
- Console .WriteLine ();
296
- }
297
- }
298
- ```
299
- ---
300
-
88
+ [ !INCLUDE [ upload-files-code-examples] ( ../../includes/file-search/upload-files-code-examples.md )]
301
89
302
90
::: zone-end
303
91
304
92
::: zone pivot="azure-blob-storage-code-examples"
305
- ## Quickstart – Use existing files in Azure Blob Storage with file search
306
- In this example, we’ll use Azure AI Agent Service to create an agent that can help answer questions on information from files in Azure Blob Storage.
307
93
308
- ### Prerequisites
309
- Complete the [ standard agent setup] ( ../../quickstart.md ) .
310
-
311
- > [ !NOTE]
312
- > Azure Blob Storage is only available with the standard agent setup. The basic agent setup does not support this file source.
313
-
314
- ### Step 1: Create a project client
315
- # [ Python] ( #tab/python )
316
-
317
- ``` python
318
- import os
319
- from azure.ai.projects import AIProjectClient
320
- from azure.ai.projects.models import FileSearchTool, VectorStoreDataSource, VectorStoreDataSourceAssetType
321
- from azure.identity import DefaultAzureCredential
322
-
323
-
324
- # Create an Azure AI Client from a connection string, copied from your AI Studio project.
325
- # At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<ProjectName>"
326
- # Customer needs to login to Azure subscription via Azure CLI and set the environment variables
327
-
328
- credential = DefaultAzureCredential()
329
- project_client = AIProjectClient.from_connection_string(
330
- credential = credential, conn_str = os.environ[" PROJECT_CONNECTION_STRING" ]
331
- )
332
- ```
94
+ [ !INCLUDE [ azure-blob-storage-code-examples] ( ../../includes/file-search/azure-blob-storage-code-examples.md )]
333
95
334
96
::: zone-end
97
+
335
98
::: zone pivot="supported-filetypes"
336
99
337
100
### Supported file types
0 commit comments