|
| 1 | +package io.graversen.replicate.llama3; |
| 2 | + |
| 3 | +import io.graversen.replicate.common.TextConversation; |
| 4 | +import io.graversen.replicate.common.TextMessage; |
| 5 | +import jakarta.annotation.Nullable; |
| 6 | +import lombok.NonNull; |
| 7 | +import lombok.experimental.UtilityClass; |
| 8 | + |
| 9 | +import java.util.LinkedList; |
| 10 | +import java.util.Objects; |
| 11 | +import java.util.function.Consumer; |
| 12 | +import java.util.stream.Collectors; |
| 13 | + |
| 14 | +@UtilityClass |
| 15 | +public class Llama3Tokenizer { |
| 16 | + private static final String BEGIN_OF_TEXT = "<|begin_of_text|>"; |
| 17 | + private static final String START_HEADER_ID = "<|start_header_id|>"; |
| 18 | + private static final String END_HEADER_ID = "<|end_header_id|>"; |
| 19 | + private static final String END_OF_TEXT_ID = "<|eot_id|>"; |
| 20 | + |
| 21 | + public static final Integer DEFAULT_CONTEXT_WINDOW_SIZE = 8000; |
| 22 | + public static final Integer APPROXIMATE_CHARACTERS_PER_TOKEN = 4; |
| 23 | + public static final String ROLE_USER = "user"; |
| 24 | + public static final String ROLE_ASSISTANT = "assistant"; |
| 25 | + public static final String ROLE_SYSTEM = "system"; |
| 26 | + |
| 27 | + public static String beginOfText(@NonNull String text) { |
| 28 | + return String.format("%s%s", BEGIN_OF_TEXT, text); |
| 29 | + } |
| 30 | + |
| 31 | + public static String endOfText(@NonNull String text) { |
| 32 | + return String.format("%s%s", text, END_OF_TEXT_ID); |
| 33 | + } |
| 34 | + |
| 35 | + public static String header(@NonNull String text) { |
| 36 | + return String.format("%s%s%s", START_HEADER_ID, text, END_HEADER_ID); |
| 37 | + } |
| 38 | + |
| 39 | + public static String userHeader() { |
| 40 | + return Llama3Tokenizer.header(ROLE_USER); |
| 41 | + } |
| 42 | + |
| 43 | + public static String assistantHeader() { |
| 44 | + return Llama3Tokenizer.header(ROLE_ASSISTANT); |
| 45 | + } |
| 46 | + |
| 47 | + public static String systemHeader() { |
| 48 | + return Llama3Tokenizer.header(ROLE_SYSTEM); |
| 49 | + } |
| 50 | + |
| 51 | + public static Llama3TextCompletion generateTextCompletion(@NonNull TextConversation conversation) { |
| 52 | + final var textCompletionBuilder = new StringBuilder(); |
| 53 | + textCompletionBuilder |
| 54 | + .append(BEGIN_OF_TEXT) |
| 55 | + .append(systemHeader()) |
| 56 | + .append(conversation.getSystemMessage()); |
| 57 | + |
| 58 | + conversation.getMessages().forEach(addMessageToTextCompletion(textCompletionBuilder)); |
| 59 | + final var textCompletion = textCompletionBuilder.toString(); |
| 60 | + return new Llama3TextCompletion(textCompletion); |
| 61 | + } |
| 62 | + |
| 63 | + public static Integer approximateConversationContextSize(@NonNull TextConversation conversation, @Nullable Integer tokenSize) { |
| 64 | + final var conversationPlainText = conversation.getMessages().stream() |
| 65 | + .map(TextMessage::getText) |
| 66 | + .collect(Collectors.joining(System.lineSeparator())); |
| 67 | + |
| 68 | + return getTokens(conversationPlainText, tokenSize); |
| 69 | + } |
| 70 | + |
| 71 | + public static TextConversation fitToContextWindow(@NonNull TextConversation conversation, @Nullable Integer contextWindowSize) { |
| 72 | + contextWindowSize = Objects.requireNonNullElse(contextWindowSize, DEFAULT_CONTEXT_WINDOW_SIZE); |
| 73 | + |
| 74 | + final var systemMessage = conversation.getSystemMessage(); |
| 75 | + final var systemMessageTokens = getTokens(systemMessage, null); |
| 76 | + int remainingTokens = contextWindowSize - systemMessageTokens; |
| 77 | + |
| 78 | + final var messages = conversation.getMessages(); |
| 79 | + final var fittedMessages = new LinkedList<TextMessage>(); |
| 80 | + |
| 81 | + for (int i = messages.size() - 1; i >= 0; i--) { |
| 82 | + final var message = messages.get(i); |
| 83 | + final var messageTokens = getTokens(message.getText(), null); |
| 84 | + |
| 85 | + if (remainingTokens - messageTokens >= 0) { |
| 86 | + fittedMessages.addFirst(message); |
| 87 | + remainingTokens -= messageTokens; |
| 88 | + } else { |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return new TextConversation(systemMessage, fittedMessages); |
| 94 | + } |
| 95 | + |
| 96 | + Consumer<TextMessage> addMessageToTextCompletion(@NonNull StringBuilder textCompletionBuilder) { |
| 97 | + return textMessage -> textCompletionBuilder |
| 98 | + .append(header(textMessage.getRole())) |
| 99 | + .append(textMessage.getText()) |
| 100 | + .append(END_OF_TEXT_ID); |
| 101 | + } |
| 102 | + |
| 103 | + private Integer getTokens(@NonNull String string, @Nullable Integer tokenSize) { |
| 104 | + tokenSize = Objects.requireNonNullElse(tokenSize, APPROXIMATE_CHARACTERS_PER_TOKEN); |
| 105 | + return string.length() / tokenSize; |
| 106 | + } |
| 107 | +} |
0 commit comments