Skip to content

Commit 22af58c

Browse files
authored
Android llama demo skip special tokens (#82)
Handle "<|start_header_id|>{role}<|end_header_id|>"
1 parent 53c1f57 commit 22af58c

File tree

1 file changed

+15
-6
lines changed
  • llm/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo

1 file changed

+15
-6
lines changed

llm/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo/MainActivity.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,28 @@ public class MainActivity extends AppCompatActivity implements Runnable, LlmCall
8484
private long startPos = 0;
8585
private static final int CONVERSATION_HISTORY_MESSAGE_LOOKBACK = 2;
8686
private Executor executor;
87+
private boolean sawStartHeaderId = false;
8788

8889
@Override
8990
public void onResult(String result) {
9091
if (result.equals(PromptFormat.getStopToken(mCurrentSettingsFields.getModelType()))) {
9192
return;
9293
}
9394
result = PromptFormat.replaceSpecialToken(mCurrentSettingsFields.getModelType(), result);
94-
if (result.equals("\n\n") || result.equals("\n")) {
95-
if (!mResultMessage.getText().isEmpty()) {
96-
mResultMessage.appendText(result);
97-
run();
98-
}
99-
} else {
95+
96+
if (mCurrentSettingsFields.getModelType() == ModelType.LLAMA_3 && result.equals("<|start_header_id|>")) {
97+
sawStartHeaderId = true;
98+
}
99+
if (mCurrentSettingsFields.getModelType() == ModelType.LLAMA_3 && result.equals("<|end_header_id|>")) {
100+
sawStartHeaderId = false;
101+
return;
102+
}
103+
if (sawStartHeaderId) {
104+
return;
105+
}
106+
107+
boolean keepResult = !(result.equals("\n") || result.equals("\n\n")) || !mResultMessage.getText().isEmpty();
108+
if (keepResult) {
100109
mResultMessage.appendText(result);
101110
run();
102111
}

0 commit comments

Comments
 (0)