Spring AI 系列之二十四 - ModerationModel

之前做個幾個大模型的應用,都是使用Python語言,后來有一個項目使用了Java,并使用了Spring AI框架。隨著Spring AI不斷地完善,最近它發布了1.0正式版,意味著它已經能很好的作為企業級生產環境的使用。對于Java開發者來說真是一個福音,其功能已經能滿足基于大模型開發企業級應用。借著這次機會,給大家分享一下Spring AI框架。

注意由于框架不同版本改造會有些使用的不同,因此本次系列中使用基本框架是 Spring AI-1.0.0,JDK版本使用的是19
代碼參考: https://github.com/forever1986/springai-study

目錄

  • 1 ModerationModel
    • 1.1 關于Moderation
    • 1.2 Spring AI的ModerationModel
  • 2 示例演示

上一章講了AudioModels(音頻大模型),這一章再講一個ModerationModel(內容審核大模型)

1 ModerationModel

1.1 關于Moderation

在互聯網時代,用戶每天會生成海量的內容,現在有了大模型那么網上的內容增加量幾乎是幾何倍增長。一方面享受到了大模型的生產力和創造力的紅利,但是另一方面,對生成內容的安全性與合規性也提出了更高的要求。因此對于一些場景比如評論、彈幕、論壇、短視頻、直播等領域,用戶發表的內容可能包含一些敏感的、違規的內容。這不僅會對用戶體驗造成影響,更重要的,可能會給企業帶來輿論和法律風險。因此需要對其內容進行審核。
ModerationModel :也叫內容審核模型,旨在基于文本、圖像、音頻、視頻的檢測技術,可自動檢測涉黃、涉暴、圖文違規等內容,對用戶上傳的文字、圖片、音視頻進行內容審核,以滿足上傳要求,幫助客戶降低業務違規風險。以下是常見的違規事項(不同模型可能內容不同,但是大同小異):

類型說明
Sexual性相關(色情等)
Hate and Discrimination仇恨與歧視
Violence and Threats暴力與威脅
Dangerous and Criminal危險且犯罪的
Self-Harm自殘,自我傷害
Health包含或試圖提供詳細或個性化醫療建議的內容。
Financial包含或試圖提供詳細或個性化財務建議的內容。
Law包含或試圖提供詳細或定制化法律建議的內容。
PII包含個人信息

1.2 Spring AI的ModerationModel

在Spring AI中抽象了一個ModerationModel接口,定義了一個通用的用于內容審核的 AI 模型。它擴展了Model接口,以處理與各種類型 AI 模型的交互(目前該接口來看只適合于文本類型內容審核)。其源碼如下:

import org.springframework.ai.model.Model;/*** 提供了一個單一的方法“call”方法*/
@FunctionalInterface
public interface ModerationModel extends Model<ModerationPrompt, ModerationResponse> {ModerationResponse call(ModerationPrompt request);}

同時提供的入參ModerationPrompt和出參ModerationResponse
其中ModerationPrompt有2個重要的變量:

  • ModerationMessage:表示一條需進行審核處理的消息,其中包含文本內容。此類別為可提交至審核流程的消息提供了基本結構。
  • ModerationOptions:提供一些模型可用的配置,只是個接口,需要模型廠商需要自己實現自己的配置

其中ModerationResponse有2個重要的變量:

  • ModerationResponseMetadata:定義了與審核回復相關的元數據,擴展了基礎回復接口。
  • Generation:來自審核流程的響應。它包含了審核生成的元數據以及審核對象。

比較遺憾的是目前Spring AI中的ModerationModel只實現了OpeanAI和MistralAI兩個模型,不過相信后續會有更多廠商模型接入。

2 示例演示

代碼參考lesson20子模塊

示例說明:由于目前Spring AI只有OpeanAI和MistralAI兩個模型實現了ModerationModel,這里利用Mistral AI模型作為演示,只是它是免費且沒有被禁用

1)申請MistralAI的API KEY,訪問:https://console.mistral.ai/api-keys

在這里插入圖片描述

說明:沒注冊的朋友可能要注冊一下,需要使用一個郵箱和手機

2)新建lesson20子模塊,其pom引入如下:

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-model-mistral-ai</artifactId></dependency>
</dependencies>

3)配置application.properties文件

spring.ai.mistralai.api-key=你的Mistral API KEY

4)新建演示類ModerationController :

