mirror of
https://github.com/carlrobertoh/ProxyAI.git
synced 2026-05-12 05:51:28 +00:00
Add interactive total token count label, codebase refactoring
This commit is contained in:
parent
d8e5e18998
commit
ec3120a5e6
31 changed files with 804 additions and 322 deletions
|
|
@ -6,6 +6,7 @@ import com.knuddels.jtokkit.Encodings;
|
|||
import com.knuddels.jtokkit.api.Encoding;
|
||||
import com.knuddels.jtokkit.api.EncodingRegistry;
|
||||
import com.knuddels.jtokkit.api.EncodingType;
|
||||
import ee.carlrobert.codegpt.conversations.Conversation;
|
||||
import ee.carlrobert.llm.client.openai.completion.chat.request.OpenAIChatCompletionMessage;
|
||||
|
||||
@Service
|
||||
|
|
@ -21,9 +22,23 @@ public final class EncodingManager {
|
|||
return ApplicationManager.getApplication().getService(EncodingManager.class);
|
||||
}
|
||||
|
||||
public int countConversationTokens(Conversation conversation) {
|
||||
if (conversation != null) {
|
||||
return conversation.getMessages().stream()
|
||||
.mapToInt(
|
||||
message -> countTokens(message.getPrompt()) + countTokens(message.getResponse()))
|
||||
.sum();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int countMessageTokens(OpenAIChatCompletionMessage message) {
|
||||
return countMessageTokens(message.getRole(), message.getContent());
|
||||
}
|
||||
|
||||
public int countMessageTokens(String role, String content) {
|
||||
var tokensPerMessage = 4; // every message follows <|start|>{role/name}\n{content}<|end|>\n
|
||||
return encoding.countTokens(message.getRole() + message.getContent()) + tokensPerMessage;
|
||||
return countTokens(role + content) + tokensPerMessage;
|
||||
}
|
||||
|
||||
public int countTokens(String text) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue