Skip to content

Commit

Permalink
add tooling utilities and azure AI search sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Grace Brigham committed Dec 4, 2024
1 parent 419844f commit f266b3e
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 37 deletions.
26 changes: 23 additions & 3 deletions sdk/ai/ai-projects/review/ai-projects.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,29 @@ export interface CreateAndRunThreadOptions {
truncation_strategy?: TruncationObject | null;
}

// @public
export function createAzureAISearchTool(indexConnectionId: string, indexName: string): {
definition: AzureAISearchToolDefinition[];
resources: ToolResources;
};

// @public
export function createCodeInterpreterTool(fileIds?: string[], dataSources?: Array<VectorStoreDataSource>): {
definition: CodeInterpreterToolDefinition[];
resources: ToolResources;
};

// @public
export function createConnectionTool(toolType: connectionToolType, connectionIds: string[]): {
definition: ToolDefinition[];
};

// @public
export function createFileSearchTool(vectorStoreIds?: string[], vectorStores?: Array<VectorStoreConfigurations>, definitionDetails?: FileSearchToolDefinitionDetails): {
definition: FileSearchToolDefinition[];
resources: ToolResources;
};

// @public
export interface CreateRunOptions {
additional_instructions?: string | null;
Expand Down Expand Up @@ -538,9 +561,6 @@ export type Frequency = string;
// @public
export type FrequencyOutput = string;

// @public
export function fromConnectionId(toolType: connectionToolType, connectionIds: string[]): ToolDefinitionParent;

// @public
export function fromFunctionDefinition(functionDefintion: FunctionDefinition): FunctionToolDefinition;

Expand Down
91 changes: 91 additions & 0 deletions sdk/ai/ai-projects/samples-dev/agents/agents_azure_ai_search.ts
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);
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* 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.
* the Azure Agents service using a client.
*
* USAGE:
* npx ts-node agents_bing_grounding.ts
Expand All @@ -21,7 +21,7 @@
* BING_CONNECTION_NAME - the name of the connection with Bing search grounding
*/

import { AIProjectsClient, fromConnectionId, connectionToolType, MessageContentOutput, isOutputOfType, MessageTextContentOutput } from "@azure/ai-projects"
import { AIProjectsClient, createConnectionTool, connectionToolType, MessageContentOutput, isOutputOfType, MessageTextContentOutput } from "@azure/ai-projects"
import { delay } from "@azure/core-util";
import { DefaultAzureCredential } from "@azure/identity";

Expand All @@ -39,18 +39,17 @@ export async function main(): Promise<void> {
const connectionId = bingConnection.id;

// Initialize agent bing tool with the connection id
const bingTool = fromConnectionId(connectionToolType.BingGrounding, [connectionId]);
const bingTool = createConnectionTool(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]
tools: bingTool.definition
}, {
headers: {"x-ms-enable-preview": "true"}
});
console.log(connectionId)
console.log(`Created agent, agent ID : ${agent.id}`);

// Create thread for communication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* 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.
* the Azure Agents service using a client and streaming.
*
* USAGE:
* npx ts-node agents_bing_grounding_streaming.ts
Expand All @@ -22,7 +22,7 @@
* BING_CONNECTION_NAME - the name of the connection with Bing search grounding
*/

import { AIProjectsClient, DoneEvent, ErrorEvent, MessageDeltaChunk, MessageDeltaTextContent, MessageStreamEvent, RunStreamEvent, ThreadRunOutput, fromConnectionId, connectionToolType, MessageContentOutput, isOutputOfType, MessageTextContentOutput } from "@azure/ai-projects"
import { AIProjectsClient, DoneEvent, ErrorEvent, MessageDeltaChunk, MessageDeltaTextContent, MessageStreamEvent, RunStreamEvent, ThreadRunOutput, createConnectionTool, connectionToolType, MessageContentOutput, isOutputOfType, MessageTextContentOutput } from "@azure/ai-projects"
import { DefaultAzureCredential } from "@azure/identity";

import * as dotenv from "dotenv";
Expand All @@ -35,17 +35,16 @@ export async function main(): Promise<void> {
const bingConnection = await client.connections.getConnection(process.env["BING_CONNECTION_NAME"] || "<connection-name>");
const connectionId = bingConnection.id;

const bingTool = fromConnectionId(connectionToolType.BingGrounding, [connectionId]);
const bingTool = createConnectionTool(connectionToolType.BingGrounding, [connectionId]);

const agent = await client.agents.createAgent(
"gpt-4-0125-preview", {
name: "my-agent",
instructions: "You are a helpful agent",
tools: [bingTool]
tools: bingTool.definition
}, {
headers: {"x-ms-enable-preview": "true"}
});
console.log(connectionId)
console.log(`Created agent, agent ID : ${agent.id}`);

const thread = await client.agents.createThread()
Expand Down
8 changes: 4 additions & 4 deletions sdk/ai/ai-projects/samples-dev/agents/agents_fabric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* DESCRIPTION:
* This sample demonstrates how to use agent operations with the Microsoft Fabric tool from
* the Azure Agents service using a asynchronous client.
* the Azure Agents service using a client.
*
* USAGE:
* npx ts-node agents_fabric.ts
Expand All @@ -21,7 +21,7 @@
* FABRIC_CONNECTION_NAME
*/

import { AIProjectsClient, fromConnectionId, connectionToolType, MessageContentOutput, isOutputOfType, MessageTextContentOutput } from "@azure/ai-projects";
import { AIProjectsClient, createConnectionTool, connectionToolType, MessageContentOutput, isOutputOfType, MessageTextContentOutput } from "@azure/ai-projects";
import { delay } from "@azure/core-util";
import { DefaultAzureCredential } from "@azure/identity";

Expand All @@ -39,14 +39,14 @@ export async function main(): Promise<void> {
const connectionId = fabricConnection.id;

// Initialize agent Microsoft Fabric tool with the connection id
const fabricTool = fromConnectionId(connectionToolType.MicrosoftFabric, [connectionId]);
const fabricTool = createConnectionTool(connectionToolType.MicrosoftFabric, [connectionId]);

// Create agent with the Microsoft Fabric 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: [fabricTool]
tools: fabricTool.definition
}, {
headers: {"x-ms-enable-preview": "true"}
});
Expand Down
8 changes: 4 additions & 4 deletions sdk/ai/ai-projects/samples-dev/agents/agents_sharepoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* DESCRIPTION:
* This sample demonstrates how to use agent operations with the Sharepoint tool from
* the Azure Agents service using a asynchronous client.
* the Azure Agents service using a client.
*
* USAGE:
* npx ts-node agents_sharepoint.ts
Expand All @@ -21,7 +21,7 @@
* SHAREPOINT_CONNECTION_NAME
*/

