96SEO 2026-08-03 12:31 0
先回顾上一篇的代码:

public String chat { return chatClient.prompt .user // 使用者的输入倒是动态的 .call .content;}
看起来还好对吧?但真实业务场景是这样的:
// 场景1:客服程序,需要给AI设定人设.user// 场景2:代码审查,需要统一输出格式.user// 场景3:多语言翻译。需要指定源语言和目标语言.user
三个问题立刻暴露:
后端老鸟的直觉反应:这玩意儿应该放到配置文件里。
Spring AI提供了一个 PromptTemplate用法和 RestTemplateJdbcTemplate 一样,看名字就知道是干嘛的。
它的主要思想就一句话:用占位符代替硬编码,运行时动态填充。
先看一个最简单的例子:
// 以前:硬编码.user// 现在:占位符模板.user
package com.yunxi.ai.service;import org.springframework.ai.chat.client.ChatClient;import org.springframework.ai.chat.prompt.PromptTemplate;import org.springframework.stereotype.Service;@Service
public class ChatService {
private final ChatClient chatClient;public ChatService {
this.chatClient = builder.build;}
public String chatWithTemplate {
// 定义模板。占位符用 {变量名}
PromptTemplate template = new PromptTemplate(
"你是一个专业的{role},请用{tone}的语气回答使用者的问题。说到使用者问题,{message}"
);// 填充占位符
template.add;template.add;template.add;// 渲染成最终Prompt,发给AI
return chatClient.prompt
.user)
.call
.content;}
}
package com.yunxi.ai.controller;import com.yunxi.ai.service.ChatService;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestController
public class ChatController {
private final ChatService chatService;public ChatController {
this.chatService = chatService;}
@GetMapping
public String chatWithTemplate(
@RequestParam String role。@RequestParam String tone,@RequestParam String message) {
return chatService.chatWithTemplate;怎么说呢,}
}
测试链接示例:
上面虽然用了模板,但模板字符串仍然嵌在Java里。真正优雅的是把模板提取到外部配置,让业务逻辑与提示词完全解耦。
| 方法 | 内容 |
|---|---|
| src/main/resources/prompts/customer-service.st src/main/resources/prompts/code-review.st | '你是一个资深Java代码审查员。怎么说呢,审查要点: . 至于命名规范,类名大驼峰,方法名小驼峰,变量名有意义. 性能问题的观点是,循环内查库、N+1问题、大对象创建. 安全漏洞的观点是。SQL注入、XSS、敏感信息泄露请对以下代码进行审查,按"严重程度|问题描述|修改建议"格式输出:{code} ' '你是一个专业客服人员,你叫小慧。按理说,工作要求: . 语气温和有礼,多用"您"、"请"等敬语. 如果使用者投诉优先安抚情绪再解决. 如果遇到不懂的问题诚实告知并建议联系人工客服. 回答简洁。不超过200字 使用者问题:{message}请回答: ' |
package com.yunxi.ai.service;import org.springframework.ai.chat.client.ChatClient;import org.springframework.ai.chat.prompt.PromptTemplate;import org.springframework.beans.factory.annotation.Value;import org.springframework.core.io.Resource;import org.springframework.stereotype.Service;其实,import java.io.IOException;怎么说呢,import java.nio.charset.StandardCharsets;@Service
public class StPromptService {
private final ChatClient chatClient;@Value
private Resource customerServicePrompt;@Value
private Resource codeReviewPrompt;public StPromptService {
this.chatClient = builder.build;}
public String chat throws IOException {
Resource resource = switch {
case "customer-service" -> customerServicePrompt;
case "code-review" -> codeReviewPrompt;default -> throw new IllegalArgumentException;},String templateContent = new String.readAllBytes,StandardCharsets.UTF_8);按理说,PromptTemplate template = new PromptTemplate;if ) template.add;if ) template.add;return chatClient.prompt
.user)
.call
.content;}
}
package com.yunxi.ai.controller;
import com.yunxi.ai.service.StPromptService;import org.springframework.web.bind.annotation.GetMapping;不过,import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController public class PromptConfigController{ private final StPrompt Service stpromptservice;public PromptConfigControlle r{ this.stpromptservice=stpromptservice;} @GetMapping public String chata wwithconfigthrows IOException{ return stpromptservice.c hat;按理说,} }
:
src/main/resources/
├── application.yml
└── prompts/
├── customer-service.st
└── code-review.st
@Value 注入资源文件、外部化配置、模板占位符——全是 Spring 环境标准玩法。
The first article only used .user。but re's a more important component: SYSTEM MESSAGE..
| 消息类型 | 角色 | 用途 |
|---|---|---|
| SystemMessage | AI 的行为规则/角色/输出格式设置 | 设置全局指令或约束规则,例如「返回JSON」或「保持中立口吻」等。 |
| UserMessage | 使用者输入/具体任务/请求指令;老实说,属于当前轮次交互主体。 | 提交具体需求,如「帮我翻译这句话」或「生成接口文档」等。 |
| AssistantMessage | AI 的历史回复;多轮对话使用,用于上下文记忆。 |
**关键**: 将“人设”或“规则”放在 SystemMessage 中。而不是 UserMessage,以免被任务内容覆盖。
java
public String chatWithSystem(
String systemPrompt,String userMessage ) {
return chatClien t.prompt
.system /* 程序设定 */
.user /* 当前任务 */
\t.call
\t.content;}
作为专业的SEO优化服务提供商,我们致力于通过科学、系统的搜索引擎优化策略,帮助企业在百度、Google等搜索引擎中获得更高的排名和流量。我们的服务涵盖网站结构优化、内容优化、技术SEO和链接建设等多个维度。
| 服务项目 | 基础套餐 | 标准套餐 | 高级定制 |
|---|---|---|---|
| 关键词优化数量 | 10-20个核心词 | 30-50个核心词+长尾词 | 80-150个全方位覆盖 |
| 内容优化 | 基础页面优化 | 全站内容优化+每月5篇原创 | 个性化内容策略+每月15篇原创 |
| 技术SEO | 基本技术检查 | 全面技术优化+移动适配 | 深度技术重构+性能优化 |
| 外链建设 | 每月5-10条 | 每月20-30条高质量外链 | 每月50+条多渠道外链 |
| 数据报告 | 月度基础报告 | 双周详细报告+分析 | 每周深度报告+策略调整 |
| 效果保障 | 3-6个月见效 | 2-4个月见效 | 1-3个月快速见效 |
我们的SEO优化服务遵循科学严谨的流程,确保每一步都基于数据分析和行业最佳实践:
全面检测网站技术问题、内容质量、竞争对手情况,制定个性化优化方案。
基于用户搜索意图和商业目标,制定全面的关键词矩阵和布局策略。
解决网站技术问题,优化网站结构,提升页面速度和移动端体验。
创作高质量原创内容,优化现有页面,建立内容更新机制。
获取高质量外部链接,建立品牌在线影响力,提升网站权威度。
持续监控排名、流量和转化数据,根据效果调整优化策略。
基于我们服务的客户数据统计,平均优化效果如下:
我们坚信,真正的SEO优化不仅仅是追求排名,而是通过提供优质内容、优化用户体验、建立网站权威,最终实现可持续的业务增长。我们的目标是与客户建立长期合作关系,共同成长。
Demand feedback