-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Grace Brigham <[email protected]>
- Loading branch information
1 parent
28839c7
commit 8239038
Showing
4 changed files
with
223 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
sdk/ai/ai-projects/samples-dev/agents/agents_bing_grounding.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
/** | ||
* | ||
* FILE: agents_bing_grounding.ts | ||
* | ||
* DESCRIPTION: | ||
* This sample demonstrates how to use agent operations with the Grounding with Bing Search tool from | ||
* the Azure Agents service using a asynchronous client. | ||
* | ||
* USAGE: | ||
* npx ts-node agents_bing_grounding.ts | ||
* | ||
* Before running the sample: | ||
* | ||
* npm install @azure/ai-projects @azure/identity @azure/core-util dotenv | ||
* | ||
* Set this environment variables with your own values: | ||
* AZURE_AI_PROJECTS_CONNECTION_STRING - the Azure AI Project connection string, as found in your AI Studio Project | ||
* BING_CONNECTION_NAME - the name of the connection with Bing search grounding | ||
*/ | ||
|
||
import { AIProjectsClient, fromConnectionId, connectionToolType } from "@azure/ai-projects" | ||
import { delay } from "@azure/core-util"; | ||
import { DefaultAzureCredential } from "@azure/identity"; | ||
|
||
import * as dotenv from "dotenv"; | ||
dotenv.config(); | ||
|
||
const connectionString = process.env["AZURE_AI_PROJECTS_CONNECTION_STRING"] || "<endpoint>>;<subscription>;<resource group>;<project>"; | ||
|
||
export async function main(): Promise<void> { | ||
// 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 | ||
const client = AIProjectsClient.fromConnectionString(connectionString || "", new DefaultAzureCredential()); | ||
const bingConnection = await client.connections.getConnection(process.env["BING_CONNECTION_NAME"] || "<connection-name>"); | ||
const connectionId = bingConnection.id; | ||
|
||
// Initialize agent bing tool with the connection id | ||
const bingTool = fromConnectionId(connectionToolType.BingGrounding, [connectionId]); | ||
|
||
// Create agent with the bing tool and process assistant run | ||
const agent = await client.agents.createAgent( | ||
"gpt-4-0125-preview", { | ||
name: "my-agent", | ||
instructions: "You are a helpful agent", | ||
tools: [bingTool] | ||
}, { | ||
headers: {"x-ms-enable-preview": "true"} | ||
}); | ||
console.log(connectionId) | ||
console.log(`Created agent, agent ID : ${agent.id}`); | ||
|
||
// Create thread for communication | ||
const thread = await client.agents.createThread() | ||
console.log(`Created thread, thread ID: ${thread.id}`); | ||
|
||
// Create message to thread | ||
const message = await client.agents.createMessage(thread.id, {role: "user", content: "How does wikipedia explain Euler's Identity?"}); | ||
console.log(`Created message, message ID: ${message.id}`); | ||
|
||
// Create and process agent run in thread with tools | ||
let run = await client.agents.createRun(thread.id, agent.id); | ||
while (run.status === "queued" || run.status === "in_progress") { | ||
await delay(1000); | ||
run = await client.agents.getRun(thread.id, run.id); | ||
} | ||
if (run.status === "failed") { | ||
console.log(`Run failed: ${run.last_error}`); | ||
} | ||
console.log(`Run finished with status: ${run.status}`); | ||
|
||
// Delete the assistant when done | ||
client.agents.deleteAgent(agent.id) | ||
console.log(`Deleted agent, agent ID: ${agent.id}`); | ||
|
||
// Fetch and log all messages | ||
const messages = await client.agents.listMessages(thread.id) | ||
console.log(`Messages:`); | ||
messages.data.forEach((m) => console.log(m.content)); | ||
} | ||
|
||
main().catch((err) => { | ||
console.error("The sample encountered an error:", err); | ||
}); |
99 changes: 99 additions & 0 deletions
99
sdk/ai/ai-projects/samples-dev/agents/agents_bing_grounding_streaming.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
/** | ||
* | ||
* FILE: agents_bing_grounding_streaming.ts | ||
* | ||
* DESCRIPTION: | ||
* This sample demonstrates how to use agent operations with the Grounding with Bing Search tool from | ||
* the Azure Agents service using a asynchronous client and streaming. | ||
* | ||
* USAGE: | ||
* npx ts-node agents_bing_grounding_streaming.ts | ||
* | ||
* Before running the sample: | ||
* | ||
* npm install @azure/ai-projects @azure/identity @azure/core-util dotenv | ||
* | ||
* Set this environment variables with your own values: | ||
* AZURE_AI_PROJECTS_CONNECTION_STRING - the Azure AI Project connection string, as found in your AI Studio Project | ||
* BING_CONNECTION_NAME - the name of the connection with Bing search grounding | ||
*/ | ||
|
||
import { AIProjectsClient, DoneEvent, ErrorEvent, MessageDeltaChunk, MessageDeltaTextContent, MessageStreamEvent, RunStreamEvent, ThreadRunOutput, fromConnectionId, connectionToolType } from "@azure/ai-projects" | ||
import { DefaultAzureCredential } from "@azure/identity"; | ||
|
||
import * as dotenv from "dotenv"; | ||
dotenv.config(); | ||
|
||
const connectionString = process.env["AZURE_AI_PROJECTS_CONNECTION_STRING"] || "<endpoint>>;<subscription>;<resource group>;<project>"; | ||
|
||
export async function main(): Promise<void> { | ||
const client = AIProjectsClient.fromConnectionString(connectionString || "", new DefaultAzureCredential()); | ||
const bingConnection = await client.connections.getConnection(process.env["BING_CONNECTION_NAME"] || "<connection-name>"); | ||
const connectionId = bingConnection.id; | ||
|
||
const bingTool = fromConnectionId(connectionToolType.BingGrounding, [connectionId]); | ||
|
||
const agent = await client.agents.createAgent( | ||
"gpt-4-0125-preview", { | ||
name: "my-agent", | ||
instructions: "You are a helpful agent", | ||
tools: [bingTool] | ||
}, { | ||
headers: {"x-ms-enable-preview": "true"} | ||
}); | ||
console.log(connectionId) | ||
console.log(`Created agent, agent ID : ${agent.id}`); | ||
|
||
const thread = await client.agents.createThread() | ||
console.log(`Created thread, thread ID: ${thread.id}`); | ||
|
||
const message = await client.agents.createMessage(thread.id, {role: "user", content: "How does wikipedia explain Euler's Identity?"}); | ||
console.log(`Created message, message ID: ${message.id}`); | ||
|
||
const streamEventMessages = await client.agents.createRunStreaming(thread.id, agent.id); | ||
|
||
for await (const eventMessage of streamEventMessages) { | ||
switch (eventMessage.event) { | ||
case RunStreamEvent.ThreadRunCreated: | ||
console.log(`ThreadRun status: ${(eventMessage.data as ThreadRunOutput).status}`) | ||
break; | ||
case MessageStreamEvent.ThreadMessageDelta: | ||
{ | ||
const messageDelta = eventMessage.data as MessageDeltaChunk; | ||
messageDelta.delta.content.forEach((contentPart) => { | ||
if (contentPart.type === "text") { | ||
const textContent = contentPart as MessageDeltaTextContent | ||
const textValue = textContent.text?.value || "No text" | ||
console.log(`Text delta received:: ${textValue}`) | ||
} | ||
}); | ||
} | ||
break; | ||
|
||
case RunStreamEvent.ThreadRunCompleted: | ||
console.log("Thread Run Completed"); | ||
break; | ||
case ErrorEvent.Error: | ||
console.log(`An error occurred. Data ${eventMessage.data}`); | ||
break; | ||
case DoneEvent.Done: | ||
console.log("Stream completed."); | ||
break; | ||
} | ||
} | ||
|
||
client.agents.deleteAgent(agent.id) | ||
console.log(`Deleted agent, agent ID: ${agent.id}`); | ||
|
||
const messages = await client.agents.listMessages(thread.id) | ||
console.log(`Messages:`); | ||
messages.data.forEach((m) => console.log(m.content)); | ||
} | ||
|
||
main().catch((err) => { | ||
console.error("The sample encountered an error:", err); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters