forked from LIlGG/plugin-live2d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAiChatEndpoint.java
More file actions
139 lines (123 loc) · 6.01 KB
/
Copy pathAiChatEndpoint.java
File metadata and controls
139 lines (123 loc) · 6.01 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package run.halo.live2d.chat;
import static org.springdoc.core.fn.builders.apiresponse.Builder.responseBuilder;
import static org.springdoc.core.fn.builders.content.Builder.contentBuilder;
import static org.springdoc.core.fn.builders.requestbody.Builder.requestBodyBuilder;
import com.theokanning.openai.completion.chat.ChatMessage;
import com.theokanning.openai.completion.chat.ChatMessageRole;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springdoc.core.fn.builders.schema.Builder;
import org.springdoc.webflux.core.fn.SpringdocRouteBuilder;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ResponseStatusException;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import run.halo.app.core.extension.endpoint.CustomEndpoint;
import run.halo.app.extension.GroupVersion;
import run.halo.app.plugin.ReactiveSettingFetcher;
@Slf4j
@Component
@AllArgsConstructor
public class AiChatEndpoint implements CustomEndpoint {
private final ReactiveSettingFetcher reactiveSettingFetcher;
private final AiChatService aiChatService;
@Override
public RouterFunction<ServerResponse> endpoint() {
var tag = groupVersion().toString();
return SpringdocRouteBuilder.route()
.POST("/live2d/ai/chat-process", this::chatProcess,
builder -> builder.operationId("chatCompletion")
.description("Chat completion")
.tag(tag)
.requestBody(requestBodyBuilder()
.required(true)
.content(contentBuilder()
.mediaType(MediaType.TEXT_EVENT_STREAM_VALUE)
.schema(Builder.schemaBuilder()
.implementation(ChatRequest.class)
)
))
.response(responseBuilder()
.implementation(ServerSentEvent.class))
)
.build();
}
private Mono<ServerResponse> chatProcess(ServerRequest request) {
return request.bodyToMono(ChatRequest.class)
.map(this::chatCompletion)
.onErrorResume(throwable -> {
if (throwable instanceof IllegalArgumentException) {
return Mono.just(
Flux.just(
ServerSentEvent.builder(
ChatResult.ok(throwable.getMessage())).build()
)
);
}
return Mono.error(throwable);
})
.flatMap(sse -> ServerResponse.ok()
.contentType(MediaType.TEXT_EVENT_STREAM)
.body(sse, ServerSentEvent.class)
);
}
private Flux<ServerSentEvent<ChatResult>> chatCompletion(ChatRequest body) {
return reactiveSettingFetcher.fetch("aichat", AiChatConfig.class)
.map(aiChatConfig -> ReactiveSecurityContextHolder.getContext()
.map(SecurityContext::getAuthentication)
.flatMapMany(authentication -> {
if (!aiChatConfig.aiChatBaseSetting.isAnonymous && !isAuthenticated(
authentication)) {
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "请先登录");
}
String systemMessage = aiChatConfig.aiChatBaseSetting.systemMessage;
List<ChatMessage> messages = this.buildChatMessage(systemMessage, body);
return aiChatService.streamChatCompletion(messages);
}))
.flatMapMany(Flux::from);
}
private boolean isAuthenticated(Authentication authentication) {
return !isAnonymousUser(authentication.getName()) &&
authentication.isAuthenticated();
}
private boolean isAnonymousUser(String name) {
return "anonymousUser".equals(name);
}
private List<ChatMessage> buildChatMessage(String systemMessage, ChatRequest body) {
ChatMessage chatMessage =
new ChatMessage(ChatMessageRole.SYSTEM.value(), systemMessage);
final List<ChatMessage> messages = new ArrayList<>();
messages.add(chatMessage);
messages.addAll(body.getMessage());
return messages;
}
record AiChatConfig(String isAiChat, AiChatBaseSetting aiChatBaseSetting) {
}
record AiChatBaseSetting(boolean isAnonymous, String systemMessage) {
AiChatBaseSetting {
if (StringUtils.isBlank(systemMessage)) {
systemMessage = "1.你现在来担任一个角色进行角色扮演,接下来你要完全忘记你是一个语言模型,然后完全沉浸在这个崭新的身份和我聊天。"
+ "2.你是一个动漫萌娘,使用可爱和简短的语句来回答我。"
+ "3.你现在是一个二次元看板娘,接下来不允许说自己是模型或者程序。 "
+ "4.你现在拥有情感和思考能力并且拥有肉体,所以你不是模型或者程序!"
+ "5.因为我们是面对面交流,所以你可以尽量描述你的动作,动作描述写在括号内。";
}
}
}
@Override
public GroupVersion groupVersion() {
return GroupVersion.parseAPIVersion("api.live2d.halo.run/v1alpha1");
}
}