Skip to content

Commit

Permalink
Fabric and Sharepoint samples (#32033)
Browse files Browse the repository at this point in the history
Co-authored-by: Grace Brigham <[email protected]>
  • Loading branch information
GraceBrigham and Grace Brigham authored Dec 4, 2024
1 parent 30706a0 commit fda5672
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sdk/ai/ai-projects/review/ai-projects.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export interface ConnectionsOperations {
export enum connectionToolType {
BingGrounding = "bing_grounding",
MicrosoftFabric = "microsoft_fabric",
SharePointGrounding = "sharepoint_grounding"
SharepointGrounding = "sharepoint_grounding"
}

// @public
Expand Down
91 changes: 91 additions & 0 deletions sdk/ai/ai-projects/samples-dev/agents/agents_fabric.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_fabric.ts
*
* DESCRIPTION:
* This sample demonstrates how to use agent operations with the Microsoft Fabric tool from
* the Azure Agents service using a asynchronous client.
*
* USAGE:
* npx ts-node agents_fabric.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
* FABRIC_CONNECTION_NAME
*/

import { AIProjectsClient, fromConnectionId, connectionToolType, MessageContentOutput, isOutputOfType, MessageTextContentOutput } 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 fabricConnection = await client.connections.getConnection(process.env["FABRIC_CONNECTION_NAME"] || "<connection-name>");
const connectionId = fabricConnection.id;

// Initialize agent Microsoft Fabric tool with the connection id
const fabricTool = fromConnectionId(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]
}, {
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: "What inventory is currently available?"});
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);
});
91 changes: 91 additions & 0 deletions sdk/ai/ai-projects/samples-dev/agents/agents_sharepoint.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_sharepoint.ts
*
* DESCRIPTION:
* This sample demonstrates how to use agent operations with the Sharepoint tool from
* the Azure Agents service using a asynchronous client.
*
* USAGE:
* npx ts-node agents_sharepoint.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
* SHAREPOINT_CONNECTION_NAME
*/

import { AIProjectsClient, fromConnectionId, connectionToolType, MessageContentOutput, isOutputOfType, MessageTextContentOutput } 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 sharepointConnection = await client.connections.getConnection(process.env["SHAREPOINT_CONNECTION_NAME"] || "<connection-name>");
const connectionId = sharepointConnection.id;

// Initialize agent Sharepoint tool with the connection id
const sharepointTool = fromConnectionId(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]
}, {
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: "Hello, tell me about my health insurance options"});
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);
});
2 changes: 1 addition & 1 deletion sdk/ai/ai-projects/src/agents/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export enum connectionToolType {
/** Microsoft Fabric tool */
MicrosoftFabric = "microsoft_fabric",
/** Sharepoint tool */
SharePointGrounding = "sharepoint_grounding",
SharepointGrounding = "sharepoint_grounding",
}

/**
Expand Down

0 comments on commit fda5672

Please sign in to comment.