SpringBoot集成Spring AI構(gòu)建AI智能客服模塊
一、前言
Spring Boot是Java最主流的框架,Spring官方也適時(shí)推出了Spring AI項(xiàng)目,讓我們可以在Spring Boot應(yīng)用中輕松集成AI能力。本文手把手教你搭建一個(gè)AI智能客服模塊。
二、創(chuàng)建Spring Boot項(xiàng)目
2.1 使用Spring Initializr創(chuàng)建項(xiàng)目
訪問(wèn) https://start.spring.io/ ,勾選以下依賴:
- Spring Web
- Spring AI(2024版本)
- Thymeleaf(可選,用于簡(jiǎn)單頁(yè)面展示)
2.2 配置pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring AI OpenAI -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>2024.0.0.0</version>
</dependency>
</dependencies>三、配置API Key
在 application.yml 中配置:
OpenAI配置
spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
chat:
options:
model: gpt-3.5-turbo
temperature: 0.7阿里通義千問(wèn)配置
spring:
ai:
dashscope:
api-key: ${DASHSCOPE_API_KEY}
chat:
options:
model: qwen-turbo四、開發(fā)AI對(duì)話服務(wù)
4.1 Service層
package com.example.ai.service;
import org.springframework.ai.chat.*;
import org.springframework.ai.chat.messages.*;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AIService {
private final ChatClient chatClient;
public AIService(ChatClient chatClient) {
this.chatClient = chatClient;
}
/**
* 簡(jiǎn)單對(duì)話(無(wú)上下文)
*/
public String chat(String userMessage) {
return chatClient.call(userMessage);
}
/**
* 帶上下文的對(duì)話(記住對(duì)話歷史)
*/
public String chatWithContext(String userMessage, ChatMemory chatMemory) {
UserMessage userMsg = new UserMessage(userMessage);
ChatResponse response = chatClient.call(
new Prompt(
List.of(userMsg),
ChatOptions.builder()
.model("gpt-3.5-turbo")
.temperature(0.7f)
.build()
)
);
return response.getResult().getOutput().getText();
}
/**
* 系統(tǒng)提示詞(設(shè)定AI角色)
*/
public String chatAsCustomerService(String userMessage) {
SystemPromptTemplate systemPrompt = new SystemPromptTemplate("""
你是一個(gè)專業(yè)的電商客服,名字叫小e。
你的職責(zé)是:
1. 熱情友好地回復(fù)客戶咨詢
2. 了解客戶需求,給出專業(yè)建議
3. 引導(dǎo)客戶下單,解答售后問(wèn)題
請(qǐng)用簡(jiǎn)潔、親切的語(yǔ)言回復(fù)。
""");
UserMessage userMsg = new UserMessage(userMessage);
Prompt prompt = new Prompt(List.of(userMsg));
return chatClient.call(prompt).getResult().getOutput().getText();
}
}
4.2 Controller層
package com.example.ai.controller;
import com.example.ai.service.AIService;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/ai")
public class AIController {
private final AIService aiService;
public AIController(AIService aiService) {
this.aiService = aiService;
}
/**
* 普通對(duì)話接口
* 調(diào)用示例:GET /api/ai/chat?message=這件商品有優(yōu)惠嗎
*/
@GetMapping("/chat")
public String chat(@RequestParam String message) {
return aiService.chat(message);
}
/**
* 扮演客服角色對(duì)話
*/
@GetMapping("/customer-service")
public String customerService(@RequestParam String message) {
return aiService.chatAsCustomerService(message);
}
/**
* POST請(qǐng)求,支持JSON body
*/
@PostMapping("/chat")
public String chatPost(@RequestBody ChatRequest request) {
return aiService.chatAsCustomerService(request.getMessage());
}
}
class ChatRequest {
private String message;
public String getMessage() { return message; }
public void setMessage(String message) { this.message = message; }
}
五、測(cè)試驗(yàn)證
5.1 啟動(dòng)應(yīng)用
mvn spring-boot:run
5.2 接口測(cè)試
# 普通對(duì)話 curl "http://localhost:8080/api/ai/chat?message=Java如何定義變量" # 客服角色對(duì)話 curl "http://localhost:8080/api/ai/customer-service?message=這件裙子有幾種顏色"
5.3 Postman測(cè)試
POST http://localhost:8080/api/ai/chat
Content-Type: application/json
{
"message": "你們的退貨政策是怎樣的?"
}
到此這篇關(guān)于SpringBoot集成Spring AI構(gòu)建AI智能客服模塊的文章就介紹到這了,更多相關(guān)SpringBoot AI智能客服模塊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決微服務(wù)中關(guān)于用戶token處理到的坑
這篇文章主要介紹了解決微服務(wù)中關(guān)于用戶token處理到的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java五子棋簡(jiǎn)單實(shí)現(xiàn)代碼舉例
Java五子棋游戲是一種經(jīng)典的兩人對(duì)戰(zhàn)棋類游戲,它基于簡(jiǎn)單的規(guī)則,即任何一方的棋子在棋盤上形成連續(xù)的五個(gè),無(wú)論是橫、豎還是斜線,都將獲勝,這篇文章主要介紹了Java五子棋實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2024-10-10
Java入門絆腳石之Override和Overload的區(qū)別詳解
重寫是子類對(duì)父類的允許訪問(wèn)的方法的實(shí)現(xiàn)過(guò)程進(jìn)行重新編寫, 返回值和形參都不能改變。即外殼不變,核心重寫!重寫的好處在于子類可以根據(jù)需要,定義特定于自己的行為。重載是在一個(gè)類里面,方法名字相同,而參數(shù)不同。返回類型可以相同也可以不同2021-10-10
Springboot啟動(dòng)同時(shí)創(chuàng)建數(shù)據(jù)庫(kù)和表實(shí)現(xiàn)方法
這篇文章主要介紹了Springboot啟動(dòng)同時(shí)創(chuàng)建數(shù)據(jù)庫(kù)和表,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01
定義hashcode時(shí)使用31系數(shù)的原因
這篇文章主要介紹了定義hashcode時(shí)使用31系數(shù)的原因,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
Java Benchmark 基準(zhǔn)測(cè)試的實(shí)例詳解
這篇文章主要介紹了Java Benchmark 基準(zhǔn)測(cè)試的實(shí)例詳解的相關(guān)資料,這里提供實(shí)例幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08

