|
package store |
|
|
|
import ( |
|
"context" |
|
"fmt" |
|
|
|
grpc "github.com/mudler/LocalAI/pkg/grpc" |
|
"github.com/mudler/LocalAI/pkg/grpc/proto" |
|
) |
|
|
|
|
|
|
|
|
|
|
|
func SetCols(ctx context.Context, c grpc.Backend, keys [][]float32, values [][]byte) error { |
|
protoKeys := make([]*proto.StoresKey, len(keys)) |
|
for i, k := range keys { |
|
protoKeys[i] = &proto.StoresKey{ |
|
Floats: k, |
|
} |
|
} |
|
protoValues := make([]*proto.StoresValue, len(values)) |
|
for i, v := range values { |
|
protoValues[i] = &proto.StoresValue{ |
|
Bytes: v, |
|
} |
|
} |
|
setOpts := &proto.StoresSetOptions{ |
|
Keys: protoKeys, |
|
Values: protoValues, |
|
} |
|
|
|
res, err := c.StoresSet(ctx, setOpts) |
|
if err != nil { |
|
return err |
|
} |
|
|
|
if res.Success { |
|
return nil |
|
} |
|
|
|
return fmt.Errorf("failed to set keys: %v", res.Message) |
|
} |
|
|
|
|
|
|
|
func SetSingle(ctx context.Context, c grpc.Backend, key []float32, value []byte) error { |
|
return SetCols(ctx, c, [][]float32{key}, [][]byte{value}) |
|
} |
|
|
|
|
|
|
|
func DeleteCols(ctx context.Context, c grpc.Backend, keys [][]float32) error { |
|
protoKeys := make([]*proto.StoresKey, len(keys)) |
|
for i, k := range keys { |
|
protoKeys[i] = &proto.StoresKey{ |
|
Floats: k, |
|
} |
|
} |
|
deleteOpts := &proto.StoresDeleteOptions{ |
|
Keys: protoKeys, |
|
} |
|
|
|
res, err := c.StoresDelete(ctx, deleteOpts) |
|
if err != nil { |
|
return err |
|
} |
|
|
|
if res.Success { |
|
return nil |
|
} |
|
|
|
return fmt.Errorf("failed to delete keys: %v", res.Message) |
|
} |
|
|
|
|
|
|
|
func DeleteSingle(ctx context.Context, c grpc.Backend, key []float32) error { |
|
return DeleteCols(ctx, c, [][]float32{key}) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
func GetCols(ctx context.Context, c grpc.Backend, keys [][]float32) ([][]float32, [][]byte, error) { |
|
protoKeys := make([]*proto.StoresKey, len(keys)) |
|
for i, k := range keys { |
|
protoKeys[i] = &proto.StoresKey{ |
|
Floats: k, |
|
} |
|
} |
|
getOpts := &proto.StoresGetOptions{ |
|
Keys: protoKeys, |
|
} |
|
|
|
res, err := c.StoresGet(ctx, getOpts) |
|
if err != nil { |
|
return nil, nil, err |
|
} |
|
|
|
ks := make([][]float32, len(res.Keys)) |
|
for i, k := range res.Keys { |
|
ks[i] = k.Floats |
|
} |
|
vs := make([][]byte, len(res.Values)) |
|
for i, v := range res.Values { |
|
vs[i] = v.Bytes |
|
} |
|
|
|
return ks, vs, nil |
|
} |
|
|
|
|
|
|
|
func GetSingle(ctx context.Context, c grpc.Backend, key []float32) ([]byte, error) { |
|
_, values, err := GetCols(ctx, c, [][]float32{key}) |
|
if err != nil { |
|
return nil, err |
|
} |
|
|
|
if len(values) > 0 { |
|
return values[0], nil |
|
} |
|
|
|
return nil, fmt.Errorf("failed to get key") |
|
} |
|
|
|
|
|
func Find(ctx context.Context, c grpc.Backend, key []float32, topk int) ([][]float32, [][]byte, []float32, error) { |
|
findOpts := &proto.StoresFindOptions{ |
|
Key: &proto.StoresKey{ |
|
Floats: key, |
|
}, |
|
TopK: int32(topk), |
|
} |
|
|
|
res, err := c.StoresFind(ctx, findOpts) |
|
if err != nil { |
|
return nil, nil, nil, err |
|
} |
|
|
|
ks := make([][]float32, len(res.Keys)) |
|
vs := make([][]byte, len(res.Values)) |
|
|
|
for i, k := range res.Keys { |
|
ks[i] = k.Floats |
|
} |
|
|
|
for i, v := range res.Values { |
|
vs[i] = v.Bytes |
|
} |
|
|
|
return ks, vs, res.Similarities, nil |
|
} |
|
|