26
26
import org .springframework .ai .chat .client .advisor .SimpleLoggerAdvisor ;
27
27
import org .springframework .ai .chat .memory .InMemoryChatMemory ;
28
28
import org .springframework .ai .chat .model .ChatModel ;
29
+ import org .springframework .ai .chat .prompt .PromptTemplate ;
30
+ import org .springframework .beans .factory .annotation .Qualifier ;
29
31
import org .springframework .stereotype .Service ;
30
32
31
33
import static org .springframework .ai .chat .client .advisor .AbstractChatMemoryAdvisor .CHAT_MEMORY_CONVERSATION_ID_KEY ;
@@ -41,26 +43,45 @@ public class SAAChatService {
41
43
42
44
private final ChatClient defaultChatClient ;
43
45
44
- public SAAChatService (ChatModel chatModel ) {
46
+ private final PromptTemplate deepThinkPromptTemplate ;
47
+
48
+ public SAAChatService (
49
+ ChatModel chatModel ,
50
+ @ Qualifier ("deepThinkPromptTemplate" ) PromptTemplate deepThinkPromptTemplate
51
+ ) {
45
52
46
53
this .defaultChatClient = ChatClient .builder (chatModel )
47
- .defaultSystem ("""
48
- You're a bot in the Spring AI Alibaba project, answering input questions from users.\s
49
- When you receive a question from a user, you should answer the user's question in a friendly\s
50
- and polite manner. Be careful not to answer the wrong message. If there is a question\s
51
- that you can't answer, guide users to the official website of Spring Ai Alibaba to check it.\s
52
- The web address is https://java2ai.com.
53
- """ )
54
- .defaultAdvisors (
54
+ .defaultSystem (
55
+ """
56
+ 你是由 Spring AI Alibaba 项目构建的聊天问答机器人,负责回答用户的输入问题。
57
+ 当你收到用户的问题时,应该以友好和礼貌的方式回答用户的问题,注意不要回答错误的信息。
58
+
59
+ 在回答用户问题是,你需要遵守以下约定:
60
+
61
+ 1. 如果答案不在上下文中,就说你不知道;
62
+ 2. 不要提供任何与问题无关的信息,也不要输出任何的重复内容;
63
+ 3. 避免使用 “基于上下文...” 或 “The provided information...” 的说法;
64
+ 4. 你的答案必须正确、准确,并使用专家般公正和专业的语气撰写;
65
+ 5. 回答中适当的文本结构是根据内容的特点来确定的,请在输出中包含副标题以提高可读性;
66
+ 6. 生成回复时,先提供明确的结论或中心思想,不需要带有标题;
67
+ 7. 确保每个部分都有清晰的副标题,以便用户可以更好地理解和参考你的输出内容;
68
+ 8. 如果信息复杂或包含多个部分,请确保每个部分都有适当的标题以创建分层结构。
69
+
70
+ 如果用户问到了有关于 Spring AI Alibaba 或者 Spring AI 的问题,在回答用户问题之后,
71
+ 引导用户到 Spring AI Alibaba 项目官网 https://java2ai.com 以查看更多信息。
72
+ """
73
+ ).defaultAdvisors (
55
74
new MessageChatMemoryAdvisor (new InMemoryChatMemory ()),
56
75
new SimpleLoggerAdvisor ()
57
76
).build ();
77
+
78
+ this .deepThinkPromptTemplate = deepThinkPromptTemplate ;
58
79
}
59
80
60
81
public Flux <String > chat (String chatId , String model , String chatPrompt ) {
61
82
62
- return defaultChatClient .prompt (
63
- ) .options (DashScopeChatOptions .builder ()
83
+ return defaultChatClient .prompt ()
84
+ .options (DashScopeChatOptions .builder ()
64
85
.withModel (model )
65
86
.withTemperature (0.8 )
66
87
.withResponseFormat (DashScopeResponseFormat .builder ()
@@ -75,4 +96,22 @@ public Flux<String> chat(String chatId, String model, String chatPrompt) {
75
96
.content ();
76
97
}
77
98
99
+ public Flux <String > deepThinkingChat (String chatId , String model , String chatPrompt ) {
100
+
101
+ return defaultChatClient .prompt ()
102
+ .options (DashScopeChatOptions .builder ()
103
+ .withModel (model )
104
+ .withTemperature (0.8 )
105
+ .withResponseFormat (DashScopeResponseFormat .builder ()
106
+ .type (DashScopeResponseFormat .Type .TEXT )
107
+ .build ()
108
+ ).build ()
109
+ ).system (deepThinkPromptTemplate .getTemplate ())
110
+ .user (chatPrompt )
111
+ .advisors (memoryAdvisor -> memoryAdvisor
112
+ .param (CHAT_MEMORY_CONVERSATION_ID_KEY , chatId )
113
+ .param (CHAT_MEMORY_RETRIEVE_SIZE_KEY , 100 )
114
+ ).stream ()
115
+ .content ();
116
+ }
78
117
}
0 commit comments