AgentScope Java 集成 Spring AI Alibaba Workflow 完整指南
結(jié)合 agentscope-ai/agentscope-java、alibaba/spring-ai-alibaba 及 java2ai 生態(tài)中 Graph Core 工作流規(guī)范,以下是可落地的集成方案,涵蓋核心思路、工程配置、代碼實(shí)現(xiàn)、最佳實(shí)踐四部分,兼顧 AgentScope 智能體特性與 Spring AI 工作流的工程化能力。
一、核心集成思路
1. 能力邊界劃分(關(guān)鍵前提)
| 框架/組件 | 核心職責(zé) |
|---|---|
| AgentScope Java | 智能體(Agent)生命周期管理、多智能體協(xié)作、工具調(diào)用、上下文(Context)管理 |
| Spring AI Alibaba | 阿里云大模型(通義千問(wèn)/百煉)標(biāo)準(zhǔn)化調(diào)用、Workflow 聲明式編排、Spring 生態(tài)適配 |
| Java2AI Graph Core | 工作流節(jié)點(diǎn)標(biāo)準(zhǔn)化定義、執(zhí)行引擎適配、可視化編排規(guī)范(參考) |
2. 集成核心邏輯
以 “AgentScope 為智能體核心 + Spring AI Alibaba 為工作流引擎” 為核心,通過(guò)三層適配實(shí)現(xiàn)能力融合:
- 模型層復(fù)用:AgentScope 復(fù)用 Spring AI Alibaba 的 DashScope 客戶(hù)端,統(tǒng)一大模型調(diào)用;
- 工作流層封裝:將 Spring AI Alibaba Workflow 封裝為 AgentScope 可調(diào)用的“工具/服務(wù)”;
- 執(zhí)行層適配:對(duì)齊異步模型(Reactor/CompletableFuture)、生命周期(Spring/AgentScope)、上下文數(shù)據(jù)格式。
二、前置工程配置
1. 依賴(lài)整合(Maven pom.xml)
需兼容 JDK 17+、Spring Boot 3.2+,核心依賴(lài)如下(版本以官方最新為準(zhǔn)):
<!-- Spring Boot 核心(支撐 Spring AI) -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/>
</parent>
<dependencies>
<!-- 1. Spring AI Alibaba 核心(含 Workflow + DashScope 客戶(hù)端) -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>spring-ai-alibaba-dashscope-spring-boot-starter</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>spring-ai-alibaba-workflow-core</artifactId>
<version>0.1.0</version>
</dependency>
<!-- 2. AgentScope Java 核心 -->
<dependency>
<groupId>io.agentscope</groupId>
<artifactId>agentscope-core</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>io.agentscope</groupId>
<artifactId>agentscope-spring-boot-starter</artifactId>
<version>0.1.0</version> <!-- 簡(jiǎn)化 Spring 集成 -->
</dependency>
<!-- 3. Java2AI Graph Core(可選,標(biāo)準(zhǔn)化工作流節(jié)點(diǎn)) -->
<dependency>
<groupId>com.java2ai</groupId>
<artifactId>graph-core</artifactId>
<version>1.0.0</version>
</dependency>
<!-- 4. 基礎(chǔ)依賴(lài) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>2. 配置文件(application.yml)
統(tǒng)一模型密鑰、工作流引擎、AgentScope 配置:
# 1. Spring AI Alibaba 配置
spring:
ai:
# 阿里云 DashScope 配置(通義千問(wèn))
dashscope:
api-key: ${DASHSCOPE_API_KEY:你的阿里云API密鑰}
chat:
options:
model: qwen-turbo
temperature: 0.7
# Spring AI Workflow 配置
workflow:
executor:
thread-pool-size: 8
persistence:
enabled: true # 開(kāi)啟工作流持久化(可選)
# 2. AgentScope 配置(復(fù)用 Spring AI 的模型密鑰)
agentscope:
core:
agent:
default-context-size: 1000 # 智能體默認(rèn)上下文大小
model:
dashscope:
api-key: ${spring.ai.dashscope.api-key}
model-name: ${spring.ai.dashscope.chat.options.model}
spring:
integration:
enabled: true # 開(kāi)啟 AgentScope 與 Spring 集成
# 3. Java2AI Graph Core 配置(可選)
java2ai:
graph:
core:
node-package: com.yourpackage.agent.workflow.node # 工作流節(jié)點(diǎn)掃描包三、核心代碼實(shí)現(xiàn)
步驟 1:定義 Spring AI Alibaba Workflow 模板
參考 Java2AI Graph Core 節(jié)點(diǎn)規(guī)范,定義標(biāo)準(zhǔn)化的 AI 工作流(以醫(yī)療場(chǎng)景“病歷分析”為例):
package com.yourpackage.workflow;
import com.alibaba.spring.ai.workflow.annotation.Workflow;
import com.alibaba.spring.ai.workflow.annotation.WorkflowNode;
import com.alibaba.spring.ai.workflow.executor.WorkflowContext;
import org.springframework.ai.dashscope.DashScopeChatClient;
import org.springframework.stereotype.Component;
/**
* 基于 Spring AI Alibaba 定義的病歷分析工作流
* 節(jié)點(diǎn)1:提取病歷關(guān)鍵信息 → 節(jié)點(diǎn)2:校驗(yàn)數(shù)據(jù)完整性 → 節(jié)點(diǎn)3:生成分析報(bào)告
*/
@Workflow(name = "medical-record-analysis", description = "醫(yī)療病歷分析工作流")
@Component
public class MedicalRecordAnalysisWorkflow {
private final DashScopeChatClient dashScopeChatClient;
public MedicalRecordAnalysisWorkflow(DashScopeChatClient dashScopeChatClient) {
this.dashScopeChatClient = dashScopeChatClient;
}
/**
* 節(jié)點(diǎn)1:提取病歷關(guān)鍵信息(大模型調(diào)用)
*/
@WorkflowNode(name = "extract-info", order = 1, requiredParams = "medicalRecord")
public String extractMedicalInfo(WorkflowContext context) {
String medicalRecord = context.getParam("medicalRecord", String.class);
String prompt = """
提取以下病歷的關(guān)鍵信息(患者姓名、癥狀、檢查結(jié)果、診斷結(jié)論):
%s
要求:結(jié)構(gòu)化輸出,簡(jiǎn)潔明了
""".formatted(medicalRecord);
// 調(diào)用 Spring AI Alibaba 的 DashScope 客戶(hù)端
return dashScopeChatClient.call(prompt).getResult().getOutput().getContent();
}
/**
* 節(jié)點(diǎn)2:校驗(yàn)數(shù)據(jù)完整性(工具調(diào)用)
*/
@WorkflowNode(name = "validate-data", order = 2, dependOn = "extract-info")
public String validateData(WorkflowContext context) {
String extractedInfo = context.getResult("extract-info", String.class);
// 自定義校驗(yàn)邏輯(可復(fù)用 AgentScope 工具)
boolean isComplete = extractedInfo.contains("檢查結(jié)果") && extractedInfo.contains("診斷結(jié)論");
return isComplete ? "數(shù)據(jù)完整" : "缺少檢查結(jié)果/診斷結(jié)論,數(shù)據(jù)不完整";
}
/**
* 節(jié)點(diǎn)3:生成分析報(bào)告(結(jié)果聚合)
*/
@WorkflowNode(name = "generate-report", order = 3, dependOn = "validate-data")
public String generateReport(WorkflowContext context) {
String extractedInfo = context.getResult("extract-info", String.class);
String validateResult = context.getResult("validate-data", String.class);
String prompt = """
基于以下信息生成醫(yī)療分析報(bào)告:
1. 提取的病歷信息:%s
2. 數(shù)據(jù)校驗(yàn)結(jié)果:%s
要求:專(zhuān)業(yè)、簡(jiǎn)潔,符合醫(yī)療規(guī)范
""".formatted(extractedInfo, validateResult);
return dashScopeChatClient.call(prompt).getResult().getOutput().getContent();
}
}步驟 2:封裝 Workflow 為 AgentScope 工具
將 Spring AI Workflow 封裝為 AgentScope 可調(diào)用的“工具”,符合 AgentScope 工具規(guī)范:
package com.yourpackage.agent.tool;
import io.agentscope.core.tool.Tool;
import io.agentscope.core.tool.ToolParam;
import com.alibaba.spring.ai.workflow.executor.WorkflowExecutor;
import com.alibaba.spring.ai.workflow.model.WorkflowExecutionResult;
import org.springframework.stereotype.Component;
/**
* AgentScope 工具:調(diào)用 Spring AI Alibaba Workflow
*/
@Component
@Tool(name = "medical_record_workflow_tool", description = "執(zhí)行醫(yī)療病歷分析工作流")
public class MedicalRecordWorkflowTool {
private final WorkflowExecutor workflowExecutor;
public MedicalRecordWorkflowTool(WorkflowExecutor workflowExecutor) {
this.workflowExecutor = workflowExecutor;
}
/**
* 工具執(zhí)行方法(AgentScope 調(diào)用入口)
* @param medicalRecord 病歷文本
* @return 工作流執(zhí)行結(jié)果(分析報(bào)告)
*/
public String execute(
@ToolParam(name = "medicalRecord", description = "待分析的病歷文本", required = true)
String medicalRecord
) {
// 執(zhí)行 Spring AI Workflow
WorkflowExecutionResult result = workflowExecutor.execute(
"medical-record-analysis", // 工作流名稱(chēng)
param -> param.put("medicalRecord", medicalRecord)
);
// 返回最終節(jié)點(diǎn)結(jié)果
return result.getNodeResult("generate-report", String.class);
}
}步驟 3:AgentScope 智能體集成 Workflow 工具
創(chuàng)建 ReAct 智能體,將 Workflow 工具注冊(cè)到 Agent 中,實(shí)現(xiàn)“智能體決策 + 工作流執(zhí)行”:
package com.yourpackage.agent;
import io.agentscope.core.agent.ReActAgent;
import io.agentscope.core.model.dashscope.DashScopeChatModel;
import io.agentscope.core.tool.Toolkit;
import io.agentscope.spring.annotation.Agent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
/**
* AgentScope 智能體:集成 Spring AI Workflow 工具
*/
@Agent
public class MedicalAnalysisAgent {
@Autowired
private MedicalRecordWorkflowTool medicalRecordWorkflowTool;
@Autowired
private DashScopeChatModel dashScopeChatModel;
/**
* 創(chuàng)建 ReAct 智能體(核心)
*/
@Bean
public ReActAgent createMedicalAnalysisAgent() {
// 1. 初始化工具包,注冊(cè) Workflow 工具
Toolkit toolkit = Toolkit.createDefault();
toolkit.registration()
.tool(medicalRecordWorkflowTool::execute)
.group("workflow_tools")
.apply();
// 2. 構(gòu)建 ReAct 智能體
return ReActAgent.builder()
.id("medical-analysis-agent")
.name("MedicalAnalysisAgent")
.model(dashScopeChatModel) // 復(fù)用 Spring AI 的 DashScope 模型
.toolkit(toolkit) // 注冊(cè) Workflow 工具
.sysPrompt("""
你是醫(yī)療病歷分析智能體,用戶(hù)輸入病歷文本后,必須調(diào)用「medical_record_workflow_tool」工具執(zhí)行分析,
并返回最終的分析報(bào)告,禁止直接生成結(jié)果。
""")
.build();
}
}步驟 4:業(yè)務(wù)入口(API 調(diào)用示例)
通過(guò) Spring Boot Web 暴露接口,實(shí)現(xiàn)“前端調(diào)用 → Agent 決策 → Workflow 執(zhí)行 → 結(jié)果返回”:
package com.yourpackage.controller;
import io.agentscope.core.agent.ReActAgent;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 業(yè)務(wù)入口:病歷分析接口
*/
@RestController
@RequestMapping("/api/agent")
public class MedicalAgentController {
private final ReActAgent medicalAnalysisAgent;
public MedicalAgentController(ReActAgent medicalAnalysisAgent) {
this.medicalAnalysisAgent = medicalAnalysisAgent;
}
/**
* 智能體 + 工作流 執(zhí)行接口
*/
@PostMapping("/analyze-medical-record")
public ResponseEntity<String> analyzeMedicalRecord(@RequestBody String medicalRecord) {
// 1. 智能體處理用戶(hù)請(qǐng)求(自動(dòng)決策調(diào)用 Workflow 工具)
String response = medicalAnalysisAgent.chat("分析以下病歷:" + medicalRecord).getTextContent();
// 2. 返回結(jié)果
return ResponseEntity.ok(response);
}
}四、關(guān)鍵適配與最佳實(shí)踐
1. 異步模型對(duì)齊(核心)
AgentScope 基于 Reactor 異步編程,Spring AI Workflow 支持 CompletableFuture,需統(tǒng)一異步模型:
// 改造 Workflow 工具為異步執(zhí)行
public CompletableFuture<String> executeAsync(String medicalRecord) {
return CompletableFuture.supplyAsync(() -> {
WorkflowExecutionResult result = workflowExecutor.execute(
"medical-record-analysis",
param -> param.put("medicalRecord", medicalRecord)
);
return result.getNodeResult("generate-report", String.class);
});
}
// AgentScope 智能體調(diào)用異步工具
toolkit.registration()
.tool(medicalRecordWorkflowTool::executeAsync)
.async(true) // 標(biāo)記為異步工具
.apply();2. 上下文數(shù)據(jù)互通
實(shí)現(xiàn) AgentScope 智能體上下文與 Spring AI Workflow 上下文的雙向同步:
// Workflow 工具中注入 Agent 上下文
public String execute(String medicalRecord, @RequestAttribute("agentContext") Map<String, Object> agentContext) {
WorkflowExecutionResult result = workflowExecutor.execute(
"medical-record-analysis",
param -> {
param.put("medicalRecord", medicalRecord);
param.put("agentContext", agentContext); // 傳遞 Agent 上下文
}
);
// 將 Workflow 結(jié)果同步回 Agent 上下文
agentContext.put("workflowResult", result.getNodeResult("generate-report", String.class));
return result.getNodeResult("generate-report", String.class);
}3. 異常處理兜底
為 Workflow 執(zhí)行添加異常捕獲,確保 Agent 穩(wěn)定性:
public String execute(String medicalRecord) {
try {
WorkflowExecutionResult result = workflowExecutor.execute(
"medical-record-analysis",
param -> param.put("medicalRecord", medicalRecord)
);
return result.getNodeResult("generate-report", String.class);
} catch (Exception e) {
// 兜底邏輯:AgentScope 智能體降級(jí)處理
return "病歷分析失?。? + e.getMessage() + ",已觸發(fā)人工審核流程";
}
}4. 可觀測(cè)性集成
復(fù)用 Spring Boot Actuator 監(jiān)控 Workflow 與 Agent 狀態(tài):
# application.yml 新增
management:
endpoints:
web:
exposure:
include: health, metrics, workflows, agents
metrics:
enable: true
endpoint:
workflows:
enabled: true # 暴露 Workflow 執(zhí)行指標(biāo)
agents:
enabled: true # 暴露 AgentScope 智能體指標(biāo)五、總結(jié)
核心集成要點(diǎn)
- 依賴(lài)層:復(fù)用 Spring AI Alibaba 的 DashScope 客戶(hù)端,避免重復(fù)配置模型密鑰;
- 工具層:將 Spring AI Workflow 封裝為 AgentScope 標(biāo)準(zhǔn)工具,符合智能體調(diào)用規(guī)范;
- 執(zhí)行層:對(duì)齊異步模型、上下文數(shù)據(jù)、異常處理,確保二者協(xié)同穩(wěn)定;
- 工程層:復(fù)用 Spring 生態(tài)的可觀測(cè)性、持久化、微服務(wù)能力,降低運(yùn)維成本。
關(guān)鍵參考資源
- AgentScope Java 工具開(kāi)發(fā):https://github.com/agentscope-ai/agentscope-java/blob/main/docs/zh/tool/tool.md
- Spring AI Alibaba Workflow:https://github.com/alibaba/spring-ai-alibaba/tree/main/spring-ai-alibaba-workflow
- Java2AI Graph Core 節(jié)點(diǎn)規(guī)范:https://java2ai.com/docs/frameworks/graph-core/node-definition/
到此這篇關(guān)于AgentScope Java 集成 Spring AI Alibaba Workflow 完整指南的文章就介紹到這了,更多相關(guān)AgentScope Java 集成 Spring AI Alibaba Workflow內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)簡(jiǎn)單連連看游戲
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單連連看游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
從字符串中截取等長(zhǎng)字節(jié)的Java代碼
這篇文章主要介紹了從字符串中截取等長(zhǎng)字節(jié)的Java代碼,有需要的朋友可以參考一下2013-12-12
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(18)
下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你2021-07-07
SpringBoot集成cache緩存的實(shí)現(xiàn)
日常開(kāi)發(fā)中,緩存是解決數(shù)據(jù)庫(kù)壓力的一種方案,本文記錄springboot中使用cache緩存。需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
Mybatis傳遞多個(gè)參數(shù)進(jìn)行SQL查詢(xún)的用法
本文給大家介紹Mybatis傳遞多個(gè)參數(shù)進(jìn)行SQL查詢(xún)的用法的相關(guān)知識(shí),本文還給大家介紹了mybatis通過(guò)Map傳遞多個(gè)參數(shù)和JavaBean傳遞多個(gè)參數(shù),本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-06-06

