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.
99 lines
2.5 KiB
Go
99 lines
2.5 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 knowledgeindexing
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/cloudwego/eino-ext/components/indexer/redis"
|
|
"github.com/cloudwego/eino/components/indexer"
|
|
"github.com/cloudwego/eino/schema"
|
|
"github.com/google/uuid"
|
|
redisCli "github.com/redis/go-redis/v9"
|
|
|
|
redispkg "github.com/cloudwego/eino-examples/quickstart/eino_assistant/pkg/redis"
|
|
)
|
|
|
|
func init() {
|
|
err := redispkg.Init()
|
|
if err != nil {
|
|
log.Fatalf("failed to init redis index: %v", err)
|
|
}
|
|
}
|
|
|
|
func defaultRedisIndexerConfig(ctx context.Context) (*redis.IndexerConfig, error) {
|
|
redisAddr := os.Getenv("REDIS_ADDR")
|
|
redisClient := redisCli.NewClient(&redisCli.Options{
|
|
Addr: redisAddr,
|
|
Protocol: 2,
|
|
})
|
|
|
|
config := &redis.IndexerConfig{
|
|
Client: redisClient,
|
|
KeyPrefix: redispkg.RedisPrefix,
|
|
BatchSize: 1,
|
|
DocumentToHashes: func(ctx context.Context, doc *schema.Document) (*redis.Hashes, error) {
|
|
if doc.ID == "" {
|
|
doc.ID = uuid.New().String()
|
|
}
|
|
key := doc.ID
|
|
|
|
metadataBytes, err := json.Marshal(doc.MetaData)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to marshal metadata: %w", err)
|
|
}
|
|
|
|
return &redis.Hashes{
|
|
Key: key,
|
|
Field2Value: map[string]redis.FieldValue{
|
|
redispkg.ContentField: {Value: doc.Content, EmbedKey: redispkg.VectorField},
|
|
redispkg.MetadataField: {Value: metadataBytes},
|
|
},
|
|
}, nil
|
|
},
|
|
}
|
|
|
|
embeddingCfg11, err := defaultArkEmbeddingConfig(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
embeddingIns11, err := NewArkEmbedding(ctx, embeddingCfg11)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
config.Embedding = embeddingIns11
|
|
return config, nil
|
|
}
|
|
|
|
func NewRedisIndexer(ctx context.Context, config *redis.IndexerConfig) (idr indexer.Indexer, err error) {
|
|
if config == nil {
|
|
config, err = defaultRedisIndexerConfig(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
idr, err = redis.NewIndexer(ctx, config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return idr, nil
|
|
}
|