將 Nacos AI Registry 中的 Skill 包作為 AgentScope 的技能來源。通過 NacosSkillRepository,Agent 可以直接從 Nacos 下載 Skill ZIP 並載入為 AgentSkill 使用。
前提條件
已開通阿里雲 MSE AI 治理中心執行個體,並完成工作空間建立。
已準備好阿里雲 AccessKey / SecretKey,且 RAM 子帳號已授予 Skill 相關讀許可權,參考 AI 治理中心 RAM 許可權配置指南。
已根據 Agent 運行環境完成網路接入配置:
本地開發、IDC 或未部署在阿里雲 VPC 內的應用,請參考 AI 治理中心公網訪問配置指南 配置公網存取點和 IP 白名單。
部署在阿里雲 VPC 內的應用,請參考 AI 治理中心 VPC 私網訪問配置指南 配置 VPC 私網訪問。
AgentScope 1.0.12+,JDK 17+,Maven 3.8+。
模組資訊
Maven Artifact:
io.agentscope:agentscope-extensions-nacos-skillAgentScope 版本要求:1.0.12+
主要依賴:
io.agentscope:agentscope-core、com.alibaba.nacos:nacos-clientJDK 要求:JDK 17+
注意:Nacos 匯出的
SKILL.md中 YAML frontmatter 可能存在多行縮排,NacosSkillRepository載入時自動處理為扁平格式,無需手動幹預。
完整樣本
以下是一個端到端的可運行樣本,展示從初始化 Nacos 串連到 Agent 對話的完整鏈路。請將 SERVER_ADDR 替換為公網存取點網域名稱或 VPC 私網網域名稱:
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.ai.AiFactory;
import com.alibaba.nacos.api.ai.AiService;
import io.agentscope.core.ReActAgent;
import io.agentscope.core.model.DashScopeChatModel;
import io.agentscope.core.nacos.skill.NacosSkillRepository;
import io.agentscope.core.skill.AgentSkill;
import io.agentscope.core.skill.SkillBox;
import java.util.Properties;
public class NacosSkillDemo {
public static void main(String[] args) throws Exception {
// 1. 建立 AiService
Properties props = new Properties();
props.put(PropertyKeyConst.SERVER_ADDR, "airegistry.cn-hangzhou.mse.aliyuncs.com:80");
props.put(PropertyKeyConst.NAMESPACE, "your-namespace-id");
props.put(PropertyKeyConst.ACCESS_KEY, "your-access-key");
props.put(PropertyKeyConst.SECRET_KEY, "your-secret-key");
AiService aiService = AiFactory.createAiService(props);
// 2. 建立 NacosSkillRepository,載入 Skill
try (NacosSkillRepository repo = new NacosSkillRepository(aiService, "public")) {
AgentSkill skill = repo.getSkill("safe-commit-helper");
System.out.println("Loaded: " + skill.getName());
System.out.println("Description: " + skill.getDescription());
// 3. 綁定到 Agent
SkillBox skillBox = new SkillBox(repo);
var model = DashScopeChatModel.builder()
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.modelName("qwen-max")
.build();
var agent = ReActAgent.builder()
.name("DevAssistant")
.systemPrompt("你是一個編程助手。")
.model(model)
.skillBox(skillBox)
.build();
// 4. 使用 Agent
var response = agent.call(io.agentscope.core.message.Msg.of("幫我產生一個commit message"));
System.out.println(response.getText());
} finally {
aiService.shutdown();
}
}
}分步詳解
1. 添加依賴
<dependency>
<groupId>io.agentscope</groupId>
<artifactId>agentscope-extensions-nacos-skill</artifactId>
</dependency>推薦通過 agentscope-bom 統一管理版本:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.agentscope</groupId>
<artifactId>agentscope-bom</artifactId>
<version>1.0.12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>2. 初始化 AiService
建立 Nacos AiService 執行個體,配置 AI 治理中心訪問地址和憑證。SERVER_ADDR 可填寫公網存取點網域名稱或 VPC 私網網域名稱:
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.ai.AiFactory;
import com.alibaba.nacos.api.ai.AiService;
import java.util.Properties;
Properties props = new Properties();
props.put(PropertyKeyConst.SERVER_ADDR, "airegistry.cn-hangzhou.mse.aliyuncs.com:80");
props.put(PropertyKeyConst.NAMESPACE, "your-namespace-id");
props.put(PropertyKeyConst.ACCESS_KEY, "LTAI5tXXXXXXXXXXXXXX");
props.put(PropertyKeyConst.SECRET_KEY, "XXXXXXXXXXXXXXXXXXXXxx");
AiService aiService = AiFactory.createAiService(props);一個AiService執行個體對應一個命名空間。應用內盡量複用執行個體,退出時調用aiService.shutdown()釋放資源。
3. 建立 NacosSkillRepository 並載入 Skill
使用 try-with-resources 方式建立 NacosSkillRepository:
import io.agentscope.core.nacos.skill.NacosSkillRepository;
import io.agentscope.core.skill.AgentSkill;
try (NacosSkillRepository repo = new NacosSkillRepository(aiService, "public")) {
String name = "safe-commit-helper";
if (repo.skillExists(name)) {
AgentSkill skill = repo.getSkill(name);
System.out.println(skill.getName() + " - " + skill.getDescription());
} else {
System.out.println("Skill not found: " + name);
}
}如果 namespaceId 為空白或 null,自動使用 "public"。
4. 將 Skill 綁定到 Agent
通過 SkillBox 將 Skill 倉庫關聯到 Agent,Agent 在需要時按需從倉庫載入 Skill:
import io.agentscope.core.skill.SkillBox;
import io.agentscope.core.ReActAgent;
SkillBox skillBox = new SkillBox(repo);
var agent = ReActAgent.builder()
.name("MyAgent")
.systemPrompt("你是一個智能助手。")
.model(model)
.skillBox(skillBox)
.build();版本與標籤配置
NacosSkillRepository 根據 skillVersion 和 skillLabel 決定從 Nacos 下載哪個版本的 Skill ZIP,兩者各自按以下優先順序解析,首個非空值生效:
優先順序 | 來源 | 配置 Key |
1(最高) | 建構函式傳入的 |
|
2 | JVM 系統屬性 | 同上 |
3(最低) | 環境變數 |
|
當 Properties、JVM 屬性、環境變數同時設定 version 時,Properties 中的值優先生效;當 version 和 label 同時配置時,version 優先,label 被忽略。
配置方式
通過建構函式 Properties:
按版本載入:
Properties appProps = new Properties();
appProps.setProperty(NacosSkillRepository.SKILL_VERSION_PATH, "v1.0.0");
try (NacosSkillRepository repo =
new NacosSkillRepository(aiService, "public", appProps)) {
AgentSkill skill = repo.getSkill("safe-commit-helper");
}按標籤載入:
Properties appProps = new Properties();
appProps.setProperty(NacosSkillRepository.SKILL_LABEL_PATH, "stable");
try (NacosSkillRepository repo =
new NacosSkillRepository(aiService, "public", appProps)) {
AgentSkill skill = repo.getSkill("safe-commit-helper");
}通過環境變數:
export AGENTSCOPE_NACOS_SKILL_VERSION=v1.0.0
# 或
export AGENTSCOPE_NACOS_SKILL_LABEL=stable通過 JVM 系統屬性:
java -Dagentscope.nacos.skill.version=v1.0.0 -jar myapp.jar優先順序樣本:
// JVM: -Dagentscope.nacos.skill.version=jvm-ver
// ENV: AGENTSCOPE_NACOS_SKILL_VERSION=env-ver
Properties props = new Properties();
props.setProperty(NacosSkillRepository.SKILL_VERSION_PATH, "app-ver");
// 此處 "app-ver" 生效
NacosSkillRepository repo = new NacosSkillRepository(aiService, "public", props);
repo.getSkill("my-skill"); // 實際調用 downloadSkillZipByVersion("my-skill", "app-ver")下載 API 選擇邏輯
以下為 NacosSkillRepository 內部實現說明,供理解調用邏輯參考。
NacosSkillRepository 根據 version / label 自動選擇下載方式(version 優先):
若
skillVersion非空 →AiService.downloadSkillZipByVersion(name, version)否則若
skillLabel非空 →AiService.downloadSkillZipByLabel(name, label)否則 →
AiService.downloadSkillZip(name)
NacosSkillRepository 能力矩陣
核心類:io.agentscope.core.nacos.skill.NacosSkillRepository,實現了 AgentSkillRepository 介面。
能力 | 方法 | 狀態 | 說明 |
載入單個 Skill |
| 支援 | 下載 ZIP → 適配 frontmatter → 構建 |
判斷 Skill 是否存在 |
| 支援 | 依據下載結果 / |
擷取倉庫元資訊 |
| 支援 | 類型固定 |
擷取來源標識 |
| 支援 | 格式: |
唯讀聲明 |
| 恒為 |
|
列舉全部 Skill |
| 暫未實現 | 返回空集合,列印 warn |
寫操作 |
| 暫未實現 | 返回 |
環境變數參考
以下為 Nacos 通用環境變數參考,本文程式碼範例使用 PropertyKeyConst 常量方式進行配置,與下表中的環境變數無關。
變數 | 說明 |
| 指定 Skill 版本(可選) |
| 指定 Skill 標籤(可選) |
更多參考
AI Registry Skill 快速上手指南:Skill 的建立、審核、發布流程
Nacos Java SDK 接入 AI 治理中心:Nacos Java SDK 的接入配置與能力概覽
Nacos CLI 接入 AI 治理中心:通過 Nacos CLI 接入 AI Registry
AI 治理中心公網訪問配置指南:公網存取點和 IP 白名單的配置步驟
AI 治理中心工作空間管理指南:工作空間的建立、查詢、編輯
AI 治理中心 Skill 管理:控制台側 Skill 的版本管理與發布
AI 治理中心 VPC 私網訪問配置指南:VPC 私網訪問的配置步驟
AI 治理中心 RAM 許可權配置指南:RAM 子帳號授權配置