Files
2dph/bin/kbsearch/modeldir.go
eSlider f220bcd95a kbsearch: Go implementation with daemon model serving
- New nested module bin/kbsearch with Go implementation of bin/kb/search
- Embedding model (potion-multilingual-128M) served by localhost daemon
  so repeated CLI calls reuse the loaded model
- Bash launcher bin/kb/search builds binary on first run, caches to var/bin/
- Hybrid FTS + vector search (RRF k=60) matching Python kblib behavior
- YAML output via port of yamlout.py (ordered keys, same format)
- JSON output with proper field order
- All flags: --root, --repo, -n, --json, --list-model
- Root go.mod reverted to 1.25.0 (kbsearch is isolated nested module)
- CI passes: go test ./... and go vet ./... unaffected by kbsearch
2026-08-11 23:57:39 +01:00

60 lines
1.7 KiB
Go

// modelDir returns the resolved potion-multilingual-128m model directory.
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
)
func modelDir() (string, error) {
// 1. Explicit env
if v := os.Getenv("KBSEARCH_MODEL"); v != "" {
return v, nil
}
// 2. Next to the binary (dev or installed)
self, err := os.Executable()
if err == nil {
if dir, err := filepath.EvalSymlinks(filepath.Dir(self)); err == nil {
cand := filepath.Join(dir, "potion-multilingual-128m")
if st, err := os.Stat(cand); err == nil && st.IsDir() {
return cand, nil
}
}
}
// 3. Repo root lib/ (where other scripts expect it)
if v := os.Getenv("KB_ROOT"); v != "" {
cand := filepath.Join(v, "lib", "potion-multilingual-128m")
if st, err := os.Stat(cand); err == nil && st.IsDir() {
return cand, nil
}
}
// 4. HF cache (new layout: models--*/snapshots/*)
if v := os.Getenv("HF_HOME"); v != "" {
base := filepath.Join(v, "hub")
if entries, err := os.ReadDir(base); err == nil {
for _, e := range entries {
if strings.HasPrefix(e.Name(), "models--") {
snapDir := filepath.Join(base, e.Name(), "snapshots")
if snaps, err := os.ReadDir(snapDir); err == nil {
for _, s := range snaps {
cand := filepath.Join(snapDir, s.Name())
if st, _ := os.Stat(cand); st != nil && st.IsDir() {
return cand, nil
}
}
}
}
}
}
}
// 5. Legacy HF cache (symlinked model dir)
if v := os.Getenv("HF_HOME"); v != "" {
cand := filepath.Join(v, "potion-multilingual-128m")
if st, err := os.Stat(cand); err == nil && st.IsDir() {
return cand, nil
}
}
return "", fmt.Errorf("model not found (set KBSEARCH_MODEL or KB_ROOT, or download to HF cache)")
}