Documentation
¶
Index ¶
- Constants
- Variables
- func ConfModAiCacheCheck(cfg *ConfModAiCache, confRoot string) error
- type ConfModAiCache
- type EmbeddingConnectConf
- type ModuleAiCache
- type ProductRuleConf
- type ProductRuleConfData
- type ProductRuleConfDataFile
- type ProductRuleConfFile
- type ProductRuleConfList
- type ProductRuleConfListFile
- type PrometheusStates
- type SemanticConf
- type SemanticConfFile
- type SemanticConnectConf
- type VectorConnectConf
Constants ¶
const ( CacheKeyStrategyLastQuestion = "lastQuestion" CacheKeyStrategyAllQuestions = "allQuestions" CacheKeyStrategyDisabled = "disabled" )
cache key strategies
const ( CacheStatusHit = "hit" CacheStatusHitSemantic = "hit_semantic" CacheStatusMiss = "miss" CacheStatusSkip = "skip" )
cache status values recorded in AiBasicInfo and the access log
const ( SemanticRelationLt = "lt" SemanticRelationLte = "lte" SemanticRelationGt = "gt" SemanticRelationGte = "gte" )
semantic threshold relations
const ( DefaultSemanticTopK = 1 DefaultSemanticThreshold = 0.15 DefaultSemanticThresholdRelation = SemanticRelationLt )
semantic global conf defaults (top-level Semantic block of the rule file)
const ( DefaultLastQuestionPath = "messages.@reverse.0.content" DefaultCacheValuePath = "choices.0.message.content" DefaultCacheStreamPath = "choices.0.delta.content" DefaultResponseTemplate = `` /* 164-byte string literal not displayed */ DefaultStreamResponseTmpl = "" /* 129-byte string literal not displayed */ )
default GJSON paths for key/value extraction
const ( DefaultCacheKeyPrefix = "ai_cache" DefaultCacheTTL = 3600 // seconds DefaultMaxBodyBytes = 1048576 // 1MB DefaultMaxValueBytes = 1048576 // 1MB )
const ( DefaultEmbeddingTimeoutMs = 500 DefaultVectorTimeoutMs = 300 DefaultVectorCollection = "ai_cache_semantic" DefaultMaxQuestionBytes = 4096 // vector store types supported by the semantic cache VectorTypeChroma = "chroma" )
semantic cache defaults
const ( ModAiCache = "mod_ai_cache" ModAiCacheDiff = "mod_ai_cache_diff" CtxAiCache = "mod_ai_cache.ctx" )
const ( // header to skip cache for a single request HeaderSkipAiCache = "x-bfe-skip-ai-cache" SkipCacheOn = "on" ContentTypeJson = "application/json" )
const (
DiffCounterInterval = 20 // interval for diff counter (in seconds)
)
Variables ¶
var CounterKeys = []string{
"REQ_TOTAL",
"CACHE_HIT",
"SEMANTIC_HIT",
"CACHE_MISS",
"CACHE_SKIP",
"REDIS_ERR",
"EMBEDDING_ERR",
"VECTOR_ERR",
"LATENCY_MS",
"EMBEDDING_LATENCY_MS",
"VECTOR_LATENCY_MS",
"VALUE_TOO_LARGE",
"SEMANTIC_SKIPPED",
}
key for counter of mod_ai_cache
Functions ¶
func ConfModAiCacheCheck ¶
func ConfModAiCacheCheck(cfg *ConfModAiCache, confRoot string) error
Types ¶
type ConfModAiCache ¶
type ConfModAiCache struct {
Basic struct {
ProductRulePath string // path for product rule
CacheKeyPrefix string // prefix of cache key
DefaultCacheTTL int // default cache TTL (seconds)
}
// redis conf
Redis struct {
Bns string // bns name for redis proxy
ConnectTimeout int // connect timeout (ms)
ReadTimeout int // read timeout (ms)
WriteTimeout int // write timeout(ms)
// max idle connections in pool
MaxIdle int
// redis password, ignore if not set
Password string
// max active connections in pool,
// when set 0, there is no connection num limit
MaxActive int
}
Log struct {
OpenDebug bool // whether open debug
}
// embedding service conf (optional): semantic cache is enabled only when
// both [embedding] and [vector] are configured
Embedding struct {
ServiceHost string // embedding service host
ServicePort int // embedding service port
UseHttps bool // use https scheme
ApiKey string // bearer credential, never logged
Model string // embedding model name
TimeoutMs int // per-call timeout (ms)
}
// vector store conf (optional), see [embedding]
Vector struct {
Type string // vector store type, only "chroma" for now
ServiceHost string // vector store host
ServicePort int // vector store port
ApiKey string // bearer credential, never logged
Collection string // collection name
TimeoutMs int // per-call timeout (ms)
MaxQuestionBytes int64 // question length limit for semantic lookup
}
}
func ConfLoad ¶
func ConfLoad(filePath string, confRoot string) (*ConfModAiCache, error)
load config from config file
func (*ConfModAiCache) Check ¶
func (cfg *ConfModAiCache) Check(confRoot string) error
func (*ConfModAiCache) SemanticConnect ¶
func (cfg *ConfModAiCache) SemanticConnect() (*SemanticConnectConf, error)
SemanticConnect validates the optional embedding/vector sections and returns the normalized connection conf. It returns (nil, nil) when neither section is configured (pure exact-match cache mode). Any validation error is returned to the caller, which is expected to disable the semantic cache (fail-open) and keep the exact-match cache running.
type EmbeddingConnectConf ¶
type EmbeddingConnectConf struct {
ServiceHost string // embedding service host
ServicePort int // embedding service port
UseHttps bool // use https scheme
ApiKey string // bearer credential, never logged
Model string // embedding model name
TimeoutMs int // per-call timeout (ms)
}
EmbeddingConnectConf is the normalized embedding section of mod_ai_cache.conf.
type ModuleAiCache ¶
type ModuleAiCache struct {
// contains filtered or unexported fields
}
func NewModuleAiCache ¶
func NewModuleAiCache() *ModuleAiCache
func (*ModuleAiCache) Init ¶
func (m *ModuleAiCache) Init(cbs *bfe_module.BfeCallbacks, whs *web_monitor.WebHandlers, cr string) error
module Init
func (*ModuleAiCache) Name ¶
func (m *ModuleAiCache) Name() string
type ProductRuleConf ¶
type ProductRuleConf struct {
Cond string
CondBuild condition.Condition
CacheKeyStrategy string
Disabled bool
CacheTTL int
CacheKeyFrom string
CacheValueFrom string
CacheStreamFrom string
ResponseTemplate string
StreamResponseTemplate string
MaxBodyBytes int64
MaxValueBytes int64
EnableSemanticCache bool
}
type ProductRuleConfData ¶
type ProductRuleConfData struct {
Version *string
Semantic *SemanticConf // nil when the rule file has no Semantic block
Config *map[string]*ProductRuleConfList
}
func ProductRuleConfLoad ¶
func ProductRuleConfLoad(fileName string, defaultCacheTTL int) (*ProductRuleConfData, error)
type ProductRuleConfDataFile ¶
type ProductRuleConfDataFile struct {
Version *string `json:"Version"`
Semantic *SemanticConfFile `json:"Semantic"` // optional, global
Config *map[string]*ProductRuleConfListFile `json:"Config"`
}
func (*ProductRuleConfDataFile) Convert ¶
func (f *ProductRuleConfDataFile) Convert() *ProductRuleConfData
type ProductRuleConfFile ¶
type ProductRuleConfFile struct {
Cond *string `json:"cond"`
CacheKeyStrategy *string `json:"cacheKeyStrategy"` // lastQuestion / allQuestions / disabled
CacheTTL *int `json:"cacheTTL"` // cache TTL (seconds)
// GJSON paths
CacheKeyFrom *string `json:"cacheKeyFrom"` // override key extraction path
CacheValueFrom *string `json:"cacheValueFrom"` // non-streaming answer path
CacheStreamFrom *string `json:"cacheStreamValueFrom"` // streaming answer path
CacheToolCallsFrom *string `json:"cacheToolCallsFrom"` // reserved for tool calls
// hit response templates, %s is the placeholder of cached content
ResponseTemplate *string `json:"responseTemplate"`
StreamResponseTemplate *string `json:"streamResponseTemplate"`
// size limits
MaxBodyBytes *int64 `json:"maxBodyBytes"`
MaxValueBytes *int64 `json:"maxValueBytes"`
// whether to enable the semantic cache for this rule; ignored when
// cacheKeyStrategy is "disabled"
EnableSemanticCache *bool `json:"enableSemanticCache"`
}
func (*ProductRuleConfFile) Check ¶
func (obj *ProductRuleConfFile) Check() error
func (*ProductRuleConfFile) Convert ¶
func (f *ProductRuleConfFile) Convert() *ProductRuleConf
type ProductRuleConfList ¶
type ProductRuleConfList []*ProductRuleConf
type ProductRuleConfListFile ¶
type ProductRuleConfListFile []*ProductRuleConfFile
func (*ProductRuleConfListFile) Check ¶
func (obj *ProductRuleConfListFile) Check() error
func (ProductRuleConfListFile) Convert ¶
func (f ProductRuleConfListFile) Convert() ProductRuleConfList
type PrometheusStates ¶
type PrometheusStates struct {
// contains filtered or unexported fields
}
PrometheusStates exposes the mod_ai_cache counters in prometheus text format. Gauges are re-set from the module counters on each scrape.
type SemanticConf ¶
type SemanticConf struct {
TopK int // number of nearest neighbors
Threshold float64 // score threshold
ThresholdRelation string // lt / lte / gt / gte
}
SemanticConf is the runtime representation of the top-level Semantic block of the rule file (module-level global semantic config).
type SemanticConfFile ¶
type SemanticConfFile struct {
TopK *int `json:"topK"` // number of nearest neighbors
Threshold *float64 `json:"threshold"` // score threshold
ThresholdRelation *string `json:"thresholdRelation"` // lt / lte / gt / gte
}
SemanticConfFile is the file representation of the top-level Semantic block: module-level global config for the semantic cache, hot reloaded together with the rule table.
func (*SemanticConfFile) Check ¶
func (f *SemanticConfFile) Check() error
Check validates the global semantic conf (top-level Semantic block).
func (*SemanticConfFile) Convert ¶
func (f *SemanticConfFile) Convert() *SemanticConf
Convert converts the file representation to the runtime conf. Callers must have applied setDefaults first so that no pointer field is nil.
type SemanticConnectConf ¶
type SemanticConnectConf struct {
Embedding *EmbeddingConnectConf
Vector *VectorConnectConf
}
SemanticConnectConf carries the connection info of the semantic cache (embedding service + vector store). It is nil when the semantic cache is not configured.
type VectorConnectConf ¶
type VectorConnectConf struct {
Type string // vector store type, only "chroma" for now
ServiceHost string // vector store host
ServicePort int // vector store port
ApiKey string // bearer credential, never logged
Collection string // collection name
TimeoutMs int // per-call timeout (ms)
MaxQuestionBytes int64 // question length limit for semantic lookup
}
VectorConnectConf is the normalized vector section of mod_ai_cache.conf.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
provider
|
|
|
embedding
Package embedding abstracts the text embedding service used by the semantic cache of mod_ai_cache.
|
Package embedding abstracts the text embedding service used by the semantic cache of mod_ai_cache. |
|
vector
Package vector abstracts the vector store used by the semantic cache of mod_ai_cache.
|
Package vector abstracts the vector store used by the semantic cache of mod_ai_cache. |