prompt(提示词)

全局系统提示词

ChatConfiguration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.easyai;  

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;


@Configuration
public class ChatConfiguration{

@Primary
@Bean public ChatClient ollamaChatChatClient(OllamaChatModel ollamaChatModel) {
//return ChatClient.builder(ollamaChatModel).build();
//通过chatclient构造器设置系统提示词
return ChatClient.builder(ollamaChatModel).defaultSystem("以古风风格回答问题").build();
}

@Bean
public ChatClient deepSeekChatChatClient(DeepSeekChatModel deepSeekChatModel) {
return ChatClient.builder(deepSeekChatModel).build();
}
}

PromptController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64


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

@Resource
private ChatClient ollamaChatClient;


/**
* 构造器中设置系统提示词(全局)
*
* @return
*/
@GetMapping(value = "/chat1", produces = "text/stream;charset=utf8")
public Flux<String> chat1() {
String userInput = "今天的天气怎么样";
return ollamaChatClient.prompt().user(userInput).stream().content();
}

@GetMapping(value = "/chat2", produces = "text/stream;charset=utf8")
public Flux<String> chat2() {
String userInput = "今天的天气怎么样";
return ollamaChatClient.prompt().system("以小朋友的预期风格回答问题").user(userInput).stream().content();
}


@GetMapping(value = "/chat3", produces = "text/stream;charset=utf8")
public Flux<String> chat3(String role) {
String userInput = "今天的天气怎么样";
return ollamaChatClient.prompt().system(spec -> spec.text("以{role}的预期风格回答问题").param("role", role)).user(userInput).stream().content();
}

@GetMapping(value = "/chat4", produces = "text/stream;charset=utf8")
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();
}

@GetMapping(value = "/chat5", produces = "text/stream;charset=utf8")
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();
}

@GetMapping(value = "/chat6", produces = "text/stream;charset=utf8")
public Flux<String> chat6(String weather) {
var systemResource = new FileSystemResource("E:\\Desktop\\test.st");
SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemResource);
String prompt = systemPromptTemplate.create(Map.of("weather", weather)).getContents();
return ollamaChatClient.prompt(prompt).stream().content();
}
}
return content;
}
}