Skip to content

Commit c02f20b

Browse files
authored
Merge pull request #5940 from aahill/main
Adding python samples
2 parents 69de8bf + d44e3e3 commit c02f20b

14 files changed

+254
-65
lines changed

articles/ai-foundry/agents/how-to/tools/bing-custom-search-samples.md

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Find samples to ground Azure AI Foundry Agents using Custom Bing Se
55
author: aahill
66
ms.author: aahi
77
manager: nitinme
8-
ms.date: 04/15/2025
8+
ms.date: 07/09/2025
99
ms.service: azure-ai-agent-service
1010
ms.topic: how-to
1111
ms.custom:
@@ -16,6 +16,7 @@ zone_pivot_groups: selection-bing-custom-grounding
1616

1717
# How to use Grounding with Bing Custom Search (preview)
1818

19+
Use this article to find step-by-step instructions and code samples for using the Grounding with Bing Custom Search tool in the Azure AI Foundry Agent Service.
1920

2021
::: zone pivot="portal"
2122

@@ -36,18 +37,43 @@ zone_pivot_groups: selection-bing-custom-grounding
3637

3738
:::zone-end
3839

39-
<!--
4040
::: zone pivot="python"
4141

42+
## Prerequisites
43+
44+
* Your Azure AI Foundry Project endpoint.
45+
46+
[!INCLUDE [endpoint-string-portal](../../includes/endpoint-string-portal.md)]
47+
48+
Save this endpoint to an environment variable named `PROJECT_ENDPOINT`.
49+
50+
* The name of your Grounding with Bing Custom Search resource name. You can find it in the Azure AI Foundry portal by selecting **Management center** from the left navigation menu. Then selecting **Connected resources**.
51+
52+
:::image type="content" source="../../media/tools/bing/custom-resource-name.png" alt-text="A screenshot showing the Grounding with Bing Custom Search resource name. " lightbox="../../media/tools/bing/custom-resource-name.png":::
53+
54+
Save this resource name to an environment variable named `BING_CUSTOM_CONNECTION_NAME`.
55+
56+
* The name of your Grounding with Bing Custom Search configuration, which contains the URLs you want to allow or disallow. You can find it by navigating to the overview page for your resource in the [Azure portal](https://portal.azure.com/). Select **Configurations**, then select your configuration.
57+
58+
:::image type="content" source="../../media/tools/bing/custom-connection-name.png" alt-text="A screenshot showing the Grounding with Bing Custom Search connection name. " lightbox="../../media/tools/bing/custom-connection-name.png":::
59+
60+
Save this connection name to an environment variable named `BING_CUSTOM_CONNECTION_NAME`.
61+
62+
* The names of your model's deployment name. You can find it in **Models + Endpoints** in the left navigation menu.
63+
64+
:::image type="content" source="../../media/tools/model-deployment-portal.png" alt-text="A screenshot showing the model deployment screen the AI Foundry portal." lightbox="../../media/tools/model-deployment-portal.png":::
65+
66+
Save the name of your model deployment name as an environment variable named `MODEL_DEPLOYMENT_NAME`.
67+
4268
## Create a project client
4369

4470
Create a client object, which will contain the connection string for connecting to your AI project and other resources.
4571

4672
```python
4773
import os
4874
from azure.ai.projects import AIProjectClient
49-
from azure.ai.projects.models import MessageRole, BingCustomSearchTool
5075
from azure.identity import DefaultAzureCredential
76+
from azure.ai.agents.models import BingCustomSearchTool
5177

5278

5379
# Create an Azure AI Client from an endpoint, copied from your Azure AI Foundry project.
@@ -57,32 +83,34 @@ project_endpoint = os.environ["PROJECT_ENDPOINT"] # Ensure the PROJECT_ENDPOINT
5783
# Create an AIProjectClient instance
5884
project_client = AIProjectClient(
5985
endpoint=project_endpoint,
60-
credential=DefaultAzureCredential(), # Use Azure Default Credential for authentication
61-
api_version="latest",
86+
credential=DefaultAzureCredential(),
6287
)
6388
```
6489

6590

6691
## Create an Agent with the Grounding with Bing Custom Search tool enabled
6792

68-
To make the Grounding with Bing Custom Search tool available to your agent, use a connection to initialize the tool and attach it to the agent. You can find your connection in the **connected resources** section of your project in the [Azure AI Foundry portal](https://ai.azure.com/?cid=learnDocs).
93+
To make the Grounding with Bing Custom Search tool available to your agent, use a connection to initialize the tool and attach it to the agent.
6994

7095
```python
71-
bing_custom_connection = project_client.connections.get(connection_name=os.environ["BING_CUSTOM_CONNECTION_NAME"])
96+
bing_custom_connection = project_client.connections.get(name=os.environ["BING_CUSTOM_CONNECTION_NAME"])
7297
conn_id = bing_custom_connection.id
7398

7499
print(conn_id)
75100

76-
# Initialize agent bing custom search tool and add the connection id
77-
bing_custom_tool = BingCustomSearchTool(connection_id=conn_id, instance_name="<config_instance_name>")
101+
configuration_name = os.environ["BING_CUSTOM_INSTANCE_NAME"]
102+
# Initialize Bing Custom Search tool with connection id and configuration name
103+
bing_custom_tool = BingCustomSearchTool(connection_id=conn_id, instance_name=configuration_name)
78104

79105
# Create agent with the bing custom search tool and process assistant run
80106
with project_client:
81-
agent = project_client.agents.create_agent(
107+
agents_client = project_client.agents
108+
109+
agent = agents_client.create_agent(
82110
model=os.environ["MODEL_DEPLOYMENT_NAME"],
83111
name="my-agent",
84112
instructions="You are a helpful agent",
85-
tools=bing_custom_tool.definitions
113+
tools=bing_custom_tool.definitions,
86114
)
87115
print(f"Created agent, ID: {agent.id}")
88116
```
@@ -91,13 +119,13 @@ with project_client:
91119

92120
```python
93121
# Create thread for communication
94-
thread = project_client.agents.create_thread()
122+
thread = agents_client.threads.create()
95123
print(f"Created thread, ID: {thread.id}")
96124

97125
# Create message to thread
98-
message = project_client.agents.create_message(
126+
message = agents_client.messages.create(
99127
thread_id=thread.id,
100-
role=MessageRole.USER,
128+
role="user",
101129
content="How many medals did the USA win in the 2024 summer olympics?",
102130
)
103131
print(f"Created message, ID: {message.id}")
@@ -109,31 +137,29 @@ Create a run and observe that the model uses the Grounding with Bing Search tool
109137

110138

111139
```python
112-
# Create and process agent run in thread with tools
113-
run = project_client.agents.create_and_process_run(thread_id=thread.id, agent_id=agent.id)
140+
# Create and process Agent run in thread with tools
141+
run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
114142
print(f"Run finished with status: {run.status}")
115143

116144
if run.status == "failed":
117145
print(f"Run failed: {run.last_error}")
118146

119-
# Delete the assistant when done
120-
project_client.agents.delete_agent(agent.id)
121-
print("Deleted agent")
122-
123-
# Print the Agent's response message with optional citation
124-
response_message = project_client.agents.list_messages(thread_id=thread.id).get_last_message_by_role(
125-
MessageRole.AGENT
126-
)
127-
if response_message:
128-
for text_message in response_message.text_messages:
129-
print(f"Agent response: {text_message.text.value}")
130-
for annotation in response_message.url_citation_annotations:
131-
print(f"URL Citation: [{annotation.url_citation.title}]({annotation.url_citation.url})")
147+
# Uncomment these lines to delete the Agent when done
148+
#agents_client.delete_agent(agent.id)
149+
#print("Deleted agent")
150+
151+
# Fetch and log all messages
152+
messages = agents_client.messages.list(thread_id=thread.id)
153+
for msg in messages:
154+
if msg.text_messages:
155+
for text_message in msg.text_messages:
156+
print(f"Agent response: {text_message.text.value}")
157+
for annotation in msg.url_citation_annotations:
158+
print(f"URL Citation: [{annotation.url_citation.title}]({annotation.url_citation.url})")
132159
```
133160

134161

135162
:::zone-end
136-
-->
137163

138164
<!--
139165
::: zone pivot="csharp"

articles/ai-foundry/agents/how-to/tools/deep-research-samples.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ Use this article to learn how to use the Deep Research tool with the Azure AI Pr
2929

3030
* Your Azure AI Foundry Project endpoint.
3131

32-
You can find your endpoint in the main project **overview** for your project in the [Azure AI Foundry portal](https://ai.azure.com/?cid=learnDocs), under **Endpoint and keys** > **Libraries** > **Azure AI Foundry**.
33-
34-
:::image type="content" source="../../media/quickstart/portal-endpoint-string.png" alt-text="A screenshot showing the endpoint in the Azure AI Foundry portal." lightbox="../../media/quickstart/portal-endpoint-string.png":::
32+
33+
[!INCLUDE [endpoint-string-portal](../../includes/endpoint-string-portal.md)]
3534

3635
Save this endpoint to an environment variable named `PROJECT_ENDPOINT`.
3736

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

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to perform data analytics in Azure AI Foundry Agents usin
55
author: aahill
66
ms.author: aahi
77
manager: nitinme
8-
ms.date: 06/17/2025
8+
ms.date: 07/09/2025
99
ms.service: azure-ai-agent-service
1010
ms.topic: how-to
1111
ms.custom:
@@ -23,7 +23,7 @@ You need to first build and publish a Fabric data agent and then connect your Fa
2323

2424
|Azure AI foundry support | Python SDK | C# SDK | JavaScript SDK | REST API |Basic agent setup | Standard agent setup |
2525
|---------|---------|---------|---------|---------|---------|---------|
26-
| ✔️ | | | | ✔️ | ✔️ | ✔️ |
26+
| ✔️ | ✔️ | | | ✔️ | ✔️ | ✔️ |
2727

2828
## Prerequisites
2929
* You have created and published a Fabric data agent endpoint
@@ -34,7 +34,29 @@ You need to first build and publish a Fabric data agent and then connect your Fa
3434

3535
* Your Fabric Data Agent and Azure AI Foundry Agent need to be in the same tenant.
3636

37+
38+
* Your Azure AI Foundry Project endpoint.
39+
40+
[!INCLUDE [endpoint-string-portal](../../includes/endpoint-string-portal.md)]
41+
42+
Save this endpoint to an environment variable named `PROJECT_ENDPOINT`.
43+
44+
45+
* The name of your Microsoft Fabric connection name. You can find it in the Azure AI Foundry portal by selecting **Management center** from the left navigation menu. Then selecting **Connected resources**.
46+
47+
:::image type="content" source="../../media/tools/fabric-connection.png" alt-text="A screenshot showing the SharePoint connection name. " lightbox="../../media/tools/fabric-connection.png":::
48+
49+
Save this endpoint to an environment variable named `FABRIC_CONNECTION_ID`
50+
51+
52+
* The names of your model's deployment name. You can find it in **Models + Endpoints** in the left navigation menu.
53+
54+
:::image type="content" source="../../media/tools/model-deployment-portal.png" alt-text="A screenshot showing the model deployment screen the AI Foundry portal." lightbox="../../media/tools/model-deployment-portal.png":::
55+
56+
Save the name of your model deployment name as an environment variable named `MODEL_DEPLOYMENT_NAME`.
57+
3758
## Setup
59+
3860
> [!NOTE]
3961
> * The model you selected in Azure AI Foundry Agent setup is only used for agent orchestration and response generation. It doesn't impact which model Fabric data agent uses for NL2SQL operation.
4062
> * To help your model invoke your Microsoft Fabric tool in the expected way, make sure you update agent instructions with descriptions of your Fabric data agent and what data it can access. An example is "for customer and product sales related data, please use the Fabric tool". We recommend using a smaller AI model such as `gpt-4o-mini`. You can also use `tool_choice` parameter in SDK or API to force Fabric tool to be invoked at each run.
@@ -65,7 +87,7 @@ You can add the Microsoft Fabric tool to an agent programmatically using the cod
6587
:::image type="content" source="../../media\tools\fabric-foundry.png" alt-text="A screenshot showing the fabric connection in the Azure AI Foundry portal." lightbox="../../media\tools\fabric-foundry.png":::
6688

6789
:::zone-end
68-
<!--
90+
6991
:::zone pivot="python"
7092

7193
## Create a project client
@@ -74,18 +96,17 @@ Create a client object, which will contain the connection string for connecting
7496

7597
```python
7698
import os
77-
from azure.identity import DefaultAzureCredential
7899
from azure.ai.projects import AIProjectClient
79-
from azure.ai.agents.models import FabricTool
100+
from azure.identity import DefaultAzureCredential
101+
from azure.ai.agents.models import FabricTool, ListSortOrder
80102

81103
# Retrieve the endpoint and credentials
82104
project_endpoint = os.environ["PROJECT_ENDPOINT"] # Ensure the PROJECT_ENDPOINT environment variable is set
83105

84106
# Initialize the AIProjectClient
85107
project_client = AIProjectClient(
86108
endpoint=project_endpoint,
87-
credential=DefaultAzureCredential(exclude_interactive_browser_credential=False), # Use Azure Default Credential for authentication
88-
api_version="latest",
109+
credential=DefaultAzureCredential(),
89110
)
90111
```
91112

@@ -104,13 +125,14 @@ conn_id = os.environ["FABRIC_CONNECTION_ID"] # Ensure the FABRIC_CONNECTION_ID
104125
fabric = FabricTool(connection_id=conn_id)
105126

106127
# Create an agent with the Fabric tool
128+
# Create an Agent with the Fabric tool and process an Agent run
107129
with project_client:
108-
agent = project_client.agents.create_agent(
109-
model=os.environ["MODEL_DEPLOYMENT_NAME"], # Model deployment name
110-
name="my-agent", # Name of the agent
111-
instructions="You are a helpful agent", # Instructions for the agent
112-
tools=fabric.definitions, # Attach the Fabric tool
113-
headers={"x-ms-enable-preview": "true"}, # Enable preview features
130+
agents_client = project_client.agents
131+
agent = agents_client.create_agent(
132+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
133+
name="my-agent",
134+
instructions="You are a helpful agent",
135+
tools=fabric.definitions,
114136
)
115137
print(f"Created Agent, ID: {agent.id}")
116138
```
@@ -134,26 +156,27 @@ print(f"Created message, ID: {message['id']}")
134156
## Create a run and check the output
135157

136158
```python
137-
# Create and process an agent run in the thread
138-
run = project_client.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
159+
# Create and process an Agent run in thread with tools
160+
run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
139161
print(f"Run finished with status: {run.status}")
140162

141-
# Check if the run failed
142163
if run.status == "failed":
143164
print(f"Run failed: {run.last_error}")
144165

145-
# Fetch and log all messages from the thread
146-
messages = project_client.agents.messages.list(thread_id=thread.id)
147-
for message in messages.data:
148-
print(f"Role: {message.role}, Content: {message.content}")
166+
# Uncomment the following lines to delete the agent when done
167+
#agents_client.delete_agent(agent.id)
168+
#print("Deleted agent")
149169

150-
# Delete the agent after use
151-
project_client.agents.delete_agent(agent.id)
152-
print("Deleted agent")
170+
# Fetch and log all messages
171+
messages = agents_client.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)
172+
for msg in messages:
173+
if msg.text_messages:
174+
last_text = msg.text_messages[-1]
175+
print(f"{msg.role}: {last_text.text.value}")
153176
```
154177

155178
:::zone-end
156-
-->
179+
157180

158181
<!--
159182
:::zone pivot="csharp"

0 commit comments

Comments
 (0)