Skip to content

Commit

Permalink
Adds token-estimate for prompts
Browse files Browse the repository at this point in the history
I thought it would be helpful to give the user an idea as to how many tokens will be used up by their system prompt. In this change, we assume a 4/1 (chars to tokens) relationship, and calculate the tokens-used estimate for the user's prompt. This estimate is shared next to the label/tooltip, above the textarea.
  • Loading branch information
jonathansampson committed Oct 18, 2024
1 parent 79b3b48 commit f024708
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/brave_settings_strings.grdp
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,9 @@ Leo. This action can't be undone.
<message name="IDS_SETTINGS_LEO_ASSISTANT_MODEL_SYSTEM_PROMPT_DESC" desc="A description message for the custom model system prompt">
Custom instructions and/or context given to the model to guide its responses in a specific direction or style. Use &#x25;datetime&#x25; where you would like the current date and time to be inserted.
</message>
<message name="IDS_SETTINGS_LEO_ASSISTANT_TOKENS_COUNT" desc="A label for the (estimated) number of tokens in the system prompt">
≈<ph name="TOKENS_COUNT">$1<ex>175</ex></ph> tokens
</message>

<!-- Settings / Playlist -->
<message name="IDS_SETTINGS_PLAYLIST_ENABLE_PLAYLIST_LABEL" desc="The text for setting to en/disable Playlist feature">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
margin-bottom: 5px;
}

.inline-note {
color: var(--leo-color-text-tertiary);
font: var(--leo-font-small-regular);
}

.actions-container leo-button:first-of-type {
margin-right: 5px;
}
Expand Down Expand Up @@ -249,13 +254,20 @@
<cr-icon class="small" icon="info-outline"></cr-icon>
</leo-button>
</leo-tooltip>
<template
is="dom-if"
if="[[promptTokensEstimate]]"
>
<span class="inline-note">[[promptTokensEstimateString]]</span>
</template>
</div>
</div>
<cr-textarea
value="[[modelSystemPrompt]]"
rows="4"
placeholder="$i18n{braveLeoAssistantModelSystemPromptPlaceholder}"
on-change="onModelSystemPromptChange_"
on-input="constructTokenEstimateString_"
></cr-textarea>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ export class ModelConfigUI extends ModelConfigUIBase {
type: String,
value: ''
},
promptTokensEstimate: {
type: Number,
value: 0
},
promptTokensEstimateString: {
type: String,
value: ''
},
endpointUrl: {
type: String
},
Expand Down Expand Up @@ -78,6 +86,8 @@ export class ModelConfigUI extends ModelConfigUIBase {
modelRequestName: string
contextSize: number
modelSystemPrompt: string | null
promptTokensEstimate: number
promptTokensEstimateString: string
endpointUrl: string
apiKey: string
modelItem: mojom.Model | null
Expand Down Expand Up @@ -167,6 +177,25 @@ export class ModelConfigUI extends ModelConfigUIBase {
this.apiKey = e.target.value
}

constructTokenEstimateString_( e?: any ) {
let charsLength = 0;
let tokensLength = 0;

if ( e?.target?.value ) {
charsLength = e.target.value.length;
} else if ( this.modelSystemPrompt ) {
charsLength = this.modelSystemPrompt.length;
}

tokensLength = charsLength > 0 ? Math.ceil(charsLength/4) : 0;

this.promptTokensEstimate = tokensLength
this.promptTokensEstimateString = this.i18n(
'braveLeoAssistantTokensCount',
this.promptTokensEstimate
)
}

private saveEnabled_() {
// Make sure all required fields are filled
return this.label && this.modelRequestName && this.endpointUrl && !this.isUrlInvalid
Expand All @@ -184,6 +213,7 @@ export class ModelConfigUI extends ModelConfigUIBase {
this.modelSystemPrompt =
newValue.options.customModelOptions.modelSystemPrompt
}
this.constructTokenEstimateString_()
}

private computeIsEditing_(modelItem: mojom.Model | null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source,
IDS_SETTINGS_LEO_ASSISTANT_MODEL_SYSTEM_PROMPT_TITLE},
{"braveLeoAssistantModelSystemPromptDesc",
IDS_SETTINGS_LEO_ASSISTANT_MODEL_SYSTEM_PROMPT_DESC},
{"braveLeoAssistantTokensCount", IDS_SETTINGS_LEO_ASSISTANT_TOKENS_COUNT},

// New Tab Page
{"braveNewTab", IDS_SETTINGS_NEW_TAB},
Expand Down

0 comments on commit f024708

Please sign in to comment.