-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement cli-args parsing for examples
- Loading branch information
Showing
5 changed files
with
150 additions
and
70 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
local _M = {} | ||
|
||
function _M.parse(cli_args) | ||
local args = {} | ||
for i, v in ipairs(cli_args) do | ||
if string.sub(v, 1, 2) == "--" then | ||
if cli_args[i + 1] and string.sub(cli_args[i + 1], 1, 2) ~= "--" then | ||
args[string.sub(v, 3)] = cli_args[i + 1] | ||
else | ||
args[string.sub(v, 3)] = true | ||
end | ||
end | ||
end | ||
return args | ||
end | ||
|
||
function _M.help(description, usage) | ||
require("cgemma").info() | ||
print() | ||
print(description) | ||
print() | ||
print(string.format("Usage: %s", usage)) | ||
print() | ||
print("Available options:") | ||
print(" --tokenizer: Path of tokenizer model file. (default: tokenizer.spm)") | ||
print(" --model: Model type (default: 2b-it)") | ||
print(" 2b-it = 2B parameters, instruction-tuned") | ||
print(" 7b-it = 7B parameters instruction-tuned") | ||
print(" 9b-it = 9B parameters instruction-tuned") | ||
print(" 27b-it = 27B parameters instruction-tuned") | ||
print(" gr2b-it = griffin 2B parameters, instruction-tuned") | ||
print(" --weights: Path of model weights file. (default: 2b-it-sfp.sbs)") | ||
print(" --weight_type: Weight type (default: sfp)") | ||
end | ||
|
||
return _M |
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,71 @@ | ||
local args = require("argparse").parse(arg) | ||
if args["help"] then | ||
require("argparse").help( | ||
"Normal mode chatbot demo.", | ||
"resty normal_mode.lua [options]" | ||
) | ||
print(" --kv_cache: Path of KV cache file.") | ||
return | ||
end | ||
|
||
-- Create a Gemma instance | ||
local gemma, err = require("cgemma").new({ | ||
tokenizer = args["tokenizer"] or "tokenizer.spm", | ||
model = args["model"] or "2b-it", | ||
weights = args["weights"] or "2b-it-sfp.sbs", | ||
weight_type = args["weight_type"] | ||
}) | ||
if not gemma then | ||
print("Opoos! ", err) | ||
return | ||
end | ||
|
||
-- Create a chat session | ||
local session, seed = gemma:session() | ||
if not session then | ||
print("Opoos! ", seed) | ||
return | ||
end | ||
|
||
print("Random seed of session: ", seed) | ||
while true do | ||
if args["kv_cache"] then | ||
-- Restore the previous session | ||
local ok, err = session:load(args["kv_cache"]) | ||
if ok then | ||
print("Previous conversation restored") | ||
else | ||
print("New conversation started") | ||
end | ||
else | ||
print("New conversation started") | ||
end | ||
|
||
-- Multi-turn chat | ||
while session:ready() do | ||
io.write("> ") | ||
local text = io.read() | ||
if not text then | ||
if args["kv_cache"] then | ||
print("End of file, dumping current session ...") | ||
-- Dump the current session | ||
local ok, err = session:dump(args["kv_cache"]) | ||
if not ok then | ||
print("Opoos! ", err) | ||
return | ||
end | ||
end | ||
print("Done") | ||
return | ||
end | ||
local reply, err = session(text) | ||
if not reply then | ||
print("Opoos! ", err) | ||
return | ||
end | ||
print("reply: ", reply) | ||
end | ||
|
||
print("Exceed the maximum number of tokens") | ||
session:reset() | ||
end |
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 was deleted.
Oops, something went wrong.