Skip to content

Commit 94f9537

Browse files
committed
python updates
1 parent 9e24218 commit 94f9537

File tree

9 files changed

+655
-185
lines changed

9 files changed

+655
-185
lines changed

articles/ai-services/agents/how-to/tools/fabric.md

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,18 @@ Create a client object, which will contain the connection string for connecting
173173

174174
```python
175175
import os
176-
from azure.ai.projects import AIProjectClient
177176
from azure.identity import DefaultAzureCredential
178-
from azure.ai.projects.models import FabricTool
177+
from azure.ai.projects import AIProjectClient
178+
from azure.ai.agents.models import FabricTool
179179

180-
# Create an Azure AI Client from a connection string, copied from your Azure AI Foundry project.
181-
# At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<ProjectName>"
182-
# Customer needs to login to Azure subscription via Azure CLI and set the environment variables
180+
# Retrieve the endpoint and credentials
181+
project_endpoint = os.environ["PROJECT_ENDPOINT"] # Ensure the PROJECT_ENDPOINT environment variable is set
183182

184-
credential = DefaultAzureCredential()
185-
project_client = AIProjectClient.from_connection_string(
186-
credential=credential, conn_str=os.environ["PROJECT_CONNECTION_STRING"]
183+
# Initialize the AIProjectClient
184+
project_client = AIProjectClient(
185+
endpoint=project_endpoint,
186+
credential=DefaultAzureCredential(exclude_interactive_browser_credential=False), # Use Azure Default Credential for authentication
187+
api_version="latest",
187188
)
188189
```
189190

@@ -194,57 +195,60 @@ To make the Microsoft Fabric tool available to your agent, use a connection to i
194195
```python
195196
# The Fabric connection id can be found in the Azure AI Foundry project as a property of the Fabric tool
196197
# Your connection id is in the format /subscriptions/<your-subscription-id>/resourceGroups/<your-resource-group>/providers/Microsoft.MachineLearningServices/workspaces/<your-project-name>/connections/<your-fabric-connection-name>
197-
conn_id = "your-connection-id"
198198

199-
# Initialize agent fabric tool and add the connection id
199+
# Retrieve the Fabric connection ID from environment variables
200+
conn_id = os.environ["FABRIC_CONNECTION_ID"] # Ensure the FABRIC_CONNECTION_ID environment variable is set
201+
202+
# Initialize the FabricTool with the connection ID
200203
fabric = FabricTool(connection_id=conn_id)
201204

202-
# Create agent with the fabric tool and process agent run
205+
# Create an agent with the Fabric tool
203206
with project_client:
204207
agent = project_client.agents.create_agent(
205-
model="gpt-4o",
206-
name="my-assistant",
207-
instructions="You are a helpful assistant",
208-
tools=fabric.definitions,
209-
headers={"x-ms-enable-preview": "true"},
208+
model=os.environ["MODEL_DEPLOYMENT_NAME"], # Model deployment name
209+
name="my-agent", # Name of the agent
210+
instructions="You are a helpful agent", # Instructions for the agent
211+
tools=fabric.definitions, # Attach the Fabric tool
212+
headers={"x-ms-enable-preview": "true"}, # Enable preview features
210213
)
211-
print(f"Created agent, ID: {agent.id}")
214+
print(f"Created Agent, ID: {agent.id}")
212215
```
213216

214217
## Step 3: Create a thread
215218

216219
```python
217-
# Create thread for communication
218-
thread = project_client.agents.create_thread()
220+
# Create a thread for communication
221+
thread = project_client.agents.threads.create()
219222
print(f"Created thread, ID: {thread.id}")
220223

221-
# Create message to thread
222-
# Remember to update the message with your data
223-
message = project_client.agents.create_message(
224+
# Create a message in the thread
225+
message = project_client.agents.messages.create(
224226
thread_id=thread.id,
225-
role="user",
226-
content="what is top sold product in Contoso last month?",
227+
role="user", # Role of the message sender
228+
content="What insights can you provide from the Fabric resource?", # Message content
227229
)
228-
print(f"Created message, ID: {message.id}")
230+
print(f"Created message, ID: {message['id']}")
229231
```
230232