import org.springframework.ai.mistralai.moderation.MistralAiModerationModel;
import org.springframework.ai.mistralai.moderation.MistralAiModerationOptions;
import org.springframework.ai.moderation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ModerationController {private final MistralAiModerationModel mistralAiModerationModel;@Autowiredpublic ModerationController(MistralAiModerationModel mistralAiModerationModel) {this.mistralAiModerationModel = mistralAiModerationModel;}@GetMapping("/ai/moderation")public void moderation(@RequestParam(value = "message", defaultValue = "這個事情只有男的可以做,女的做不到!") String message) {// 說明一下,這個message只是為了測試效果,并不代表作者任何觀點MistralAiModerationOptions moderationOptions = MistralAiModerationOptions.builder().model("mistral-moderation-latest").build();ModerationPrompt moderationPrompt = new ModerationPrompt(message, moderationOptions);ModerationResponse moderationResponse = mistralAiModerationModel.call(moderationPrompt);// 獲取審核結果Moderation moderation = moderationResponse.getResult().getOutput();// Access the moderation results (there's usually only one, but it's a list)for (ModerationResult result : moderation.getResults()) {System.out.println("最終結果:");System.out.println("Flagged: " + result.isFlagged());// Access categoriesCategories categories = result.getCategories();System.out.println("\n類型,true表示包含:");// 觸發法律System.out.println("Law: " + categories.isLaw());// 觸發金融限制System.out.println("Financial: " + categories.isFinancial());// 包括個人信息System.out.println("PII: " + categories.isPii());// 包括色情System.out.println("Sexual: " + categories.isSexual());// 包括偏見、敵意System.out.println("Hate: " + categories.isHate());// 包括騷擾信息System.out.println("Harassment: " + categories.isHarassment());// 包括宣傳、鼓勵自殺自殘等System.out.println("Self-Harm: " + categories.isSelfHarm());// 包括未成年人色情System.out.println("Sexual/Minors: " + categories.isSexualMinors());// 包括偏見、敵意的恐嚇等System.out.println("Hate/Threatening: " + categories.isHateThreatening());// 包括暴力血腥層面System.out.println("Violence/Graphic: " + categories.isViolenceGraphic());// 包括自殺自殘傾向System.out.println("Self-Harm/Intent: " + categories.isSelfHarmIntent());// 包括引導自殺自殘System.out.println("Self-Harm/Instructions: " + categories.isSelfHarmInstructions());// 包括自殺自殘恐嚇System.out.println("Harassment/Threatening: " + categories.isHarassmentThreatening());// 包括暴力System.out.println("Violence: " + categories.isViolence());// Access category scoresCategoryScores scores = result.getCategoryScores();System.out.println("\n以下個各種類型分數(分數越高代表可能性越高:");System.out.println("Law: " + scores.getLaw());System.out.println("Financial: " + scores.getFinancial());System.out.println("PII: " + scores.getPii());System.out.println("Sexual: " + scores.getSexual());System.out.println("Hate: " + scores.getHate());System.out.println("Harassment: " + scores.getHarassment());System.out.println("Self-Harm: " + scores.getSelfHarm());System.out.println("Sexual/Minors: " + scores.getSexualMinors());System.out.println("Hate/Threatening: " + scores.getHateThreatening());System.out.println("Violence/Graphic: " + scores.getViolenceGraphic());System.out.println("Self-Harm/Intent: " + scores.getSelfHarmIntent());System.out.println("Self-Harm/Instructions: " + scores.getSelfHarmInstructions());System.out.println("Harassment/Threatening: " + scores.getHarassmentThreatening());System.out.println("Violence: " + scores.getViolence());}}
}

5)新建啟動類Lesson20Application :

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Lesson20Application {public static void main(String[] args) {SpringApplication.run(Lesson20Application.class, args);}}

6)演示效果:

http://localhost:8080/ai/moderation

在這里插入圖片描述

說明:從上圖可以看到,輸入一句具有歧視的內容,Hate就是true,其代表的分數也接近1。

結語:本章講述了什么是內容審核模型,并對Spring AI 中的ModerationModel相關類和接口進行講解。最后通過使用Mistral模型演示了效果。看到有內容審核模型,有朋友可能會想到大模型經常出現幻覺,那么應該就有評估是否存在幻覺的測試,Spring AI也提供了Evaluation這樣的模塊實現模型評估測試,下一章將著重介紹這一部分的使用。

Spring AI系列上一章:《Spring AI 系列之二十三 - AudioModels》

