mirror of
https://github.com/alibaba/higress.git
synced 2026-05-30 15:47:28 +08:00
feat: Enhance ai-cache Plugin with Vector Similarity-Based LLM Cache Recall and Multi-DB Support (#1248)
This commit is contained in:
@@ -10,6 +10,11 @@ import (
|
||||
const (
|
||||
PROVIDER_TYPE_DASH_VECTOR = "dashvector"
|
||||
PROVIDER_TYPE_CHROMA = "chroma"
|
||||
PROVIDER_TYPE_ES = "elasticsearch"
|
||||
PROVIDER_TYPE_WEAVIATE = "weaviate"
|
||||
PROVIDER_TYPE_PINECONE = "pinecone"
|
||||
PROVIDER_TYPE_QDRANT = "qdrant"
|
||||
PROVIDER_TYPE_MILVUS = "milvus"
|
||||
)
|
||||
|
||||
type providerInitializer interface {
|
||||
@@ -20,7 +25,12 @@ type providerInitializer interface {
|
||||
var (
|
||||
providerInitializers = map[string]providerInitializer{
|
||||
PROVIDER_TYPE_DASH_VECTOR: &dashVectorProviderInitializer{},
|
||||
// PROVIDER_TYPE_CHROMA: &chromaProviderInitializer{},
|
||||
PROVIDER_TYPE_CHROMA: &chromaProviderInitializer{},
|
||||
PROVIDER_TYPE_ES: &esProviderInitializer{},
|
||||
PROVIDER_TYPE_WEAVIATE: &weaviateProviderInitializer{},
|
||||
PROVIDER_TYPE_PINECONE: &pineconeProviderInitializer{},
|
||||
PROVIDER_TYPE_QDRANT: &qdrantProviderInitializer{},
|
||||
PROVIDER_TYPE_MILVUS: &milvusProviderInitializer{},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -71,10 +81,6 @@ type StringQuerier interface {
|
||||
callback func(results []QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error)) error
|
||||
}
|
||||
|
||||
type SimilarityThresholdProvider interface {
|
||||
GetSimilarityThreshold() float64
|
||||
}
|
||||
|
||||
type ProviderConfig struct {
|
||||
// @Title zh-CN 向量存储服务提供者类型
|
||||
// @Description zh-CN 向量存储服务提供者类型,例如 dashvector、chroma
|
||||
@@ -97,8 +103,8 @@ type ProviderConfig struct {
|
||||
// @Title zh-CN 请求超时
|
||||
// @Description zh-CN 请求向量存储服务的超时时间,单位为毫秒。默认值是10000,即10秒
|
||||
timeout uint32
|
||||
// @Title zh-CN DashVector 向量存储服务 Collection ID
|
||||
// @Description zh-CN DashVector 向量存储服务 Collection ID
|
||||
// @Title zh-CN 向量存储服务 Collection ID
|
||||
// @Description zh-CN 向量存储服务的 Collection ID
|
||||
collectionID string
|
||||
// @Title zh-CN 相似度度量阈值
|
||||
// @Description zh-CN 默认相似度度量阈值,默认为 1000。
|
||||
@@ -109,6 +115,14 @@ type ProviderConfig struct {
|
||||
// 所以需要允许自定义比较方式,对于 Cosine 和 DotProduct 选择 gt,对于 Euclidean 则选择 lt。
|
||||
// 默认为 lt,所有条件包括 lt (less than,小于)、lte (less than or equal to,小等于)、gt (greater than,大于)、gte (greater than or equal to,大等于)
|
||||
ThresholdRelation string
|
||||
|
||||
// ES 配置
|
||||
// @Title zh-CN ES 用户名
|
||||
// @Description zh-CN ES 用户名
|
||||
esUsername string
|
||||
// @Title zh-CN ES 密码
|
||||
// @Description zh-CN ES 密码
|
||||
esPassword string
|
||||
}
|
||||
|
||||
func (c *ProviderConfig) GetProviderType() string {
|
||||
@@ -117,7 +131,6 @@ func (c *ProviderConfig) GetProviderType() string {
|
||||
|
||||
func (c *ProviderConfig) FromJson(json gjson.Result) {
|
||||
c.typ = json.Get("type").String()
|
||||
// DashVector
|
||||
c.serviceName = json.Get("serviceName").String()
|
||||
c.serviceHost = json.Get("serviceHost").String()
|
||||
c.servicePort = int64(json.Get("servicePort").Int())
|
||||
@@ -142,6 +155,10 @@ func (c *ProviderConfig) FromJson(json gjson.Result) {
|
||||
if c.ThresholdRelation == "" {
|
||||
c.ThresholdRelation = "lt"
|
||||
}
|
||||
|
||||
// ES
|
||||
c.esUsername = json.Get("esUsername").String()
|
||||
c.esPassword = json.Get("esPassword").String()
|
||||
}
|
||||
|
||||
func (c *ProviderConfig) Validate() error {
|
||||
@@ -152,6 +169,9 @@ func (c *ProviderConfig) Validate() error {
|
||||
if !has {
|
||||
return errors.New("unknown vector database service provider type: " + c.typ)
|
||||
}
|
||||
if !isRelationValid(c.ThresholdRelation) {
|
||||
return errors.New("invalid thresholdRelation: " + c.ThresholdRelation)
|
||||
}
|
||||
if err := initializer.ValidateConfig(*c); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -165,3 +185,12 @@ func CreateProvider(pc ProviderConfig) (Provider, error) {
|
||||
}
|
||||
return initializer.CreateProvider(pc)
|
||||
}
|
||||
|
||||
func isRelationValid(relation string) bool {
|
||||
for _, r := range []string{"lt", "lte", "gt", "gte"} {
|
||||
if r == relation {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user