Skip to content

Commit a271a11

Browse files
committed
🐛 Missing end-of-text token after system message
1 parent fe3fbdd commit a271a11

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

spring-boot-starter-replicate/src/main/java/io/graversen/replicate/llama3/Llama3Tokenizer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ Integer getTokens(@NonNull String string, @Nullable Integer tokenSize) {
143143
StringBuilder createBeginOfText(@NonNull String systemMessage) {
144144
return new StringBuilder().append(BEGIN_OF_TEXT)
145145
.append(systemHeader())
146-
.append(systemMessage);
146+
.append(systemMessage)
147+
.append(END_OF_TEXT_ID);
147148
}
148149
}

spring-boot-starter-replicate/src/test/java/io/graversen/replicate/llama3/Llama3TokenizerTest.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ class Llama3TokenizerTest {
2020
);
2121

2222
@Test
23-
void parseTextCompletion_exampleConversation() {
23+
void parseTextCompletion_exampleConversation1() {
2424
final var tokenizedConversation =
25-
"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n" +
26-
"You are a helpful assistant.<|eot_id|><|start_header_id|>user<|end_header_id|>\n" +
27-
"Hello. How are you?<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n" +
28-
"Hello! I'm doing great, thanks for asking! I'm here to help you with anything you need, so please feel free to ask me any questions or share what's on your mind. How about you? How's your day going so far?<|eot_id|><|start_header_id|>user<|end_header_id|>\n" +
29-
"It is going good thanks<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n" +
30-
"That's wonderful to hear! I'm glad to know that your day is going well. If you don't mind me asking, what's been the highlight of your day so far? Is there anything exciting or interesting that's happened? I'm all ears and happy to listen!<|eot_id|><|start_header_id|>user<|end_header_id|>\n" +
25+
"<|begin_of_text|><|start_header_id|>system<|end_header_id|>" +
26+
"You are a helpful assistant.<|eot_id|><|start_header_id|>user<|end_header_id|>" +
27+
"Hello. How are you?<|eot_id|><|start_header_id|>assistant<|end_header_id|>" +
28+
"Hello! I'm doing great, thanks for asking! I'm here to help you with anything you need, so please feel free to ask me any questions or share what's on your mind. How about you? How's your day going so far?<|eot_id|><|start_header_id|>user<|end_header_id|>" +
29+
"It is going good thanks<|eot_id|><|start_header_id|>assistant<|end_header_id|>" +
30+
"That's wonderful to hear! I'm glad to know that your day is going well. If you don't mind me asking, what's been the highlight of your day so far? Is there anything exciting or interesting that's happened? I'm all ears and happy to listen!<|eot_id|><|start_header_id|>user<|end_header_id|>" +
3131
"Nah fam<|eot_id|>";
3232

3333
final var conversation = Llama3Tokenizer.parseTextCompletion(tokenizedConversation);
@@ -51,6 +51,32 @@ void parseTextCompletion_exampleConversation() {
5151
assertEquals("Nah fam", conversation.getMessages().get(4).getText());
5252
}
5353

54+
@Test
55+
void parseTextCompletion_exampleConversation2() {
56+
final var tokenizedConversation =
57+
"<|begin_of_text|><|start_header_id|>system<|end_header_id|>You are a calculator app. Respond only with the result of the math query.<|eot_id|><|start_header_id|>user<|end_header_id|>What is 2 + 2?<|eot_id|><|start_header_id|>assistant<|end_header_id|>4<|eot_id|><|start_header_id|>user<|end_header_id|>What is 3 * 3?<|eot_id|><|start_header_id|>assistant<|end_header_id|>9<|eot_id|><|start_header_id|>user<|end_header_id|>What is seven minus two?<|eot_id|>";
58+
59+
final var conversation = Llama3Tokenizer.parseTextCompletion(tokenizedConversation);
60+
61+
assertEquals("You are a calculator app. Respond only with the result of the math query.", conversation.getSystemMessage());
62+
assertEquals(5, conversation.getMessages().size());
63+
64+
assertEquals("user", conversation.getMessages().get(0).getRole());
65+
assertEquals("What is 2 + 2?", conversation.getMessages().get(0).getText());
66+
67+
assertEquals("assistant", conversation.getMessages().get(1).getRole());
68+
assertEquals("4", conversation.getMessages().get(1).getText());
69+
70+
assertEquals("user", conversation.getMessages().get(2).getRole());
71+
assertEquals("What is 3 * 3?", conversation.getMessages().get(2).getText());
72+
73+
assertEquals("assistant", conversation.getMessages().get(3).getRole());
74+
assertEquals("9", conversation.getMessages().get(3).getText());
75+
76+
assertEquals("user", conversation.getMessages().get(3).getRole());
77+
assertEquals("What is seven minus two?", conversation.getMessages().get(3).getText());
78+
}
79+
5480
@Test
5581
public void fitToContextWindow_defaultWindow() {
5682
final var conversation = new TextConversation(systemMessage, messages);

0 commit comments

Comments
 (0)