Azure AI Projects SDK for TypeScript
Azure AI Foundry 项目的高级 SDK,支持智能体、连接、部署和评估。
安装
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()
);
操作组
| 组 | 用途 |
|---|---|
client.agents |
创建和管理 AI 智能体 |
client.connections |
列出已连接的 Azure 资源 |
client.deployments |
列出模型部署 |
client.datasets |
上传和管理数据集 |
client.indexes |
创建和管理搜索索引 |
client.evaluators |
管理评估指标 |
client.memoryStores |
管理智能体内存 |
获取 OpenAI 客户端
const openAIClient = await client.getOpenAIClient();
// 用于响应
const response = await openAIClient.responses.create({
model: "gpt-4o",
input: "What is the capital of France?"
});
// 用于对话
const conversation = await openAIClient.conversations.create({
items: [{ type: "message", role: "user", content: "Hello!" }]
});
智能体
创建智能体
const agent = await client.agents.createVersion("my-agent", {
kind: "prompt",
model: "gpt-4o",
instructions: "You are a helpful assistant."
});
带工具的智能体
// 代码解释器
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" } }]
});
// 文件搜索
const agent = await client.agents.createVersion("search-agent", {
kind: "prompt",
model: "gpt-4o",
tools: [{ type: "file_search", vector_store_ids: [vectorStoreId] }]
});
// 网页搜索
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"
}]
}
}]
});
// 函数工具
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 工具
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"
}]
});
运行智能体
const openAIClient = await client.getOpenAIClient();
// 创建对话
const conversation = await openAIClient.conversations.create({
items: [{ type: "message", role: "user", content: "Hello!" }]
});
// 使用智能体生成响应
const response = await openAIClient.responses.create(
{ conversation: conversation.id },
{ body: { agent: { name: agent.name, type: "agent_reference" } } }
);
// 清理
await openAIClient.conversations.delete(conversation.id);
await client.agents.deleteVersion(agent.name, agent.version);
连接
// 列出所有连接
for await (const conn of client.connections.list()) {
console.log(conn.name, conn.type);
}
// 按名称获取连接
const conn = await client.connections.get("my-connection");
// 获取带凭据的连接
const connWithCreds = await client.connections.getWithCredentials("my-connection");
// 按类型获取默认连接
const defaultAzureOpenAI = await client.connections.getDefault("AzureOpenAI", true);
部署
// 列出所有部署
for await (const deployment of client.deployments.list()) {
if (deployment.type === "ModelDeployment") {
console.log(deployment.name, deployment.modelName);
}
}
// 按发布者筛选
for await (const d of client.deployments.list({ modelPublisher: "OpenAI" })) {
console.log(d.name);
}
// 获取特定部署
const deployment = await client.deployments.get("gpt-4o");
数据集
// 上传单个文件
const dataset = await client.datasets.uploadFile(
"my-dataset",
"1.0",
"./data/training.jsonl"
);
// 上传文件夹
const dataset = await client.datasets.uploadFolder(
"my-dataset",
"2.0",
"./data/documents/"
);
// 获取数据集
const ds = await client.datasets.get("my-dataset", "1.0");
// 列出版本
for await (const version of client.datasets.listVersions("my-dataset")) {
console.log(version);
}
// 删除
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"
};
// 创建索引
const index = await client.indexes.createOrUpdate("my-index", "1", indexConfig);
// 列出索引
for await (const idx of client.indexes.list()) {
console.log(idx.name);
}
// 删除
await client.indexes.delete("my-index", "1");
关键类型
import {
AIProjectClient,
AIProjectClientOptionalParams,
Connection,
ModelDeployment,
DatasetVersionUnion,
AzureAISearchIndex
} from "@azure/ai-projects";
最佳实践
- 使用 getOpenAIClient() - 用于响应、对话、文件和向量存储
- 版本化智能体 - 使用
createVersion创建可复现的智能体定义 - 清理资源 - 完成后删除智能体、对话
- 使用连接 - 从项目连接获取凭据,不要硬编码
- 筛选部署 - 使用
modelPublisher筛选器查找特定模型
适用场景
本技能适用于执行概述中描述的工作流或操作。
局限性
- 仅在任务明确符合上述范围时使用本技能。
- 不要将输出视为环境特定验证、测试或专家审查的替代品。
- 如果缺少必需的输入、权限、安全边界或成功标准,请停止并请求澄清。