AgentScope 对接 Nacos AI Registry 使用 Skill

更新时间:
复制 MD 格式

将 Nacos AI Registry 中的 Skill 包作为 AgentScope 的技能来源。通过 NacosSkillRepository,Agent 可以直接从 Nacos 下载 Skill ZIP 并加载为 AgentSkill 使用。

前提条件

模块信息

  • Maven Artifactio.agentscope:agentscope-extensions-nacos-skill

  • AgentScope 版本要求:1.0.12+

  • 主要依赖io.agentscope:agentscope-corecom.alibaba.nacos:nacos-client

  • JDK 要求: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 根据 skillVersionskillLabel 决定从 Nacos 下载哪个版本的 Skill ZIP,两者各自按以下优先级解析,首个非空值生效:

优先级

来源

配置 Key

1(最高)

构造函数传入的 Properties

agentscope.nacos.skill.version / agentscope.nacos.skill.label

2

JVM 系统属性

同上

3(最低)

环境变量

AGENTSCOPE_NACOS_SKILL_VERSION / AGENTSCOPE_NACOS_SKILL_LABEL

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 优先):

  1. skillVersion 非空 → AiService.downloadSkillZipByVersion(name, version)

  2. 否则若 skillLabel 非空 → AiService.downloadSkillZipByLabel(name, label)

  3. 否则 → AiService.downloadSkillZip(name)

NacosSkillRepository 能力矩阵

核心类:io.agentscope.core.nacos.skill.NacosSkillRepository,实现了 AgentSkillRepository 接口。

能力

方法

状态

说明

加载单个 Skill

getSkill(name)

支持

下载 ZIP → 适配 frontmatter → 构建 AgentSkill

判断 Skill 是否存在

skillExists(name)

支持

依据下载结果 / NOT_FOUND 错误码判定

获取仓库元信息

getRepositoryInfo()

支持

类型固定 nacos,位置为 namespace:<namespaceId>

获取来源标识

getSource()

支持

格式:nacos:<namespaceId>

只读声明

isWriteable()

恒为 false

setWriteable() 被忽略并打印 warn

列举全部 Skill

getAllSkillNames() / getAllSkills()

暂未实现

返回空集合,打印 warn

写操作

save() / delete()

暂未实现

返回 false,打印 warn

环境变量参考

以下为 Nacos 通用环境变量参考,本文代码示例使用 PropertyKeyConst 常量方式进行配置,与下表中的环境变量无关。

变量

说明

AGENTSCOPE_NACOS_SKILL_VERSION

指定 Skill 版本(可选)

AGENTSCOPE_NACOS_SKILL_LABEL

指定 Skill 标签(可选)

更多参考