91 lines
2.6 KiB
C++
91 lines
2.6 KiB
C++
#ifndef HUMANUS_MEMORY_VECTOR_STORE_BASE_H
|
||
#define HUMANUS_MEMORY_VECTOR_STORE_BASE_H
|
||
|
||
#include "config.h"
|
||
#include <memory>
|
||
#include <unordered_map>
|
||
#include <string>
|
||
|
||
namespace humanus {
|
||
|
||
class VectorStore {
|
||
private:
|
||
static std::unordered_map<std::string, std::shared_ptr<VectorStore>> instances_;
|
||
|
||
protected:
|
||
std::shared_ptr<VectorStoreConfig> config_;
|
||
|
||
// Constructor
|
||
VectorStore(const std::shared_ptr<VectorStoreConfig>& config) : config_(config) {}
|
||
|
||
public:
|
||
// Get the singleton instance
|
||
static std::shared_ptr<VectorStore> get_instance(const std::string& config_name = "default", const std::shared_ptr<VectorStoreConfig>& config = nullptr);
|
||
|
||
virtual ~VectorStore() = default;
|
||
|
||
virtual void reset() = 0;
|
||
|
||
/**
|
||
* @brief 插入向量到集合中
|
||
* @param vector 向量数据
|
||
* @param vector_id 向量ID
|
||
* @param metadata 元数据
|
||
*/
|
||
virtual void insert(const std::vector<float>& vector,
|
||
const size_t vector_id,
|
||
const MemoryItem& metadata = MemoryItem()) = 0;
|
||
|
||
/**
|
||
* @brief 搜索相似向量
|
||
* @param query 查询向量
|
||
* @param limit 返回结果数量限制
|
||
* @param filter 可选的过滤条件
|
||
* @return 相似向量的MemoryItem列表
|
||
*/
|
||
virtual std::vector<MemoryItem> search(const std::vector<float>& query,
|
||
size_t limit = 5,
|
||
const FilterFunc& filter = nullptr) = 0;
|
||
|
||
/**
|
||
* @brief 通过ID删除向量
|
||
* @param vector_id 向量ID
|
||
*/
|
||
virtual void remove(size_t vector_id) = 0;
|
||
|
||
/**
|
||
* @brief 更新向量及其负载
|
||
* @param vector_id 向量ID
|
||
* @param vector 新向量数据
|
||
* @param metadata 新负载数据
|
||
*/
|
||
virtual void update(size_t vector_id, const std::vector<float>& vector = std::vector<float>(), const MemoryItem& metadata = MemoryItem()) = 0;
|
||
|
||
/**
|
||
* @brief 通过ID获取向量
|
||
* @param vector_id 向量ID
|
||
* @return 向量数据
|
||
*/
|
||
virtual MemoryItem get(size_t vector_id) = 0;
|
||
|
||
/**
|
||
* @brief Set metadata for a vector
|
||
* @param vector_id Vector ID
|
||
* @param metadata New metadata
|
||
*/
|
||
virtual void set(size_t vector_id, const MemoryItem& metadata) = 0;
|
||
|
||
/**
|
||
* @brief 列出所有记忆
|
||
* @param limit 可选的结果数量限制
|
||
* @param filter 可选的过滤条件(isIDAllowed)
|
||
* @return 记忆ID列表
|
||
*/
|
||
virtual std::vector<MemoryItem> list(size_t limit = 0, const FilterFunc& filter = nullptr) = 0;
|
||
};
|
||
|
||
} // namespace humanus
|
||
|
||
|
||
#endif // HUMANUS_MEMORY_VECTOR_STORE_BASE_H
|