forked from mudler/LocalAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefixhash.go
More file actions
37 lines (31 loc) · 1.28 KB
/
Copy pathprefixhash.go
File metadata and controls
37 lines (31 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package distributedhdr
import "context"
type prefixChainKey struct{}
// WithPrefixChain attaches a prompt prefix-hash chain to ctx so the distributed
// router can make a prefix-cache-aware decision. Set at inference entry where
// the rendered prompt is known; read in SmartRouter.Route.
func WithPrefixChain(ctx context.Context, chain []uint64) context.Context {
return context.WithValue(ctx, prefixChainKey{}, chain)
}
// PrefixChain returns the chain attached by WithPrefixChain, or nil.
func PrefixChain(ctx context.Context) []uint64 {
if v, ok := ctx.Value(prefixChainKey{}).([]uint64); ok {
return v
}
return nil
}
// PrefixChainHook, when set at startup (distributed mode only), builds a prefix
// hash chain from a model id and rendered prompt. Left nil in single-process
// mode so there is zero overhead. See core/application/distributed.go.
var PrefixChainHook func(model, prompt string) []uint64
// MaybeWithPrefixChain attaches a prefix chain to ctx iff the hook is set and
// returns a non-empty chain. Otherwise returns ctx unchanged.
func MaybeWithPrefixChain(ctx context.Context, model, prompt string) context.Context {
if PrefixChainHook == nil {
return ctx
}
if chain := PrefixChainHook(model, prompt); len(chain) > 0 {
return WithPrefixChain(ctx, chain)
}
return ctx
}