mcpField
A
azure-ai-projects-ts

azure-ai-projects-ts

github.com/sickn33Agent 工作流48.7k78 次安装今天更新
代码仓库

关于 azure-ai-projects-ts

面向开发者的 TypeScript SDK 技能,用于操作 Azure AI Foundry 项目并管理 Agent 和项目资源。示例涵盖 Agent 的创建与运行,以及连接、部署、数据集、索引和评估。

主要功能

  1. 01创建并版本化 Agent
  2. 02运行带工具的 Agent
  3. 03管理项目连接
  4. 04列出模型部署
  5. 05上传数据集并管理索引
  6. 06管理评估指标与记忆

适用场景

  1. 01定义并运行项目 Agent
  2. 02查找已连接资源和模型部署
  3. 03为项目工作流上传数据集
  4. 04配置搜索索引和评估

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/sickn33/agentic-awesome-skills --skill azure-ai-projects-ts

安装后在 Claude Code 中输入/azure-ai-projects-ts确认已加载。

详细介绍

Azure AI Projects SDK for TypeScript

用于 Azure AI Foundry 项目的高级 SDK,支持 Agent、连接、部署和评估。

安装

npm install @azure/ai-projects @azure/identity

用于追踪:

npm install @azure/monitor-opentelemetry @opentelemetry/api

环境变量

AZURE_AI_PROJECT_ENDPOINT=https://<resource>.services.ai.azure.com/api/projects/<project>
MODEL_DEPLOYMENT_NAME=gpt-4o

身份验证

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

const client = new AIProjectClient(
  process.env.AZURE_AI_PROJECT_ENDPOINT!,
  new DefaultAzureCredential()
);

SDK 生成与影响

这些相互独立的代码片段涉及对版本敏感的项目和 Agent API。使用前,请锁定实际的软件包/API 版本,并确认可用的操作组。运行 Agent、上传数据集或使用工具前,请确认租户/项目和预算;代码解释器/MCP 可能会执行代码或泄露私有数据。getWithCredentials 会返回机密信息,不应作为默认的发现操作。清理操作只能针对由此任务创建并拥有的资源。

操作组

组用途
client.agents创建和管理 AI Agent
client.connections列出已连接的 Azure 资源
client.deployments列出模型部署
client.datasets上传和管理数据集
client.indexes创建和管理搜索索引
client.evaluators管理评估指标
client.memoryStores管理 Agent 记忆

获取 OpenAI 客户端

const openAIClient = await client.getOpenAIClient();

// Use for responses
const response = await openAIClient.responses.create({
  model: "gpt-4o",
  input: "What is the capital of France?"
});

// Use for conversations
const conversation = await openAIClient.conversations.create({
  items: [{ type: "message", role: "user", content: "Hello!" }]
});

Agent

创建 Agent
const agent = await client.agents.createVersion("my-agent", {
  kind: "prompt",
  model: "gpt-4o",
  instructions: "You are a helpful assistant."
});
带工具的 Agent
// Code Interpreter
const agent = await client.agents.createVersion("code-agent", {
  kind: "prompt",
  model: "gpt-4o",
  instructions: "You can execute code.",
  tools: [{ type: "code_interpreter", container: { type: "auto" } }]
});

// File Search
const agent = await client.agents.createVersion("search-agent", {
  kind: "prompt",
  model: "gpt-4o",
  tools: [{ type: "file_search", vector_store_ids: [vectorStoreId] }]
});

// Web Search
const agent = await client.agents.createVersion("web-agent", {
  kind: "prompt",
  model: "gpt-4o",
  tools: [{
    type: "web_search_preview",
    user_location: { type: "approximate", country: "US", city: "Seattle" }
  }]
});

// Azure AI Search
const agent = await client.agents.createVersion("aisearch-agent", {
  kind: "prompt",
  model: "gpt-4o",
  tools: [{
    type: "azure_ai_search",
    azure_ai_search: {
      indexes: [{
        project_connection_id: connectionId,
        index_name: "my-index",
        query_type: "simple"
      }]
    }
  }]
});

