|
| 1 | +## Quickstart – Use existing files in Azure Blob Storage with file search |
| 2 | +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. |
| 3 | + |
| 4 | +### Prerequisites |
| 5 | +Complete the [standard agent setup](../../quickstart.md). |
| 6 | + |
| 7 | +> [!NOTE] |
| 8 | +> Azure Blob Storage is only available with the standard agent setup. The basic agent setup does not support this file source. |
| 9 | +
|
| 10 | +### Step 1: Create a project client |
| 11 | +```python |
| 12 | +import os |
| 13 | +from azure.ai.projects import AIProjectClient |
| 14 | +from azure.ai.projects.models import FileSearchTool, VectorStoreDataSource, VectorStoreDataSourceAssetType |
| 15 | +from azure.identity import DefaultAzureCredential |
| 16 | + |
| 17 | + |
| 18 | +# Create an Azure AI Client from a connection string, copied from your AI Studio project. |
| 19 | +# At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<ProjectName>" |
| 20 | +# Customer needs to login to Azure subscription via Azure CLI and set the environment variables |
| 21 | + |
| 22 | +credential = DefaultAzureCredential() |
| 23 | +project_client = AIProjectClient.from_connection_string( |
| 24 | + credential=credential, conn_str=os.environ["PROJECT_CONNECTION_STRING"] |
| 25 | +) |
| 26 | +``` |
| 27 | +### Step 2: Upload local files to your project Azure Blob Storage container |
| 28 | +We will upload the local file to your project Azure Blob Storage container. This is the same storage account you connected to your agent during the agent setup. |
| 29 | +The project asset ID is the URI of the uploaded file and we print out this value. If you create more agents in the same project that want to use the uploaded file, you can reuse this asset ID. |
| 30 | +That way you don't need to upload the file again. |
| 31 | +```python |
| 32 | +# We will upload the local file to your project Azure Blob Storage container and will use it for vector store creation. |
| 33 | +_, asset_uri = project_client.upload_file("C:\\Users\\fosteramanda\\Downloads\\hub bicep\\azure-ai-agents\\data\\product_info_1.md") |
| 34 | +print(f"Uploaded file, asset URI: {asset_uri}") |
| 35 | + |
| 36 | +# create a vector store with no file and wait for it to be processed |
| 37 | +ds = VectorStoreDataSource(asset_identifier=asset_uri, asset_type=VectorStoreDataSourceAssetType.URI_ASSET) |
| 38 | +vector_store = project_client.agents.create_vector_store_and_poll(data_sources=[ds], name="sample_vector_store-3") |
| 39 | +print(f"Created vector store, vector store ID: {vector_store.id}") |
| 40 | +``` |
| 41 | +### Step 3: Create agent_1 with access to the file search tool |
| 42 | + |
| 43 | +```python |
| 44 | +# create a file search tool |
| 45 | +file_search_tool = FileSearchTool(vector_store_ids=[vector_store.id]) |
| 46 | + |
| 47 | +# notices that FileSearchTool as tool and tool_resources must be added or the assistant unable to search the file |
| 48 | +agent_1 = project_client.agents.create_agent( |
| 49 | + model="gpt-4o-mini", |
| 50 | + name="my-assistant", |
| 51 | + instructions="You are helpful assistant", |
| 52 | + tools=file_search_tool.definitions, |
| 53 | + tool_resources=file_search_tool.resources, |
| 54 | +) |
| 55 | +# [END upload_file_and_create_agent_with_file_search] |
| 56 | +print(f"Created agent_1, agent_1 ID: {agent_1.id}") |
| 57 | + |
| 58 | +thread = project_client.agents.create_thread() |
| 59 | +print(f"Created thread, thread ID: {thread.id}") |
| 60 | + |
| 61 | +message = project_client.agents.create_message( |
| 62 | + thread_id=thread.id, role="user", content="What feature does Smart Eyewear offer?" |
| 63 | +) |
| 64 | +print(f"Created message, message ID: {message.id}") |
| 65 | + |
| 66 | +run = project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent_1.id) |
| 67 | + |
| 68 | +project_client.agents.delete_vector_store(vector_store.id) |
| 69 | +print("Deleted vector store") |
| 70 | + |
| 71 | +project_client.agents.delete_agent(agent.id) |
| 72 | +print("Deleted agent") |
| 73 | + |
| 74 | +messages = project_client.agents.list_messages(thread_id=thread.id) |
| 75 | +print(f"Messages: {messages}") |
| 76 | +``` |
| 77 | + |
| 78 | +### Step 4: Create second vector store using the previously uploaded file |
| 79 | +```python |
| 80 | + |
| 81 | +# create a vector store with no file and wait for it to be processed |
| 82 | +ds_2 = VectorStoreDataSource(asset_identifier=asset_uri, asset_type=VectorStoreDataSourceAssetType.URI_ASSET) |
| 83 | +vector_store_2 = project_client.agents.create_vector_store_and_poll(data_sources=[ds_2], name="sample_vector_store_2") |
| 84 | +print(f"Created vector store, vector store ID: {vector_store.id}") |
| 85 | + |
| 86 | +``` |
| 87 | + |
| 88 | +### Step 5: Create agent_2 with access to the file search tool |
| 89 | +```python |
| 90 | +file_search_tool_2 = FileSearchTool(vector_store_ids=[vector_store_2.id]) |
| 91 | +# notices that FileSearchTool as tool and tool_resources must be added or the assistant unable to search the file |
| 92 | +agent_2 = project_client.agents.create_agent( |
| 93 | + model="gpt-4o-mini", |
| 94 | + name="my-assistant", |
| 95 | + instructions="You are helpful assistant", |
| 96 | + tools=file_search_tool_2.definitions, |
| 97 | + tool_resources=file_search_tool_2.resources, |
| 98 | +) |
| 99 | +# [END upload_file_and_create_agent_with_file_search] |
| 100 | +print(f"Created agent, agent ID: {agent_2.id}") |
| 101 | +``` |
0 commit comments