-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mccloskey/llama2 hf #17
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6491ed2
Adding token counter and prompt builder class
colinmccloskey 3ade3a0
Local tokenizer
colinmccloskey 31dfcd5
Adding token counter and prompt builder class
colinmccloskey 172c587
Local tokenizer
colinmccloskey 2a1fe09
Finishing token counting and adding tests
colinmccloskey 2c3fb97
Merge branch 'mccloskey/llama2-hf' of https://github.com/facebookincu…
colinmccloskey ab41ff3
deleting old tokenizer.json
colinmccloskey c802798
Fixing to truncate oldest messages not newest
colinmccloskey a70d5a3
Formatting, removing comments, removing now-duplicated system message…
colinmccloskey d8441e1
Fixing problem where new tokenizer, etc was created each time
Colin1104 422367d
Removing unused files and functions
Colin1104 e640b6a
Update OpenAIPluginTest to have default system message in ordered cor…
Colin1104 068752d
Deleting comments/unused imports
Colin1104 1bdd4a4
Delete unused line
Colin1104 18541c7
Changing pruneMessages to not create middle array list and return opt…
Colin1104 bea6e96
Removing comments
colinmccloskey 6342b3d
Addressing comments
colinmccloskey 0154e86
Merge branch 'main' into mccloskey/llama2-hf
colinmccloskey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
109 changes: 109 additions & 0 deletions
109
src/main/java/com/meta/cp4m/llm/HuggingFaceLlamaPrompt.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,109 @@ | ||
/* | ||
* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.meta.cp4m.llm; | ||
|
||
import ai.djl.huggingface.tokenizers.Encoding; | ||
import ai.djl.huggingface.tokenizers.HuggingFaceTokenizer; | ||
import com.meta.cp4m.message.Message; | ||
import com.meta.cp4m.message.ThreadState; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.nio.file.Paths; | ||
import java.util.*; | ||
|
||
public class HuggingFaceLlamaPrompt<T extends Message> { | ||
|
||
private final String systemMessage; | ||
private final long maxInputTokens; | ||
private final HuggingFaceTokenizer tokenizer; | ||
|
||
public HuggingFaceLlamaPrompt(String systemMessage, long maxInputTokens) { | ||
|
||
this.systemMessage = systemMessage; | ||
this.maxInputTokens = maxInputTokens; | ||
URL llamaTokenizerUrl = | ||
Objects.requireNonNull( | ||
HuggingFaceLlamaPrompt.class.getClassLoader().getResource("llamaTokenizer.json")); | ||
URI llamaTokenizer; | ||
try { | ||
llamaTokenizer = llamaTokenizerUrl.toURI(); | ||
tokenizer = HuggingFaceTokenizer.newInstance(Paths.get(llamaTokenizer)); | ||
|
||
} catch (URISyntaxException | IOException e) { | ||
// this should be impossible | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public Optional<String> createPrompt(ThreadState<T> threadState) { | ||
|
||
PromptBuilder builder = new PromptBuilder(); | ||
|
||
int totalTokens = tokenCount(this.systemMessage) + 5; // Account for closing tokens | ||
builder.addSystem(this.systemMessage); | ||
|
||
for (int i = threadState.messages().size() - 1; i >= 0; i--) { | ||
Message m = threadState.messages().get(i); | ||
totalTokens += tokenCount(m.message()); | ||
if (totalTokens > maxInputTokens) { | ||
if (i == threadState.messages().size() - 1){ | ||
return Optional.empty(); | ||
} | ||
break; | ||
} | ||
switch (m.role()) { | ||
case USER -> builder.addUser(m.message()); | ||
case ASSISTANT -> builder.addAssistant(m.message()); | ||
} | ||
} | ||
|
||
return Optional.of(builder.build()); | ||
} | ||
|
||
private int tokenCount(String message) { | ||
Encoding encoding = tokenizer.encode(message); | ||
return encoding.getTokens().length; | ||
} | ||
|
||
private static class PromptBuilder { | ||
colinmccloskey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
StringBuilder promptStringBuilder = new StringBuilder(); | ||
StringBuilder messagesStringBuilder = new StringBuilder(); | ||
|
||
void addSystem(String message) { | ||
promptStringBuilder | ||
.append("<s>[INST] <<SYS>>\n") | ||
.append(message) | ||
.append("\n<</SYS>>\n\n"); | ||
} | ||
|
||
void addAssistant(String message) { | ||
StringBuilder tempBuilder = new StringBuilder(); | ||
tempBuilder | ||
.append(message) | ||
.append(" </s><s>[INST] "); | ||
messagesStringBuilder.append(tempBuilder.reverse()); | ||
} | ||
|
||
void addUser(String message) { | ||
StringBuilder tempBuilder = new StringBuilder(); | ||
tempBuilder | ||
.append(message) | ||
.append(" [/INST] "); | ||
messagesStringBuilder.append(tempBuilder.reverse()); | ||
} | ||
|
||
String build() { | ||
return promptStringBuilder.append(messagesStringBuilder.reverse()).toString().strip(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