2025-03-23 14:35:54 +08:00
|
|
|
|
#ifndef HUMANUS_MEMORY_MEM0_VECTOR_STORE_BASE_H
|
|
|
|
|
#define HUMANUS_MEMORY_MEM0_VECTOR_STORE_BASE_H
|
|
|
|
|
|
2025-03-26 00:38:43 +08:00
|
|
|
|
#include "config.h"
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
namespace humanus::mem0 {
|
|
|
|
|
|
|
|
|
|
class VectorStore {
|
|
|
|
|
private:
|
|
|
|
|
static std::unordered_map<std::string, std::shared_ptr<VectorStore>> instances_;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
std::shared_ptr<VectorStoreConfig> config_;
|
2025-03-23 14:35:54 +08:00
|
|
|
|
|
2025-03-26 00:38:43 +08:00
|
|
|
|
// Constructor
|
|
|
|
|
VectorStore(const std::shared_ptr<VectorStoreConfig>& config) : config_(config) {}
|
2025-03-23 14:35:54 +08:00
|
|
|
|
|
2025-03-26 00:38:43 +08:00
|
|
|
|
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);
|
2025-03-23 14:35:54 +08:00
|
|
|
|
|
2025-03-26 00:38:43 +08:00
|
|
|
|
virtual ~VectorStore() = default;
|
|
|
|
|
|
|
|
|
|
virtual void reset() = 0;
|
2025-03-23 14:35:54 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 插入向量到集合中
|
2025-03-26 00:38:43 +08:00
|
|
|
|
* @param vector 向量数据
|
|
|
|
|
* @param vector_id 向量ID
|
2025-03-26 19:28:02 +08:00
|
|
|
|
* @param metadata 元数据
|
2025-03-23 14:35:54 +08:00
|
|
|
|
*/
|
2025-03-26 00:38:43 +08:00
|
|
|
|
virtual void insert(const std::vector<float>& vector,
|
|
|
|
|
const size_t vector_id,
|
2025-03-26 19:28:02 +08:00
|
|
|
|
const MemoryItem& metadata) = 0;
|
2025-03-23 14:35:54 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 搜索相似向量
|
|
|
|
|
* @param query 查询向量
|
|
|
|
|
* @param limit 返回结果数量限制
|
2025-03-26 19:28:02 +08:00
|
|
|
|
* @param filter 可选的过滤条件
|
2025-03-26 00:38:43 +08:00
|
|
|
|
* @return 相似向量的MemoryItem列表
|
2025-03-23 14:35:54 +08:00
|
|
|
|
*/
|
2025-03-26 00:38:43 +08:00
|
|
|
|
virtual std::vector<MemoryItem> search(const std::vector<float>& query,
|
2025-03-26 19:28:02 +08:00
|
|
|
|
size_t limit = 5,
|
|
|
|
|
const FilterFunc& filter = nullptr) = 0;
|
2025-03-23 14:35:54 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 通过ID删除向量
|
|
|
|
|
* @param vector_id 向量ID
|
|
|
|
|
*/
|
|
|
|
|
virtual void delete_vector(size_t vector_id) = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 更新向量及其负载
|
|
|
|
|
* @param vector_id 向量ID
|
2025-03-26 19:28:02 +08:00
|
|
|
|
* @param vector 新向量数据
|
|
|
|
|
* @param metadata 新负载数据
|
2025-03-23 14:35:54 +08:00
|
|
|
|
*/
|
2025-03-26 19:28:02 +08:00
|
|
|
|
virtual void update(size_t vector_id, const std::vector<float>& vector = std::vector<float>(), const MemoryItem& metadata = MemoryItem()) = 0;
|
2025-03-23 14:35:54 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 通过ID获取向量
|
|
|
|
|
* @param vector_id 向量ID
|
|
|
|
|
* @return 向量数据
|
|
|
|
|
*/
|
2025-03-26 00:38:43 +08:00
|
|
|
|
virtual MemoryItem get(size_t vector_id) = 0;
|
2025-03-23 14:35:54 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 列出所有记忆
|
|
|
|
|
* @param limit 可选的结果数量限制
|
2025-03-26 19:28:02 +08:00
|
|
|
|
* @param filter 可选的过滤条件(isIDAllowed)
|
2025-03-23 14:35:54 +08:00
|
|
|
|
* @return 记忆ID列表
|
|
|
|
|
*/
|
2025-03-26 19:28:02 +08:00
|
|
|
|
virtual std::vector<MemoryItem> list(size_t limit = 0, const FilterFunc& filter = nullptr) = 0;
|
2025-03-23 14:35:54 +08:00
|
|
|
|
};
|
|
|
|
|
|
2025-03-26 00:38:43 +08:00
|
|
|
|
} // namespace humanus::mem0
|
2025-03-23 14:35:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // HUMANUS_MEMORY_MEM0_VECTOR_STORE_BASE_H
|