|
| 1 | +package com.redis.vl.extensions.messagehistory; |
| 2 | + |
| 3 | +import static com.redis.vl.extensions.Constants.*; |
| 4 | + |
| 5 | +import com.github.f4b6a3.ulid.UlidCreator; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +/** |
| 12 | + * Base class for message history implementations. |
| 13 | + * |
| 14 | + * <p>Matches the Python BaseMessageHistory from redisvl.extensions.message_history.base_history |
| 15 | + */ |
| 16 | +public abstract class BaseMessageHistory { |
| 17 | + |
| 18 | + protected final String name; |
| 19 | + protected final String sessionTag; |
| 20 | + |
| 21 | + /** |
| 22 | + * Initialize message history. |
| 23 | + * |
| 24 | + * @param name The name of the message history index |
| 25 | + * @param sessionTag Tag to be added to entries to link to a specific conversation session. |
| 26 | + * Defaults to instance ULID. |
| 27 | + */ |
| 28 | + protected BaseMessageHistory(String name, String sessionTag) { |
| 29 | + this.name = name; |
| 30 | + this.sessionTag = (sessionTag != null) ? sessionTag : UlidCreator.getUlid().toString(); |
| 31 | + } |
| 32 | + |
| 33 | + /** Clears the chat message history. */ |
| 34 | + public abstract void clear(); |
| 35 | + |
| 36 | + /** Clear all conversation history and remove any search indices. */ |
| 37 | + public abstract void delete(); |
| 38 | + |
| 39 | + /** |
| 40 | + * Remove a specific exchange from the conversation history. |
| 41 | + * |
| 42 | + * @param id The id of the entry to delete. If null then the last entry is deleted. |
| 43 | + */ |
| 44 | + public abstract void drop(String id); |
| 45 | + |
| 46 | + /** Returns the full chat history. */ |
| 47 | + public abstract List<Map<String, Object>> getMessages(); |
| 48 | + |
| 49 | + /** |
| 50 | + * Retrieve the recent conversation history in sequential order. |
| 51 | + * |
| 52 | + * @param topK The number of previous messages to return. Default is 5. |
| 53 | + * @param asText Whether to return the conversation as a list of content strings, or list of |
| 54 | + * message maps. |
| 55 | + * @param raw Whether to return the full Redis hash entry or just the role/content/tool_call_id. |
| 56 | + * @param sessionTag Tag of the entries linked to a specific conversation session. Defaults to |
| 57 | + * instance ULID. |
| 58 | + * @return List of messages (either as text strings or maps depending on asText parameter) |
| 59 | + * @throws IllegalArgumentException if topK is not an integer greater than or equal to 0 |
| 60 | + */ |
| 61 | + public abstract <T> List<T> getRecent(int topK, boolean asText, boolean raw, String sessionTag); |
| 62 | + |
| 63 | + /** |
| 64 | + * Insert a prompt:response pair into the message history. |
| 65 | + * |
| 66 | + * @param prompt The user prompt to the LLM |
| 67 | + * @param response The corresponding LLM response |
| 68 | + * @param sessionTag The tag to mark the messages with. Defaults to instance session tag. |
| 69 | + */ |
| 70 | + public abstract void store(String prompt, String response, String sessionTag); |
| 71 | + |
| 72 | + /** |
| 73 | + * Insert a list of prompts and responses into the message history. |
| 74 | + * |
| 75 | + * @param messages The list of user prompts and LLM responses |
| 76 | + * @param sessionTag The tag to mark the messages with. Defaults to instance session tag. |
| 77 | + */ |
| 78 | + public abstract void addMessages(List<Map<String, String>> messages, String sessionTag); |
| 79 | + |
| 80 | + /** |
| 81 | + * Insert a single prompt or response into the message history. |
| 82 | + * |
| 83 | + * @param message The user prompt or LLM response |
| 84 | + * @param sessionTag The tag to mark the message with. Defaults to instance session tag. |
| 85 | + */ |
| 86 | + public abstract void addMessage(Map<String, String> message, String sessionTag); |
| 87 | + |
| 88 | + /** |
| 89 | + * Formats messages from Redis into either text strings or structured maps. |
| 90 | + * |
| 91 | + * @param messages The messages from the message history index |
| 92 | + * @param asText Whether to return as text strings or maps |
| 93 | + * @return Formatted messages |
| 94 | + */ |
| 95 | + @SuppressWarnings("unchecked") |
| 96 | + protected <T> List<T> formatContext(List<Map<String, Object>> messages, boolean asText) { |
| 97 | + List<T> context = new ArrayList<>(); |
| 98 | + |
| 99 | + for (Map<String, Object> message : messages) { |
| 100 | + ChatMessage chatMessage = ChatMessage.fromDict(message); |
| 101 | + |
| 102 | + if (asText) { |
| 103 | + context.add((T) chatMessage.getContent()); |
| 104 | + } else { |
| 105 | + Map<String, Object> chatMessageDict = new HashMap<>(); |
| 106 | + chatMessageDict.put(ROLE_FIELD_NAME, chatMessage.getRole()); |
| 107 | + chatMessageDict.put(CONTENT_FIELD_NAME, chatMessage.getContent()); |
| 108 | + |
| 109 | + if (chatMessage.getToolCallId() != null) { |
| 110 | + chatMessageDict.put(TOOL_FIELD_NAME, chatMessage.getToolCallId()); |
| 111 | + } |
| 112 | + |
| 113 | + context.add((T) chatMessageDict); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return context; |
| 118 | + } |
| 119 | + |
| 120 | + public String getName() { |
| 121 | + return name; |
| 122 | + } |
| 123 | + |
| 124 | + public String getSessionTag() { |
| 125 | + return sessionTag; |
| 126 | + } |
| 127 | +} |
0 commit comments