// Function Tool
const agent = await client.agents.createVersion("func-agent", {
  kind: "prompt",
  model: "gpt-4o",
  tools: [{
    type: "function",
    function: {
      name: "get_weather",
      description: "Get weather for a location",
      strict: true,
      parameters: {
        type: "object",
        properties: { location: { type: "string" } },
        required: ["location"]
      }
    }
  }]
});

// MCP Tool
const agent = await client.agents.createVersion("mcp-agent", {
  kind: "prompt",
  model: "gpt-4o",
  tools: [{
    type: "mcp",
    server_label: "my-mcp",
    server_url: "https://mcp-server.example.com",
    require_approval: "always"
  }]
});
运行 Agent
const openAIClient = await client.getOpenAIClient();

// Create conversation
const conversation = await openAIClient.conversations.create({
  items: [{ type: "message", role: "user", content: "Hello!" }]
});

// Generate response using agent
const response = await openAIClient.responses.create(
  { conversation: conversation.id },
  { body: { agent: { name: agent.name, type: "agent_reference" } } }
);

// Cleanup
await openAIClient.conversations.delete(conversation.id);
await client.agents.deleteVersion(agent.name, agent.version);

连接

// List all connections
for await (const conn of client.connections.list()) {
  console.log(conn.name, conn.type);
}

// Get connection by name
const conn = await client.connections.get("my-connection");

// Get connection with credentials
const connWithCreds = await client.connections.getWithCredentials("my-connection");

// Get default connection by type
const defaultAzureOpenAI = await client.connections.getDefault("AzureOpenAI", true);

部署

// List all deployments
for await (const deployment of client.deployments.list()) {
  if (deployment.type === "ModelDeployment") {
    console.log(deployment.name, deployment.modelName);
  }
}

// Filter by publisher
for await (const d of client.deployments.list({ modelPublisher: "OpenAI" })) {
  console.log(d.name);
}

// Get specific deployment
const deployment = await client.deployments.get("gpt-4o");

数据集

// Upload single file
const dataset = await client.datasets.uploadFile(
  "my-dataset",
  "1.0",
  "./data/training.jsonl"
);

// Upload folder
const dataset = await client.datasets.uploadFolder(
  "my-dataset",
  "2.0",
  "./data/documents/"
);

// Get dataset
const ds = await client.datasets.get("my-dataset", "1.0");

// List versions
for await (const version of client.datasets.listVersions("my-dataset")) {
  console.log(version);
}

// Delete
await client.datasets.delete("my-dataset", "1.0");

索引

import { AzureAISearchIndex } from "@azure/ai-projects";

const indexConfig: AzureAISearchIndex = {
  name: "my-index",
  type: "AzureSearch",
  version: "1",
  indexName: "my-index",
  connectionName: "search-connection"
};

// Create index
const index = await client.indexes.createOrUpdate("my-index", "1", indexConfig);

// List indexes
for await (const idx of client.indexes.list()) {
  console.log(idx.name);
}

// Delete
await client.indexes.delete("my-index", "1");

关键类型

import {
  AIProjectClient,
  AIProjectClientOptionalParams,
  Connection,
  ModelDeployment,
  DatasetVersionUnion,
  AzureAISearchIndex
} from "@azure/ai-projects";

最佳实践

  1. 使用 getOpenAIClient() - 用于响应、对话、文件和向量存储
  2. 为 Agent 设置版本 - 使用 createVersion 创建可复现的 Agent 定义
  3. 清理资源 - 完成后删除 Agent 和对话
  4. 使用连接 - 从项目连接获取凭据,不要硬编码
  5. 筛选部署 - 使用 modelPublisher 筛选条件查找特定模型

适用场景

当需要执行概述中所述的工作流或操作时,可使用此技能。

限制

  • 仅当任务明确符合上述范围时,才使用此技能。
  • 不要将输出视为环境特定验证、测试或专家审查的替代品。
  • 如果缺少必要的输入、权限、安全边界或成功标准,请停止并请求澄清。

评论

支持 Markdown