spring-ai 工作流

目錄

    • 工作流概念
    • 工作流程圖
    • spring-boot 編碼
      • 定義節點 (Node)
      • 定義節點圖StateGraph
      • controller測試
        • 瀏覽器測試用戶輸入

工作流概念

工作流是以相對固化的模式來人為地拆解任務,將一個大任務拆解為包含多個分支的固化流程。工作流的優勢是確定性強,模型作為流程中的一個節點起到的更多是一個分類決策、內容生成的職責,因此它更適合意圖識別等類別屬性強的應用場景。

參考文檔:https://java2ai.com/docs/1.0.0.2/get-started/workflow/?spm=4347728f.7cee0e64.0.0.39076dd1jbppqZ

工作流程圖

商品評價分類流程圖:
在這里插入圖片描述

如用戶反饋

  • This product is excellent, I love it!
    則輸出:Praise, no action taken.
    說明:很好,不需要改進措施

  • The product broke after one day, very disappointed."
    則輸出:product quality
    說明:有問題,產品質量問題

spring-boot 編碼

使用:Spring AI Alibaba Graph

附maven的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.4.6</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo-spring-test</artifactId><version>0.0.1-SNAPSHOT</version><name>demo-spring-test</name><description>Demo project for Spring Boot</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>17</java.version><spring-ai.version>1.0.0</spring-ai.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring AI Alibaba(通義大模型支持) --><dependency><groupId>com.alibaba.cloud.ai</groupId><artifactId>spring-ai-alibaba-starter</artifactId><version>1.0.0-M6.1</version></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-core</artifactId><version>1.0.0-M6</version></dependency><dependency><groupId>com.alibaba.cloud.ai</groupId><artifactId>spring-ai-alibaba-autoconfigure</artifactId><version>1.0.0-M6.1</version></dependency><!-- 引入 Graph 核心依賴 --><dependency><groupId>com.alibaba.cloud.ai</groupId><artifactId>spring-ai-alibaba-graph-core</artifactId><version>1.0.0.2</version></dependency><dependency><groupId>com.alibaba.cloud.ai</groupId><artifactId>spring-ai-alibaba-starter-document-parser-tika</artifactId><version>1.0.0.2</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

定義節點 (Node)

創建工作流中的核心節點,包括兩個文本分類節點和一個記錄節點

分類

// 評價正負分類節點
QuestionClassifierNode feedbackClassifier = QuestionClassifierNode.builder().chatClient(chatClient).inputTextKey("input").categories(List.of("positive feedback", "negative feedback")).classificationInstructions(List.of("Try to understand the user's feeling when he/she is giving the feedback.")).build();
// 負面評價具體問題分類節點
QuestionClassifierNode specificQuestionClassifier = QuestionClassifierNode.builder().chatClient(chatClient).inputTextKey("input").categories(List.of("after-sale service", "transportation", "product quality", "others")).classificationInstructions(List.of("What kind of service or help the customer is trying to get from us? " +"Classify the question based on your understanding.")).build();

記錄節點 RecordingNode:


import com.alibaba.cloud.ai.graph.OverAllState;
import com.alibaba.cloud.ai.graph.action.NodeAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.util.HashMap;
import java.util.Map;public class RecordingNode implements NodeAction {private static final Logger logger = LoggerFactory.getLogger(RecordingNode.class);@Overridepublic Map<String, Object> apply(OverAllState state) throws Exception {String feedback = (String) state.value("classifier_output").get();Map<String, Object> updatedState = new HashMap<>();if (feedback.contains("positive")) {logger.info("Received positive feedback: {}", feedback);updatedState.put("solution", "Praise, no action taken.");} else {logger.info("Received negative feedback: {}", feedback);updatedState.put("solution", feedback);}return updatedState;}}

定義節點圖StateGraph

StateGraph graph = new StateGraph("Consumer Service Workflow Demo", stateFactory)// 添加節點.addNode("feedback_classifier", node_async(feedbackClassifier)).addNode("specific_question_classifier", node_async(specificQuestionClassifier)).addNode("recorder", node_async(recordingNode))// 定義邊(流程順序).addEdge(START, "feedback_classifier")  // 起始節點.addConditionalEdges("feedback_classifier",edge_async(new CustomerServiceController.FeedbackQuestionDispatcher()),Map.of("positive", "recorder", "negative", "specific_question_classifier")).addConditionalEdges("specific_question_classifier",edge_async(new CustomerServiceController.SpecificQuestionDispatcher()),Map.of("after-sale", "recorder", "transportation", "recorder","quality", "recorder", "others", "recorder")).addEdge("recorder", END);  // 結束節點System.out.println("\n");return graph;

