mirror of
https://github.com/ccmjga/zhilu-admin
synced 2026-03-14 05:33:42 +08:00
add zhipu
This commit is contained in:
@@ -5,6 +5,6 @@ import dev.langchain4j.service.TokenStream;
|
||||
import dev.langchain4j.service.UserMessage;
|
||||
import dev.langchain4j.service.memory.ChatMemoryAccess;
|
||||
|
||||
public interface DeepSeekChatAssistant extends ChatMemoryAccess {
|
||||
public interface AiChatAssistant extends ChatMemoryAccess {
|
||||
TokenStream chat(@MemoryId String memoryId, @UserMessage String userMessage);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.zl.mjga.config.ai;
|
||||
|
||||
import dev.langchain4j.community.model.zhipu.ZhipuAiStreamingChatModel;
|
||||
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
|
||||
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
|
||||
import dev.langchain4j.service.AiServices;
|
||||
@@ -12,6 +13,18 @@ import org.springframework.context.annotation.Configuration;
|
||||
public class ChatModelConfig {
|
||||
|
||||
private final DeepSeekConfiguration deepSeekConfiguration;
|
||||
private final ZhiPuConfiguration zhiPuConfiguration;
|
||||
private final PromptConfiguration promptConfiguration;
|
||||
|
||||
@Bean
|
||||
public ZhipuAiStreamingChatModel zhipuChatModel() {
|
||||
return ZhipuAiStreamingChatModel.builder()
|
||||
.model(zhiPuConfiguration.getModelName())
|
||||
.apiKey(zhiPuConfiguration.getApiKey())
|
||||
.logRequests(true)
|
||||
.logResponses(true)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public OpenAiStreamingChatModel deepSeekChatModel() {
|
||||
@@ -23,10 +36,19 @@ public class ChatModelConfig {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DeepSeekChatAssistant deepSeekChatAssistant(OpenAiStreamingChatModel deepSeekChatModel) {
|
||||
return AiServices.builder(DeepSeekChatAssistant.class)
|
||||
public AiChatAssistant deepSeekChatAssistant(OpenAiStreamingChatModel deepSeekChatModel) {
|
||||
return AiServices.builder(AiChatAssistant.class)
|
||||
.streamingChatModel(deepSeekChatModel)
|
||||
.systemMessageProvider(chatMemoryId -> deepSeekConfiguration.getPrompt().getSystem())
|
||||
.systemMessageProvider(chatMemoryId -> promptConfiguration.getSystem())
|
||||
.chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AiChatAssistant zhiPuChatAssistant(ZhipuAiStreamingChatModel zhipuChatModel) {
|
||||
return AiServices.builder(AiChatAssistant.class)
|
||||
.streamingChatModel(zhipuChatModel)
|
||||
.systemMessageProvider(chatMemoryId -> promptConfiguration.getSystem())
|
||||
.chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
package com.zl.mjga.config.ai;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Data
|
||||
@@ -15,22 +9,7 @@ import org.springframework.stereotype.Component;
|
||||
@ConfigurationProperties(prefix = "deep-seek")
|
||||
public class DeepSeekConfiguration {
|
||||
|
||||
@jakarta.annotation.Resource private ResourceLoader resourceLoader;
|
||||
|
||||
private String baseUrl;
|
||||
private String apiKey;
|
||||
private Prompt prompt;
|
||||
private String modelName;
|
||||
|
||||
@Data
|
||||
public static class Prompt {
|
||||
private String system;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() throws IOException {
|
||||
Resource resource = resourceLoader.getResource("classpath:prompt.txt");
|
||||
prompt = new Prompt();
|
||||
prompt.setSystem(Files.readString(Paths.get(resource.getURI())));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.zl.mjga.config.ai;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import lombok.Data;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Data
|
||||
@Component
|
||||
public class PromptConfiguration {
|
||||
|
||||
@jakarta.annotation.Resource private ResourceLoader resourceLoader;
|
||||
private String system;
|
||||
|
||||
@PostConstruct
|
||||
public void init() throws IOException {
|
||||
Resource resource = resourceLoader.getResource("classpath:prompt.txt");
|
||||
system = Files.readString(Paths.get(resource.getURI()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.zl.mjga.config.ai;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "zhipu")
|
||||
public class ZhiPuConfiguration {
|
||||
|
||||
private String baseUrl;
|
||||
private String apiKey;
|
||||
private String modelName;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.zl.mjga.controller;
|
||||
|
||||
import com.zl.mjga.service.DeepSeekAiService;
|
||||
import com.zl.mjga.service.AiChatService;
|
||||
import dev.langchain4j.service.TokenStream;
|
||||
import java.security.Principal;
|
||||
import java.time.Duration;
|
||||
@@ -17,12 +17,12 @@ import reactor.core.publisher.Sinks;
|
||||
@Slf4j
|
||||
public class AiController {
|
||||
|
||||
private final DeepSeekAiService deepSeekAiService;
|
||||
private final AiChatService aiChatService;
|
||||
|
||||
@PostMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
public Flux<String> chat(Principal principal, @RequestBody String userMessage) {
|
||||
Sinks.Many<String> sink = Sinks.many().unicast().onBackpressureBuffer();
|
||||
TokenStream chat = deepSeekAiService.chat(principal.getName(), userMessage);
|
||||
TokenStream chat = aiChatService.chatWithZhiPu(principal.getName(), userMessage);
|
||||
chat.onPartialResponse(text -> sink.tryEmitNext(text.replace(" ", "␣").replace("\t", "⇥")))
|
||||
.onCompleteResponse(
|
||||
r -> {
|
||||
|
||||
24
backend/src/main/java/com/zl/mjga/service/AiChatService.java
Normal file
24
backend/src/main/java/com/zl/mjga/service/AiChatService.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.zl.mjga.service;
|
||||
|
||||
import com.zl.mjga.config.ai.AiChatAssistant;
|
||||
import dev.langchain4j.service.TokenStream;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class AiChatService {
|
||||
|
||||
private final AiChatAssistant deepSeekChatAssistant;
|
||||
private final AiChatAssistant zhiPuChatAssistant;
|
||||
|
||||
public TokenStream chatWithDeepSeek(String sessionIdentifier, String userMessage) {
|
||||
return deepSeekChatAssistant.chat(sessionIdentifier, userMessage);
|
||||
}
|
||||
|
||||
public TokenStream chatWithZhiPu(String sessionIdentifier, String userMessage) {
|
||||
return zhiPuChatAssistant.chat(sessionIdentifier, userMessage);
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.zl.mjga.service;
|
||||
|
||||
import com.zl.mjga.config.ai.DeepSeekChatAssistant;
|
||||
import dev.langchain4j.service.TokenStream;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class DeepSeekAiService {
|
||||
|
||||
private final DeepSeekChatAssistant deepSeekChatAssistant;
|
||||
|
||||
public TokenStream chat(String sessionIdentifier, String userMessage) {
|
||||
return deepSeekChatAssistant.chat(sessionIdentifier, userMessage);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user