Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions scenarios/samples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Azure AI Agent Service Samples

## Getting Started
* [Quickstart](https://github.com/Azure/azure-ai-agents/blob/main/quickstart.md)
* [Use Llama models with Agents](https://github.com/Azure/azure-ai-agents/blob/main/samples/llama-3.md)
* [Use OpenAI Python SDK with Azure AI Agent Service](https://github.com/Azure/azure-ai-agents/blob/main/samples/use-openai.md)

## E2E Samples
* [Azure AI Travel Agent](https://github.com/Azure-Samples/azureai-travel-agent-python/tree/main)
* [Sample App using Azure AI Agents](https://github.com/Azure-Samples/azure-ai-projects-file-search)

## Tool Specific Samples
* [File Search](https://github.com/Azure/azure-ai-agents/tree/main/samples/file-search)
* [Code Interpreter](https://github.com/Azure/azure-ai-agents/tree/main/samples/code-interpreter)
* [Bing Search](https://github.com/Azure/azure-ai-agents/tree/main/samples/bing-search)
* [Function Calling](https://github.com/Azure/azure-ai-agents/tree/main/samples/function-calling)

## Other Samples
* [Tracing using Application Insights](https://github.com/Azure/azure-ai-agents/tree/main/samples/tracing)
* [Personal Finance Agent](https://github.com/Azure/azure-ai-agents/tree/main/samples/investment_advisor)
90 changes: 90 additions & 0 deletions scenarios/samples/bing-search/BingSearch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable disable

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.TestFramework;
using NUnit.Framework;

namespace Azure.AI.Projects.Tests;

public partial class Sample_Agent_Bing_Grounding : SamplesBase<AIProjectsTestEnvironment>
{
[Test]
public async Task BingGroundingExample()
{
var connectionString = TestEnvironment.AzureAICONNECTIONSTRING;

var clientOptions = new AIProjectClientOptions();

// Adding the custom headers policy
clientOptions.AddPolicy(new CustomHeadersPolicy(), HttpPipelinePosition.PerCall);
var projectClient = new AIProjectClient(connectionString, new DefaultAzureCredential(), clientOptions);

GetConnectionResponse bingConnection = await projectClient.GetConnectionsClient().GetConnectionAsync(TestEnvironment.BINGCONNECTIONNAME);
var connectionId = bingConnection.Id;

AgentsClient agentClient = projectClient.GetAgentsClient();

ToolConnectionList connectionList = new ToolConnectionList
{
ConnectionList = { new ToolConnection(connectionId) }
};
BingGroundingToolDefinition bingGroundingTool = new BingGroundingToolDefinition(connectionList);

Response<Agent> agentResponse = await agentClient.CreateAgentAsync(
model: "gpt-4-1106-preview",
name: "my-assistant",
instructions: "You are a helpful assistant.",
tools: new List<ToolDefinition> { bingGroundingTool });
Agent agent = agentResponse.Value;

// Create thread for communication
Response<AgentThread> threadResponse = await agentClient.CreateThreadAsync();
AgentThread thread = threadResponse.Value;

// Create message to thread
Response<ThreadMessage> messageResponse = await agentClient.CreateMessageAsync(
thread.Id,
MessageRole.User,
"How does wikipedia explain Euler's Identity?");
ThreadMessage message = messageResponse.Value;

// Run the agent
Response<ThreadRun> runResponse = await agentClient.CreateRunAsync(thread, agent);

do
{
await Task.Delay(TimeSpan.FromMilliseconds(500));
runResponse = await agentClient.GetRunAsync(thread.Id, runResponse.Value.Id);
}
while (runResponse.Value.Status == RunStatus.Queued
|| runResponse.Value.Status == RunStatus.InProgress);

Response<PageableList<ThreadMessage>> afterRunMessagesResponse
= await agentClient.GetMessagesAsync(thread.Id);
IReadOnlyList<ThreadMessage> messages = afterRunMessagesResponse.Value.Data;

// Note: messages iterate from newest to oldest, with the messages[0] being the most recent
foreach (ThreadMessage threadMessage in messages)
{
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: ");
foreach (MessageContent contentItem in threadMessage.ContentItems)
{
if (contentItem is MessageTextContent textItem)
{
Console.Write(textItem.Text);
}
else if (contentItem is MessageImageFileContent imageFileItem)
{
Console.Write($"<image from ID: {imageFileItem.FileId}");
}
Console.WriteLine();
}
}
}
}
87 changes: 87 additions & 0 deletions scenarios/samples/bing-search/bing-python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------

"""
FILE: sample_agents_bing_grounding.py

DESCRIPTION:
This sample demonstrates how to use agent operations with the Grounding with Bing Search tool from
the Azure Agents service using a synchronous client.

USAGE:
python sample_agents_bing_grounding.py

Before running the sample:

pip install azure.ai.projects azure-identity

Set this environment variables with your own values:
PROJECT_CONNECTION_STRING - the Azure AI Project connection string, as found in your AI Studio Project.
BING_CONNECTION_NAME - the name of the connection of Grounding with Bing Search

"""

import os
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from azure.ai.projects.models import BingGroundingTool


# Create an Azure AI Client from a connection string, copied from your AI Studio project.
# At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<HubName>"
# Customer needs to login to Azure subscription via Azure CLI and set the environment variables

project_client = AIProjectClient.from_connection_string(
credential=DefaultAzureCredential(),
conn_str='eastus.api.azureml.ms;921496dc-987f-410f-bd57-426eb2611356;rg-test16;project-demo-iopw',
)

bing_connection = project_client.connections.get(
connection_name='hub-demo-iopw-connection-Bing'
)
conn_id = bing_connection.id

print(conn_id)

# Initialize agent bing tool and add the connection id
bing = BingGroundingTool(connection_id=conn_id)

# Create agent with the bing tool and process assistant run
with project_client:
agent = project_client.agents.create_agent(
model="gpt-4o",
name="my-assistant",
instructions="You are a helpful assistant",
tools=bing.definitions,
headers={"x-ms-enable-preview": "true"}
)
print(f"Created agent, ID: {agent.id}")

# Create thread for communication
thread = project_client.agents.create_thread()
print(f"Created thread, ID: {thread.id}")

# Create message to thread
message = project_client.agents.create_message(
thread_id=thread.id,
role="user",
content="How is the weather in Seattle today?",
)
print(f"Created message, ID: {message.id}")

# Create and process agent run in thread with tools
run = project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent.id)
print(f"Run finished with status: {run.status}")

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

# Delete the assistant when done
project_client.agents.delete_agent(agent.id)
print("Deleted agent")

# Fetch and log all messages
messages = project_client.agents.list_messages(thread_id=thread.id)
print(f"Messages: {messages}")
24 changes: 24 additions & 0 deletions scenarios/samples/bing-search/bingsearch_arm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"name": { "type": "String" },
"location": {
"type": "String",
"defaultValue": "global"
},
"sku": { "type": "String" },
"resourceTags": { "type": "object" }
},
"resources": [
{
"type": "Microsoft.Bing/accounts",
"apiVersion": "2020-06-10",
"name": "[parameters('name')]",
"location": "[parameters('location')]",
"tags": "[parameters('resourceTags')]",
"sku": { "name": "G1" },
"kind": "Bing.Grounding"
}
]
}
21 changes: 21 additions & 0 deletions scenarios/samples/bing-search/bingsearch_para.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"name": {
"value": "BING_RESOURCE_NAME"},
"sku": {
"value": "G1"
},
"location": {
"value": "GLOBAL"
},
"resourceTags": {
"value": {
"Name1": "Value1",
"Name2": "Value2",
"Name3": "Value3"
}
}
}
}
70 changes: 70 additions & 0 deletions scenarios/samples/bing-search/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Grounding with Bing Search

Grounding with Bing Search allows your Azure AI Agents to incorporate real-time public web data when generating responses. To start with, you need to create a Grounding with Bing Search resource, then connect this resource to your Azure AI Agents. When a user sends a query, Azure AI Agents will decide if Grounding with Bing Search should be leveraged or not. If so, it will leverage Bing to search over public web data and return relevant chunks. Lastly, Azure AI Agents will use returned chunks to generate a response.

Citations show links to websites used to generate response, but don’t show links to the bing query used for the search. Developers and end users don’t have access to raw content returned from Grounding with Bing Search.

You can ask questions such as "what is the weather in Seattle?" "what is the recent update in ratail industry in the US?" that require real-time public data.

> [!IMPORTANT]
Grounding with Bing Search is a free service during private preview and your usage will start incurring cost since the integration with Azure AI Agent Service releases to public preview.

## Setup

> [!IMPORTANT]
> 1. Grounding with Bing Search has a separate [Terms of Use agreement](https://www.microsoft.com/en-us/bing/apis/grounding-legal-preview) you need to agree to in order to move forward. Please [use this form](https://forms.office.com/r/2j3Sgu8S9K) to sign the agreement. After you have signed the form, it will take 1-3 days for us to whitelist your subscription.
> 2. Please make sure your resource is created in `EastUS`.
> 3. We recommend using the following models: `gpt-3.5-turbo-0125`, `gpt-4-0125-preview`, `gpt-4-turbo-preview`, `gpt-4-turbo`, `gpt-4-turbo-2024-04-09`, `gpt-4o`, `gpt-4o-mini`, `gpt-4o-mini-2024-07-18`

1. Ensure you've completed the prerequisites and setup steps in the [quickstart](../../quickstart.md).

1. Ensure you have loged in to Azure, using `az login`

1. Register the Bing Search provider
```console
az provider register --namespace 'Microsoft.Bing'
```

1. Create a new Grounding with Bing Search resource. You can find the the template file [here](./bingsearch_arm.json) and parameter file [here](./bingsearch_para.json). Make sure you have replace "BING_RESOURCE_NAME" in the parameter file. You can use Azure CLI command:

```console
az deployment group create​ 
--name "$deployment_name"​ 
--resource-group "$resource_group"​ 
--template-file "$path_to_arm_template_file"​ 
--parameters "$path_to_parameters_file";​ 
```
An example of the CLI command:
```console
az deployment group create​ 
--name az-cli-ARM-TEST
--resource-group ApiSearch-Test-WestUS2
--template-file bingsearch_arm.json
--parameters bingsearch_para.json
```
Make sure you have created this Grounding with Bing Search resource in the same resource group of your Azure AI Agent, AI Project, etc.
1. After you have created a Grounding with Bing Search resource, you can find it in [Azure Portal](https://ms.portal.azure.com/#home). Going to the resource group you have created the resource at, search for the Grounding with Bing Search resource you have created.
![image](https://github.com/user-attachments/assets/3b22c48d-987c-4234-a9eb-67aefe3af81c)
1. Click the Grounding with Bing Search resource you have created and copy any of the API key
![image](https://github.com/user-attachments/assets/be98e07d-c91d-4ff9-a97c-6f02c3265221)
1. Go to [Azure AI Studio](https://ai.azure.com/) and select the AI Project(make sure it's in the same resource group of your Grounding with Bing Search resource). Click Settings and then "+new connection" button in Settings page
![image](https://github.com/user-attachments/assets/28bfebda-f3a4-4638-b714-a128a8fa48cb)
![image](https://github.com/user-attachments/assets/7bb9c98e-dd46-4031-be9d-17c70613f222)
1. Select "API key" custom connection in other resource types
![image](https://github.com/user-attachments/assets/7577c912-cf0f-433a-910b-3d9e0ad138c4)
1. Enter the following information and then create a new connection to your Grounding with Bing Search resource
- Endpoint: https://api.bing.microsoft.com/
- Key: YOUR_API_KEY
- Connection name: YOUR_CONNECTION_NAME (You will use this connection name in the sample code below.)
- Access: you can choose either "this project only" or "shared to all projects". Just make sure in the sample code below, the project you entered connection string for has access to this connection.


## Examples

Run the code samples below and view the output.

>[!NOTE]
> Be sure that you've [installed the SDK](../../quickstart.md#install-the-sdk-package) for your language.

* [Python](./bing-python.py)
* [C#](./BingSearch.cs)
Loading
Loading