Azure AI 项目 Java 版

azure-ai-projects-java
分类通用
作者Agentic Awesome Skills 社区
许可MIT
评分4.80/5
使用7.3K

Azure AI Projects Java SDK

用于 Azure AI Foundry 项目管理的高级 SDK,可访问连接、数据集、索引和评估。

安装

xml
<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-ai-projects</artifactId>
    <version>1.0.0-beta.1</version>
</dependency>

环境变量

bash
PROJECT_ENDPOINT=https://<resource>.services.ai.azure.com/api/projects/<project>

身份验证

java
import com.azure.ai.projects.AIProjectClientBuilder;
import com.azure.identity.DefaultAzureCredentialBuilder;

AIProjectClientBuilder builder = new AIProjectClientBuilder()
.endpoint(System.getenv("PROJECT_ENDPOINT"))
.credential(new DefaultAzureCredentialBuilder().build());

客户端层级

该 SDK 为不同操作提供了多个子客户端:

| 客户端 | 用途 |
|--------|---------|
| ConnectionsClient | 枚举已连接的 Azure 资源 |
| DatasetsClient | 上传文档并管理数据集 |
| DeploymentsClient | 枚举 AI 模型部署 |
| IndexesClient | 创建并管理搜索索引 |
| EvaluationsClient | 运行 AI 模型评估 |
| EvaluatorsClient | 管理评估器配置 |
| SchedulesClient | 管理计划任务操作 |

java
// 通过 builder 构建子客户端
ConnectionsClient connectionsClient = builder.buildConnectionsClient();
DatasetsClient datasetsClient = builder.buildDatasetsClient();
DeploymentsClient deploymentsClient = builder.buildDeploymentsClient();
IndexesClient indexesClient = builder.buildIndexesClient();
EvaluationsClient evaluationsClient = builder.buildEvaluationsClient();

核心操作

列出连接

java
import com.azure.ai.projects.models.Connection;
import com.azure.core.http.rest.PagedIterable;

PagedIterable<Connection> connections = connectionsClient.listConnections();
for (Connection connection : connections) {
System.out.println("Name: " + connection.getName());
System.out.println("Type: " + connection.getType());
System.out.println("Credential Type: " + connection.getCredentials().getType());
}

列出索引

java
indexesClient.listLatest().forEach(index -> {
    System.out.println("Index name: " + index.getName());
    System.out.println("Version: " + index.getVersion());
    System.out.println("Description: " + index.getDescription());
});

创建或更新索引

java
import com.azure.ai.projects.models.AzureAISearchIndex;
import com.azure.ai.projects.models.Index;

String indexName = "my-index";
String indexVersion = "1.0";
String searchConnectionName = System.getenv("AI_SEARCH_CONNECTION_NAME");
String searchIndexName = System.getenv("AI_SEARCH_INDEX_NAME");

Index index = indexesClient.createOrUpdate(
indexName,
indexVersion,
new AzureAISearchIndex()
.setConnectionName(searchConnectionName)
.setIndexName(searchIndexName)
);

System.out.println("Created index: " + index.getName());

访问 OpenAI 评估

该 SDK 提供了对 OpenAI 官方评估 SDK 的访问:

java
import com.openai.services.EvalService;

EvalService evalService = evaluationsClient.getOpenAIClient();
// 直接使用 OpenAI 评估 API


最佳实践

1. 使用 DefaultAzureCredential 进行生产环境身份验证
2. 复用客户端生成器 (client builder) 以高效创建多个子客户端
3. 处理分页:在使用 PagedIterable 列出资源时请处理分页
4. 使用环境变量 配置连接名称和相关设置
5. 检查连接类型:在访问凭据前先验证连接类型

错误处理

java
import com.azure.core.exception.HttpResponseException;
import com.azure.core.exception.ResourceNotFoundException;

try {
Index index = indexesClient.get(indexName, version);
} catch (ResourceNotFoundException e) {
System.err.println("Index not found: " + indexName);
} catch (HttpResponseException e) {
System.err.println("Error: " + e.getResponse().getStatusCode());
}

参考链接

| 资源 | URL |
|----------|-----|
| 产品文档 | https://learn.microsoft.com/azure/ai-studio/ |
| API 参考 | https://learn.microsoft.com/rest/api/aifoundry/aiprojects/ |
| GitHub 源码 | https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/ai/azure-ai-projects |
| 示例代码 | https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/ai/azure-ai-projects/src/samples |

适用场景

本技能适用于执行概览中所描述的工作流或操作。

局限性

  • 仅在任务与上述范围明确匹配时使用此技能。
  • 不要将输出结果视为针对特定环境的验证、测试或专家评审的替代方案。
  • 如果缺失必要的输入、权限、安全边界或成功标准,请停止操作并寻求澄清。