a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
97 lines
2.8 KiB
Go
97 lines
2.8 KiB
Go
package embedding
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
// sharedStatic memoises a single process-wide StaticProvider. The baked
|
|
// GloVe vectors are ~3.7MB compressed and decompress into a ~20k-entry
|
|
// map that is safe for concurrent reads, so one instance serves every
|
|
// rerank call. Constructed lazily on first use.
|
|
var (
|
|
sharedStaticOnce sync.Once
|
|
sharedStaticInst *StaticProvider
|
|
)
|
|
|
|
// SharedStatic returns the process-wide static word-vector provider,
|
|
// constructing it on first call. Returns nil only when the baked
|
|
// vectors fail to load (a corrupt build); callers treat nil as "no
|
|
// semantic-cosine channel". Safe for concurrent use.
|
|
func SharedStatic() *StaticProvider {
|
|
sharedStaticOnce.Do(func() {
|
|
p, err := NewStaticProvider()
|
|
if err != nil {
|
|
return
|
|
}
|
|
sharedStaticInst = p
|
|
})
|
|
return sharedStaticInst
|
|
}
|
|
|
|
// EmbedTextFunc adapts a provider into the plain func the rerank
|
|
// Context wants: text -> normalised vector, errors and nil providers
|
|
// collapsing to a nil result the signal reads as "cannot embed".
|
|
func EmbedTextFunc(p Provider) func(string) []float32 {
|
|
if p == nil {
|
|
return nil
|
|
}
|
|
return func(text string) []float32 {
|
|
vec, err := p.Embed(context.Background(), text)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return vec
|
|
}
|
|
}
|
|
|
|
// sharedCode memoises the process-wide code-embedding provider used by
|
|
// the rerank's semantic-cosine channel: the bundled static code model
|
|
// (potion) when its files resolve — explicit dir, exec-adjacent
|
|
// sidecar, per-user models dir, or a checksum-verified first-use
|
|
// download — and the baked GloVe word vectors otherwise, so an offline
|
|
// install without the sidecar still gets a semantic channel.
|
|
var (
|
|
sharedCodeOnce sync.Once
|
|
sharedCodeInst Provider
|
|
)
|
|
|
|
// SharedCodeEmbedder returns the process-wide code embedder for the
|
|
// rerank's semantic-cosine channel. Never returns an error — the
|
|
// fallback chain ends at the baked static provider; nil only when even
|
|
// that failed to load. Safe for concurrent use. GORTEX_POTION=0 pins
|
|
// the GloVe fallback (diagnostic escape hatch).
|
|
func SharedCodeEmbedder() Provider {
|
|
sharedCodeOnce.Do(func() {
|
|
if v := strings.TrimSpace(os.Getenv("GORTEX_POTION")); v == "0" || strings.EqualFold(v, "false") || strings.EqualFold(v, "off") {
|
|
sharedCodeInst = staticOrNil()
|
|
return
|
|
}
|
|
dir := resolvePotionDir()
|
|
if dir == "" {
|
|
if d, err := downloadPotion(); err == nil {
|
|
dir = d
|
|
}
|
|
}
|
|
if dir != "" {
|
|
if p, err := NewPotionProviderFromDir(dir); err == nil {
|
|
sharedCodeInst = p
|
|
return
|
|
}
|
|
}
|
|
sharedCodeInst = staticOrNil()
|
|
})
|
|
return sharedCodeInst
|
|
}
|
|
|
|
// staticOrNil adapts SharedStatic's concrete return into the Provider
|
|
// interface without wrapping a typed nil.
|
|
func staticOrNil() Provider {
|
|
if p := SharedStatic(); p != nil {
|
|
return p
|
|
}
|
|
return nil
|
|
}
|