Skip to content

Commit

Permalink
Merge pull request #146 from quarkiverse/#144
Browse files Browse the repository at this point in the history
Make @ApplicationScoped beans work with ChatMemoryRemover
  • Loading branch information
geoand authored Dec 13, 2023
2 parents d9b008a + 266f7f0 commit 1f7a7c3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import io.quarkiverse.langchain4j.runtime.aiservice.ChatMemoryRemovable;
import io.quarkus.arc.ClientProxy;

/**
* Allows the application to manually control when a {@link ChatMemory} should be removed from the underlying
Expand All @@ -22,7 +23,8 @@ private ChatMemoryRemover() {
* @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) {
var obj = ClientProxy.unwrap(aiService);
if (obj instanceof ChatMemoryRemovable r) {
r.remove(memoryId);
}
}
Expand All @@ -32,7 +34,8 @@ public static void remove(Object aiService, Object memoryId) {
* @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) {
var obj = ClientProxy.unwrap(aiService);
if (obj instanceof ChatMemoryRemovable r) {
r.remove(memoryIds.toArray(EMPTY_OBJECT_ARRAY));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
Expand Down Expand Up @@ -81,7 +82,7 @@ void setup() {
}

@RegisterAiService
@Singleton
@ApplicationScoped
interface ChatWithSeparateMemoryForEachUser {

String chat(@MemoryId int memoryId, @UserMessage String userMessage);
Expand Down

0 comments on commit 1f7a7c3

Please sign in to comment.