定制 x-laravel/embedding-mongodb-driver 二次开发

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

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

x-laravel/embedding-mongodb-driver

Composer 安装命令:

composer require x-laravel/embedding-mongodb-driver

包简介

MongoDB native vector driver for x-laravel/embedding.

README 文档

README

Tests PHP Laravel License

⚠️ This package has been cancelled and is not functional. See CLAUDE.md for details.

MongoDB vector driver for x-laravel/embedding.

How It Works

  • Provides MongodbEmbedding — a MongoDB-native Eloquent model that stores vectors as BSON arrays, reads the collection name from embedding.database.table config
  • Implements SimilarityDriver — registers as the mongodb driver for MongoDB Atlas Vector Search using $vectorSearch aggregation
  • Community MongoDB (without Atlas): vectors are stored natively as BSON arrays; similarity search falls back to PhpDriver automatically

Requirements

  • PHP ^8.3 + ext-mongodb
  • Laravel ^12.0 | ^13.0
  • x-laravel/embedding ^1.0
  • mongodb/laravel-mongodb ^4.0
  • MongoDB 8.0+ (community) or MongoDB Atlas (for native vector search)

Installation

composer require x-laravel/embedding-mongodb-driver

The MongodbEmbeddingServiceProvider is auto-discovered and registers the mongodb driver automatically.

Setup

1. Configure x-laravel/embedding

Publish the config if you haven't already:

php artisan vendor:publish --tag=embedding-config

Set the MongoDB connection and swap the Embedding model in config/embedding.php:

'database' => [
    'connection' => env('EMBEDDINGS_DATABASE_CONNECTION', 'mongodb'),
    'table'      => env('EMBEDDINGS_DB_TABLE', 'embeddings'),
],

// Swap the default SQL model with the MongoDB-native model
'model' => \XLaravel\Embedding\Driver\Mongodb\Models\MongodbEmbedding::class,

'similarity' => [
    'driver' => env('EMBEDDING_SIMILARITY_DRIVER', 'auto'),
],

2. Configure the MongoDB connection

In config/database.php:

'connections' => [
    'mongodb' => [
        'driver'   => 'mongodb',
        'host'     => env('DB_HOST', '127.0.0.1'),
        'port'     => env('DB_PORT', 27017),
        'database' => env('DB_DATABASE', 'myapp'),
        'username' => env('DB_USERNAME'),
        'password' => env('DB_PASSWORD'),
    ],
],

3. Create the embeddings collection

php artisan migrate

Or publish the migration first:

php artisan vendor:publish --tag=embedding-mongodb-migrations
php artisan migrate

This creates a unique index on (embeddable_type, embeddable_id, slot).

4. MongoDB Atlas Vector Search (optional)

For native DB-level similarity search, create a Vector Search index on the embeddings collection in Atlas with path vector and set the similarity driver:

'similarity' => ['driver' => 'mongodb'],

Note: Without Atlas, similarity search automatically uses PhpDriver (vectors loaded into PHP memory). For large datasets, Atlas Vector Search is strongly recommended.

5. Model

Follow the standard x-laravel/embedding setup. No MongoDB-specific changes are needed on your models.

use XLaravel\Embedding\Attributes\EmbedOn;
use XLaravel\Embedding\Concerns\Embeddable;
use XLaravel\Embedding\Contracts\HasEmbeddings;

#[EmbedOn(['title', 'body'])]
class Post extends Model implements HasEmbeddings
{
    use Embeddable;

    public function toEmbeddingText(): string
    {
        return $this->title.' '.$this->body;
    }
}

Usage

The driver is transparent — use the standard x-laravel/embedding API:

Post::similarToText('web framework', limit: 10);
Post::similarTo($vector, limit: 10, threshold: 0.8);
Post::rankByRelevance($posts, 'web framework');

$post->mostSimilar(limit: 5);
$post->similarityTo($otherPost);

All methods set a similarity_score float attribute on each returned model.

Testing

# Build first (once per PHP version)
DOCKER_BUILDKIT=0 docker compose --profile php83 build

# Run tests
docker compose --profile php83 up
docker compose --profile php84 up
docker compose --profile php85 up

License

This package is open-sourced software licensed under the MIT license.

x-laravel/embedding-mongodb-driver 适用场景与选型建议

x-laravel/embedding-mongodb-driver 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「mongodb」 「laravel」 「vector」 「ai」 「embedding」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 x-laravel/embedding-mongodb-driver 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-05-09