Skip to content

Commit 25095ae

Browse files
glharperCopilot
andauthored
[Projects] add v2 bing grounding sample (#36620)
### Packages impacted by this PR ### Issues associated with this PR ### Describe the problem that is addressed by this PR ### What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen? ### Are there test cases added in this PR? _(If not, why?)_ ### Provide a list of related PRs _(if any)_ ### Command used to generate this PR:**_(Applicable only to SDK release request PRs)_ ### Checklists - [ ] Added impacted package name to the issue description - [ ] Does this PR needs any fixes in the SDK Generator?** _(If so, create an Issue in the [Autorest/typescript](https://github.com/Azure/autorest.typescript) repository and link it here)_ - [ ] Added a changelog (if necessary) --------- Co-authored-by: Copilot <[email protected]>
1 parent 04de48a commit 25095ae

File tree

5 files changed

+316
-0
lines changed

5 files changed

+316
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* This sample demonstrates how to create an AI agent with Bing grounding capabilities
6+
* using the "bing_grounding" tool type and synchronous Azure AI Projects client. The agent can search
7+
* the web for current information and provide grounded responses with URL citations.
8+
*
9+
* @summary This sample demonstrates how to create an agent with Bing grounding tool capabilities,
10+
* search the web for current information, and process streaming responses with source citations.
11+
*
12+
* @azsdk-weight 100
13+
*/
14+
15+
import { DefaultAzureCredential } from "@azure/identity";
16+
import { AIProjectClient } from "@azure/ai-projects";
17+
import "dotenv/config";
18+
19+
const projectEndpoint = process.env["AZURE_AI_PROJECT_ENDPOINT"] || "<project endpoint>";
20+
const deploymentName = process.env["AZURE_AI_MODEL_DEPLOYMENT_NAME"] || "<model deployment name>";
21+
const bingProjectConnectionId =
22+
process.env["BING_PROJECT_CONNECTION_ID"] || "<bing project connection id>";
23+
24+
export async function main(): Promise<void> {
25+
const project = new AIProjectClient(projectEndpoint, new DefaultAzureCredential());
26+
const openAIClient = await project.getOpenAIClient();
27+
28+
console.log("Creating agent with Bing grounding tool...");
29+
30+
const agent = await project.agents.createVersion("MyBingGroundingAgent", {
31+
kind: "prompt",
32+
model: deploymentName,
33+
instructions: "You are a helpful assistant.",
34+
tools: [
35+
{
36+
type: "bing_grounding",
37+
bing_grounding: {
38+
search_configurations: [
39+
{
40+
project_connection_id: bingProjectConnectionId,
41+
},
42+
],
43+
},
44+
},
45+
],
46+
});
47+
console.log(`Agent created (id: ${agent.id}, name: ${agent.name}, version: ${agent.version})`);
48+
49+
// Send request that requires current information from the web
50+
console.log("\nSending request to Bing grounding agent with streaming...");
51+
const streamResponse = await openAIClient.responses.create(
52+
{
53+
input: "What is today's date and weather in Seattle?",
54+
stream: true,
55+
},
56+
{
57+
body: {
58+
agent: { name: agent.name, type: "agent_reference" },
59+
tool_choice: "required",
60+
},
61+
},
62+
);
63+
64+
// Process the streaming response
65+
for await (const event of streamResponse) {
66+
if (event.type === "response.created") {
67+
console.log(`Follow-up response created with ID: ${event.response.id}`);
68+
} else if (event.type === "response.output_text.delta") {
69+
process.stdout.write(event.delta);
70+
} else if (event.type === "response.output_text.done") {
71+
console.log("\n\nFollow-up response done!");
72+
} else if (event.type === "response.output_item.done") {
73+
if (event.item.type === "message") {
74+
const item = event.item;
75+
if (item.content && item.content.length > 0) {
76+
const lastContent = item.content[item.content.length - 1];
77+
if (lastContent.type === "output_text" && lastContent.annotations) {
78+
for (const annotation of lastContent.annotations) {
79+
if (annotation.type === "url_citation") {
80+
console.log(
81+
`URL Citation: ${annotation.url}, Start index: ${annotation.start_index}, End index: ${annotation.end_index}`,
82+
);
83+
}
84+
}
85+
}
86+
}
87+
}
88+
} else if (event.type === "response.completed") {
89+
console.log("\nFollow-up completed!");
90+
}
91+
}
92+
93+
// Clean up resources by deleting the agent version
94+
// This prevents accumulation of unused resources in your project
95+
console.log("\nCleaning up resources...");
96+
await project.agents.deleteVersion(agent.name, agent.version);
97+
console.log("Agent deleted");
98+
99+
console.log("\nBing grounding agent sample completed!");
100+
}
101+
102+
main().catch((err) => {
103+
console.error("The sample encountered an error:", err);
104+
});

sdk/ai/ai-projects/samples/v2-beta/javascript/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ These sample programs show how to use the JavaScript client libraries for Azure
1919
| [agents/tools/agentAgentToAgent.js][agents_tools_agentagenttoagent] | This sample demonstrates how to create an agent with A2A tool capabilities, enable inter-agent communication, and process streaming responses. |
2020
| [agents/tools/agentAiSearch.js][agents_tools_agentaisearch] | This sample demonstrates how to create an agent with Azure AI Search tool capabilities, send queries to search indexed content, and process streaming responses with citations. |
2121
| [agents/tools/agentBingCustomSearch.js][agents_tools_agentbingcustomsearch] | This sample demonstrates how to create an agent with Bing Custom Search tool capabilities, search custom search instances, and process streaming responses with citations. |
22+
| [agents/tools/agentBingGrounding.js][agents_tools_agentbinggrounding] | This sample demonstrates how to create an agent with Bing grounding tool capabilities, search the web for current information, and process streaming responses with source citations. |
2223
| [agents/tools/agentBrowserAutomation.js][agents_tools_agentbrowserautomation] | This sample demonstrates how to create an agent with Browser Automation tool, perform web browsing tasks, and process streaming responses with browser automation events. |
2324
| [agents/tools/agentComputerUse.js][agents_tools_agentcomputeruse] | This sample demonstrates how to create a Computer Use Agent that can interact with computer interfaces through simulated actions and screenshots. |
2425
| [agents/tools/agentFabric.js][agents_tools_agentfabric] | This sample demonstrates how to create an agent with Microsoft Fabric tool capabilities, send queries to Fabric data sources, and clean up resources. |
@@ -87,6 +88,7 @@ Take a look at our [API Documentation][apiref] for more information about the AP
8788
[agents_tools_agentagenttoagent]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/ai/ai-projects/samples/v2-beta/javascript/agents/tools/agentAgentToAgent.js
8889
[agents_tools_agentaisearch]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/ai/ai-projects/samples/v2-beta/javascript/agents/tools/agentAiSearch.js
8990
[agents_tools_agentbingcustomsearch]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/ai/ai-projects/samples/v2-beta/javascript/agents/tools/agentBingCustomSearch.js
91+
[agents_tools_agentbinggrounding]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/ai/ai-projects/samples/v2-beta/javascript/agents/tools/agentBingGrounding.js
9092
[agents_tools_agentbrowserautomation]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/ai/ai-projects/samples/v2-beta/javascript/agents/tools/agentBrowserAutomation.js
9193
[agents_tools_agentcomputeruse]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/ai/ai-projects/samples/v2-beta/javascript/agents/tools/agentComputerUse.js
9294
[agents_tools_agentfabric]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/ai/ai-projects/samples/v2-beta/javascript/agents/tools/agentFabric.js
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* This sample demonstrates how to create an AI agent with Bing grounding capabilities
6+
* using the `bing_grounding` tool type and synchronous Azure AI Projects client. The agent can search
7+
* the web for current information and provide grounded responses with URL citations.
8+
*
9+
* @summary This sample demonstrates how to create an agent with Bing grounding tool capabilities,
10+
* search the web for current information, and process streaming responses with source citations.
11+
*/
12+
13+
const { DefaultAzureCredential } = require("@azure/identity");
14+
const { AIProjectClient } = require("@azure/ai-projects");
15+
require("dotenv/config");
16+
17+
const projectEndpoint = process.env["AZURE_AI_PROJECT_ENDPOINT"] || "<project endpoint>";
18+
const deploymentName = process.env["AZURE_AI_MODEL_DEPLOYMENT_NAME"] || "<model deployment name>";
19+
const bingProjectConnectionId =
20+
process.env["BING_PROJECT_CONNECTION_ID"] || "<bing project connection id>";
21+
22+
async function main() {
23+
const project = new AIProjectClient(projectEndpoint, new DefaultAzureCredential());
24+
const openAIClient = await project.getOpenAIClient();
25+
26+
console.log("Creating agent with Bing grounding tool...");
27+
28+
const agent = await project.agents.createVersion("MyBingGroundingAgent", {
29+
kind: "prompt",
30+
model: deploymentName,
31+
instructions: "You are a helpful assistant.",
32+
tools: [
33+
{
34+
type: "bing_grounding",
35+
bing_grounding: {
36+
search_configurations: [
37+
{
38+
project_connection_id: bingProjectConnectionId,
39+
},
40+
],
41+
},
42+
},
43+
],
44+
});
45+
console.log(`Agent created (id: ${agent.id}, name: ${agent.name}, version: ${agent.version})`);
46+
47+
// Send request that requires current information from the web
48+
console.log("\nSending request to Bing grounding agent with streaming...");
49+
const streamResponse = await openAIClient.responses.create(
50+
{
51+
input: "What is today's date and weather in Seattle?",
52+
stream: true,
53+
},
54+
{
55+
body: {
56+
agent: { name: agent.name, type: "agent_reference" },
57+
tool_choice: "required",
58+
},
59+
},
60+
);
61+
62+
// Process the streaming response
63+
for await (const event of streamResponse) {
64+
if (event.type === "response.created") {
65+
console.log(`Follow-up response created with ID: ${event.response.id}`);
66+
} else if (event.type === "response.output_text.delta") {
67+
process.stdout.write(event.delta);
68+
} else if (event.type === "response.output_text.done") {
69+
console.log("\n\nFollow-up response done!");
70+
} else if (event.type === "response.output_item.done") {
71+
if (event.item.type === "message") {
72+
const item = event.item;
73+
if (item.content && item.content.length > 0) {
74+
const lastContent = item.content[item.content.length - 1];
75+
if (lastContent.type === "output_text" && lastContent.annotations) {
76+
for (const annotation of lastContent.annotations) {
77+
if (annotation.type === "url_citation") {
78+
console.log(
79+
`URL Citation: ${annotation.url}, Start index: ${annotation.start_index}, End index: ${annotation.end_index}`,
80+
);
81+
}
82+
}
83+
}
84+
}
85+
}
86+
} else if (event.type === "response.completed") {
87+
console.log("\nFollow-up completed!");
88+
}
89+
}
90+
91+
// Clean up resources by deleting the agent version
92+
// This prevents accumulation of unused resources in your project
93+
console.log("\nCleaning up resources...");
94+
await project.agents.deleteVersion(agent.name, agent.version);
95+
console.log("Agent deleted");
96+
97+
console.log("\nBing grounding agent sample completed!");
98+
}
99+
100+
main().catch((err) => {
101+
console.error("The sample encountered an error:", err);
102+
});
103+
104+
module.exports = { main };

sdk/ai/ai-projects/samples/v2-beta/typescript/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ These sample programs show how to use the TypeScript client libraries for Azure
1919
| [agents/tools/agentAgentToAgent.ts][agents_tools_agentagenttoagent] | This sample demonstrates how to create an agent with A2A tool capabilities, enable inter-agent communication, and process streaming responses. |
2020
| [agents/tools/agentAiSearch.ts][agents_tools_agentaisearch] | This sample demonstrates how to create an agent with Azure AI Search tool capabilities, send queries to search indexed content, and process streaming responses with citations. |
2121
| [agents/tools/agentBingCustomSearch.ts][agents_tools_agentbingcustomsearch] | This sample demonstrates how to create an agent with Bing Custom Search tool capabilities, search custom search instances, and process streaming responses with citations. |
22+
| [agents/tools/agentBingGrounding.ts][agents_tools_agentbinggrounding] | This sample demonstrates how to create an agent with Bing grounding tool capabilities, search the web for current information, and process streaming responses with source citations. |
2223
| [agents/tools/agentBrowserAutomation.ts][agents_tools_agentbrowserautomation] | This sample demonstrates how to create an agent with Browser Automation tool, perform web browsing tasks, and process streaming responses with browser automation events. |
2324
| [agents/tools/agentComputerUse.ts][agents_tools_agentcomputeruse] | This sample demonstrates how to create a Computer Use Agent that can interact with computer interfaces through simulated actions and screenshots. |
2425
| [agents/tools/agentFabric.ts][agents_tools_agentfabric] | This sample demonstrates how to create an agent with Microsoft Fabric tool capabilities, send queries to Fabric data sources, and clean up resources. |
@@ -99,6 +100,7 @@ Take a look at our [API Documentation][apiref] for more information about the AP
99100
[agents_tools_agentagenttoagent]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/ai/ai-projects/samples/v2-beta/typescript/src/agents/tools/agentAgentToAgent.ts
100101
[agents_tools_agentaisearch]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/ai/ai-projects/samples/v2-beta/typescript/src/agents/tools/agentAiSearch.ts
101102
[agents_tools_agentbingcustomsearch]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/ai/ai-projects/samples/v2-beta/typescript/src/agents/tools/agentBingCustomSearch.ts
103+
[agents_tools_agentbinggrounding]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/ai/ai-projects/samples/v2-beta/typescript/src/agents/tools/agentBingGrounding.ts
102104
[agents_tools_agentbrowserautomation]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/ai/ai-projects/samples/v2-beta/typescript/src/agents/tools/agentBrowserAutomation.ts
103105
[agents_tools_agentcomputeruse]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/ai/ai-projects/samples/v2-beta/typescript/src/agents/tools/agentComputerUse.ts
104106
[agents_tools_agentfabric]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/ai/ai-projects/samples/v2-beta/typescript/src/agents/tools/agentFabric.ts
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* This sample demonstrates how to create an AI agent with Bing grounding capabilities
6+
* using the bing_grounding tool type and synchronous Azure AI Projects client. The agent can search
7+
* the web for current information and provide grounded responses with URL citations.
8+
*
9+
* @summary This sample demonstrates how to create an agent with Bing grounding tool capabilities,
10+
* search the web for current information, and process streaming responses with source citations.
11+
*
12+
* @azsdk-weight 100
13+
*/
14+
15+
import { DefaultAzureCredential } from "@azure/identity";
16+
import { AIProjectClient } from "@azure/ai-projects";
17+
import "dotenv/config";
18+
19+
const projectEndpoint = process.env["AZURE_AI_PROJECT_ENDPOINT"] || "<project endpoint>";
20+
const deploymentName = process.env["AZURE_AI_MODEL_DEPLOYMENT_NAME"] || "<model deployment name>";
21+
const bingProjectConnectionId =
22+
process.env["BING_PROJECT_CONNECTION_ID"] || "<bing project connection id>";
23+
24+
export async function main(): Promise<void> {
25+
const project = new AIProjectClient(projectEndpoint, new DefaultAzureCredential());
26+
const openAIClient = await project.getOpenAIClient();
27+
28+
console.log("Creating agent with Bing grounding tool...");
29+
30+
const agent = await project.agents.createVersion("MyBingGroundingAgent", {
31+
kind: "prompt",
32+
model: deploymentName,
33+
instructions: "You are a helpful assistant.",
34+
tools: [
35+
{
36+
type: "bing_grounding",
37+
bing_grounding: {
38+
search_configurations: [
39+
{
40+
project_connection_id: bingProjectConnectionId,
41+
},
42+
],
43+
},
44+
},
45+
],
46+
});
47+
console.log(`Agent created (id: ${agent.id}, name: ${agent.name}, version: ${agent.version})`);
48+
49+
// Send request that requires current information from the web
50+
console.log("\nSending request to Bing grounding agent with streaming...");
51+
const streamResponse = await openAIClient.responses.create(
52+
{
53+
input: "What is today's date and weather in Seattle?",
54+
stream: true,
55+
},
56+
{
57+
body: {
58+
agent: { name: agent.name, type: "agent_reference" },
59+
tool_choice: "required",
60+
},
61+
},
62+
);
63+
64+
// Process the streaming response
65+
for await (const event of streamResponse) {
66+
if (event.type === "response.created") {
67+
console.log(`Follow-up response created with ID: ${event.response.id}`);
68+
} else if (event.type === "response.output_text.delta") {
69+
process.stdout.write(event.delta);
70+
} else if (event.type === "response.output_text.done") {
71+
console.log("\n\nFollow-up response done!");
72+
} else if (event.type === "response.output_item.done") {
73+
if (event.item.type === "message") {
74+
const item = event.item;
75+
if (item.content && item.content.length > 0) {
76+
const lastContent = item.content[item.content.length - 1];
77+
if (lastContent.type === "output_text" && lastContent.annotations) {
78+
for (const annotation of lastContent.annotations) {
79+
if (annotation.type === "url_citation") {
80+
console.log(
81+
`URL Citation: ${annotation.url}, Start index: ${annotation.start_index}, End index: ${annotation.end_index}`,
82+
);
83+
}
84+
}
85+
}
86+
}
87+
}
88+
} else if (event.type === "response.completed") {
89+
console.log("\nFollow-up completed!");
90+
}
91+
}
92+
93+
// Clean up resources by deleting the agent version
94+
// This prevents accumulation of unused resources in your project
95+
console.log("\nCleaning up resources...");
96+
await project.agents.deleteVersion(agent.name, agent.version);
97+
console.log("Agent deleted");
98+
99+
console.log("\nBing grounding agent sample completed!");
100+
}
101+
102+
main().catch((err) => {
103+
console.error("The sample encountered an error:", err);
104+
});

0 commit comments

Comments
 (0)