Skip to content
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
5e965f7
tmp commit
lhpqaq Aug 24, 2024
05f43dc
finish platform api
lhpqaq Aug 26, 2024
547bc57
tmp talk
lhpqaq Aug 26, 2024
fd9b8ae
remain talk and history
lhpqaq Aug 26, 2024
4c65a1d
mvn spotless:apply
lhpqaq Aug 26, 2024
fc4ffdc
Merge branch 'ai/chat' into dev
lhpqaq Aug 26, 2024
5686fbb
some details left
lhpqaq Aug 26, 2024
cab275d
remain test
lhpqaq Aug 26, 2024
7ebdff9
spotless:check
lhpqaq Aug 26, 2024
afb0d0d
add language
lhpqaq Aug 26, 2024
64bec89
spotless:apply
lhpqaq Aug 26, 2024
24fd4dd
finish
lhpqaq Aug 27, 2024
2308681
last spotless:apply
lhpqaq Aug 27, 2024
81dc33d
add license
lhpqaq Aug 27, 2024
95ad7cf
fix some chore and add ddl
lhpqaq Aug 27, 2024
37fce01
remove a comment and log
lhpqaq Aug 27, 2024
6115c1b
modify openai url
lhpqaq Aug 27, 2024
45eb6cc
add ai assistant func
lhpqaq Aug 28, 2024
8d5caea
add bigmodel moudle
lhpqaq Aug 28, 2024
1486aab
mvn spotless:apply
lhpqaq Aug 28, 2024
c21e81d
add bigmodel
lhpqaq Aug 28, 2024
e8bfd1e
remove a variable
lhpqaq Aug 28, 2024
b4da0dd
add a constructor function
lhpqaq Aug 28, 2024
f7f06ed
add enum MessageSender
lhpqaq Aug 28, 2024
2e158f9
fix var name
lhpqaq Aug 28, 2024
e703386
add qianfan
lhpqaq Aug 29, 2024
4696f7a
remove url
lhpqaq Aug 29, 2024
f2ac4d5
revert app.yml
lhpqaq Aug 29, 2024
0ecabe4
remove qianfan and zhipu
lhpqaq Aug 29, 2024
49b1466
apply diff from @kevinw66
lhpqaq Aug 29, 2024
b3ce436
fix some bug
lhpqaq Aug 29, 2024
531634d
rename dao
lhpqaq Aug 29, 2024
aa74b09
tmp commit
lhpqaq Aug 29, 2024
b55b95b
finish switch to mybatis
lhpqaq Aug 29, 2024
1aac351
remove JsonToMapConverter
lhpqaq Aug 29, 2024
71cd3de
revert pom.xml
lhpqaq Aug 29, 2024
cba1b1f
fix some chores
lhpqaq Aug 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bigtop-manager-ai/bigtop-manager-ai-assistant/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,18 @@
<groupId>org.apache.bigtop</groupId>
<artifactId>bigtop-manager-ai-openai</artifactId>
</dependency>
<dependency>
<groupId>org.apache.bigtop</groupId>
<artifactId>bigtop-manager-ai-bigmodel</artifactId>
</dependency>
<dependency>
<groupId>org.apache.bigtop</groupId>
<artifactId>bigtop-manager-ai-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.bigtop</groupId>
<artifactId>bigtop-manager-dao</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.bigtop.manager.ai.assistant;

import org.apache.bigtop.manager.ai.assistant.provider.LocSystemPromptProvider;
import org.apache.bigtop.manager.ai.bigmodel.BigModelAssistant;
import org.apache.bigtop.manager.ai.core.AbstractAIAssistantFactory;
import org.apache.bigtop.manager.ai.core.enums.PlatformType;
import org.apache.bigtop.manager.ai.core.factory.AIAssistant;
Expand All @@ -44,6 +45,10 @@ public GeneralAssistantFactory(SystemPromptProvider systemPromptProvider) {
this.systemPromptProvider = systemPromptProvider;
}

public GeneralAssistantFactory(ChatMemoryStore chatMemoryStore) {
this.chatMemoryStore = chatMemoryStore;
}

public GeneralAssistantFactory(SystemPromptProvider systemPromptProvider, ChatMemoryStore chatMemoryStore) {
this.systemPromptProvider = systemPromptProvider;
this.chatMemoryStore = chatMemoryStore;
Expand All @@ -68,6 +73,14 @@ public AIAssistant create(PlatformType platformType, AIAssistantConfigProvider a
.build();
aiAssistant.setSystemPrompt(systemPromptProvider.getSystemPrompt());
return aiAssistant;
} else if (Objects.requireNonNull(platformType) == PlatformType.BIGMODEL) {
AIAssistant aiAssistant = BigModelAssistant.builder()
.id(id)
.memoryStore(chatMemoryStore)
.withConfigProvider(assistantConfig)
.build();
aiAssistant.setSystemPrompt(systemPromptProvider.getSystemPrompt());
return aiAssistant;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private SystemMessage loadPromptFromFile(String fileName) {
log.error(
"Exception occurred while loading SystemPrompt from local. Here is some information:{}",
e.getMessage());
return SystemMessage.from("");
return SystemMessage.from("You are a helpful assistant.");
}
}
}
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);
}
}
44 changes: 44 additions & 0 deletions bigtop-manager-ai/bigtop-manager-ai-bigmodel/pom.xml
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>
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()
.apiKey(apiKey)
.baseUrl(baseUrl)
.modelName(modelName)
.build();
MessageWindowChatMemory chatMemory = MessageWindowChatMemory.builder()
.id(id)
.chatMemoryStore(chatMemoryStore)
.maxMessages(memoryLen)
.build();
return new BigModelAssistant(bigModelChatModel, bigModelStreamChatModel, chatMemory);
}
}
}
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;
}
}
Loading