Skip to content

Commit 78803c4

Browse files
committed
Add count_tokens.ps1 script
1 parent 97d79b0 commit 78803c4

File tree

3 files changed

+117
-2
lines changed

3 files changed

+117
-2
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ Execute the following to get detailed help on further options of the server scri
110110
Get-Help -Detailed .\examples\server.ps1
111111
```
112112

113-
114113
### Chat via CLI
115114

116115
You can now chat with the model:
@@ -203,6 +202,34 @@ Execute the following to measure the perplexity of the GGML formatted model:
203202
--file "./vendor/wikitext-2-raw-v1/wikitext-2-raw/wiki.test.raw"
204203
```
205204

205+
### Count prompt tokens
206+
207+
You can easily count the tokens of a specific text input for a specific model by using the [.\examples\count_tokens.ps1](./examples/count_tokens.ps1) script:
208+
209+
```PowerShell
210+
.\examples\count_tokens.ps1 `
211+
-model ".\vendor\llama.cpp\models\openchat-3.5-0106.Q5_K_M.gguf" `
212+
-file ".\prompts\chat_with_llm.txt"
213+
```
214+
215+
To inspect the actual tokenization result you can use the `-debug` flag:
216+
217+
```PowerShell
218+
.\examples\count_tokens.ps1 `
219+
-model ".\vendor\llama.cpp\models\openchat-3.5-0106.Q5_K_M.gguf" `
220+
-prompt "Hello Word!" `
221+
-debug
222+
```
223+
224+
> [!NOTE]
225+
> The script is a simple wrapper for the [tokenize.cpp](https://github.com/ggerganov/llama.cpp/blob/master/examples/tokenize/tokenize.cpp) example of the llama.cpp project.
226+
227+
Execute the following to get detailed help on further options of the server script:
228+
229+
```PowerShell
230+
Get-Help -Detailed .\examples\count_tokens.ps1
231+
```
232+
206233
## Build
207234

208235
### Rebuild llama.cpp

examples/count_tokens.ps1

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#Requires -Version 5.0
2+
3+
<#
4+
.SYNOPSIS
5+
Counts tokens of a prompt file for a specific model.
6+
7+
.DESCRIPTION
8+
This script counts tokens of a prompt file for a specific model.
9+
10+
.PARAMETER model
11+
Specifies the path to the GGUF model file.
12+
13+
.PARAMETER file
14+
Specifies the path to the prompt text file.
15+
16+
.PARAMETER prompt
17+
Specifies the prompt.
18+
19+
.PARAMETER debug
20+
Logs the result of the tokenization.
21+
22+
.EXAMPLE
23+
.\count_tokens.ps1 -model "C:\models\openchat-3.5-0106.Q5_K_M.gguf" -file "C:\prompts\chat_with_llm.txt"
24+
25+
.EXAMPLE
26+
.\count_tokens.ps1 -model "C:\models\openchat-3.5-0106.Q5_K_M.gguf" -prompt "Hello world!"
27+
28+
.EXAMPLE
29+
.\count_tokens.ps1 -model "C:\models\openchat-3.5-0106.Q5_K_M.gguf" -prompt "Hello world!" -debug
30+
#>
31+
32+
Param (
33+
34+
[Parameter(
35+
HelpMessage="The path to the GGUF model file.",
36+
Mandatory=$true
37+
)]
38+
[String]
39+
$model,
40+
41+
[Parameter(
42+
HelpMessage="The path to the prompt text file."
43+
)]
44+
[String]
45+
$file,
46+
47+
[Parameter(
48+
HelpMessage="The prompt input."
49+
)]
50+
[String]
51+
$prompt
52+
)
53+
54+
if ((!$file -and !$prompt) -or ($file -and $prompt)) {
55+
throw "One prompt text to tokenize is required: Either specify the -file or the -prompt parameter."
56+
}
57+
58+
$debug = $PSCmdlet.MyInvocation.BoundParameters["Debug"].IsPresent -eq $true
59+
$verbose = $PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent -eq $true
60+
61+
# We are resolving the absolute path to the llama.cpp project directory.
62+
$llamaCppPath = Resolve-Path -Path "${PSScriptRoot}\..\vendor\llama.cpp"
63+
64+
$modelPath = Resolve-Path -Path "${model}"
65+
66+
if ($file) {
67+
$filePath = Resolve-Path -Path "${file}"
68+
}
69+
70+
if ($debug) {
71+
72+
# For debugging purposes we are logging the default output of the tokenization.
73+
Invoke-Expression "${llamaCppPath}\build\bin\Release\tokenize.exe ``
74+
$(if ($modelPath) {"--model '${modelPath}'"}) ``
75+
$(if ($filePath) {"--file '${filePath}'"} else {"--prompt '${prompt}'"})"
76+
}
77+
78+
# We are only interested in the numerical token IDs array format like [1, 2, 3].
79+
$tokensPythonArrayString = Invoke-Expression "${llamaCppPath}\build\bin\Release\tokenize.exe ``
80+
--log-disable ``
81+
--ids ``
82+
$(if ($modelPath) {"--model '${modelPath}'"}) ``
83+
$(if ($filePath) {"--file '${filePath}'"} else {"--prompt '${prompt}'"})"
84+
85+
# We are converting the Python array string into an PowerShell array.
86+
$tokens = "${tokensPythonArrayString}".Trim('[', ']') -split ',' | % { [int]$_ }
87+
88+
Write-Host $tokens.Length

vendor/llama.cpp

0 commit comments

Comments
 (0)