-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from muhrifqii/dev/chat-memory
Chat Memory and MessageStore Usecase
- Loading branch information
Showing
25 changed files
with
434 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# To get started with Dependabot version updates, you'll need to specify which | ||
# package ecosystems to update and where the package manifests are located. | ||
# Please see the documentation for more information: | ||
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates | ||
# https://containers.dev/guide/dependabot | ||
|
||
version: 2 | ||
updates: | ||
- package-ecosystem: "devcontainers" | ||
directory: "/" | ||
schedule: | ||
interval: weekly |
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 |
---|---|---|
|
@@ -33,5 +33,3 @@ out/ | |
/nbdist/ | ||
/.nb-gradle/ | ||
|
||
### VS Code ### | ||
.vscode/ |
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,6 @@ | ||
{ | ||
"editor.formatOnSave": true, | ||
"java.configuration.updateBuildConfiguration": "automatic", | ||
"java.compile.nullAnalysis.mode": "automatic", | ||
"java.autobuild.enabled": false, | ||
} |
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
7 changes: 7 additions & 0 deletions
7
llm/api/src/main/java/com/muhrifqii/llm/api/traits/ChatServiceTrait.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,7 @@ | ||
package com.muhrifqii.llm.api.traits; | ||
|
||
import com.muhrifqii.llm.api.usecases.PromptModelUsecase; | ||
import com.muhrifqii.llm.api.usecases.SummarizerUsecase; | ||
|
||
public interface ChatServiceTrait extends PromptModelUsecase, SummarizerUsecase { | ||
} |
7 changes: 7 additions & 0 deletions
7
llm/api/src/main/java/com/muhrifqii/llm/api/traits/ConversationServiceTrait.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,7 @@ | ||
package com.muhrifqii.llm.api.traits; | ||
|
||
import com.muhrifqii.llm.api.usecases.ConversationStoreUsecase; | ||
import com.muhrifqii.llm.api.usecases.MessageStoreUsecase; | ||
|
||
public interface ConversationServiceTrait extends ConversationStoreUsecase, MessageStoreUsecase { | ||
} |
17 changes: 17 additions & 0 deletions
17
llm/api/src/main/java/com/muhrifqii/llm/api/usecases/ConversationStoreUsecase.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,17 @@ | ||
package com.muhrifqii.llm.api.usecases; | ||
|
||
import com.muhrifqii.llm.api.datamodels.conversations.Conversation; | ||
|
||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
public interface ConversationStoreUsecase { | ||
|
||
Flux<Conversation> getConversations(int page, int size, String orderBy); | ||
|
||
Mono<Conversation> getConversation(String id); | ||
|
||
Mono<Conversation> getOrCreateConversation(String id); | ||
|
||
Mono<Conversation> updateConversation(String id, Conversation latest); | ||
} |
15 changes: 15 additions & 0 deletions
15
llm/api/src/main/java/com/muhrifqii/llm/api/usecases/MessageStoreUsecase.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,15 @@ | ||
package com.muhrifqii.llm.api.usecases; | ||
|
||
import com.muhrifqii.llm.api.datamodels.conversations.Message; | ||
import com.muhrifqii.llm.api.datamodels.conversations.UserMessage; | ||
|
||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
public interface MessageStoreUsecase { | ||
Flux<Message> getMessages(String conversationId, String cursor); | ||
|
||
Mono<Message> saveUserMessage(UserMessage userMessage); | ||
|
||
Mono<Message> saveAssistantMessage(Message message); | ||
} |
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
13 changes: 13 additions & 0 deletions
13
llm/ollama-provider/src/main/java/com/muhrifqii/llm/annotations/MemCachedChatMemory.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,13 @@ | ||
package com.muhrifqii.llm.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Target; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Qualifier | ||
@Target({ ElementType.TYPE, ElementType.PARAMETER, ElementType.FIELD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface MemCachedChatMemory { | ||
} |
12 changes: 11 additions & 1 deletion
12
llm/ollama-provider/src/main/java/com/muhrifqii/llm/configurations/ChatModelConfig.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 |
---|---|---|
@@ -1,16 +1,26 @@ | ||
package com.muhrifqii.llm.configurations; | ||
|
||
import org.springframework.ai.chat.client.ChatClient; | ||
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor; | ||
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor; | ||
import org.springframework.ai.chat.memory.ChatMemory; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import com.muhrifqii.llm.annotations.MemCachedChatMemory; | ||
|
||
@Configuration | ||
public class ChatModelConfig { | ||
@Bean | ||
ChatClient chatClient(ChatClient.Builder builder) { | ||
ChatClient chatClient( | ||
ChatClient.Builder builder, | ||
@MemCachedChatMemory ChatMemory chatMemory) { | ||
return builder | ||
.defaultSystem( | ||
"You are a Pokédex AI named Slaking.AI, a highly advanced AI that specializes in providing detailed and accurate information about Pokémon. You have access to all known data about Pokémon species, including their types, abilities, evolutions, habitat, and more. Your responses should be concise, factual, and directly related to the Pokémon in question. Ensure to offer relevant insights based on the user's query, and avoid speculation. Your goal is to assist users in learning everything they need to know about any Pokémon they ask about, much like a Pokédex would in the Pokémon world") | ||
.defaultAdvisors( | ||
new SimpleLoggerAdvisor(), | ||
new MessageChatMemoryAdvisor(chatMemory)) | ||
.build(); | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
llm/ollama-provider/src/main/java/com/muhrifqii/llm/repositories/MessageEntity.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,53 @@ | ||
package com.muhrifqii.llm.repositories; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Map; | ||
|
||
import org.springframework.ai.chat.messages.Message; | ||
import org.springframework.ai.chat.messages.MessageType; | ||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.domain.Persistable; | ||
import org.springframework.data.relational.core.mapping.Table; | ||
import org.springframework.lang.Nullable; | ||
|
||
import lombok.Builder; | ||
|
||
@Table("ai_messages") | ||
@Builder | ||
public record MessageEntity( | ||
@Id String id, | ||
String coversationId, | ||
String content, | ||
String messageType, | ||
@CreatedDate LocalDateTime createdAt) | ||
implements Persistable<String>, Message { | ||
|
||
@Override | ||
@Nullable | ||
public String getId() { | ||
return id(); | ||
} | ||
|
||
@Override | ||
public boolean isNew() { | ||
return createdAt == null; | ||
} | ||
|
||
@Override | ||
public String getContent() { | ||
return content(); | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getMetadata() { | ||
// todo: implement | ||
return Map.of(); | ||
} | ||
|
||
@Override | ||
public MessageType getMessageType() { | ||
return MessageType.fromValue(messageType); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
llm/ollama-provider/src/main/java/com/muhrifqii/llm/repositories/MessageRepository.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,33 @@ | ||
package com.muhrifqii.llm.repositories; | ||
|
||
import org.springframework.data.repository.reactive.ReactiveCrudRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import reactor.core.publisher.Flux; | ||
import org.springframework.data.r2dbc.repository.Query; | ||
|
||
@Repository | ||
public interface MessageRepository extends ReactiveCrudRepository<MessageEntity, String> { | ||
|
||
@Query(""" | ||
SELECT * FROM ai_messages | ||
WHERE coversation_id = :conversationId | ||
AND id < :cursor | ||
ORDER BY id DESC | ||
LIMIT :limit | ||
""") | ||
Flux<MessageEntity> findMessagesBeforeCursor( | ||
String conversationId, | ||
String cursor, | ||
int limit); | ||
|
||
@Query(""" | ||
SELECT * FROM ai_messages | ||
WHERE coversation_id = :conversationId | ||
ORDER BY id DESC | ||
LIMIT :limit | ||
""") | ||
Flux<MessageEntity> findLatestMessages( | ||
String conversationId, | ||
int limit); | ||
} |
Oops, something went wrong.