You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.4 KiB
Go
82 lines
2.4 KiB
Go
/*
|
|
* Copyright 2025 CloudWeGo Authors
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package einoagent
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
|
|
redispkg "github.com/cloudwego/eino-examples/quickstart/eino_assistant/pkg/redis"
|
|
"github.com/cloudwego/eino/schema"
|
|
redisCli "github.com/redis/go-redis/v9"
|
|
|
|
"github.com/cloudwego/eino-ext/components/retriever/redis"
|
|
"github.com/cloudwego/eino/components/retriever"
|
|
)
|
|
|
|
// newRetriever component initialization function of node 'RedisRetriever' in graph 'EinoAgent'
|
|
func newRetriever(ctx context.Context) (rtr retriever.Retriever, err error) {
|
|
// TODO Modify component configuration here.
|
|
redisAddr := os.Getenv("REDIS_ADDR")
|
|
redisClient := redisCli.NewClient(&redisCli.Options{
|
|
Addr: redisAddr,
|
|
Protocol: 2,
|
|
})
|
|
config := &redis.RetrieverConfig{
|
|
Client: redisClient,
|
|
Index: fmt.Sprintf("%s%s", redispkg.RedisPrefix, redispkg.IndexName),
|
|
Dialect: 2,
|
|
ReturnFields: []string{redispkg.ContentField, redispkg.MetadataField, redispkg.DistanceField},
|
|
TopK: 8,
|
|
VectorField: redispkg.VectorField,
|
|
DocumentConverter: func(ctx context.Context, doc redisCli.Document) (*schema.Document, error) {
|
|
resp := &schema.Document{
|
|
ID: doc.ID,
|
|
Content: "",
|
|
MetaData: map[string]any{},
|
|
}
|
|
for field, val := range doc.Fields {
|
|
if field == redispkg.ContentField {
|
|
resp.Content = val
|
|
} else if field == redispkg.MetadataField {
|
|
resp.MetaData[field] = val
|
|
} else if field == redispkg.DistanceField {
|
|
distance, err := strconv.ParseFloat(val, 64)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
resp.WithScore(1 - distance)
|
|
}
|
|
}
|
|
|
|
return resp, nil
|
|
},
|
|
}
|
|
embeddingIns11, err := newEmbedding(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
config.Embedding = embeddingIns11
|
|
rtr, err = redis.NewRetriever(ctx, config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return rtr, nil
|
|
}
|