Skip to content

Commit d5eb7da

Browse files
authored
Merge pull request #1962 from fosteramanda/release-azure-agents
Adding code for Azure Blob Storage + more fixes
2 parents 6c0f0db + 94646b8 commit d5eb7da

File tree

13 files changed

+578
-344
lines changed

13 files changed

+578
-344
lines changed

articles/ai-services/agents/how-to/tools/azure-ai-search.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ zone_pivot_groups: selection-azure-ai-search
1414
---
1515

1616
# Use an existing AI Search index with the Azure AI Search tool
17-
::: zone pivot="overview"
18-
17+
::: zone pivot="overview-azure-ai-search"
1918
Use an existing Azure AI Search index with the agent's Azure AI Search tool.
2019

2120
> [!NOTE]

articles/ai-services/agents/how-to/tools/file-search.md

Lines changed: 13 additions & 324 deletions
Large diffs are not rendered by default.

articles/ai-services/agents/includes/azure-search/code-examples.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
---
2+
manager: nitinme
3+
author: fosteramanda
4+
ms.author: fosteramanda
5+
ms.service: azure
6+
ms.topic: include
7+
ms.date: 12/10/2024
8+
---
19
## Quickstart – Use an existing Azure AI Search index with the Azure AI Search tool
210

311
This quickstart shows how to use an existing Azure AI Search index with the Azure AI Search tool.
412

513
### Prerequisites
6-
Complete the [Azure AI Search tool setup](?pivot=setup).
14+
Complete the [Azure AI Search tool setup](../../how-to/tools/azure-ai-search.md?pivot=overview-azure-ai-search).
715

816
### Step 1: Create an Azure AI Client
917
First, create an Azure AI Client using the connection string of your project.
@@ -119,7 +127,6 @@ agent = project_client.agents.create_agent(
119127
instructions="You are a helpful assistant",
120128
tools=ai_search.definitions,
121129
tool_resources = ai_search.resources,
122-
headers={"x-ms-enable-preview": "true"},
123130
)
124131
print(f"Created agent, ID: {agent.id}")
125132
```

articles/ai-services/agents/includes/azure-search/setup.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ ms.date: 12/10/2024
1010
## Setup: Create an agent that can use an existing Azure AI Search index
1111

1212
#### Prerequisite: Have an existing Azure AI Search index
13-
A prerequisite of using the Azure AI Search tool is to have an existing Azure AI Search index. If you don't have an existing index, you can create one in the Azure portal or via REST API.
14-
- [Quickstart: Create a vector index using the Azure portal](../../../../search/search-get-started-portal-import-vectors.md)
15-
- [Quickstart: Create a vector index using REST API](../../../../search/search-get-started-vector.md)
13+
A prerequisite of using the Azure AI Search tool is to have an existing Azure AI Search index. If you don't have an existing index, you can create one in the Azure portal using the import and vectorize data wizard.
14+
- [Quickstart: Create a vector index with the import and vectorize data wizard in the Azure portal](../../../../search/search-get-started-portal-import-vectors.md)
1615

1716

1817
#### Complete the agent setup
@@ -22,7 +21,9 @@ A prerequisite of using the Azure AI Search tool is to have an existing Azure AI
2221

2322

2423
#### Create a project connection to the Azure AI Search resource with the index you want to use
25-
If you already connected the AI Search resource that contains the index you want to use to your project, skip this step.
24+
Once you have completed the standard agent setup, you must create a project connection to the Azure AI Search resource that contains the index you want to use.
25+
26+
If you already connected the AI Search resource that contains the index you want to use to your project, skip this step.
2627

2728
##### Get your Azure AI Search resource connection key and endpoint
2829
1. Access your Azure AI Search resource.
@@ -43,11 +44,11 @@ If you already connected the AI Search resource that contains the index you want
4344
**Create the following connections.yml file**
4445

4546

46-
You can use either an API key or credential-less YAML configuration file. For more information on the YAML configuration file, see the [Azure AI Search connection YAML schema](../../../../machine-learning/reference-yaml-connection-ai-search.md):
47+
You can use either an API key or credential-less YAML configuration file. Replace the placeholders for ```name```, ```endpoint``` and ```api_key``` with your Azure AI Search resource values. For more information on the YAML configuration file, see the [Azure AI Search connection YAML schema](../../../../machine-learning/reference-yaml-connection-ai-search.md).
4748
- API Key example:
4849

4950
```yml
50-
name: myazaics_apk
51+
name: my_project_acs_connection_keys
5152
type: azure_ai_search
5253
endpoint: https://contoso.search.windows.net/
5354
api_key: XXXXXXXXXXXXXXX
@@ -56,7 +57,7 @@ You can use either an API key or credential-less YAML configuration file. For mo
5657
- Credential-less
5758
5859
```yml
59-
name: myazaics_ei
60+
name: my_project_acs_connection_credentialless
6061
type: azure_ai_search
6162
endpoint: https://contoso.search.windows.net/
6263
```

