-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BIGTOP-4213: Add LLM platform DashScope (#59)
- Loading branch information
Showing
26 changed files
with
868 additions
and
170 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
61 changes: 61 additions & 0 deletions
61
...rc/main/java/org/apache/bigtop/manager/ai/assistant/provider/PersistentStoreProvider.java
This file contains 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,61 @@ | ||
/* | ||
* 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.provider; | ||
|
||
import org.apache.bigtop.manager.ai.assistant.store.PersistentChatMemoryStore; | ||
import org.apache.bigtop.manager.ai.assistant.store.PersistentMessageRepository; | ||
import org.apache.bigtop.manager.ai.core.provider.MessageStoreProvider; | ||
import org.apache.bigtop.manager.ai.core.repository.MessageRepository; | ||
import org.apache.bigtop.manager.dao.repository.ChatMessageDao; | ||
import org.apache.bigtop.manager.dao.repository.ChatThreadDao; | ||
|
||
import dev.langchain4j.store.memory.chat.ChatMemoryStore; | ||
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class PersistentStoreProvider implements MessageStoreProvider { | ||
private final ChatThreadDao chatThreadDao; | ||
private final ChatMessageDao chatMessageDao; | ||
|
||
public PersistentStoreProvider(ChatThreadDao chatThreadDao, ChatMessageDao chatMessageDao) { | ||
this.chatThreadDao = chatThreadDao; | ||
this.chatMessageDao = chatMessageDao; | ||
} | ||
|
||
public PersistentStoreProvider() { | ||
chatMessageDao = null; | ||
chatThreadDao = null; | ||
} | ||
|
||
@Override | ||
public MessageRepository getMessageRepository() { | ||
return new PersistentMessageRepository(chatThreadDao, chatMessageDao); | ||
} | ||
|
||
@Override | ||
public ChatMemoryStore getChatMemoryStore() { | ||
if (chatThreadDao == null) { | ||
return new InMemoryChatMemoryStore(); | ||
} | ||
return new PersistentChatMemoryStore(chatThreadDao, chatMessageDao); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...c/main/java/org/apache/bigtop/manager/ai/assistant/store/PersistentMessageRepository.java
This file contains 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,80 @@ | ||
/* | ||
* 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.ai.core.repository.MessageRepository; | ||
import org.apache.bigtop.manager.dao.po.ChatMessagePO; | ||
import org.apache.bigtop.manager.dao.po.ChatThreadPO; | ||
import org.apache.bigtop.manager.dao.repository.ChatMessageDao; | ||
import org.apache.bigtop.manager.dao.repository.ChatThreadDao; | ||
|
||
public class PersistentMessageRepository implements MessageRepository { | ||
private final ChatThreadDao chatThreadDao; | ||
private final ChatMessageDao chatMessageDao; | ||
|
||
private boolean noPersistent() { | ||
return chatThreadDao == null || chatMessageDao == null; | ||
} | ||
|
||
public PersistentMessageRepository(ChatThreadDao chatThreadDao, ChatMessageDao chatMessageDao) { | ||
this.chatThreadDao = chatThreadDao; | ||
this.chatMessageDao = chatMessageDao; | ||
} | ||
|
||
private ChatMessagePO getChatMessagePO(String message, Long threadId, MessageSender sender) { | ||
if (noPersistent()) { | ||
return null; | ||
} | ||
ChatThreadPO chatThreadPO = chatThreadDao.findById(threadId); | ||
ChatMessagePO chatMessagePO = new ChatMessagePO(); | ||
chatMessagePO.setUserId(chatThreadPO.getUserId()); | ||
chatMessagePO.setThreadId(threadId); | ||
chatMessagePO.setSender(sender.getValue()); | ||
chatMessagePO.setMessage(message); | ||
return chatMessagePO; | ||
} | ||
|
||
@Override | ||
public void saveUserMessage(String message, Long threadId) { | ||
if (noPersistent()) { | ||
return; | ||
} | ||
ChatMessagePO chatMessagePO = getChatMessagePO(message, threadId, MessageSender.USER); | ||
chatMessageDao.save(chatMessagePO); | ||
} | ||
|
||
@Override | ||
public void saveAiMessage(String message, Long threadId) { | ||
if (noPersistent()) { | ||
return; | ||
} | ||
ChatMessagePO chatMessagePO = getChatMessagePO(message, threadId, MessageSender.AI); | ||
chatMessageDao.save(chatMessagePO); | ||
} | ||
|
||
@Override | ||
public void saveSystemMessage(String message, Long threadId) { | ||
if (noPersistent()) { | ||
return; | ||
} | ||
ChatMessagePO chatMessagePO = getChatMessagePO(message, threadId, MessageSender.SYSTEM); | ||
chatMessageDao.save(chatMessagePO); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.