Skip to content

Commit

Permalink
BIGTOP-4220: Remove Message Repository and replace it with ChatMemory (
Browse files Browse the repository at this point in the history
  • Loading branch information
lhpqaq authored Sep 12, 2024
1 parent 077a5f6 commit a87d2e8
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 285 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,41 @@
package org.apache.bigtop.manager.ai.assistant;

import org.apache.bigtop.manager.ai.assistant.provider.LocSystemPromptProvider;
import org.apache.bigtop.manager.ai.assistant.provider.PersistentStoreProvider;
import org.apache.bigtop.manager.ai.core.AbstractAIAssistantFactory;
import org.apache.bigtop.manager.ai.core.enums.PlatformType;
import org.apache.bigtop.manager.ai.core.enums.SystemPrompt;
import org.apache.bigtop.manager.ai.core.exception.PlatformNotFoundException;
import org.apache.bigtop.manager.ai.core.factory.AIAssistant;
import org.apache.bigtop.manager.ai.core.factory.ToolBox;
import org.apache.bigtop.manager.ai.core.provider.AIAssistantConfigProvider;
import org.apache.bigtop.manager.ai.core.provider.MessageStoreProvider;
import org.apache.bigtop.manager.ai.core.provider.SystemPromptProvider;
import org.apache.bigtop.manager.ai.dashscope.DashScopeAssistant;
import org.apache.bigtop.manager.ai.openai.OpenAIAssistant;

import org.apache.commons.lang3.NotImplementedException;

import java.util.Objects;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore;

public class GeneralAssistantFactory extends AbstractAIAssistantFactory {

private final SystemPromptProvider systemPromptProvider;
private final MessageStoreProvider messageStoreProvider;
private final ChatMemoryStore chatMemoryStore;

public GeneralAssistantFactory() {
this(new LocSystemPromptProvider(), new PersistentStoreProvider());
this(new LocSystemPromptProvider(), new InMemoryChatMemoryStore());
}

public GeneralAssistantFactory(SystemPromptProvider systemPromptProvider) {
this(systemPromptProvider, new PersistentStoreProvider());
this(systemPromptProvider, new InMemoryChatMemoryStore());
}

public GeneralAssistantFactory(MessageStoreProvider messageStoreProvider) {
this(new LocSystemPromptProvider(), messageStoreProvider);
public GeneralAssistantFactory(ChatMemoryStore chatMemoryStore) {
this(new LocSystemPromptProvider(), chatMemoryStore);
}

public GeneralAssistantFactory(
SystemPromptProvider systemPromptProvider, MessageStoreProvider messageStoreProvider) {
public GeneralAssistantFactory(SystemPromptProvider systemPromptProvider, ChatMemoryStore chatMemoryStore) {
this.systemPromptProvider = systemPromptProvider;
this.messageStoreProvider = messageStoreProvider;
this.chatMemoryStore = chatMemoryStore;
}

@Override
Expand All @@ -65,22 +62,17 @@ public AIAssistant createWithPrompt(
AIAssistantConfigProvider assistantConfig,
Object id,
SystemPrompt systemPrompts) {
AIAssistant aiAssistant;
if (Objects.requireNonNull(platformType) == PlatformType.OPENAI) {
aiAssistant = OpenAIAssistant.builder()
.id(id)
.memoryStore(messageStoreProvider.getChatMemoryStore())
.withConfigProvider(assistantConfig)
.build();
} else if (Objects.requireNonNull(platformType) == PlatformType.DASH_SCOPE) {
aiAssistant = DashScopeAssistant.builder()
.id(id)
.withConfigProvider(assistantConfig)
.messageRepository(messageStoreProvider.getMessageRepository())
.build();
} else {
throw new PlatformNotFoundException(platformType.getValue());
}
AIAssistant.Builder builder =
switch (platformType) {
case OPENAI -> OpenAIAssistant.builder();
case DASH_SCOPE -> DashScopeAssistant.builder();
};

AIAssistant aiAssistant = builder.id(id)
.memoryStore((id == null) ? new InMemoryChatMemoryStore() : chatMemoryStore)
.withConfigProvider(assistantConfig)
.build();

String systemPrompt = systemPromptProvider.getSystemMessage(systemPrompts);
aiAssistant.setSystemPrompt(systemPrompt);
String locale = assistantConfig.getLanguage();
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,46 @@
package org.apache.bigtop.manager.ai.core;

import org.apache.bigtop.manager.ai.core.factory.AIAssistant;
import org.apache.bigtop.manager.ai.core.provider.AIAssistantConfigProvider;

import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;

public abstract class AbstractAIAssistant implements AIAssistant {

protected static final Integer MEMORY_LEN = 10;
protected final ChatMemory chatMemory;

protected AbstractAIAssistant(ChatMemory chatMemory) {
this.chatMemory = chatMemory;
}

@Override
public boolean test() {
return ask("1+1=") != null;
}

public abstract static class Builder implements AIAssistant.Builder {
protected Object id;

protected ChatMemoryStore chatMemoryStore;
protected AIAssistantConfigProvider configProvider;

public Builder() {}

public Builder withConfigProvider(AIAssistantConfigProvider configProvider) {
this.configProvider = configProvider;
return this;
}

public Builder id(Object id) {
this.id = id;
return this;
}

public Builder memoryStore(ChatMemoryStore chatMemoryStore) {
this.chatMemoryStore = chatMemoryStore;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
package org.apache.bigtop.manager.ai.core.factory;

import org.apache.bigtop.manager.ai.core.enums.PlatformType;
import org.apache.bigtop.manager.ai.core.provider.AIAssistantConfigProvider;

import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import reactor.core.publisher.Flux;

import java.util.HashMap;
Expand Down Expand Up @@ -73,4 +75,14 @@ default Map<String, String> createThread() {
* @return
*/
boolean test();

interface Builder {
Builder id(Object id);

Builder memoryStore(ChatMemoryStore memoryStore);

Builder withConfigProvider(AIAssistantConfigProvider configProvider);

AIAssistant build();
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit a87d2e8

Please sign in to comment.