azure-ai-agents-persistent-java
microsoft/skills
使用 Azure Java SDK,透過執行緒、訊息、執行程序和工具來建立及管理持久性 AI 代理。
...展開全部Azure AI Agents 持久化 SDK(Java 版)
用於建立和管理具執行緒、訊息、執行階段及工具之持久性 AI 代理程式的低階 SDK。
安裝
com.azure
azure-ai-agents-persistent
1.0.0-beta.1
環境變數
PROJECT_ENDPOINT=https://.services.ai.azure.com/api/projects/ # 專案設定所需
MODEL_DEPLOYMENT_NAME=gpt-4o-mini # 代理程式模型選取所需
AZURE_TOKEN_CREDENTIALS=prod # 僅當在生產環境中使用 DefaultAzureCredential 時才需設定
驗證
import com.azure.ai.agents.persistent.PersistentAgentsClient;
import com.azure.ai.agents.persistent.PersistentAgentsClientBuilder;
import com.azure.core.credential.TokenCredential;
import com.azure.identity.AzureIdentityEnvVars;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.identity.ManagedIdentityCredentialBuilder;
String endpoint = System.getenv("PROJECT_ENDPOINT");
TokenCredential credential = new DefaultAzureCredentialBuilder()
.requireEnvVars(AzureIdentityEnvVars.AZURE_TOKEN_CREDENTIALS)
.build();
// 或者在生產環境中直接使用特定的憑證:
// 請參閱 https://learn.microsoft.com/java/api/overview/azure/identity-readme?view=azure-java-stable#credential-classes
// TokenCredential credential = new ManagedIdentityCredentialBuilder().build();
PersistentAgentsClient client = new PersistentAgentsClientBuilder()
.endpoint(endpoint)
.credential(credential)
.buildClient();
關鍵概念
Azure AI Agents 持久化 SDK 提供了一組用於管理持久化代理的低階 API,這些代理可在不同工作階段間重複使用。
客戶端層級結構
| 客戶端 | 用途 |
|---|---|
PersistentAgentsClient |
用於代理程式操作的同步客戶端 |
PersistentAgentsAsyncClient |
用於代理程式操作的非同步客戶端 |
核心工作流程
1. 建立代理程式
// 使用工具建立代理程式
PersistentAgent agent = client.createAgent(
modelDeploymentName,
"數學家教",
"您是一位私人數學家教。"
);
2. 建立執行緒
PersistentAgentThread thread = client.createThread();
3. 新增訊息
client.createMessage(
thread.getId(),
MessageRole.USER,
"我需要方程式方面的幫助。"
);
4. 執行代理程式
ThreadRun run = client.createRun(thread.getId(), agent.getId());
// 輪詢執行狀態
while (run.getStatus() == RunStatus.QUEUED || run.getStatus() == RunStatus.IN_PROGRESS) {
Thread.sleep(500);
run = client.getRun(thread.getId(), run.getId());
}
5. 取得回應
PagedIterable messages = client.listMessages(thread.getId());
for (PersistentThreadMessage message : messages) {
System.out.println(message.getRole() + ": " + message.getContent());
}
6. 清理
client.deleteThread(thread.getId());
client.deleteAgent(agent.getId());
最佳實務
- 在生產環境中使用 DefaultAzureCredential進行驗證
- 以適當的延遲進行輪詢— 建議狀態檢查間隔為 500 毫秒
- 清理資源— 完成後刪除執行緒和代理程式
- 處理所有執行狀態— 檢查是否為「需要操作」、「失敗」或「已取消」
- 在高並發情境下使用非同步客戶端以提升吞吐量
錯誤處理
import com.azure.core.exception.HttpResponseException;
try {
PersistentAgent agent = client.createAgent(modelName, name, instructions);
} catch (HttpResponseException e) {
System.err.println("錯誤: " + e.getResponse().getStatusCode() + " - " + e.getMessage());
}
參考連結
| 資源 | URL |
|---|---|
| Maven 套件 | https://central.sonatype.com/artifact/com.azure/azure-ai-agents-persistent |
| GitHub 原始碼 | https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/ai/azure-ai-agents-persistent |
---
name: azure-ai-agents-persistent-java
description: Create and manage persistent AI agents with threads, messages, runs, and tools using the Azure SDK for Java.
license: MIT
---
# Azure AI Agents Persistent SDK for Java
Low-level SDK for creating and managing persistent AI agents with threads, messages, runs, and tools.
## Installation
```xml
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-agents-persistent</artifactId>
<version>1.0.0-beta.1</version>
</dependency>
```
## Environment Variables
```bash
PROJECT_ENDPOINT=https://<resource>.services.ai.azure.com/api/projects/<project> # Required for project configuration
MODEL_DEPLOYMENT_NAME=gpt-4o-mini # Required for agent model selection
AZURE_TOKEN_CREDENTIALS=prod # Required only if DefaultAzureCredential is used in production
```
## Authentication
```java
import com.azure.ai.agents.persistent.PersistentAgentsClient;
import com.azure.ai.agents.persistent.PersistentAgentsClientBuilder;
import com.azure.core.credential.TokenCredential;
import com.azure.identity.AzureIdentityEnvVars;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.identity.ManagedIdentityCredentialBuilder;
String endpoint = System.getenv("PROJECT_ENDPOINT");
TokenCredential credential = new DefaultAzureCredentialBuilder()
.requireEnvVars(AzureIdentityEnvVars.AZURE_TOKEN_CREDENTIALS)
.build();
// Or use a specific credential directly in production:
// See https://learn.microsoft.com/java/api/overview/azure/identity-readme?view=azure-java-stable#credential-classes
// TokenCredential credential = new ManagedIdentityCredentialBuilder().build();
PersistentAgentsClient client = new PersistentAgentsClientBuilder()
.endpoint(endpoint)
.credential(credential)
.buildClient();
```
## Key Concepts
The Azure AI Agents Persistent SDK provides a low-level API for managing persistent agents that can be reused across sessions.
### Client Hierarchy
| Client | Purpose |
|--------|---------|
| `PersistentAgentsClient` | Sync client for agent operations |
| `PersistentAgentsAsyncClient` | Async client for agent operations |
## Core Workflow
### 1. Create Agent
```java
// Create agent with tools
PersistentAgent agent = client.createAgent(
modelDeploymentName,
"Math Tutor",
"You are a personal math tutor."
);
```
### 2. Create Thread
```java
PersistentAgentThread thread = client.createThread();
```
### 3. Add Message
```java
client.createMessage(
thread.getId(),
MessageRole.USER,
"I need help with equations."
);
```
### 4. Run Agent
```java
ThreadRun run = client.createRun(thread.getId(), agent.getId());
// Poll for completion
while (run.getStatus() == RunStatus.QUEUED || run.getStatus() == RunStatus.IN_PROGRESS) {
Thread.sleep(500);
run = client.getRun(thread.getId(), run.getId());
}
```
### 5. Get Response
```java
PagedIterable<PersistentThreadMessage> messages = client.listMessages(thread.getId());
for (PersistentThreadMessage message : messages) {
System.out.println(message.getRole() + ": " + message.getContent());
}
```
### 6. Cleanup
```java
client.deleteThread(thread.getId());
client.deleteAgent(agent.getId());
```
## Best Practices
1. **Use DefaultAzureCredential** for production authentication
2. **Poll with appropriate delays** — 500ms recommended between status checks
3. **Clean up resources** — Delete threads and agents when done
4. **Handle all run statuses** — Check for RequiresAction, Failed, Cancelled
5. **Use async client** for better throughput in high-concurrency scenarios
## Error Handling
```java
import com.azure.core.exception.HttpResponseException;
try {
PersistentAgent agent = client.createAgent(modelName, name, instructions);
} catch (HttpResponseException e) {
System.err.println("Error: " + e.getResponse().getStatusCode() + " - " + e.getMessage());
}
```
## Reference Links
| Resource | URL |
|----------|-----|
| Maven Package | https://central.sonatype.com/artifact/com.azure/azure-ai-agents-persistent |
| GitHub Source | https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/ai/azure-ai-agents-persistent |
所有檔案
0 個檔案安裝 azure-ai-agents-persistent-java
請下載並將技能檔案解壓縮至您的 .claude/skills/ 目錄中。
下載 ZIP複製儲存庫並將技能檔案複製到您的專案中。
git clone https://github.com/microsoft/skills/tree/main/.github/plugins/azure-sdk-java/skills/azure-ai-agents-persistent-java # Copy SKILL.md to your .claude/skills/ directory
複製





首頁
