定制 allanpichardo/mysql-vector 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

allanpichardo/mysql-vector

Composer 安装命令:

composer require allanpichardo/mysql-vector

包简介

Perform vector operations natively on MySQL

README 文档

README

Overview

The VectorTable class is a PHP implementation designed to facilitate the storage, retrieval, and comparison of high-dimensional vectors in a MySQL database. This class utilizes MySQL JSON data types and a custom cosine similarity function (COSIM) to perform vector comparisons efficiently.

Search Performance

Vectors are binary quantized upon insertion into the database to optimize search speed and reranked to improve accuracy. However, this library is only suitable for small datasets (less than 1,000,000 vectors). For large datasets, it is recommended that you use a dedicated vector database such as Qdrant.

Search Benchmarks (384-dimensional vectors):

Vectors Time (seconds)
100 0.02
1000 0.02
10000 0.03
100000 0.06
1000000 0.48

Features

  • Store vectors in a MySQL database using JSON data types.
  • Calculate cosine similarity between vectors using a custom MySQL function.
  • Normalize vectors and handle vector operations such as insertion, deletion, and searching.
  • Support for vector quantization for optimized search operations.
  • Native PHP support for generating for text embeddings using the BGE embedding model.

Requirements

  • PHP 8.0 or higher.
  • MySQL 5.7 or higher with support for JSON data types and stored functions.
  • A MySQLi extension for PHP.

Installation

  1. Ensure that PHP and MySQL are installed and properly configured on your system.

  2. Install the library using Composer.

    composer require allanpichardo/mysql-vector

Usage

Initializing the Vector Table

Import the VectorTable class and create a new instance using the MySQLi connection, table name, and vector dimension.

use MHz\MysqlVector\VectorTable;


$mysqli = new mysqli("hostname", "username", "password", "database");
$tableName = "my_vector_table";
$dimension = 384;
$engine = 'InnoDB';

$vectorTable = new VectorTable($mysqli, $tableName, $dimension, $engine);

Setting Up the Vector Table in MySQL

The initialize method will create the vector table in MySQL if it does not already exist. This method will also create the COSIM function in MySQL if it does not already exist.

$vectorTable->initialize();

Inserting and Managing Vectors

// Insert a new vector
$vector = [0.1, 0.2, 0.3, ..., 0.384];
$vectorId = $vectorTable->upsert($vector);

// Update an existing vector
$vectorTable->upsert($vector, $vectorId);

// Delete a vector
$vectorTable->delete($vectorId);

Calculating Cosine Similarity

// Calculate cosine similarity between two vectors
$similarity = $vectorTable->cosim($vector1, $vector2);

Searching for Similar Vectors

Perform a search for vectors similar to a given vector using the cosine similarity criteria. The topN parameter specifies the maximum number of similar vectors to return.

// Find vectors similar to a given vector
$similarVectors = $vectorTable->search($vector, $topN);

Text Embeddings

The Embedder class calculates 384-dimensional text embeddings using the BGE embedding model. The first time you instanciate the Embedder class, the ONNX runtime will be installed automatically. The maximum length of the input text is 512 characters. The Embedder class will automatically truncate the input text to 512 characters if it is longer than 512 characters.

use MHz\MysqlVector\Nlp\Embedder;

$embedder = new Embedder();

// Calculate the embeddings for a batch of text
$texts = ["Hello world!", "This is a test."];
$embeddings = $embedder->embed($texts);

print_r($embeddings[0][0]); // [0.1, 0.2, 0.3, ..., 0.384]
print_r($embeddings[1][0]); // [0.1, 0.2, 0.3, ..., 0.384]

Contributions

Contributions to this project are welcome. Please ensure that your code adheres to the existing coding standards and includes appropriate tests.

Development

This project uses DDEV, a Docker-based development environment. To get started, install DDEV and run the following commands:

ddev start
ddev composer install

To run the tests, use the following command:

ddev composer test

License

MIT License

allanpichardo/mysql-vector 适用场景与选型建议

allanpichardo/mysql-vector 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.53k 次下载、GitHub Stars 达 83, 最近一次更新时间为 2024 年 01 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 allanpichardo/mysql-vector 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 allanpichardo/mysql-vector 我们能提供哪些服务?
定制开发 / 二次开发

基于 allanpichardo/mysql-vector 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 19.53k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 83
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 83
  • Watchers: 6
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-01-07