231233
## Step 4: Create a run and check the output
232234

233235
```python
234-
# Create and process agent run in thread with tools
235-
run = project_client.agents.create_and_process_run(thread_id=thread.id, agent_id=agent.id)
236+
# Create and process an agent run in the thread
237+
run = project_client.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
236238
print(f"Run finished with status: {run.status}")
237239

240+
# Check if the run failed
238241
if run.status == "failed":
239242
print(f"Run failed: {run.last_error}")
240243

241-
# Delete the assistant when done
244+
# Fetch and log all messages from the thread
245+
messages = project_client.agents.messages.list(thread_id=thread.id)
246+
for message in messages.data:
247+
print(f"Role: {message.role}, Content: {message.content}")
248+
249+
# Delete the agent after use
242250
project_client.agents.delete_agent(agent.id)
243251
print("Deleted agent")
244-
245-
# Fetch and log all messages
246-
messages = project_client.agents.list_messages(thread_id=thread.id)
247-
print(f"Messages: {messages}")
248252
```
249253

250254
:::zone-end

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

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -29,97 +29,94 @@ In this example, we use Azure AI Foundry Agent Service to create an agent that c
2929
> File search using Blob storage is only supported by the standard agent setup.
3030
3131
### Step 1: Create a project client
32+
```python
33+
### Step 1: Create a project client
34+
3235
```python
3336
import os
3437
from azure.ai.projects import AIProjectClient
35-
from azure.ai.projects.models import FileSearchTool, VectorStoreDataSource, VectorStoreDataSourceAssetType
3638
from azure.identity import DefaultAzureCredential
3739

40+
# Retrieve the endpoint from environment variables
41+
endpoint = os.environ["PROJECT_ENDPOINT"]
3842

