-
Notifications
You must be signed in to change notification settings - Fork 46
BIGTOP-4209: Add backend API for AI Chat functionality #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 25 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
5e965f7
tmp commit
lhpqaq 05f43dc
finish platform api
lhpqaq 547bc57
tmp talk
lhpqaq fd9b8ae
remain talk and history
lhpqaq 4c65a1d
mvn spotless:apply
lhpqaq fc4ffdc
Merge branch 'ai/chat' into dev
lhpqaq 5686fbb
some details left
lhpqaq cab275d
remain test
lhpqaq 7ebdff9
spotless:check
lhpqaq afb0d0d
add language
lhpqaq 64bec89
spotless:apply
lhpqaq 24fd4dd
finish
lhpqaq 2308681
last spotless:apply
lhpqaq 81dc33d
add license
lhpqaq 95ad7cf
fix some chore and add ddl
lhpqaq 37fce01
remove a comment and log
lhpqaq 6115c1b
modify openai url
lhpqaq 45eb6cc
add ai assistant func
lhpqaq 8d5caea
add bigmodel moudle
lhpqaq 1486aab
mvn spotless:apply
lhpqaq c21e81d
add bigmodel
lhpqaq e8bfd1e
remove a variable
lhpqaq b4da0dd
add a constructor function
lhpqaq f7f06ed
add enum MessageSender
lhpqaq 2e158f9
fix var name
lhpqaq e703386
add qianfan
lhpqaq 4696f7a
remove url
lhpqaq f2ac4d5
revert app.yml
lhpqaq 0ecabe4
remove qianfan and zhipu
lhpqaq 49b1466
apply diff from @kevinw66
lhpqaq b3ce436
fix some bug
lhpqaq 531634d
rename dao
lhpqaq aa74b09
tmp commit
lhpqaq b55b95b
finish switch to mybatis
lhpqaq 1aac351
remove JsonToMapConverter
lhpqaq 71cd3de
revert pom.xml
lhpqaq cba1b1f
fix some chores
lhpqaq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
...src/main/java/org/apache/bigtop/manager/ai/assistant/store/PersistentChatMemoryStore.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.bigtop.manager.ai.assistant.store; | ||
|
|
||
| import org.apache.bigtop.manager.ai.core.enums.MessageSender; | ||
| import org.apache.bigtop.manager.dao.po.ChatMessagePO; | ||
| import org.apache.bigtop.manager.dao.po.ChatThreadPO; | ||
| import org.apache.bigtop.manager.dao.repository.ChatMessageRepository; | ||
| import org.apache.bigtop.manager.dao.repository.ChatThreadRepository; | ||
|
|
||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import dev.langchain4j.data.message.AiMessage; | ||
| import dev.langchain4j.data.message.ChatMessage; | ||
| import dev.langchain4j.data.message.ChatMessageType; | ||
| import dev.langchain4j.data.message.SystemMessage; | ||
| import dev.langchain4j.data.message.UserMessage; | ||
| import dev.langchain4j.store.memory.chat.ChatMemoryStore; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Component | ||
| public class PersistentChatMemoryStore implements ChatMemoryStore { | ||
|
|
||
| private final ChatThreadRepository chatThreadRepository; | ||
| private final ChatMessageRepository chatMessageRepository; | ||
|
|
||
| public PersistentChatMemoryStore( | ||
| ChatThreadRepository chatThreadRepository, ChatMessageRepository chatMessageRepository) { | ||
| this.chatThreadRepository = chatThreadRepository; | ||
| this.chatMessageRepository = chatMessageRepository; | ||
| } | ||
|
|
||
| private ChatMessage convertToChatMessage(ChatMessagePO chatMessagePO) { | ||
| String sender = chatMessagePO.getSender().toUpperCase(); | ||
| if (sender.equals(MessageSender.AI.getValue())) { | ||
| return new AiMessage(chatMessagePO.getMessage()); | ||
| } else if (sender.equals(MessageSender.USER.getValue())) { | ||
| return new UserMessage(chatMessagePO.getMessage()); | ||
| } else if (sender.equals(MessageSender.SYSTEM.getValue())) { | ||
| return new SystemMessage(chatMessagePO.getMessage()); | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private ChatMessagePO convertToChatMessagePO(ChatMessage chatMessage, Long chatThreadId) { | ||
| ChatMessagePO chatMessagePO = new ChatMessagePO(); | ||
| if (chatMessage.type().equals(ChatMessageType.AI)) { | ||
| chatMessagePO.setSender(MessageSender.AI.getValue()); | ||
| AiMessage aiMessage = (AiMessage) chatMessage; | ||
| chatMessagePO.setMessage(aiMessage.text()); | ||
| } else if (chatMessage.type().equals(ChatMessageType.USER)) { | ||
| chatMessagePO.setSender(MessageSender.USER.getValue()); | ||
| UserMessage userMessage = (UserMessage) chatMessage; | ||
| chatMessagePO.setMessage(userMessage.singleText()); | ||
| } else if (chatMessage.type().equals(ChatMessageType.SYSTEM)) { | ||
| chatMessagePO.setSender(MessageSender.SYSTEM.getValue()); | ||
| SystemMessage systemMessage = (SystemMessage) chatMessage; | ||
| chatMessagePO.setMessage(systemMessage.text()); | ||
| } else { | ||
| chatMessagePO.setSender(chatMessage.type().toString()); | ||
| } | ||
| ChatThreadPO chatThreadPO = chatThreadRepository.findById(chatThreadId).orElse(null); | ||
| if (chatThreadPO != null) { | ||
| chatMessagePO.setUserPO(chatThreadPO.getUserPO()); | ||
| } else { | ||
| chatMessagePO.setUserPO(null); | ||
| } | ||
| chatMessagePO.setChatThreadPO( | ||
| chatThreadRepository.findById(chatThreadId).orElse(null)); | ||
| return chatMessagePO; | ||
| } | ||
|
|
||
| @Override | ||
| public List<ChatMessage> getMessages(Object threadId) { | ||
| ChatThreadPO chatThreadPO = null; | ||
| if (chatThreadRepository != null) { | ||
| chatThreadPO = chatThreadRepository.findById((Long) threadId).orElse(null); | ||
| } | ||
| if (chatThreadPO == null) { | ||
| return new ArrayList<>(); | ||
| } | ||
| List<ChatMessagePO> chatMessages = chatMessageRepository.findAllByChatThreadPO(chatThreadPO); | ||
| if (chatMessages.isEmpty()) { | ||
| return new ArrayList<>(); | ||
| } else { | ||
| return chatMessages.stream().map(this::convertToChatMessage).collect(Collectors.toList()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void updateMessages(Object threadId, List<ChatMessage> messages) { | ||
| ChatMessagePO chatMessagePO = convertToChatMessagePO(messages.get(messages.size() - 1), (Long) threadId); | ||
| chatMessageRepository.save(chatMessagePO); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteMessages(Object threadId) { | ||
| ChatThreadPO chatThreadPO = | ||
| chatThreadRepository.findById((Long) threadId).orElse(null); | ||
| chatMessageRepository.deleteByChatThreadPO(chatThreadPO); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
| ~ Licensed to the Apache Software Foundation (ASF) under one | ||
| ~ or more contributor license agreements. See the NOTICE file | ||
| ~ distributed with this work for additional information | ||
| ~ regarding copyright ownership. The ASF licenses this file | ||
| ~ to you under the Apache License, Version 2.0 (the | ||
| ~ "License"); you may not use this file except in compliance | ||
| ~ with the License. You may obtain a copy of the License at | ||
| ~ | ||
| ~ https://www.apache.org/licenses/LICENSE-2.0 | ||
| ~ | ||
| ~ Unless required by applicable law or agreed to in writing, | ||
| ~ software distributed under the License is distributed on an | ||
| ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| ~ KIND, either express or implied. See the License for the | ||
| ~ specific language governing permissions and limitations | ||
| ~ under the License. | ||
| --> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>org.apache.bigtop</groupId> | ||
| <artifactId>bigtop-manager-ai</artifactId> | ||
| <version>${revision}</version> | ||
| </parent> | ||
|
|
||
| <artifactId>bigtop-manager-ai-bigmodel</artifactId> | ||
| <name>${project.artifactId}</name> | ||
| <description>Bigtop Manager AI BigModel</description> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.apache.bigtop</groupId> | ||
| <artifactId>bigtop-manager-ai-core</artifactId> | ||
| <version>${revision}</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>dev.langchain4j</groupId> | ||
| <artifactId>langchain4j-open-ai</artifactId> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> |
112 changes: 112 additions & 0 deletions
112
...er-ai-bigmodel/src/main/java/org/apache/bigtop/manager/ai/bigmodel/BigModelAssistant.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.bigtop.manager.ai.bigmodel; | ||
|
|
||
| import org.apache.bigtop.manager.ai.core.AbstractAIAssistant; | ||
| import org.apache.bigtop.manager.ai.core.factory.AIAssistant; | ||
| import org.apache.bigtop.manager.ai.core.provider.AIAssistantConfigProvider; | ||
|
|
||
| import org.springframework.util.NumberUtils; | ||
|
|
||
| import dev.langchain4j.internal.ValidationUtils; | ||
| import dev.langchain4j.memory.ChatMemory; | ||
| import dev.langchain4j.memory.chat.MessageWindowChatMemory; | ||
| import dev.langchain4j.model.chat.ChatLanguageModel; | ||
| import dev.langchain4j.model.chat.StreamingChatLanguageModel; | ||
| import dev.langchain4j.model.openai.OpenAiChatModel; | ||
| import dev.langchain4j.model.openai.OpenAiStreamingChatModel; | ||
| import dev.langchain4j.store.memory.chat.ChatMemoryStore; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class BigModelAssistant extends AbstractAIAssistant { | ||
|
|
||
| private static final String PLATFORM_NAME = "bigmodel"; | ||
| private static final String BASE_URL = "https://open.bigmodel.cn/api/paas/v4/"; | ||
| private static final String MODEL_NAME = "glm-4-0520"; | ||
|
|
||
| private BigModelAssistant( | ||
| ChatLanguageModel chatLanguageModel, | ||
| StreamingChatLanguageModel streamingChatLanguageModel, | ||
| ChatMemory chatMemory) { | ||
| super(chatLanguageModel, streamingChatLanguageModel, chatMemory); | ||
| } | ||
|
|
||
| @Override | ||
| public String getPlatform() { | ||
| return PLATFORM_NAME; | ||
| } | ||
|
|
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public static class Builder { | ||
| private Object id; | ||
|
|
||
| private Map<String, String> configs = new HashMap<>(); | ||
| private ChatMemoryStore chatMemoryStore; | ||
|
|
||
| public Builder() { | ||
| configs.put("baseUrl", BASE_URL); | ||
| configs.put("modelName", MODEL_NAME); | ||
| } | ||
|
|
||
| public Builder withConfigProvider(AIAssistantConfigProvider configProvider) { | ||
| this.configs = configProvider.configs(); | ||
| return this; | ||
| } | ||
|
|
||
| public Builder id(Object id) { | ||
| this.id = id; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder memoryStore(ChatMemoryStore chatMemoryStore) { | ||
| this.chatMemoryStore = chatMemoryStore; | ||
| return this; | ||
| } | ||
|
|
||
| public AIAssistant build() { | ||
| ValidationUtils.ensureNotNull(id, "id"); | ||
| String baseUrl = configs.get("baseUrl"); | ||
| String modelName = configs.get("modelName"); | ||
| String apiKey = ValidationUtils.ensureNotNull(configs.get("apiKey"), "apiKey"); | ||
| Integer memoryLen = ValidationUtils.ensureNotNull( | ||
| NumberUtils.parseNumber(configs.get("memoryLen"), Integer.class), "memoryLen not a number."); | ||
| ChatLanguageModel bigModelChatModel = OpenAiChatModel.builder() | ||
| .apiKey(apiKey) | ||
| .baseUrl(baseUrl) | ||
| .modelName(modelName) | ||
| .build(); | ||
| StreamingChatLanguageModel bigModelStreamChatModel = OpenAiStreamingChatModel.builder() | ||
kevinw66 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .apiKey(apiKey) | ||
| .baseUrl(baseUrl) | ||
| .modelName(modelName) | ||
| .build(); | ||
| MessageWindowChatMemory chatMemory = MessageWindowChatMemory.builder() | ||
| .id(id) | ||
| .chatMemoryStore(chatMemoryStore) | ||
| .maxMessages(memoryLen) | ||
| .build(); | ||
| return new BigModelAssistant(bigModelChatModel, bigModelStreamChatModel, chatMemory); | ||
| } | ||
| } | ||
| } | ||
42 changes: 42 additions & 0 deletions
42
...ager-ai-bigmodel/src/main/java/org/apache/bigtop/manager/ai/bigmodel/BigModelToolBox.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.bigtop.manager.ai.bigmodel; | ||
|
|
||
| import org.apache.bigtop.manager.ai.core.factory.ToolBox; | ||
|
|
||
| import reactor.core.publisher.Flux; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class BigModelToolBox implements ToolBox { | ||
| @Override | ||
| public List<String> getTools() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public String invoke(String toolName) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Flux<String> streamInvoke(String toolName) { | ||
| return null; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.