|
@@ -0,0 +1,95 @@
|
|
|
+package com.sf.ai.controller.prompt;
|
|
|
+
|
|
|
+import com.sf.ai.dto.Book;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.springframework.ai.chat.messages.AssistantMessage;
|
|
|
+import org.springframework.ai.chat.model.ChatResponse;
|
|
|
+import org.springframework.ai.chat.prompt.Prompt;
|
|
|
+import org.springframework.ai.chat.prompt.PromptTemplate;
|
|
|
+import org.springframework.ai.openai.OpenAiChatModel;
|
|
|
+import org.springframework.ai.parser.BeanOutputParser;
|
|
|
+import org.springframework.ai.parser.OutputParser;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.core.io.Resource;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+// 模板语法
|
|
|
+@RestController
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class TemplateController {
|
|
|
+
|
|
|
+ private final OpenAiChatModel openAiChatModel;
|
|
|
+
|
|
|
+ // http://localhost:8090/template?author=
|
|
|
+ @GetMapping("/template")
|
|
|
+ public String template(@RequestParam("author") String author) {
|
|
|
+ // 构造提示词模板
|
|
|
+ String template = "请问{author}最受欢迎的书是哪本?什么时候发布?书的内容是什么?";
|
|
|
+ PromptTemplate promptTemplate = new PromptTemplate(template);
|
|
|
+ // 动态的将参数传进去
|
|
|
+ Map<String, Object> map = Map.of("author", author);
|
|
|
+ Prompt prompt = promptTemplate.create(map);
|
|
|
+
|
|
|
+ ChatResponse response = openAiChatModel.call(prompt);
|
|
|
+ AssistantMessage assistantMessage = response.getResult().getOutput();
|
|
|
+ return assistantMessage.getContent();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Value("classpath:st/prompt.st")
|
|
|
+ private Resource resource;
|
|
|
+
|
|
|
+ // http://localhost:8090/template1?author=
|
|
|
+ @GetMapping("/template1")
|
|
|
+ public String template1(@RequestParam("author") String author) {
|
|
|
+ // 加载资源来构造模板
|
|
|
+ PromptTemplate promptTemplate = new PromptTemplate(resource);
|
|
|
+ Map<String, Object> map = Map.of("author", author);
|
|
|
+ Prompt prompt = promptTemplate.create(map);
|
|
|
+ ChatResponse response = openAiChatModel.call(prompt);
|
|
|
+ AssistantMessage assistantMessage = response.getResult().getOutput();
|
|
|
+ return assistantMessage.getContent();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Value("classpath:st/code.st")
|
|
|
+ private Resource codeResource;
|
|
|
+
|
|
|
+ // http://localhost:8090/code?description=quicksort&language=java&method=sort
|
|
|
+ @GetMapping("/code")
|
|
|
+ public String code(@RequestParam("description") String description,
|
|
|
+ @RequestParam("language") String language,
|
|
|
+ @RequestParam("method") String method) {
|
|
|
+ PromptTemplate promptTemplate = new PromptTemplate(codeResource);
|
|
|
+ Map<String, Object> map = Map.of("description", description,
|
|
|
+ "language", language, "method", method);
|
|
|
+ Prompt prompt = promptTemplate.create(map);
|
|
|
+ ChatResponse response = openAiChatModel.call(prompt);
|
|
|
+ return response.getResult().getOutput().getContent();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // http://localhost:8090/bean?author=
|
|
|
+ @GetMapping("/bean")
|
|
|
+ public Book bean(@RequestParam("author") String author) {
|
|
|
+ String template = """
|
|
|
+ 请问{author}最受欢迎的书是哪本?什么时候发布?书的内容是什么?
|
|
|
+ {format}
|
|
|
+ """;
|
|
|
+ // 定义输出解释器
|
|
|
+ OutputParser<Book> bookOutputParser = new BeanOutputParser<>(Book.class);
|
|
|
+ PromptTemplate promptTemplate = new PromptTemplate(template);
|
|
|
+ // 构建提示词时 传入解析器的格式
|
|
|
+ Prompt prompt = promptTemplate.create(
|
|
|
+ Map.of("author", author,"format",bookOutputParser.getFormat()));
|
|
|
+ ChatResponse response = openAiChatModel.call(prompt);
|
|
|
+ AssistantMessage assistantMessage = response.getResult().getOutput();
|
|
|
+
|
|
|
+ // 使用解析器解析返回结果
|
|
|
+ Book book = bookOutputParser.parse(assistantMessage.getContent());
|
|
|
+ return book;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|