mindfulcoder49/mysql-vector-openai

使用openAI嵌入在MySQL上执行向量操作

1.0.2 2024-09-16 16:17 UTC

This package is auto-updated.

Last update: 2024-09-16 16:17:29 UTC


README

这里有一个反映您VectorTable类当前功能的README.md文件。

# MySQL Vector Table

This repository provides an implementation for storing, searching, and manipulating vectors in a MySQL database using cosine similarity and binary codes for efficient search. The main class `VectorTable` provides a variety of functions to handle vector data, including inserting, updating, searching, and computing similarity between vectors.

## Features

- **Cosine Similarity Search (COSIM)**: Efficiently compute the cosine similarity between vectors stored as JSON in MySQL.
- **Binary Code Representation**: Vectors are stored in binary form for efficient querying and similarity computation.
- **Hamming Distance Search (Optional)**: A method for searching vectors based on Hamming distance is also available, though not the primary method.
- **Vector Normalization**: Automatically normalizes vectors before inserting and computing similarity.
- **Batch Insertion**: Support for inserting multiple vectors at once.
- **Vector Management**: Functions to insert, update, delete, and retrieve vectors.
  
## Table Structure

The vectors are stored in a MySQL table with the following structure:
```sql
CREATE TABLE `your_table_name_vectors` (
    `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `vector` JSON,                  -- The original vector
    `normalized_vector` JSON,        -- The normalized vector
    `magnitude` DOUBLE,              -- The magnitude of the vector
    `binary_code` BLOB,              -- Binary representation of the vector for efficient searching
    `created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

MySQL函数: COSIM

该类定义了一个自定义MySQL函数COSIM,用于计算存储为JSON的两个向量之间的余弦相似度。

CREATE FUNCTION COSIM(v1 JSON, v2 JSON) RETURNS FLOAT DETERMINISTIC
BEGIN
    DECLARE sim FLOAT DEFAULT 0;
    DECLARE i INT DEFAULT 0;
    DECLARE len INT DEFAULT JSON_LENGTH(v1);
    
    IF JSON_LENGTH(v1) != JSON_LENGTH(v2) THEN RETURN NULL; END IF;
    
    WHILE i < len DO
        SET sim = sim + (JSON_EXTRACT(v1, CONCAT('$[', i, ']')) * JSON_EXTRACT(v2, CONCAT('$[', i, ']')));
        SET i = i + 1;
    END WHILE;
    
    RETURN sim;
END;

核心功能

search()

根据余弦相似度搜索表中与给定输入向量最相似的向量。

用法

$results = $vectorTable->search($inputVector, $n = 10);
  • $inputVector: 与存储向量进行比较的向量。
  • $n: 返回的结果数量(默认为10)。

函数返回一个数组,其中包含按余弦相似度排序的最相似向量。

searchWithHamming()

基于向量二进制表示的汉明距离搜索表中的向量。

用法

$results = $vectorTable->searchWithHamming($inputVector, $n = 10);

upsert()

在表中插入或更新一个向量。如果向量已存在(基于ID),则将更新它。否则,将插入一个新向量。

用法

$id = $vectorTable->upsert($vector, $id = null);
  • $vector: 要插入或更新的向量。
  • $id: 要更新的向量的可选ID。如果没有提供,则插入一个新向量。

batchInsert()

在单个事务中插入多个向量到表中。

用法

$ids = $vectorTable->batchInsert($vectorArray);
  • $vectorArray: 要插入的向量的数组。

select()

通过ID检索表中的向量。

用法

$vectors = $vectorTable->select($ids);
  • $ids: 要检索的向量ID数组。

selectAll()

检索表中的所有向量。

用法

$vectors = $vectorTable->selectAll();

delete()

通过ID从表中删除向量。

用法

$vectorTable->delete($id);
  • $id: 要删除的向量的ID。

normalize()

通过其幅度归一化向量。

用法

$normalizedVector = $vectorTable->normalize($vector);
  • $vector: 要归一化的向量。

cosim()

计算两个向量之间的余弦相似度。

用法

$similarity = $vectorTable->cosim($v1, $v2);
  • $v1: 第一个向量。
  • $v2: 第二个向量。

count()

获取数据库中存储的向量的总数。

用法

$totalVectors = $vectorTable->count();

vectorToBinary()

将n维向量转换为二进制代码。

用法

$binaryCode = $vectorTable->vectorToBinary($vector);

初始化

要创建必要的表和函数,您需要调用initialize()方法。

$vectorTable->initialize();

这将创建向量表和余弦相似度函数(COSIM)。

安装

  1. 设置MySQL数据库。
  2. 创建VectorTable类的实例。
  3. 调用initialize()来设置表和函数。
  4. 开始插入、更新和搜索向量!

示例

$mysqli = new \mysqli('localhost', 'user', 'password', 'database');

$vectorTable = new \MHz\MysqlVector\VectorTable($mysqli, 'example_table', 384);
$vectorTable->initialize();

$vector = [1, 2, 3, 4, 5];
$vectorTable->upsert($vector);

$similarVectors = $vectorTable->search($vector);

许可证

本项目采用MIT许可证。


This `README.md` outlines the features, core functionalities, usage examples, and setup instructions for your `VectorTable` class. It reflects the current state of your code, including both cosine similarity and Hamming distance-based searches, and explains how to initialize, insert, search, and manage vectors in the MySQL database.