Skip to content

Commit 9a8ad86

Browse files
committed
fix vector store dump bug
1 parent 08ab49e commit 9a8ad86

File tree

5 files changed

+14
-25
lines changed

5 files changed

+14
-25
lines changed

Chinese.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,13 +463,14 @@ LLM.CREATE-VECTOR-STORE key [--NX] [--XX] [--TYPE vector-store-type] [--LLM llm-
463463
目前我们只支持HNSW类型,这也是默认类型。当然,你也可以显式的通过`--TYPE hnsw`指定。它的参数如下:
464464

465465
```JSON
466-
{"max_elements": 100000, "m": 16, "ef_construction": 200, "dim": 0}
466+
{"max_elements": 100000, "m": 16, "ef_construction": 200}
467467
```
468468

469469
所有的参数都是键值对。必选参数使用*required*标识,可选参数给出了默认值。
470470

471471
- *max_elements*: vector store能存储的最大数量。
472-
- *dim*: vector store存储的embedding的维度。如果使用默认值,0,那么使用第一条插入到vector store的embedding的维度作为vector store的维度。
472+
473+
**NOTE**: vector store使用第一条插入到vector store的embedding的维度作为vector store的维度。
473474

474475
#### 返回值
475476

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,14 @@ LLM.CREATE-VECTOR-STORE key [--NX] [--XX] [--TYPE vector-store-type] [--LLM llm-
465465
Currently, we only support vector store of HNSW type, and this is also the default one. Of course, you can specify `--TYPE hnsw` explicitly. The parameters are as follows:
466466

467467
```JSON
468-
{"max_elements": 100000, "m": 16, "ef_construction": 200, "dim": 0}
468+
{"max_elements": 100000, "m": 16, "ef_construction": 200}
469469
```
470470

471471
All parameters are key-value pairs. The required ones are set as *required*. The optional ones are set with default values. If parameter is not specified, the default value is used.
472472

473473
- *max_elements*: Max number of items that can be stored in the vector store.
474-
- *dim*: Set the dimension of the vector saved in vector store. If it's 0, i.e. the default value, the dimension of the first inserted vector is used as the dimension of the vector store.
474+
475+
**NOTE**: The dimension of the first inserted vector is used as the dimension of the vector store.
475476

476477
#### Return
477478

src/sw/redis-llm/redis_llm.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void rewrite_vector_store(RedisModuleIO *aof, RedisModuleString *key, VectorStor
6464

6565
void rdb_save_vector_store(RedisModuleIO *rdb, VectorStore &store);
6666

67-
void rdb_load_vector_store(RedisModuleIO *rdb, VectorStore &store);
67+
void rdb_load_vector_store(RedisModuleIO *rdb, VectorStore &store, std::size_t dim);
6868

6969
void* rdb_load_vector_store(RedisModuleIO *rdb);
7070

@@ -502,6 +502,7 @@ void rdb_save_vector_store(RedisModuleIO *rdb, VectorStore &store) {
502502
rdb_save_config(rdb, store.conf());
503503
rdb_save_string(rdb, store.llm().to_string());
504504
rdb_save_number(rdb, store.id_idx());
505+
rdb_save_number(rdb, store.dim());
505506

506507
const auto &data_store = store.data_store();
507508
rdb_save_number(rdb, data_store.size());
@@ -524,8 +525,7 @@ void rdb_save_app(RedisModuleIO *rdb, void *value) {
524525
rdb_save_config(rdb, app->conf());
525526
}
526527

527-
void rdb_load_vector_store(RedisModuleIO *rdb, VectorStore &store) {
528-
auto dim = store.dim();
528+
void rdb_load_vector_store(RedisModuleIO *rdb, VectorStore &store, std::size_t dim) {
529529
auto size = rdb_load_number(rdb);
530530
for (auto idx = 0UL; idx < size; ++idx) {
531531
auto id = rdb_load_number(rdb);
@@ -563,11 +563,12 @@ void* rdb_load_vector_store(RedisModuleIO *rdb) {
563563
auto info_str = rdb_load_string(rdb);
564564
LlmInfo llm_info(to_sv(info_str));
565565
auto id_idx = rdb_load_number(rdb);
566+
auto dim = rdb_load_number(rdb);
566567

567568
auto store = llm.create_vector_store(type, conf, llm_info);
568569
store->set_id_idx(id_idx);
569570

570-
rdb_load_vector_store(rdb, *store);
571+
rdb_load_vector_store(rdb, *store, dim);
571572

572573
return store.get();
573574
}

src/sw/redis-llm/vector_store.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ uint64_t VectorStore::add(uint64_t id, const std::string_view &data, const Vecto
3131
// Use the first item's dimension as the dimension of the vector store.
3232
_dim = embedding.size();
3333

34+
assert(_dim > 0);
35+
3436
_lazily_init(_dim);
3537
}
3638

@@ -100,20 +102,6 @@ uint64_t VectorStore::_auto_gen_id() {
100102
return ++_id_idx;
101103
}
102104

103-
std::size_t VectorStore::_dimension() const {
104-
auto iter = _conf.find("dim");
105-
if (iter == _conf.end()) {
106-
return 0;
107-
}
108-
109-
const auto &dim = iter.value();
110-
if (!dim.is_number_unsigned()) {
111-
throw Error("invalid dim");
112-
}
113-
114-
return dim.get<std::size_t>();
115-
}
116-
117105
VectorStoreFactory::VectorStoreFactory() {
118106
_register("hnsw", std::make_unique<VectorStoreCreatorTpl<Hnsw>>());
119107
}

src/sw/redis-llm/vector_store.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace sw::redis::llm {
3232
class VectorStore : public Object {
3333
public:
3434
VectorStore(const std::string &type, const nlohmann::json &conf, const LlmInfo &llm) :
35-
_type(type), _conf(conf), _dim(_dimension()), _llm(llm) {}
35+
_type(type), _conf(conf), _dim(0), _llm(llm) {}
3636

3737
virtual ~VectorStore() = default;
3838

@@ -92,8 +92,6 @@ class VectorStore : public Object {
9292

9393
uint64_t _auto_gen_id();
9494

95-
std::size_t _dimension() const;
96-
9795
std::string _type;
9896

9997
nlohmann::json _conf;

0 commit comments

Comments
 (0)