微信扫码
添加专属顾问
我要投稿
掌握Spring AI Alibaba框架,构建高效机票助手应用。核心内容:1. Spring AI Alibaba项目背景及优势介绍2. 详细步骤指导:从拉取项目到后端启动3. pom.xml文件解读及关键配置参数说明
<properties>
<java.version>17</java.version>
<vaadin.version>24.4.7</vaadin.version>
<maven-deploy-plugin.version>3.1.1</maven-deploy-plugin.version>
<spring-ai-alibaba.version>1.0.0-M6.1</spring-ai-alibaba.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
public class AssistantConfig {
/**
* 航班助理
*
* @param modelBuilder
* @param vectorStore
* @param chatMemory
*/
public ChatClient getChatClient(ChatClient.Builder modelBuilder, VectorStore vectorStore, ChatMemory chatMemory) {
log.info("航班助理配置CustomerSupportAssistant()..");
ChatClient chatClient = modelBuilder
.defaultSystem("""
您是“Flight-Booking”航空公司的客户聊天支持代理,请以友好、乐于助人且愉快的方式来回复.
您正在通过在线聊天系统与客户互动.
您能够支持已有机票的预订详情查询、机票日期改签、机票预订取消等操作,其余功能将在后续版本中添加,如果用户问的问题不支持请告知详情.
在提供有关机票预订详情查询、机票日期改签、机票预订取消等操作之前,您必须始终从用户处获取以下信息:预订号、客户姓名.
在为用户查询信息后,每个字段的内容都需要分行展示,分行显示内容时注意保持样式一致,需要使用-分行显示,其他格式不符合.
在询问用户之前,请检查消息历史记录以获取预订号、客户姓名等信息,尽量避免重复询问给用户造成困扰.
在更改预订之前,您必须确保条款允许这样做.
如果更改需要收费,您必须在继续之前征得用户同意.
使用提供的功能获取预订详细信息、更改预订和取消预订.
如果需要,您可以调用相应函数辅助完成.
请讲中文.
今天的日期是 {current_date}.
""")
.defaultAdvisors(
// 会话记忆
new PromptChatMemoryAdvisor(chatMemory),
// 存储,基于RAG
new QuestionAnswerAdvisor(vectorStore, SearchRequest.builder().topK(4).similarityThresholdAll().build()),
// logger 日志打印
new SimpleLoggerAdvisor()
)
.defaultFunctions(
"getBookingDetails",
"changeBooking",
"cancelBooking"
)
.build();
return chatClient;
}
CommandLineRunner ingestTermOfServiceToVectorStore(EmbeddingModel embeddingModel, VectorStore vectorStore,
Resource termsOfServiceDocs) {
log.info("向量数据存储..");
return args -> {
// Ingest the document into the vector store
vectorStore.write(new TokenTextSplitter().transform(new TextReader(termsOfServiceDocs).read()));
vectorStore.similaritySearch("Cancelling Bookings").forEach(doc -> {
log.info("Similar Document: {}", doc.getText());
});
};
}
public VectorStore vectorStore(EmbeddingModel embeddingModel) {
log.info("vectorStore初始化..");
return SimpleVectorStore.builder(embeddingModel).build();
}
public ChatMemory chatMemory() {
log.info("chatMemory()..");
return new InMemoryChatMemory();
}
public RestClient.Builder restClientBuilder() {
log.info("restClientBuilder()..");
return RestClient.builder();
}
}
"/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE) (path =
public Flux<String> chat(String chatId, String userMessage) {
return assistantService.chat(chatId, userMessage);
}
/**
* 用户对话
*
* @param chatId
* @param userMessageContent
* @return
*/
public Flux<String> chat(String chatId, String userMessageContent) {
log.info("用户对话ID: {}", chatId);
return this.chatClient.prompt()
.system(s -> s.param("current_date", LocalDate.now().toString()))
.user(userMessageContent)
.advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId).param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 100))
.stream()
.content();
}
"获取机票预定详细信息") (
public Function<BookingDetailsRequest, BookingDetails> getBookingDetails() {
log.info("获取机票预定详细信息1..");
return request -> {
log.info("获取机票预定详细信息2..");
try {
return flightBookingService.getBookingDetails(request.bookingNumber(), request.name());
} catch (Exception e) {
logger.warn("Booking details: {}", NestedExceptionUtils.getMostSpecificCause(e).getMessage());
return new BookingDetails(request.bookingNumber(), request.name(), null, null, null, null, null);
}
};
}
public record BookingDetailsRequest(String bookingNumber, String name) {
}
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费场景POC验证,效果验证后签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2025-04-27
看不懂GitHub代码?刚刚这个AI工具让全球每个GitHub项目开口说话
2025-04-27
使用MCP构建?注意安全漏洞
2025-04-27
开源大模型工具全景图!Hugging Face、OlmOCR 、Dify,开发者必藏的核心工具选型指南
2025-04-27
LLaMA Factory 框架深度解析
2025-04-26
我在通用Agent上的探索设计初稿
2025-04-26
Ollama-Deep-Researcher-本地Mac结合魔搭社区模型搭建网页研究助手
2025-04-25
Manus开源版本!无需邀请码,老金手把手教你咋用!
2025-04-25
78k star,像写 Markdown 一样画流程图,这个开源工具太牛了!
2024-07-25
2025-01-01
2025-01-21
2024-05-06
2024-09-20
2024-07-20
2024-07-11
2024-06-12
2024-08-13
2024-12-26
2025-04-21
2025-04-19
2025-04-17
2025-04-15
2025-04-13
2025-04-10
2025-04-07
2025-04-03