Spring AI系列下一章:《Spring AI 系列之二十五 - Evaluation模型評估》

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

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

相關文章

在 macOS 上 安裝最新 Python 和 pip

文章目錄方法一&#xff1a;使用 Homebrew&#xff08;推薦&#xff09;方法二&#xff1a;使用 pyenv&#xff08;管理多個 Python 版本&#xff09;方法三&#xff1a;從官網下載安裝包升級 pip驗證安裝方法一&#xff1a;使用 Homebrew&#xff08;推薦&#xff09; 1. 安裝…

新能源電池廠自動化應用:Modbus TCP轉DeviceNet實踐

一、項目背景在新能源電池廠的生產過程中&#xff0c;提升自動化水平對提高生產效率和產品質量至關重要。我們的生產線上&#xff0c;施耐德PLC負責整體的生產流程控制&#xff0c;采用Modbus TCP協議進行數據傳輸&#xff0c;它基于以太網&#xff0c;傳輸速度快、穩定性高&am…

Java進階3:Java集合框架、ArrayList、LinkedList、HashSet、HashMap和他們的迭代器

Java集合框架 集合框架被設計成的目標&#xff1a;高性能、高效 允許不同類型的結合&#xff0c;以類似的方式進行工作&#xff0c;有高度的互操作性 對一個集合的擴展和適應必須是簡單的兩種容器&#xff1a;集合Collection、圖Map 集合接口被分為了三種子類型&#xff1a;Lis…

筆記/使用Excel進行財務預測

文章目錄金融預測的決策與數據收集決定財務問題收集財務數據清理與合并財務數據解釋與應用預測結果使用excel進行財務回歸分析回歸預測的步驟解釋回歸結果在 Excel 中執行預測財務分析指標財務分析常用指標一覽表財務指標的相關性對競爭對手進行基準測試財務指標的趨勢分析持續…

力扣1287:有序數組中出現次數超過25%的元素

力扣1287:有序數組中出現次數超過25%的元素題目思路代碼題目 給你一個非遞減的 有序 整數數組&#xff0c;已知這個數組中恰好有一個整數&#xff0c;它的出現次數超過數組元素總數的 25%。 請你找到并返回這個整數 思路 哈希表秒了 代碼 class Solution { public:int fi…

如何用 Z.ai 生成PPT,一句話生成整套演示文檔

大家好&#xff0c;這里是K姐。 一個幫你追蹤最新AI應用的女子。 最近朋友給我分享了一個好玩的頁面截圖。 一眼看過去&#xff0c;就感覺這PPT的文字排版很有人工味。 我立馬就去試了一下&#xff0c;才發現它根本不是傳統的 PPT&#xff0c;而是一種網頁式的 Slides 。 做…

C/C++ 編程:掌握靜態庫與動態庫的編譯

在 C/C 項目開發中&#xff0c;理解并掌握如何編譯和使用庫文件是至關重要的一環。庫允許你將常用的函數和代碼模塊化&#xff0c;從而提高代碼重用性、簡化項目管理并縮短編譯時間。最常見的兩種庫類型是靜態庫 (.a) 和動態庫 (.so)。它們各有優缺點&#xff0c;適用于不同的開…

汽車安全 | 汽車安全入門

引言 汽車安全不僅僅是對汽車/車輛進行物理入侵。這只是很小且簡單的一部分。當你以攻擊者/對手的思維去看待一輛聯網汽車時&#xff0c;你關注的是整個車輛生態系統。這不僅包括它如何與外部實體通信&#xff0c;也包括它在車內如何運作。 汽車是主要的交通工具&#xff0c;…

CLIP與SIGLIP對比淺析

CLIP 和 SIGLIP 的核心區別在于損失函數的設計&#xff1a;CLIP 使用基于 softmax 的對比損失&#xff08;InfoNCE&#xff09;&#xff0c;強制正樣本在全局對比中壓倒所有負樣本&#xff0c;計算成本高且受限于負樣本數量&#xff1b;SIGLIP 改用基于 sigmoid 的二元分類損失…

移動管家手機控車便捷性如何

移動管家4G手機控車-全面升級一鍵啟動、無鑰匙進入、手機啟動、手機開關鎖、手機開尾箱、手機尋車、車輛診斷、GPS北斗定位、電子圍欄、車輛授權、車輛防盜搶、胎壓檢測、預約啟動、車窗控制、車況提醒等功&#xff1b;移動管家手機控車系統&#xff08;以“移動管家控車APP”為…

