Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Tracing for Agents #32020

Merged
merged 13 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion sdk/ai/ai-projects/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,22 @@
"@azure/core-lro": "^2.0.0",
"tslib": "^2.6.2",
"@azure/core-paging": "^1.5.0",
"@azure/core-sse": "^2.1.3"
"@azure/core-sse": "^2.1.3",
"@azure/core-tracing": "^1.2.0"
},
"devDependencies": {
"@azure/dev-tool": "^1.0.0",
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@azure/identity": "^4.3.0",
"@azure/opentelemetry-instrumentation-azure-sdk": "^1.0.0-beta.7",
"@azure/monitor-opentelemetry-exporter": "^1.0.0-beta.27",
"@azure-tools/test-credential": "^2.0.0",
"@azure-tools/test-recorder": "^4.1.0",
"@azure-tools/test-utils-vitest": "^1.0.0",
"@microsoft/api-extractor": "^7.40.3",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/instrumentation": "0.53.0",
"@opentelemetry/sdk-trace-node": "^1.9.0",
"@vitest/browser": "^2.0.5",
"@vitest/coverage-istanbul": "^2.0.5",
"@types/node": "^18.0.0",
Expand Down Expand Up @@ -114,18 +121,22 @@
"./package.json": "./package.json",
".": {
"browser": {
"source": "./src/index.ts",
"types": "./dist/browser/index.d.ts",
"default": "./dist/browser/index.js"
},
"react-native": {
"source": "./src/index.ts",
"types": "./dist/react-native/index.d.ts",
"default": "./dist/react-native/index.js"
},
"import": {
"source": "./src/index.ts",
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"source": "./src/index.ts",
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
Expand Down
8 changes: 8 additions & 0 deletions sdk/ai/ai-projects/review/ai-projects.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export class AIProjectsClient {
readonly agents: AgentsOperations;
readonly connections: ConnectionsOperations;
static fromConnectionString(connectionString: string, credential: TokenCredential, options?: AIProjectsClientOptions): AIProjectsClient;
readonly telemetry: TelemetryOperations;
}

// @public (undocumented)
Expand Down Expand Up @@ -1458,6 +1459,13 @@ export interface SystemDataOutput {
readonly lastModifiedAt?: string;
}

// @public
export interface TelemetryOperations {
getConnectionString(): Promise<string>;
ganeshyb marked this conversation as resolved.
Show resolved Hide resolved
// Warning: (ae-forgotten-export) The symbol "TelemetryOptions" needs to be exported by the entry point index.d.ts
updateSettings(options: TelemetryOptions): void;
}

// @public
export interface ThreadDeletionStatusOutput {
deleted: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.


import { context } from "@opentelemetry/api";
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { createAzureSdkInstrumentation } from "@azure/opentelemetry-instrumentation-azure-sdk";
import { AzureMonitorTraceExporter } from "@azure/monitor-opentelemetry-exporter"
import {
ConsoleSpanExporter,
NodeTracerProvider,
SimpleSpanProcessor,
} from "@opentelemetry/sdk-trace-node";

import * as dotenv from "dotenv";
dotenv.config();

const provider = new NodeTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.register();

registerInstrumentations({
instrumentations: [createAzureSdkInstrumentation()],
});

import { AIProjectsClient } from "@azure/ai-projects"
import { DefaultAzureCredential } from "@azure/identity";

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 appInsightsConnectionString = await client.telemetry.getConnectionString();
if (appInsightsConnectionString) {
const exporter = new AzureMonitorTraceExporter({ connectionString: appInsightsConnectionString });
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
}

const agent = await client.agents.createAgent("gpt-4o", { name: "my-agent", instructions: "You are helpful agent" }, { tracingOptions: { tracingContext: context.active() } });


console.log(`Created agent, agent ID : ${agent.id}`);

await client.agents.deleteAgent(agent.id);

console.log(`Deleted agent`);
}

main().catch((err) => {
console.error("The sample encountered an error:", err);
});
118 changes: 62 additions & 56 deletions sdk/ai/ai-projects/src/agents/assistants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { Client, createRestError } from "@azure-rest/core-client";
import { AgentDeletionStatusOutput, AgentOutput, OpenAIPageableListOfAgentOutput } from "../generated/src/outputModels.js";
import { CreateAgentParameters, DeleteAgentParameters, GetAgentParameters, ListAgentsParameters, UpdateAgentParameters } from "../generated/src/parameters.js";
import { validateLimit, validateMetadata, validateOrder, validateVectorStoreDataType } from "./inputValidations.js";

import { TracingUtility } from "../tracing.js";
import { traceEndCreateAgent, traceStartCreateAgent } from "./assistantsTrace.js";

const expectedStatuses = ["200"];

Expand All @@ -21,80 +22,85 @@ enum Tools {

/** Creates a new agent. */
export async function createAgent(
context: Client,
options: CreateAgentParameters,
): Promise<AgentOutput> {
validateCreateAgentParameters(options);
const result = await context.path("/assistants").post(options);
if (!expectedStatuses.includes(result.status)) {
context: Client,
options: CreateAgentParameters,
): Promise<AgentOutput> {
validateCreateAgentParameters(options);
return TracingUtility.withSpan("CreateAgent", options,
async (updatedOptions) => {
const result = await context.path("/assistants").post(updatedOptions);
if (!expectedStatuses.includes(result.status)) {
throw createRestError(result);
}
return result.body;
}
}
return result.body as AgentOutput;
ganeshyb marked this conversation as resolved.
Show resolved Hide resolved
},
traceStartCreateAgent, traceEndCreateAgent,
);
}

/** Gets a list of agents that were previously created. */
export async function listAgents(
context: Client,
options?: ListAgentsParameters,
): Promise<OpenAIPageableListOfAgentOutput> {
validateListAgentsParameters(options);
const result = await context
/** Gets a list of agents that were previously created. */
export async function listAgents(
context: Client,
options?: ListAgentsParameters,
): Promise<OpenAIPageableListOfAgentOutput> {
validateListAgentsParameters(options);
ganeshyb marked this conversation as resolved.
Show resolved Hide resolved
const result = await context
.path("/assistants")
.get(options);
if (!expectedStatuses.includes(result.status)) {
throw createRestError(result);
}
return result.body;
if (!expectedStatuses.includes(result.status)) {
throw createRestError(result);
}
return result.body;
}

/** Retrieves an existing agent. */
/** Retrieves an existing agent. */
export async function getAgent(
context: Client,
assistantId: string,
options?: GetAgentParameters,
): Promise<AgentOutput> {
validateAssistantId(assistantId);
const result = await context
context: Client,
assistantId: string,
options?: GetAgentParameters,
): Promise<AgentOutput> {
validateAssistantId(assistantId);
const result = await context
.path("/assistants/{assistantId}", assistantId)
.get(options);
if (!expectedStatuses.includes(result.status)) {
throw createRestError(result);
}
return result.body;
if (!expectedStatuses.includes(result.status)) {
throw createRestError(result);
}
return result.body;
}

/** Modifies an existing agent. */
/** Modifies an existing agent. */
export async function updateAgent(
context: Client,
assistantId: string,
options?: UpdateAgentParameters,
): Promise<AgentOutput> {
validateUpdateAgentParameters(assistantId, options);
const result = await context
context: Client,
assistantId: string,
options?: UpdateAgentParameters,
): Promise<AgentOutput> {
validateUpdateAgentParameters(assistantId, options);
const result = await context
.path("/assistants/{assistantId}", assistantId)
.post(options);
if (!expectedStatuses.includes(result.status)) {
throw createRestError(result);
}
return result.body;
if (!expectedStatuses.includes(result.status)) {
throw createRestError(result);
}
return result.body;
}


/** Deletes an agent. */
/** Deletes an agent. */
export async function deleteAgent(
context: Client,
assistantId: string,
options?: DeleteAgentParameters,
): Promise<AgentDeletionStatusOutput> {
validateAssistantId(assistantId);
const result = await context
context: Client,
assistantId: string,
options?: DeleteAgentParameters,
): Promise<AgentDeletionStatusOutput> {
validateAssistantId(assistantId);
const result = await context
.path("/assistants/{assistantId}", assistantId)
.delete(options);
if (!expectedStatuses.includes(result.status)) {
throw createRestError(result);
}
return result.body;
if (!expectedStatuses.includes(result.status)) {
throw createRestError(result);
}
return result.body;
}

function validateCreateAgentParameters(options: CreateAgentParameters | UpdateAgentParameters): void {
if (options.body.tools) {
Expand Down Expand Up @@ -132,7 +138,7 @@ function validateCreateAgentParameters(options: CreateAgentParameters | UpdateAg
}
}
if (options.body.temperature && (options.body.temperature < 0 || options.body.temperature > 2)) {
throw new Error("Temperature must be between 0 and 2");
throw new Error("Temperature must be between 0 and 2");
}
if (options.body.metadata) {
validateMetadata(options.body.metadata);
Expand All @@ -150,7 +156,7 @@ function validateListAgentsParameters(options?: ListAgentsParameters): void {

function validateAssistantId(assistantId: string): void {
if (!assistantId) {
throw new Error("Assistant ID is required");
throw new Error("Assistant ID is required");
}
}

Expand Down
Loading
Loading