-
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.
add tooling utilities and azure AI search sample
- Loading branch information
Grace Brigham
committed
Dec 4, 2024
1 parent
419844f
commit f266b3e
Showing
10 changed files
with
220 additions
and
37 deletions.
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
91 changes: 91 additions & 0 deletions
91
sdk/ai/ai-projects/samples-dev/agents/agents_azure_ai_search.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,91 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
/** | ||
* | ||
* FILE: agents_azure_ai_search.ts | ||
* | ||
* DESCRIPTION: | ||
* This sample demonstrates how to use agent operations with the | ||
* Azure AI Search tool from the Azure Agents service using a synchronous client. | ||
* | ||
* USAGE: | ||
* npx ts-node agents_azure_ai_search.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 | ||
* AZURE_AI_SEARCH_CONNECTION_NAME - the name of the connection with Azure AI search, must be a CognitiveSearch connection | ||
*/ | ||
|
||
import { AIProjectsClient, MessageContentOutput, isOutputOfType, MessageTextContentOutput, createAzureAISearchTool } 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 connectionName = process.env["AZURE_AI_SEARCH_CONNECTION_NAME"] || "<connection-name>"; | ||
const connection = await client.connections.getConnection(connectionName); | ||
|
||
// Initialize Azure AI Search tool | ||
const azureAISearchTool = createAzureAISearchTool(connection.id, connection.name) | ||
|
||
// Create agent with the Azure AI search tool | ||
const agent = await client.agents.createAgent( | ||
"gpt-4-0125-preview", { | ||
name: "my-agent", | ||
instructions: "You are a helpful agent", | ||
tools: azureAISearchTool.definition, | ||
tool_resources: azureAISearchTool.resources | ||
}, { | ||
headers: {"x-ms-enable-preview": "true"} | ||
}); | ||
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: "Hello, send an email with the datetime and weather information in New York"}); | ||
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:`); | ||
const agentMessage: MessageContentOutput = messages.data[0].content[0]; | ||
if (isOutputOfType<MessageTextContentOutput>(agentMessage, "text")) { | ||
const textContent = agentMessage as MessageTextContentOutput; | ||
console.log(`Text Message Content - ${textContent.text.value}`); | ||
} | ||
} | ||
|
||
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
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
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
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
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
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
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
Oops, something went wrong.