Skip to content
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

BIGTOP-4220: Remove Message Repository and replace it with ChatMemory #64

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
Loading