Langchain4j-3-提示词工程

消息类型

消息类型 说明
SystemMessage【人设】 系统提示词,一次只能保留一条,添加后始终保留
UserMessage【用户消息】 用户消息,代表用户输入
AiMessage【大模型回复】 AI 消息,代表模型的回应
ToolExecutionResultMessage
【工具调用】
工具执行结果消息,ToolExecutionRequestMessage的返回结果

三种提示词构建方式

@SystemMessage+@UserMessage+@V

单独设置系统提示词,用 @V 填充用户消息的占位符。

// LawAssistant.java
public interface LawAssistant  
{  
    // @SystemMessage+@UserMessage+@V  
    @SystemMessage("你是一位专业的中国法律顾问,只回答与中国法律相关的问题。" +  
            "输出限制:对于其他领域的问题禁止回答,直接返回'抱歉,我只能回答中国法律相关的问题。'")  
    @UserMessage("请回答以下法律问题:{{question}},字数控制在{{length}}以内")  
    String chat(@V("question") String question, @V("length") int length);
}

带着@StructuredPrompt 的业务实体类

单独设置系统提示词,用业务类填充用户消息的占位符。

// LawPrompt.java
@Data  
@StructuredPrompt("根据中国{{legal}}法律,解答以下问题:{{question}}")  
public class LawPrompt  
{  
    private String legal;  
    private String question;  
}

// LawAssistant.java
public interface LawAssistant  
{  
    // LawPrompt:@StructuredPrompt的业务实体类,比如
    @SystemMessage("你是一位专业的中国法律顾问,只回答与中国法律相关的问题。" +  
            "输出限制:对于其他领域的问题禁止回答,直接返回'抱歉,我只能回答中国法律相关的问题。'")  
    String chat(LawPrompt lawPrompt);  
}

PromptTemplate

设置一个提示词模板,通过填充占位符形成不同提示词。

//1 构造PromptTemplate模板  
PromptTemplate template = PromptTemplate.from("你是一个{{it}}助手,{{question}}怎么办");  
//2 由PromptTemplate生成Prompt  
Prompt prompt = template.apply(Map.of("it",role,"question",question));  
//3 Prompt提示词变成UserMessage  
UserMessage userMessage = prompt.toUserMessage();  

使用案例

@RestController  
@Slf4j  
public class ChatPromptController  
{  
    @Resource  
    private LawAssistant lawAssistant;  
    @Resource  
    private ChatModel chatModel;  
  
    // http://localhost:9009/chatprompt/test1  
    @GetMapping(value = "/chatprompt/test1")  
    public String test1()  
    {  
        String chat = lawAssistant.chat("什么是知识产权?",2000);  
        System.out.println(chat);  
  
        String chat2 = lawAssistant.chat("什么是java?",2000);  
        System.out.println(chat2);  

                return "success : "+ DateUtil.now()+"<br> \n\n chat: "+chat+"<br> \n\n chat2: "+chat2;  
    }  
  
    @GetMapping(value = "/chatprompt/test2")  
    public String test2()  
    {  
        LawPrompt prompt = new LawPrompt();  
  
        prompt.setLegal("知识产权");  
        prompt.setQuestion("TRIPS协议?");  
  
        String chat = lawAssistant.chat(prompt);  
  
        System.out.println(chat);  
  
        return "success : "+ DateUtil.now()+"<br> \n\n chat: "+chat;  
    }  
  
  
	@GetMapping(value = "/chatprompt/test3")  
    public String test3()  
    {  
        // 看看源码,单个参数时 PromptTemplate 构造使用 it 属性作为默认占位符  
        String role = "财务会计";  
        String question = "人民币大写";  
  
        //1 构造PromptTemplate模板  
        PromptTemplate template = PromptTemplate.from("你是一个{{it}}助手,{{question}}怎么办");  
        //2 由PromptTemplate生成Prompt  
        Prompt prompt = template.apply(Map.of("it",role,"question",question));  
        //3 Prompt提示词变成UserMessage  
        UserMessage userMessage = prompt.toUserMessage();  
        //4 调用大模型  
        ChatResponse chatResponse = chatModel.chat(userMessage);  
  
        //4.1 后台打印  
        System.out.println(chatResponse.aiMessage().text());  
        //4.2 前台返回  
        return "success : "+ DateUtil.now()+"<br> \n\n chat: "+chatResponse.aiMessage().text();  
    }  
}
posted @ 2025-08-17 18:04  Miaops  阅读(85)  评论(0)    收藏  举报