Spring AI 06 提示词(Prompt)全场景实战:从基础到高级模板用法

在 Spring AI 开发中,Prompt(提示词) 是与大模型交互的核心,灵活的提示词设计能直接决定AI回答的质量。本文基于你提供的完整代码,详细讲解 Spring AI 中基础提示词、系统提示词、动态参数提示词、模板化提示词、外部文件提示词的全套用法,让你轻松掌握提示词的高级编排技巧。


一、前置说明

本文基于 Spring AI + Ollama 环境,使用我们上文中配置好的 ollamaChatClient 实现流式响应,所有接口均支持中文、无乱码、可直接运行测试。

核心依赖(确保已引入):

xml 复制代码
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>

二、完整代码 + 逐接口详解

1. 基础提示词(最简单调用)

直接传入用户问题,无任何额外配置,最基础的AI调用方式。

java 复制代码
/**
 * 1. 基础提示词:直接传入用户问题
 */
@GetMapping(value = "/chat1", produces = "text/stream;charset=utf-8")
public Flux<String> chat1() {
    String userInput = "今天的天气怎么样?";
    return ollamaChatClient.prompt()
            .user(userInput)
            .stream()
            .content();
}

适用场景:简单问答、无角色设定、无格式要求的场景。


2. 系统提示词(设定AI角色)

使用 .system() 给AI设定固定角色/风格,优先级高于全局配置

java 复制代码
/**
 * 2. 系统提示词:设定AI回答风格(优先级高于全局)
 */
@GetMapping(value = "/chat2", produces = "text/stream;charset=utf-8")
public Flux<String> chat2() {
    String userInput = "今天的天气怎么样?";
    return ollamaChatClient.prompt()
            .system("以小朋友的风格来回答") // 系统提示词:角色设定
            .user(userInput)
            .stream()
            .content();
}

效果:AI会用可爱、简单、口语化的小朋友语气回答天气问题。


3. 动态参数系统提示词(变量传参)

系统提示词支持动态参数 ,通过 .param() 传入变量,灵活切换角色。

java 复制代码
/**
 * 3. 动态参数系统提示词:前端传入角色,动态生成提示词
 */
@GetMapping(value = "/chat3", produces = "text/stream;charset=utf-8")
public Flux<String> chat3(String role) {
    String userInput = "今天的天气怎么样?";
    return ollamaChatClient.prompt()
            .system(spec -> spec.text("以{role}的语气风格回答问题").param("role", role))
            .user(userInput)
            .stream()
            .content();
}

测试示例

  • 请求:/prompt/chat3?role=天气预报员
  • AI效果:以专业天气预报员的身份回答问题

4. PromptTemplate 模板提示词

使用 PromptTemplate 标准化构建提示词,支持变量渲染。

java 复制代码
/**
 * 4. PromptTemplate模板:标准化构建提示词
 */
@GetMapping(value = "/chat4", produces = "text/stream;charset=utf-8")
public Flux<String> chat4(String weather) {
    // 定义模板
    PromptTemplate template = new PromptTemplate("今天是{weather}吗?");
    // 渲染变量
    Prompt prompt = template.create(Map.of("weather", weather));

    return ollamaChatClient.prompt(prompt)
            .stream()
            .content();
}

测试 :传入 weather=晴天,最终提示词为:今天是晴天吗?


5. 自定义分隔符模板(<> 替代 {})

默认模板使用 {} 分隔变量,可自定义分隔符(如 <>)避免冲突。

java 复制代码
/**
 * 5. 自定义模板分隔符:使用<>代替{},避免语法冲突
 */
@GetMapping(value = "/chat5", produces = "text/stream;charset=utf-8")
public Flux<String> chat5(String weather) {
    PromptTemplate promptTemplate = PromptTemplate.builder()
            // 自定义分隔符 < >
            .renderer(StTemplateRenderer.builder()
                    .startDelimiterToken('<')
                    .endDelimiterToken('>')
                    .build())
            .template("""
                    今天是<weather>吗?
                     """)
            .build();
    // 渲染变量
    String prompt = promptTemplate.render(Map.of("weather", weather));

    return ollamaChatClient.prompt(prompt)
            .stream()
            .content();
}

适用场景 :提示词中本身包含大括号{},避免变量解析错误。


6. 外部文件加载提示词(text.st

最实用的企业级用法:将提示词写在外部文件中,无需修改代码即可更新提示词。

java 复制代码
/**
 * 6. 外部文件提示词:从本地.st文件加载系统提示词
 */
@GetMapping(value = "/chat6", produces = "text/stream;charset=utf-8")
public Flux<String> chat6(String weather) {
    // 加载本地提示词文件
    var systemResource = new FileSystemResource("C:\\Users\\user\\OneDrive\\桌面\\text.st");
    // 构建系统提示模板
    SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemResource);
    // 渲染变量
    String prompt = systemPromptTemplate.create(Map.of("weather", weather)).getContents();

    return ollamaChatClient.prompt(prompt)
            .stream()
            .content();
}
📄 外部文件 text.st 内容示例:
复制代码
你是一个专业的天气助手,只回答天气相关问题。
用户问题:{weather}
请用简洁、专业的语言回答。

优势:提示词与代码解耦,支持热更新、多人协作编辑。


三、完整 Controller 代码(可直接复制)