articles/ai-services/agents/includes/bicep-setup.md

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The following section shows you how to set up the required resources for getting
1515

1616
1. Creating an Azure AI project under your Hub creates an endpoint for your app to call, and sets up app services to access to resources in your tenant.
1717

18-
1. Connecting an Azure OpenAI resource or an Azure AI resource
18+
1. Connecting an Azure OpenAI resource or an Azure AI Services resource
1919

2020

2121
## Choose Basic or Standard Agent Setup
@@ -24,15 +24,86 @@ The following section shows you how to set up the required resources for getting
2424

2525
**Standard Setup**: Agents use customer-owned, single-tenant search and storage resources. With this setup, you have full control and visibility over these resources, but you incur costs based on your usage.
2626

27-
| Description | Autodeploy |
28-
| -----------------------------------------------| -----------------------|
29-
| Deploy a basic agent setup that uses Managed Identity for authentication. | [![Deploy to Azure](https://aka.ms/deploytoazurebutton)](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-quickstart-templates%2Fmaster%2Fquickstarts%2Fmicrosoft.azure-ai-agent-service%2Fbasic-agent-identity%2Fazuredeploy.json)
30-
| Deploy a standard agent setup that uses Managed Identity for authentication. | [![Deploy to Azure](https://aka.ms/deploytoazurebutton)](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-quickstart-templates%2Frefs%2Fheads%2Fmaster%2Fquickstarts%2Fmicrosoft.azure-ai-agent-service%2Fstandard-agent%2Fazuredeploy.json)
27+
| Description | Resources | Autodeploy |
28+
| -----------------------------------------------| -----------------------|----------------------|
29+
| Deploy a basic agent setup that uses Managed Identity authentication on the AI Services and storage account connections. | AI hub, AI project, AI Services | [![Deploy to Azure](https://aka.ms/deploytoazurebutton)](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-quickstart-templates%2Fmaster%2Fquickstarts%2Fmicrosoft.azure-ai-agent-service%2Fbasic-agent-identity%2Fazuredeploy.json) |
30+
| Deploy a standard agent setup that uses Managed Identity authentication on the AI Services, storage account, and Azure AI Search connections. |AI hub, AI project, storage account, key vault, Azure AI Search, AI Services | [![Deploy to Azure](https://aka.ms/deploytoazurebutton)](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-quickstart-templates%2Frefs%2Fheads%2Fmaster%2Fquickstarts%2Fmicrosoft.azure-ai-agent-service%2Fstandard-agent%2Fazuredeploy.json)|
31+
32+
### [Optional] Model selection in autodeploy template
33+
You can customize the model used by your agent by editing the model parameters in the autodeploy template. To deploy a different model, you need to update at least the `modelName` and `modelVersion` parameters.
34+
35+
By default, the deployment template is configured with the following values:
36+
37+
| Model Parameter | Default Value |
38+
|------------------|----------------|
39+
| modelName | gpt-4o-mini |
40+
| modelFormat | OpenAI |
41+
| modelVersion | 2024-07-18 |
42+
| modelSkuName | GlobalStandard |
43+
| modelLocation | eastus |
44+
45+
> [!IMPORTANT]
46+
> **Don't change the modelFormat parameter.**
47+
>
48+
> The templates only support deployment of OpenAI models. See which OpenAI models are supported in the [Azure AI Agent Service model support](../concepts/model-region-support.md) documentation.
49+
50+
51+
### [Optional] Use your own resources in agent setup
52+
53+
> [!NOTE]
54+
> If you use an existing AI Services/AOAI resource, no model will be deployed. You can deploy a model to the resource after the agent setup is complete.
55+
56+
#### Basic agent setup: use an existing AI Services resource
57+
58+
Replace the parameter value for `aiServiceAccountResourceId` with the full arm resource ID of the AI Services account you want to use.
59+
60+
1. To get the AI Services account resource ID, sign in to the Azure CLI and select the subscription with your AI Services account:
61+
62+
```az login```
63+
2. Replace `<your-resource-group>` with the resource group containing your resource and `your-ai-service-resource-name` with the name of your AI Service resource, and run:
64+
65+
```az cognitiveservices account show --resource-group <your-resource-group> --name <your-ai-service-resource-name> --query "id" --output tsv```
66+
67+
The value returned is the `aiServiceAccountResourceId` you need to use in the template.
68+
69+
2. In the basic agent template file, set the parameter:
70+
- aiServiceAccountResourceId:/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.CognitiveServices/accounts/{serviceName}
71+
72+
#### Standard agent setup: use an existing AI Services, storage, and/or Azure AI Search resource
73+
74+
Use an existing AI Search, storage account, and/or Azure AI Search resource by providing the full arm resource ID in the standard agent template file.
75+
76+
Use an existing AI Services resource:
77+
1. Follow the steps in basic agent setup to get the AI Services account resource ID.
78+
2. In the standard agent template file, set the parameter:
79+
- aiServiceAccountResourceId:/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.CognitiveServices/accounts/{serviceName}
80+
81+
Use an existing storage account:
82+
1. To get your storage account resource ID, sign in to the Azure CLI and select the subscription with your storage account:
83+
84+
```az login```
85+
2. Then run the command:
86+
87+
```az search service show --resource-group <your-resource-group> --name <your-storage-account> --query "id" --output tsv```
88+
89+
The output is the `aiStorageAccountResourceID` you need to use in the template.
90+
3. In the standard agent template file, set the parameter:
91+
- aiStorageAccountResourceId:/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{storageAccountName}
92+
93+
Use an existing Azure AI Search resource:
94+
1. To get your Azure AI Search resource ID, sign into Azure CLI and select the subscription with your search resource:
95+
96+
```az login```
97+
2. Then run the command:
98+
99+
```az search service show --resource-group <your-resource-group> --name <your-search-service> --query "id" --output tsv```
100+
3. In the standard agent template file, set the parameter:
101+
- aiSearchServiceResourceId:/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Search/searchServices/{searchServiceName}
31102

32103
## Basic agent setup resource architecture
33104
:::image type="content" source="../media/quickstart/basic-agent-setup-resources.png" alt-text="An architecture diagram for basic agent setup." lightbox="../media/quickstart/basic-agent-setup-resources.png":::
34105

35-
Resources for the AI hub, AI project, storage account, and AI Services are created for you. The AI Services account is connected to your project/hub and a gpt-4o-mini model is deployed in the eastus region. A Microsoft-managed key vault is used by default.
106+
Resources for the AI hub, AI project, and AI Services are created for you. A storage account is created because it's a required resource for hubs, but this storage account is not used by agents. The AI Services account is connected to your project/hub and a gpt-4o-mini model is deployed in the eastus region. A Microsoft-managed key vault, storage account, and search resource is used by default.
36107

37108
## Standard agent setup resource architecture
38109
:::image type="content" source="../media/quickstart/standard-agent-setup-resources.png" alt-text="An architecture diagram for standard agent setup." lightbox="../media/quickstart/standard-agent-setup-resources.png":::
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
manager: nitinme
3+
author: fosteramanda
4+
ms.author: fosteramanda
5+
ms.service: azure
6+
ms.topic: include
7+
ms.date: 12/10/2024
8+
---
9+
10+
## Quickstart – Use existing files in Azure Blob Storage with file search
11+
In this example, we use Azure AI Agent Service to create an agent that can help answer questions on information from files in Azure Blob Storage.
12+
13+
### Prerequisites
14+
1. Complete the [standard agent setup](../../quickstart.md).
15+
16+
2. Ensure that you have the role **Storage Blob Data Contributor** on your project's storage account.
17+
18+
3. Ensure that you have the role **Azure AI Developer** on your project.
19+
20+
> [!IMPORTANT]
21+
> File search using Blob storage is only supported by the standard agent setup.
22+
23+
### Step 1: Create a project client
24+
```python
25+
import os
26+
from azure.ai.projects import AIProjectClient
27+
from azure.ai.projects.models import FileSearchTool, VectorStoreDataSource, VectorStoreDataSourceAssetType
28+
from azure.identity import DefaultAzureCredential
29+
30+
31+
# Create an Azure AI Client from a connection string, copied from your AI Studio project.
32+
# At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<ProjectName>"
33+
# Customer needs to login to Azure subscription via Azure CLI and set the environment variables
34+
35+
credential = DefaultAzureCredential()
36+
project_client = AIProjectClient.from_connection_string(
37+
credential=credential, conn_str=os.environ["PROJECT_CONNECTION_STRING"]
38+
)
39+
```
40+
41+
### Step 2: Upload local files to your project Azure Blob Storage container
42+
Upload your local file to the project’s Azure Blob Storage container. This is the same storage account you connected to your agent during setup. When creating additional agents within the same project, you can reuse the asset URIs of any previously uploaded files that those agents need. This means you don't have to upload the same file repeatedly, as the asset URIs allow you to reference the files directly.
43+
44+
Then, create a vector store using the ```asset_uri```, which is the location of your file in your project's datastore.
45+
```python
46+
# We'll upload the local file to your project Azure Blob Storage container and will use it for vector store creation.
47+
_, asset_uri = project_client.upload_file("sample_file_for_upload.md")
48+
print(f"Uploaded file, asset URI: {asset_uri}")
49+
50+
# create a vector store with a file in blob storage and wait for it to be processed
51+
ds = VectorStoreDataSource(asset_identifier=asset_uri, asset_type=VectorStoreDataSourceAssetType.URI_ASSET)
52+
vector_store = project_client.agents.create_vector_store_and_poll(data_sources=[ds], name="sample_vector_store")
53+
print(f"Created vector store, vector store ID: {vector_store.id}")
54+
```
55+
56+
### Step 3: Create an agent with access to the file search tool
57+
58+
```python
59+
# create a file search tool
60+
file_search_tool = FileSearchTool(vector_store_ids=[vector_store.id])
61+
62+
# notices that FileSearchTool as tool and tool_resources must be added or the assistant unable to search the file
63+
agent_1 = project_client.agents.create_agent(
64+
model="gpt-4o-mini",
65+
name="my-assistant",
66+
instructions="You are helpful assistant",
67+
tools=file_search_tool.definitions,
68+
tool_resources=file_search_tool.resources,
69+
)
70+
# [END upload_file_and_create_agent_with_file_search]
71+
print(f"Created agent_1, agent_1 ID: {agent_1.id}")
72+
73+
thread = project_client.agents.create_thread()
74+
print(f"Created thread, thread ID: {thread.id}")
75+
76+
message = project_client.agents.create_message(
77+
thread_id=thread.id, role="user", content="What feature does Smart Eyewear offer?"
78+
)
79+
print(f"Created message, message ID: {message.id}")
80+
81+
run = project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent_1.id)
82+
83+
project_client.agents.delete_vector_store(vector_store.id)
84+
print("Deleted vector store")
85+
86+
project_client.agents.delete_agent(agent.id)
87+
print("Deleted agent")
88+
89+
messages = project_client.agents.list_messages(thread_id=thread.id)
90+
print(f"Messages: {messages}")
91+
```
92+
93+
### Step 4: Create second vector store using the previously uploaded file
94+
Now, create a second vector store using the previously uploaded file. Using the ```asset_uri``` of a file already in Azure Blob Storage is useful if you have multiple agents that need access to the same files, as it eliminates the need to upload the same file multiple times.
95+
```python
96+
97+
# create a vector store with a previously uploaded file and wait for it to be processed
98+
ds_2 = VectorStoreDataSource(asset_identifier=asset_uri, asset_type=VectorStoreDataSourceAssetType.URI_ASSET)
99+
vector_store_2 = project_client.agents.create_vector_store_and_poll(data_sources=[ds_2], name="sample_vector_store_2")
100+
print(f"Created vector store, vector store ID: {vector_store.id}")
101+
102+
```
103+
104+
### Step 5: Create a second agent with access to the file search tool
105+
```python
106+
file_search_tool_2 = FileSearchTool(vector_store_ids=[vector_store_2.id])
107+
# notices that FileSearchTool as tool and tool_resources must be added or the assistant unable to search the file
108+
agent_2 = project_client.agents.create_agent(
109+
model="gpt-4o-mini",
110+
name="my-assistant-2",
111+
instructions="You are helpful assistant",
112+
tools=file_search_tool_2.definitions,
113+
tool_resources=file_search_tool_2.resources,
114+
)
115+
# [END upload_file_and_create_agent_with_file_search]
116+
print(f"Created agent, agent ID: {agent_2.id}")
117+
```

0 commit comments

Comments
 (0)