完整代碼:

import com.alibaba.cloud.ai.graph.OverAllState;
import com.alibaba.cloud.ai.graph.OverAllStateFactory;
import com.alibaba.cloud.ai.graph.StateGraph;
import com.alibaba.cloud.ai.graph.exception.GraphStateException;
import com.alibaba.cloud.ai.graph.node.QuestionClassifierNode;
import com.alibaba.cloud.ai.graph.state.strategy.ReplaceStrategy;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.List;
import java.util.Map;import static com.alibaba.cloud.ai.graph.StateGraph.END;
import static com.alibaba.cloud.ai.graph.StateGraph.START;
import static com.alibaba.cloud.ai.graph.action.AsyncEdgeAction.edge_async;
import static com.alibaba.cloud.ai.graph.action.AsyncNodeAction.node_async;@Configuration
public class WorkflowAutoconfiguration {@Beanpublic StateGraph workflowGraph(ChatModel chatModel) throws GraphStateException {ChatClient chatClient = ChatClient.builder(chatModel).defaultAdvisors(new SimpleLoggerAdvisor()).build();RecordingNode recordingNode = new RecordingNode();// 評價正負分類節點QuestionClassifierNode feedbackClassifier = QuestionClassifierNode.builder().chatClient(chatClient).inputTextKey("input").categories(List.of("positive feedback", "negative feedback")).classificationInstructions(List.of("Try to understand the user's feeling when he/she is giving the feedback.")).build();// 負面評價具體問題分類節點QuestionClassifierNode specificQuestionClassifier = QuestionClassifierNode.builder().chatClient(chatClient).inputTextKey("input").categories(List.of("after-sale service", "transportation", "product quality", "others")).classificationInstructions(List.of("What kind of service or help the customer is trying to get from us? " +"Classify the question based on your understanding.")).build();// 定義一個 OverAllStateFactory,用于在每次執行工作流時創建初始的全局狀態對象OverAllStateFactory stateFactory = () -> {OverAllState state = new OverAllState();state.registerKeyAndStrategy("input", new ReplaceStrategy());state.registerKeyAndStrategy("classifier_output", new ReplaceStrategy());state.registerKeyAndStrategy("solution", new ReplaceStrategy());return state;};StateGraph graph = new StateGraph("Consumer Service Workflow Demo", stateFactory).addNode("feedback_classifier", node_async(feedbackClassifier)).addNode("specific_question_classifier", node_async(specificQuestionClassifier)).addNode("recorder", node_async(recordingNode))// 定義邊(流程順序).addEdge(START, "feedback_classifier")  // 起始節點.addConditionalEdges("feedback_classifier",edge_async(new CustomerServiceController.FeedbackQuestionDispatcher()),Map.of("positive", "recorder", "negative", "specific_question_classifier")).addConditionalEdges("specific_question_classifier",edge_async(new CustomerServiceController.SpecificQuestionDispatcher()),Map.of("after-sale", "recorder", "transportation", "recorder","quality", "recorder", "others", "recorder")).addEdge("recorder", END);  // 結束節點System.out.println("\n");return graph;}}

controller測試

