The 'Prompt'` class holds an ordered list of `Message` objects – System & User roles.
Prompt prompt = new Prompt(List.of(
new SystemMessage,new UserMessage));ChatResponse response = chatModel.call;String answer = response.content;// Prompt类还提供 getUserMessage / getSystemMessage 等便捷方法。
RAG 检索提高
java
@Configuration public
class RagConfig {
@Bean
VectorStore vectorStore(JdbcTemplate jdbcTemplate,EmbeddingModel embeddingModel){
return new PgVectorStore;}
}
@RestController
public class RagController{
private final ChatClient chat;public RagController(ChatClient.Builder builder,VectorStore store){
this.chat = builder.defaultAdvisors(
QuestionAnswerAdvisor.builder.build)
.build;}
@GetMapping
public String rag{
return chat.prompt.user.call.content;}
}
`QuestionAnswerAdvisor` is an interceptor that automatically retrieves relevant chunks from vector store and appends m to Prompt before each LLM call – **zero‑coding RAG**.
对话记忆
java
@Configuration public
class MemoryConfig{
@Bean public
ChatMemoryRepository repo{return new InMemoryChatMemoryRepository;}
@Bean public
ChatMemory memory{
return MessageWindowChatMemory.builder
.chatMemoryRepository
.maxMessages
.build;}
}
@RestController
public class ChatCtrl{
private final ChatClient client;public ChatCtrl{
this.client=b.defaultAdvisors(
MessageChatMemoryAdvisor.builder.build)
.build;}
@PostMapping
public String chat(@RequestParam String session。@RequestParam String msg){
return client.prompt
.advisors(a->a.param(
AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY,session))
.user.call.content;}
}
**优势**这方面,无需自行维护会话状态。Spring AI 为每个会话提供独立内存,实现「上下文感知」。
Spring AI 的真正优势
. 零心智负担的 Spring 集成:If your project already runs on Spring Boot,adding Spring AI costs almost nothing – auto‑configuration。health checks and observability come out of box.
. 优秀的 VectorStore 抽象层:You can swap PGVector ↔ Milvus ↔ Qdrant etc. by changing a single Maven dependency and a few YAML entries.
. Structured Outputs:You can map model responses directly to POJOs – ideal for returning JSON or domain objects.
. MCP 支持 :The Model Context Protocol gives you fine‑grained control over tool calls & external data sources. Spring AI team maintains Java SDK,ensuring long‑term stability.
说明:This section focuses on two most active frameworks for production use. Semantic Kernel is omitted because it is still RC and lacks domestic model support..
建立同一个 RAG 应用的代码量对比.
:
Spring AI 实现
java
@Service
public class KnowledgeService {
private final ChatClient client;private final VectorStore vectorStore;public KnowledgeService(ChatClient.Builder builder,VectorStore vectorStore。InMemoryChatMemoryRepository repo){
// 对话记忆 + RAG Advisor 自动注入检索结果
var memory= MessageWindowChatMemory.builder
.chatMemoryRepository.maxMessages.build;话说回来,this.client=builder.defaultAdvisors(
QuestionAnswerAdvisor.builder.build。MessageChatMemoryAdvisor.builder.build)
.
build;this.vectorStore=vectorStore;}
/** PDF 文档导入 */
public void ingestPdf{
var docs=new PagePdfDocumentReader.get;var splitter=new TokenTextSplitter;话说回来,var segments=splitter.apply;不过,vectorStore.add;// 向量化并存储