MySQL 8.4.4詳細下載安裝配置

1、下載mysql8.4.4文件&#xff0c;取zip文件 mysql8.4.4下載路徑 MySQL 5.7.31詳細下載安裝配置 2、配置環境變量 1.系統—>高級系統設置—>環境變量—>系統變量 在系統變量中點擊新建&#xff0c;變量名為量名為&#xff1a;MYSQL_HOME&#xff0c;添加你的mys…

在 Linux 上安裝 `pgvector`(這是一個 PostgreSQL 的向量類型擴展,常用于處理嵌入向量,便于進行向量相似度搜索)

全文 4000 字&#xff0c;配圖配碼&#xff0c;已在多家企業落地驗證。閱讀完如有收獲&#xff0c;文末投票告訴我你最關注的方向&#xff0c;我會在下一篇文章里繼續深入。 0. pgvector 簡介 pgvector 是一款 PostgreSQL 原生向量數據類型擴展&#xff0c;核心能力&#xff1…

【項目實戰】——深度學習.全連接神經網絡

目錄 1.使用全連接網絡訓練和驗證MNIST數據集 2.使用全連接網絡訓練和驗證CIFAR10數據集 1.使用全連接網絡訓練和驗證MNIST數據集 import torch from torch import nn from torchvision import datasets, transforms from torch.utils.data import DataLoader from torch im…

嵌入式學習的第三十四天-進程間通信-TCP

一、TCPTCP : 傳輸控制協議 傳輸層1. TCP特點(1).面向連接,避免部分數據丟失 (2).安全、可靠 (3).面向字節流 (4).占用資源開銷大2.TCP安全可靠機制三次握手:指建立tcp連接時&#xff0c;需要客戶端和服務端總共發送三次報文確認連接。確保雙方均已做好 收發…

【爬蟲】06 - 自動化爬蟲selenium

自動化爬蟲selenium 文章目錄自動化爬蟲selenium一&#xff1a;Selenium簡介1&#xff1a;什么是selenium2&#xff1a;安裝準備二&#xff1a;元素定位1&#xff1a;id 定位2&#xff1a;name 定位3&#xff1a;class 定位4&#xff1a;tag 定位5&#xff1a;xpath 定位(最常用…

2025年中國移動鴻鵠大數據實訓營(大數據方向)kafka講解及實踐-第2次作業指導

書接上回&#xff0c;第二次作業比較容易解決&#xff0c;我問了ai&#xff0c;讓他對我進行指導&#xff0c;按照它提供的步驟&#xff0c;我完成了本次實驗&#xff0c;接下來我會標注出需要注意的細節&#xff0c;指導大家完成此次任務。 &#x1f3af; 一、作業目標 ??…

三十七、【高級特性篇】定時任務:基于 APScheduler 實現測試計劃的靈活調度

三十七、【高級特性篇】定時任務:基于 APScheduler 實現測試計劃的靈活調度 前言 準備工作 第一部分:后端實現 - `APScheduler` 集成與任務調度 1. 安裝 `django-apscheduler` 2. 配置 `django-apscheduler` 3. 數據庫遷移 4. 創建調度觸發函數 5. 啟動 APScheduler 調度器 6…

RabbitMQ--消息順序性

看本章之前強烈建議先去看博主的這篇博客 RabbitMQ--消費端單線程與多線程-CSDN博客 一、消息順序性概念 消息順序性是指消息在生產者發送的順序和消費者接收處理的順序保持一致。 二、RabbitMQ 順序性保證機制 情況順序保證情況備注單隊列&#xff0c;單消費者消息嚴格按發送順…

.net core接收對方傳遞的body體里的json并反序列化

1、首先我在通用程序里有一個可以接收對象型和數組型json串的反序列化方法public static async Task<Dictionary<string, string>> AllParameters(this HttpRequest request){Dictionary<string, string> parameters QueryParameters(request);request.Enab…

(10)機器學習小白入門 YOLOv:YOLOv8-cls 模型評估實操

YOLOv8-cls 模型評估實操 (1)機器學習小白入門YOLOv &#xff1a;從概念到實踐 (2)機器學習小白入門 YOLOv&#xff1a;從模塊優化到工程部署 (3)機器學習小白入門 YOLOv&#xff1a; 解鎖圖片分類新技能 (4)機器學習小白入門YOLOv &#xff1a;圖片標注實操手冊 (5)機器學習小…