java 复制代码
package com.easyai;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.ai.template.st.StTemplateRenderer;
import org.springframework.core.io.FileSystemResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.Map;

@RestController
@RequestMapping("/prompt")
@Slf4j
public class PromptController {

    @Resource
    private ChatClient ollamaChatClient;

    // 1. 基础提示词
    @GetMapping(value = "/chat1", produces = "text/stream;charset=utf-8")
    public Flux<String> chat1() {
        String userInput = "今天的天气怎么样?";
        return ollamaChatClient.prompt()
                .user(userInput)
                .stream()
                .content();
    }

    // 2. 固定系统提示词
    @GetMapping(value = "/chat2", produces = "text/stream;charset=utf-8")
    public Flux<String> chat2() {
        String userInput = "今天的天气怎么样?";
        return ollamaChatClient.prompt()
                .system("以小朋友的风格来回答")
                .user(userInput)
                .stream()
                .content();
    }

    // 3. 动态参数系统提示词
    @GetMapping(value = "/chat3", produces = "text/stream;charset=utf-8")
    public Flux<String> chat3(String role) {
        String userInput = "今天的天气怎么样?";
        return ollamaChatClient.prompt()
                .system(spec -> spec.text("以{role}的语气风格回答问题").param("role", role))
                .user(userInput)
                .stream()
                .content();
    }

    // 4. PromptTemplate模板
    @GetMapping(value = "/chat4", produces = "text/stream;charset=utf-8")
    public Flux<String> chat4(String weather) {
        PromptTemplate template = new PromptTemplate("今天是{weather}吗?");
        Prompt prompt = template.create(Map.of("weather", weather));

        return ollamaChatClient.prompt(prompt)
                .stream()
                .content();
    }

    // 5. 自定义分隔符模板
    @GetMapping(value = "/chat5", produces = "text/stream;charset=utf-8")
    public Flux<String> chat5(String weather) {
        PromptTemplate promptTemplate = PromptTemplate.builder()
                .renderer(StTemplateRenderer.builder().startDelimiterToken('<').endDelimiterToken('>').build())
                .template("""
                        今天是<weather>吗?
                         """)
                .build();
        String prompt = promptTemplate.render(Map.of("weather", weather));

        return ollamaChatClient.prompt(prompt)
                .stream()
                .content();
    }

    // 6. 外部文件加载提示词
    @GetMapping(value = "/chat6", produces = "text/stream;charset=utf-8")
    public Flux<String> chat6(String weather) {
        var systemResource = new FileSystemResource("C:\\Users\\user\\OneDrive\\桌面\\text.st");
        SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemResource);
        String prompt = systemPromptTemplate.create(Map.of("weather", weather)).getContents();

        return ollamaChatClient.prompt(prompt)
                .stream()
                .content();
    }
}

四、核心知识点总结

  1. 基础提示词.user() 直接传入问题,快速调用
  2. 系统提示词.system() 设定AI角色,优先级最高
  3. 动态提示词 :使用 {变量} + .param() 实现灵活传参
  4. 模板提示词PromptTemplate 标准化管理提示词
  5. 自定义分隔符:解决模板符号冲突问题
  6. 外部文件提示词:企业级最佳实践,提示词与代码解耦

五、测试建议

  1. 启动项目后,逐个访问接口测试效果
  2. 测试 /chat3 时传入不同角色(天气预报员、诗人、老师)
  3. 创建 text.st 文件测试外部提示词功能
  4. 所有接口均支持流式响应,浏览器访问可看到逐字输出

总结

本文覆盖了 Spring AI Prompt 提示词 100% 常用场景 ,从简单到企业级模板用法全部包含,代码可直接运行、可直接用于项目开发。配合上一篇的 多智能体 + Advisor,你已经完全掌握 Spring AI 核心开发能力!

相关推荐
qcx237 分钟前
【系统学AI】23 AI 时代产品运营与获客全景:CRM SaaS 大变局 + 增长新范式(2026 调研报告)
人工智能·产品运营·产品设计·ai agent·ai native
星轨zb7 分钟前
从通用到专属:文迹(WenJi)引入 RAG 向量库的技术复盘
java·spring·langchain4j
叶修_A8 分钟前
【COZE-08】Prompt工程进阶 - 结构化输出与思维链
大数据·人工智能·prompt
John_ToDebug10 分钟前
开源与人性:DeepSeek 战略的底层逻辑
人工智能·经验分享·ai
老吴胡编12 分钟前
eknife 2026.05.28 v0.0.5 更新 —— 支持 PDF 文档合并
人工智能·嵌入式硬件·个人开发
OCR_1337162127513 分钟前
技术实测|2026三款主流OCR横向对比:SDK15、PaddleOCR、GLM-OCR选型指南
大数据·人工智能
深蓝电商API13 分钟前
当爬虫遇见大模型:AI驱动的智能数据采集新范式
人工智能·爬虫
陈天伟教授14 分钟前
图解人工智能(37)人工智能应用-车牌识别
人工智能·深度学习
Agent手记14 分钟前
电商智能客服的退换货自动处理流程如何配置?——2026企业级Agent全链路实战指南
人工智能·ai
wuhen_n16 分钟前
阿里云百炼平台 API 接入教程(附 Node.js + TypeScript 实战)
前端·人工智能·阿里云·ai编程