-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
244 additions
and
20 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
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,187 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"log" | ||
"log/slog" | ||
"net/http" | ||
"os" | ||
"path" | ||
"runtime" | ||
|
||
"github.com/ling0322/libllm/go/llm" | ||
"github.com/schollz/progressbar/v3" | ||
) | ||
|
||
var ErrInvalidModelName = errors.New("invalid model name") | ||
var ModelCacheDir = getModelCacheDir() | ||
|
||
var modelUrls = map[string]string{ | ||
"index-chat": "https://huggingface.co/ling0322/bilibili-index-1.9b-libllm/resolve/main/bilibili-index-1.9b-chat-q4.llmpkg", | ||
"index-character": "https://huggingface.co/ling0322/bilibili-index-1.9b-libllm/resolve/main/bilibili-index-1.9b-character-q4.llmpkg", | ||
} | ||
|
||
var modelFilenames = map[string]string{ | ||
"index-chat": "bilibili-index-1.9b-chat-q4.llmpkg", | ||
"index-character": "bilibili-index-1.9b-character-q4.llmpkg", | ||
} | ||
|
||
func getModelCacheDir() string { | ||
var cacheDir string | ||
if runtime.GOOS == "linux" || runtime.GOOS == "darwin" { | ||
userDir, err := os.UserHomeDir() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
cacheDir = path.Join(userDir, ".libllm", "models") | ||
} else if runtime.GOOS == "windows" { | ||
binFile, err := os.Executable() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
binDir := path.Dir(binFile) | ||
cacheDir = path.Join(binDir, "models") | ||
} | ||
|
||
return cacheDir | ||
} | ||
|
||
func downloadModel(name string) (modelPath string, err error) { | ||
url, ok := modelUrls[name] | ||
if !ok { | ||
log.Fatal("invalid model name") | ||
} | ||
|
||
filename, ok := modelFilenames[name] | ||
if !ok { | ||
log.Fatal("invalid model name") | ||
} | ||
modelPath = path.Join(ModelCacheDir, filename) | ||
slog.Info("download model", "url", url) | ||
|
||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return | ||
} | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
modelDir := path.Dir(modelPath) | ||
err = os.MkdirAll(modelDir, os.ModePerm) | ||
if err != nil { | ||
return "", fmt.Errorf("unable to create model cache directory: %w", err) | ||
} | ||
|
||
f, err := os.OpenFile(modelPath+".download", os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
return | ||
} | ||
defer f.Close() | ||
|
||
bar := progressbar.DefaultBytes( | ||
resp.ContentLength, | ||
"Downloading", | ||
) | ||
_, err = io.Copy(io.MultiWriter(f, bar), resp.Body) | ||
if err != nil { | ||
return | ||
} | ||
|
||
err = os.Rename(modelPath+".download", modelPath) | ||
if err != nil { | ||
return | ||
} | ||
|
||
slog.Info("Save model", "path", modelPath) | ||
return modelPath, nil | ||
} | ||
|
||
// check if model exists in the cache directory. If exists, retuen the model path, otherwise, | ||
// return the error. | ||
func checkModelInCache(name string) (modelPath string, err error) { | ||
filename, ok := modelFilenames[name] | ||
if !ok { | ||
return "", ErrInvalidModelName | ||
} | ||
modelPath = path.Join(ModelCacheDir, filename) | ||
|
||
_, err = os.Stat(modelPath) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return | ||
} | ||
|
||
func getOrDownloadModel(name string) (modelPath string, err error) { | ||
modelPath, err = checkModelInCache(name) | ||
if err == nil { | ||
return | ||
} | ||
|
||
return downloadModel(name) | ||
} | ||
|
||
func createModelAutoDownload(nameOrPath string, device llm.Device) (llm.Model, error) { | ||
var modelPath string | ||
var err error | ||
|
||
_, ok := modelFilenames[nameOrPath] | ||
if ok { | ||
modelPath, err = getOrDownloadModel(nameOrPath) | ||
} else { | ||
modelPath = nameOrPath | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
_, err = os.Stat(modelPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("model not exist: %s", modelPath) | ||
} | ||
|
||
return llm.NewModel(modelPath, device) | ||
} | ||
|
||
func printDownloadUsage(fs *flag.FlagSet) { | ||
fmt.Fprintln(os.Stderr, "Usage: llm download [OPTIONS]") | ||
fmt.Fprintln(os.Stderr, "") | ||
fmt.Fprintln(os.Stderr, "Options:") | ||
fs.PrintDefaults() | ||
fmt.Fprintln(os.Stderr, "") | ||
} | ||
|
||
func downloadMain(args []string) { | ||
fs := flag.NewFlagSet("", flag.ExitOnError) | ||
fs.Usage = func() { | ||
printDownloadUsage(fs) | ||
} | ||
|
||
addModelFlag(fs) | ||
_ = fs.Parse(args) | ||
|
||
if fs.NArg() != 0 { | ||
fs.Usage() | ||
os.Exit(1) | ||
} | ||
|
||
modelName := getModelArg(fs) | ||
if modelPath, err := checkModelInCache(modelName); err == nil { | ||
fmt.Printf("model \"%s\" already downloaded. Path is \"%s\"\n", modelName, modelPath) | ||
} | ||
|
||
_, err := downloadModel(modelName) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
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,18 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw= | ||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | ||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ= | ||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= | ||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= | ||
github.com/schollz/progressbar/v3 v3.14.5 h1:97RrSxbBASxQuZN9yemnyGrFZ/swnG6IrEe2R0BseX8= | ||
github.com/schollz/progressbar/v3 v3.14.5/go.mod h1:Nrzpuw3Nl0srLY0VlTvC4V6RL50pcEymjy6qyJAaLa0= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= | ||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= | ||
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk= | ||
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4= |
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