Skip to content

Commit 146a00f

Browse files
authored
[Azure AI Agents] Sample changes to use dataset with file search (#41732)
* Sample changes to use dataset Signed-off-by: trangevi <trangevi@microsoft.com> * Add check for run status, and update message to be more applicable. Signed-off-by: trangevi <trangevi@microsoft.com> * Update snippet code Signed-off-by: trangevi <trangevi@microsoft.com> --------- Signed-off-by: trangevi <trangevi@microsoft.com>
1 parent 76db669 commit 146a00f

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

sdk/ai/azure-ai-agents/README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,17 @@ We can upload file to Azure as it is shown in the example, or use the existing A
279279
<!-- SNIPPET:sample_agents_enterprise_file_search.upload_file_and_create_agent_with_file_search -->
280280

281281
```python
282-
# We will upload the local file to Azure and will use it for vector store creation.
283-
asset_uri = os.environ["AZURE_BLOB_URI"]
284-
285-
# Create a vector store with no file and wait for it to be processed
282+
# If provided, we will upload the local file to Azure and will use it for vector store creation.
283+
# Otherwise, we'll use a previously created dataset reference
284+
if "AZURE_BLOB_URI" in os.environ:
285+
asset_uri = os.environ["AZURE_BLOB_URI"]
286+
else:
287+
dataset_name = os.environ["AZURE_DATASET_NAME"]
288+
dataset_version = os.environ["AZURE_DATASET_VERSION"]
289+
dataset = project_client.datasets.get(name=dataset_name, version=dataset_version)
290+
asset_uri = dataset.id
291+
292+
# Create a vector store and wait for it to be processed
286293
ds = VectorStoreDataSource(asset_identifier=asset_uri, asset_type=VectorStoreDataSourceAssetType.URI_ASSET)
287294
vector_store = agents_client.vector_stores.create_and_poll(data_sources=[ds], name="sample_vector_store")
288295
print(f"Created vector store, vector store ID: {vector_store.id}")

sdk/ai/azure-ai-agents/samples/agents_tools/sample_agents_enterprise_file_search.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525

2626
import os
2727
from azure.ai.projects import AIProjectClient
28-
from azure.ai.agents.models import FileSearchTool, ListSortOrder, VectorStoreDataSource, VectorStoreDataSourceAssetType
28+
from azure.ai.agents.models import FileSearchTool, ListSortOrder, VectorStoreDataSource, VectorStoreDataSourceAssetType, \
29+
RunStatus
2930
from azure.identity import DefaultAzureCredential
3031

3132
project_client = AIProjectClient(
@@ -37,10 +38,17 @@
3738
agents_client = project_client.agents
3839

3940
# [START upload_file_and_create_agent_with_file_search]
40-
# We will upload the local file to Azure and will use it for vector store creation.
41-
asset_uri = os.environ["AZURE_BLOB_URI"]
42-
43-
# Create a vector store with no file and wait for it to be processed
41+
# If provided, we will upload the local file to Azure and will use it for vector store creation.
42+
# Otherwise, we'll use a previously created dataset reference
43+
if "AZURE_BLOB_URI" in os.environ:
44+
asset_uri = os.environ["AZURE_BLOB_URI"]
45+
else:
46+
dataset_name = os.environ["AZURE_DATASET_NAME"]
47+
dataset_version = os.environ["AZURE_DATASET_VERSION"]
48+
dataset = project_client.datasets.get(name=dataset_name, version=dataset_version)
49+
asset_uri = dataset.id
50+
51+
# Create a vector store and wait for it to be processed
4452
ds = VectorStoreDataSource(asset_identifier=asset_uri, asset_type=VectorStoreDataSourceAssetType.URI_ASSET)
4553
vector_store = agents_client.vector_stores.create_and_poll(data_sources=[ds], name="sample_vector_store")
4654
print(f"Created vector store, vector store ID: {vector_store.id}")
@@ -63,13 +71,16 @@
6371
print(f"Created thread, thread ID: {thread.id}")
6472

6573
message = agents_client.messages.create(
66-
thread_id=thread.id, role="user", content="What feature does Smart Eyewear offer?"
74+
thread_id=thread.id, role="user", content="What is the content of the files you have access to?"
6775
)
6876
print(f"Created message, message ID: {message.id}")
6977

7078
run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
7179
print(f"Created run, run ID: {run.id}")
7280

81+
if run.status == RunStatus.FAILED:
82+
print(f"Run failed with: |{run.last_error}|")
83+
7384
agents_client.vector_stores.delete(vector_store.id)
7485
print("Deleted vector store")
7586

0 commit comments

Comments
 (0)