import { AIProjectsClient, fromConnectionId, connectionToolType, MessageContentOutput, isOutputOfType, MessageTextContentOutput } from "@azure/ai-projects";
import { AIProjectsClient, createConnectionTool, connectionToolType, MessageContentOutput, isOutputOfType, MessageTextContentOutput } from "@azure/ai-projects";
import { delay } from "@azure/core-util";
import { DefaultAzureCredential } from "@azure/identity";

Expand All @@ -39,14 +39,14 @@ export async function main(): Promise<void> {
const connectionId = sharepointConnection.id;

// Initialize agent Sharepoint tool with the connection id
const sharepointTool = fromConnectionId(connectionToolType.SharepointGrounding, [connectionId]);
const sharepointTool = createConnectionTool(connectionToolType.SharepointGrounding, [connectionId]);

// Create agent with the Sharepoint 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: [sharepointTool]
tools: sharepointTool.definition
}, {
headers: {"x-ms-enable-preview": "true"}
});
Expand Down
9 changes: 6 additions & 3 deletions sdk/ai/ai-projects/samples-dev/agents/code_interpreter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import {AIProjectsClient, isOutputOfType, CodeInterpreterToolDefinition, MessageTextContentOutput, ToolResources, MessageImageFileContentOutput, MessageContentOutput } from "@azure/ai-projects"
import {AIProjectsClient, isOutputOfType, MessageTextContentOutput, MessageImageFileContentOutput, createCodeInterpreterTool } from "@azure/ai-projects"
import { DefaultAzureCredential } from "@azure/identity";

import * as dotenv from "dotenv";
Expand All @@ -19,12 +19,15 @@ const localFile = await client.agents.uploadFile(localFileStream, "assistants",

console.log(`Uploaded local file, file ID : ${localFile.id}`);

// Create code interpreter tool
const codeInterpreterTool = createCodeInterpreterTool([localFile.id]);

// Notice that CodeInterpreter must be enabled in the agent creation, otherwise the agent will not be able to see the file attachment
const agent = await client.agents.createAgent("gpt-4o-mini", {
name: "my-agent",
instructions: "You are a helpful agent",
tools: [{type: "code_interpreter"} as CodeInterpreterToolDefinition],
tool_resources: { code_interpreter: {file_ids: [localFile.id]} } as ToolResources
tools: codeInterpreterTool.definition,
tool_resources: codeInterpreterTool.resources,
});
console.log(`Created agent, agent ID: ${agent.id}`);

Expand Down
11 changes: 7 additions & 4 deletions sdk/ai/ai-projects/samples-dev/agents/file_search.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { AIProjectsClient, isOutputOfType, MessageContentOutput, MessageImageFileContentOutput, MessageTextContentOutput } from "@azure/ai-projects";
import { AIProjectsClient, createFileSearchTool, isOutputOfType, MessageContentOutput, MessageImageFileContentOutput, MessageTextContentOutput } from "@azure/ai-projects";
import { delay } from "@azure/core-util";
import { DefaultAzureCredential } from "@azure/identity";

Expand All @@ -23,14 +23,17 @@ export async function main(): Promise<void> {
const vectorStore = await client.agents.createVectorStore({ file_ids: [file.id], name: "my_vector_store" });
console.log(`Created vector store, ID: ${vectorStore.id}`);

// Create agent with files
// Create file search tool
const fileSearchTool = createFileSearchTool([vectorStore.id]);

// Create agent with tool
const agent = await client.agents.createAgent(
"gpt-4o",
{
name:"SDK Test Agent - Retrieval",
instructions:"You are helpful agent that can help fetch data from files you know about.",
tools: [{type: "file_search" }],
tool_resources: {file_search: {vector_store_ids: [vectorStore.id]} }
tools: fileSearchTool.definition,
tool_resources: fileSearchTool.resources
}
);
console.log(`Created agent, agent ID : ${agent.id}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ export async function main(): Promise<void> {
console.log(`Retrieved workspace, workspace name: ${workspace.name}`);

// List the details of all the connections
const connections = await client.connections.listConnections();
const connections = await client.connections.listConnections({category: "AzureOpenAI"});
console.log(`Retrieved ${connections.value.length} connections`);
for (const connection of connections.value) {
console.log(connection);
}

// Get the details of a connection, without credentials
const connectionName = connections.value[0].name;
Expand Down
Loading

0 comments on commit f266b3e

Please sign in to comment.