  • CustomerServiceController 完整代碼
import java.util.HashMap;
import java.util.Map;import com.alibaba.cloud.ai.graph.CompiledGraph;
import com.alibaba.cloud.ai.graph.exception.GraphStateException;
import com.alibaba.cloud.ai.graph.OverAllState;
import com.alibaba.cloud.ai.graph.StateGraph;
import com.alibaba.cloud.ai.graph.action.EdgeAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/customer")
public class CustomerServiceController {private static final Logger logger = LoggerFactory.getLogger(CustomerServiceController.class);private CompiledGraph compiledGraph;public CustomerServiceController(@Qualifier("workflowGraph") StateGraph stateGraph) throws GraphStateException {this.compiledGraph = stateGraph.compile();}/*** localhost:8080/customer/chat?query=The product broke after one day, very disappointed.*/@GetMapping("/chat")public String simpleChat(String query) throws Exception {logger.info("simpleChat: {}", query);return compiledGraph.invoke(Map.of("input", query)).get().value("solution").get().toString();}public static class FeedbackQuestionDispatcher implements EdgeAction {@Overridepublic String apply(OverAllState state) throws Exception {/*** 反饋的是商品的負面內容* 分類為:negative*/String classifierOutput = (String) state.value("classifier_output").orElse("");logger.info("classifierOutput: {}", classifierOutput);if (classifierOutput.contains("positive")) {return "positive";}return "negative";}}public static class SpecificQuestionDispatcher implements EdgeAction {@Overridepublic String apply(OverAllState state) throws Exception {/*** 反饋的是產品的質量* 分類為:quality*/String classifierOutput = (String) state.value("classifier_output").orElse("");logger.info("classifierOutput: {}", classifierOutput);Map<String, String> classifierMap = new HashMap<>();classifierMap.put("after-sale", "after-sale");classifierMap.put("quality", "quality");classifierMap.put("transportation", "transportation");for (Map.Entry<String, String> entry : classifierMap.entrySet()) {if (classifierOutput.contains(entry.getKey())) {return entry.getValue();}}return "others";}}}
瀏覽器測試用戶輸入

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/86851.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/86851.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/86851.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

重頭開始學ROS(6)---Eigen庫安裝與使用

Eigen庫 矩陣運算是一種非常重要的運算方式&#xff0c;在Matlab中&#xff0c;矩陣運算可以輕松的實現&#xff0c;但在C這種偏底層的語言中&#xff0c;若不借助第三方庫&#xff0c;矩陣運算需要我們進行較為復雜的代碼設計。Eigen庫是一個用于線性運算的C模板庫&#xff0…

【STM32】外部中斷

STM32 外部中斷&#xff08;EXTI&#xff09;概述 這篇文章結合示例代碼&#xff0c;系統性地講述 STM32 外部中斷&#xff08;EXTI&#xff09;實驗的原理、以及配置流程。目的在于輔助讀者掌握STM32F1 外部中斷機制。 STM32F1xx官方資料&#xff1a;《STM32中文參考手冊V10》…

LeetCode Hot100 算法題 (矩陣篇)

1、73. 矩陣置零 給定一個 m x n 的矩陣&#xff0c;如果一個元素為 0 &#xff0c;則將其所在行和列的所有元素都設為 0 。請使用 原地 算法。 示例 1&#xff1a; 輸入&#xff1a;matrix [[1,1,1],[1,0,1],[1,1,1]] 輸出&#xff1a;[[1,0,1],[0,0,0],[1,0,1]]// 將第一行…

Flutter基礎(項目創建)

一、使用命令行創建項目 1. 確認 Flutter 環境正常 要保證 Flutter SDK 已經正確安裝&#xff0c;并且環境變量配置無誤。可以通過執行以下命令來驗證&#xff1a; flutter doctor 要保證所有檢查項都顯示綠色對勾&#xff0c;要是有問題&#xff0c;可按照提示進行修復。 …

【Actix Web】Rust Web開發實戰:Actix Web框架全面指南(2025企業級應用版)

? 在2025年高并發、低延遲成為Web服務核心指標的背景下&#xff0c;??Actix Web憑借異步Actor模型與零成本抽象??&#xff0c;成為Rust生態中生產環境部署率最高的Web框架。本指南深入解析Actix Web 4.0核心技術&#xff0c;覆蓋??百萬級并發架構設計??、??內存安全…

HTML之常用基礎標簽

HTML之常用基礎標簽 一、HTML文檔基本結構標簽1. <html>標簽2. <head>標簽3. <body>標簽 二、文本相關基礎標簽1. 標題標簽&#xff08;<h1> - <h6>&#xff09;2. 段落標簽&#xff08;<p>&#xff09;3. 換行標簽&#xff08;<br>…

外鍵列索引優化:加速JOIN查詢的關鍵

在使用數據庫時&#xff0c;特別是在執行涉及JOIN操作的查詢時&#xff0c;優化外鍵列的索引是非常重要的。外鍵通常用于建立表之間的關聯&#xff0c;而JOIN操作則是基于這些外鍵列來實現的。下面是一些關鍵步驟和技巧&#xff0c;可以幫助你優化外鍵列的索引&#xff0c;從而…

2025年 UI 自動化框架使用排行

??親愛的技術愛好者們,熱烈歡迎來到 Kant2048 的博客!我是 Thomas Kant,很開心能在CSDN上與你們相遇~?? 本博客的精華專欄: 【自動化測試】 【測試經驗】 【人工智能】 【Python】 </

【軟考高項論文】論信息系統項目的整體管理

摘要 在信息系統項目的管理中&#xff0c;整體管理處于核心地位&#xff0c;對項目全局規劃與協調起著關鍵作用&#xff0c;保障項目各階段目標一致且高效執行。本文結合作者參與的 2024 年 6 月啟動的信息系統項目&#xff0c;深入探討項目整體管理的過程&#xff0c;著重闡述…

(4)Wireshark捕獲設置

1.簡介 WireShark的強大之處就在于不用你再做任何配置就可以抓取http或者https的包。主要是講解和分享如何使用WireShark抓包。 2.運行Wireshark 安裝好 Wireshark 以后&#xff0c;就可以運行它來捕獲數據包了。方法如下&#xff1a; 1.在 Windows 的“開始”菜單中&#…

智慧校園電子班牌系統源碼的開發與應用,基于Java/SpringBoot后端、Vue2前端、MySQL5.7數據庫

智慧校園系統源碼&#xff0c;智慧班牌源碼&#xff0c;java語言 技術棧&#xff1a; ?后端開發?&#xff1a;采用Java語言和Spring Boot框架進行開發。Java是一種廣泛使用的、面向對象的編程語言&#xff0c;而Spring Boot是基于Spring框架的快速應用開發框架&#xff0c;能…

工程優化——WebSocket、WSS(WebSocket Secure)和SSE(Server-Sent Events)通信對比

WebSocket、WSS&#xff08;WebSocket Secure&#xff09;和SSE&#xff08;Server-Sent Events&#xff09;是三種常見的實時通信技術&#xff0c;它們的核心區別在于通信方向、協議實現、數據格式和適用場景。以下是分維度的詳細解釋&#xff0c;并附帶Python示例和應用場景選…

【TiDB 社區智慧合集】 TiDB x 運營商|掌上營業廳、賬務、物聯網等多核心業務場景的實戰應用案例

作者&#xff1a; Billmay表妹 原文來源&#xff1a; https://tidb.net/blog/bb1467af 在信息基礎設施國產化戰略加速落地的背景下&#xff0c;電信及廣電領域正迎來數據庫國產化替代的關鍵轉型期。TiDB 憑借自身技術創新優勢&#xff0c;深度攜手各大運營商&#xff0c;以全…

Java 17 下 Spring Boot 與 Pulsar 隊列集成實戰:生產者與消費者實現指南

Pulsar隊列與Springboot集成有2種模式&#xff1a;官方pulsar-client 或社區Starter&#xff08;如pulsar-spring-boot-starter&#xff09; 如果考慮最新、最快、最齊全的功能&#xff0c;使用官方pulsar-client如果考慮快速低成本接入&#xff0c;使用社區Starter&#xff0…

《Go語言高級編程》RPC 入門

《Go語言高級編程》RPC 入門 一、什么是 RPC&#xff1f; RPC&#xff08;Remote Procedure Call&#xff0c;遠程過程調用&#xff09;是分布式系統中不同節點間的通信方式&#xff0c;允許程序像調用本地函數一樣調用遠程服務的方法。 Go 語言的標準庫 net/rpc 提供了基礎的…

第N5周:Pytorch文本分類入門

&#x1f368; 本文為&#x1f517;365天深度學習訓練營中的學習記錄博客 &#x1f356; 原作者&#xff1a;K同學啊 一、前期準備 1.加載數據 import torch import torch.nn as nn import torchvision from torchvision import transforms,datasets import os,PIL,p…

uniappx 安卓app項目本地打包運行,騰訊地圖報錯:‘鑒權失敗,請檢查你的key‘

根目錄下添加 AndroidManifest.xml 文件&#xff0c; <application><meta-data android:name"TencentMapSDK" android:value"騰訊地圖申請的key" /> </application> manifest.json 文件中添加&#xff1a; "app": {"…

【向上教育】結構化面試開口秘籍.pdf

向 上 教 育 XI A N G S H A N G E D U C A T I O N 結構化 面試 開口秘笈 目 錄 第一章 自我認知類 ........................................................................................................................... 2 第二章 工作關系處理類 .......…

Webpack 熱更新(HMR)原理詳解

&#x1f525; Webpack 熱更新&#xff08;HMR&#xff09;原理詳解 &#x1f4cc; 本文適用于 Vue、React 等使用 Webpack 的項目開發者&#xff0c;適配 Vue CLI / 自定義 Webpack 項目。 &#x1f3af; 一、什么是 HMR&#xff1f; Hot Module Replacement 是 Webpack 提供的…

MySQL索引完全指南

一、索引是什么&#xff1f;為什么這么重要&#xff1f; 索引就像字典的目錄 想象一下&#xff0c;你要在一本1000頁的字典里找"程序員"這個詞&#xff0c;你會怎么做&#xff1f; 沒有目錄&#xff1a;從第1頁開始一頁一頁翻&#xff0c;可能要翻500頁才能找到有…