-
Notifications
You must be signed in to change notification settings - Fork 101
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
Overhaul how chat memory is configured #112
Merged
Merged
Changes from all commits
Commits
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 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
40 changes: 40 additions & 0 deletions
40
...deployment/src/main/java/io/quarkiverse/langchain4j/deployment/ChatMemoryBuildConfig.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,40 @@ | ||
package io.quarkiverse.langchain4j.deployment; | ||
|
||
import static io.quarkus.runtime.annotations.ConfigPhase.BUILD_TIME; | ||
|
||
import dev.langchain4j.memory.ChatMemory; | ||
import dev.langchain4j.memory.chat.ChatMemoryProvider; | ||
import dev.langchain4j.model.Tokenizer; | ||
import dev.langchain4j.store.memory.chat.ChatMemoryStore; | ||
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore; | ||
import io.quarkiverse.langchain4j.RegisterAiService; | ||
import io.quarkus.runtime.annotations.ConfigRoot; | ||
import io.smallrye.config.ConfigMapping; | ||
import io.smallrye.config.WithDefault; | ||
|
||
@ConfigRoot(phase = BUILD_TIME) | ||
@ConfigMapping(prefix = "quarkus.langchain4j.chat-memory") | ||
public interface ChatMemoryBuildConfig { | ||
|
||
/** | ||
* Configure the type of {@link ChatMemory} that will be used by default by the default {@link ChatMemoryProvider} bean. | ||
* <p> | ||
* The extension provides a default bean that configures {@link ChatMemoryProvider} for use with AI services | ||
* registered with {@link RegisterAiService}. This bean depends uses the {@code quarkus.langchain4j.chat-memory} | ||
* configuration to set things up while also depending on the presence of a bean of type {@link ChatMemoryStore} (for which | ||
* the extension also provides a default in the form of {@link InMemoryChatMemoryStore}). | ||
* <p> | ||
* If {@code token-window} is used, then the application must also provide a bean of type {@link Tokenizer}. | ||
* <p> | ||
* Users can choose to provide their own {@link ChatMemoryStore} bean or even their own {@link ChatMemoryProvider} bean | ||
* if full control over the details is needed. | ||
*/ | ||
@WithDefault("MESSAGE_WINDOW") | ||
Type type(); | ||
|
||
enum Type { | ||
MESSAGE_WINDOW, | ||
TOKEN_WINDOW | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
core/deployment/src/main/java/io/quarkiverse/langchain4j/deployment/ChatMemoryProcessor.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,51 @@ | ||
package io.quarkiverse.langchain4j.deployment; | ||
|
||
import java.util.function.Function; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
import org.jboss.jandex.ClassType; | ||
|
||
import dev.langchain4j.memory.chat.ChatMemoryProvider; | ||
import dev.langchain4j.model.Tokenizer; | ||
import dev.langchain4j.store.memory.chat.ChatMemoryStore; | ||
import io.quarkiverse.langchain4j.runtime.ChatMemoryRecorder; | ||
import io.quarkiverse.langchain4j.runtime.aiservice.ChatMemoryConfig; | ||
import io.quarkus.arc.SyntheticCreationalContext; | ||
import io.quarkus.arc.deployment.SyntheticBeanBuildItem; | ||
import io.quarkus.deployment.annotations.BuildProducer; | ||
import io.quarkus.deployment.annotations.BuildStep; | ||
import io.quarkus.deployment.annotations.ExecutionTime; | ||
import io.quarkus.deployment.annotations.Record; | ||
|
||
public class ChatMemoryProcessor { | ||
|
||
@BuildStep | ||
@Record(ExecutionTime.RUNTIME_INIT) | ||
void setupBeans(ChatMemoryBuildConfig buildConfig, ChatMemoryConfig runtimeConfig, | ||
ChatMemoryRecorder recorder, | ||
BuildProducer<SyntheticBeanBuildItem> syntheticBeanProducer) { | ||
|
||
Function<SyntheticCreationalContext<ChatMemoryProvider>, ChatMemoryProvider> fun; | ||
|
||
SyntheticBeanBuildItem.ExtendedBeanConfigurator configurator = SyntheticBeanBuildItem | ||
.configure(ChatMemoryProvider.class) | ||
.setRuntimeInit() | ||
.addInjectionPoint(ClassType.create(ChatMemoryStore.class)) | ||
.scope(ApplicationScoped.class) | ||
.defaultBean(); | ||
|
||
if (buildConfig.type() == ChatMemoryBuildConfig.Type.MESSAGE_WINDOW) { | ||
fun = recorder.messageWindow(runtimeConfig); | ||
} else if (buildConfig.type() == ChatMemoryBuildConfig.Type.TOKEN_WINDOW) { | ||
configurator.addInjectionPoint(ClassType.create(Tokenizer.class)); | ||
fun = recorder.tokenWindow(runtimeConfig); | ||
} else { | ||
throw new IllegalStateException( | ||
"Invalid configuration '" + buildConfig.type() + "' used in 'quarkus.langchain4j.chat-memory.type'"); | ||
} | ||
configurator.createWith(fun); | ||
|
||
syntheticBeanProducer.produce(configurator.done()); | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
core/runtime/src/main/java/io/quarkiverse/langchain4j/ChatMemoryRemover.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,39 @@ | ||
package io.quarkiverse.langchain4j; | ||
|
||
import java.util.List; | ||
|
||
import dev.langchain4j.memory.ChatMemory; | ||
import dev.langchain4j.store.memory.chat.ChatMemoryStore; | ||
import io.quarkiverse.langchain4j.runtime.aiservice.ChatMemoryRemovable; | ||
|
||
/** | ||
* Allows the application to manually control when a {@link ChatMemory} should be removed from the underlying | ||
* {@link ChatMemoryStore}. | ||
*/ | ||
public final class ChatMemoryRemover { | ||
|
||
private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; | ||
|
||
private ChatMemoryRemover() { | ||
} | ||
|
||
/** | ||
* @param aiService The bean that implements the AI Service annotated with {@link RegisterAiService} | ||
* @param memoryId The object used as memory IDs for which the corresponding {@link ChatMemory} should be removed | ||
*/ | ||
public static void remove(Object aiService, Object memoryId) { | ||
if (aiService instanceof ChatMemoryRemovable r) { | ||
r.remove(memoryId); | ||
} | ||
} | ||
|
||
/** | ||
* @param aiService The bean that implements the AI Service annotated with {@link RegisterAiService} | ||
* @param memoryIds The objects used as memory IDs for which the corresponding {@link ChatMemory} should be removed | ||
*/ | ||
public static void remove(Object aiService, List<Object> memoryIds) { | ||
if (aiService instanceof ChatMemoryRemovable r) { | ||
r.remove(memoryIds.toArray(EMPTY_OBJECT_ARRAY)); | ||
} | ||
} | ||
} |
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
13 changes: 0 additions & 13 deletions
13
core/runtime/src/main/java/io/quarkiverse/langchain4j/RemovableChatMemoryProvider.java
This file was deleted.
Oops, something went wrong.
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like using two objects here as users could easily swap them, but I don't have a better way of handling it...