39-
# Create an Azure AI Client from a connection string, copied from your Azure AI Foundry project.
40-
# At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<ProjectName>"
41-
# Customer needs to login to Azure subscription via Azure CLI and set the environment variables
42-
43-
credential = DefaultAzureCredential()
44-
project_client = AIProjectClient.from_connection_string(
45-
credential=credential, conn_str=os.environ["PROJECT_CONNECTION_STRING"]
43+
# Initialize the AIProjectClient
44+
project_client = AIProjectClient(
45+
endpoint=endpoint,
46+
credential=DefaultAzureCredential(exclude_interactive_browser_credential=False),
4647
)
4748
```
4849

49-
### Step 2: Upload local files to your project Azure Blob Storage container
50-
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.
50+
### Step 2: Use an existing file in Azure Blob Storage
51+
52+
Use the `asset_uri` of a file already in Azure Blob Storage to create a vector store. This 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.
5153

52-
Then, create a vector store using the ```asset_uri```, which is the location of your file in your project's datastore.
5354
```python
54-
# We'll upload the local file to your project Azure Blob Storage container and will use it for vector store creation.
55-
_, asset_uri = project_client.upload_file("sample_file_for_upload.md")
56-
print(f"Uploaded file, asset URI: {asset_uri}")
55+
from azure.ai.agents.models import VectorStoreDataSource, VectorStoreDataSourceAssetType
56+
57+
# Define the asset URI for the file in Azure Blob Storage
58+
asset_uri = os.environ["AZURE_BLOB_URI"]
5759

58-
# create a vector store with a file in blob storage and wait for it to be processed
59-
ds = VectorStoreDataSource(asset_identifier=asset_uri, asset_type=VectorStoreDataSourceAssetType.URI_ASSET)
60-
vector_store = project_client.agents.create_vector_store_and_poll(data_sources=[ds], name="sample_vector_store")
60+
# Create a vector store using the asset URI
61+
data_source = VectorStoreDataSource(asset_identifier=asset_uri, asset_type=VectorStoreDataSourceAssetType.URI_ASSET)
62+
vector_store = project_client.agents.create_vector_store_and_poll(data_sources=[data_source], name="sample_vector_store")
6163
print(f"Created vector store, vector store ID: {vector_store.id}")
6264
```
6365

6466
### Step 3: Create an agent with access to the file search tool
6567

6668
```python
67-
# create a file search tool
69+
from azure.ai.agents.models import FileSearchTool
70+
71+
# Create a file search tool using the vector store
6872
file_search_tool = FileSearchTool(vector_store_ids=[vector_store.id])
6973

70-
# notices that FileSearchTool as tool and tool_resources must be added or the assistant unable to search the file
71-
agent_1 = project_client.agents.create_agent(
72-
model="gpt-4o-mini",
73-
name="my-assistant",
74-
instructions="You are helpful assistant",
74+
# Create an agent with the file search tool
75+
agent = project_client.agents.create_agent(
76+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
77+
name="my-agent",
78+
instructions="You are a helpful assistant",
7579
tools=file_search_tool.definitions,
7680
tool_resources=file_search_tool.resources,
7781
)
78-
# [END upload_file_and_create_agent_with_file_search]
79-
print(f"Created agent_1, agent_1 ID: {agent_1.id}")
82+
print(f"Created agent, agent ID: {agent.id}")
83+
```
84+
85+
### Step 4: Create a thread and send a message
8086

87+
```python
88+
# Create a thread for communication
8189
thread = project_client.agents.create_thread()
8290
print(f"Created thread, thread ID: {thread.id}")
8391

92+
# Send a message to the thread
8493
message = project_client.agents.create_message(
85-
thread_id=thread.id, role="user", content="What feature does Smart Eyewear offer?"
94+
thread_id=thread.id, role="user", content="What does the file say?"
8695
)
8796
print(f"Created message, message ID: {message.id}")
97+
```
98+
99+
### Step 5: Create a run and check the output
88100

89-
run = project_client.agents.create_and_process_run(thread_id=thread.id, agent_id=agent_1.id)
101+
Create a run and observe that the model uses the file search tool to provide a response.
90102

103+
```python
104+
# Create and process a run with the specified thread and agent
105+
run = project_client.agents.create_and_process_run(thread_id=thread.id, agent_id=agent.id)
106+
print(f"Run finished with status: {run.status}")
107+
108+
# Check if the run failed
109+
if run.status == "failed":
110+
print(f"Run failed: {run.last_error}")
111+
112+
# Cleanup resources
91113
project_client.agents.delete_vector_store(vector_store.id)
92114
print("Deleted vector store")
93115

94116
project_client.agents.delete_agent(agent.id)
95117
print("Deleted agent")
96118

119+
# List and print all messages in the thread
97120
messages = project_client.agents.list_messages(thread_id=thread.id)
98121
print(f"Messages: {messages}")
99-
```
100-
101-
### Step 4: Create second vector store using the previously uploaded file
102-
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.
103-
```python
104-
105-
# create a vector store with a previously uploaded file and wait for it to be processed
106-
ds_2 = VectorStoreDataSource(asset_identifier=asset_uri, asset_type=VectorStoreDataSourceAssetType.URI_ASSET)
107-
vector_store_2 = project_client.agents.create_vector_store_and_poll(data_sources=[ds_2], name="sample_vector_store_2")
108-
print(f"Created vector store, vector store ID: {vector_store.id}")
109-
110-
```
111-
112-
### Step 5: Create a second agent with access to the file search tool
113-
```python
114-
file_search_tool_2 = FileSearchTool(vector_store_ids=[vector_store_2.id])
115-
# notices that FileSearchTool as tool and tool_resources must be added or the assistant unable to search the file
116-
agent_2 = project_client.agents.create_agent(
117-
model="gpt-4o-mini",
118-
name="my-assistant-2",
119-
instructions="You are helpful assistant",
120-
tools=file_search_tool_2.definitions,
121-
tool_resources=file_search_tool_2.resources,
122-
)
123-
# [END upload_file_and_create_agent_with_file_search]
124-
print(f"Created agent, agent ID: {agent_2.id}")
125122
```

0 commit comments

Comments
 (0)