adachsoft/embedding-contracts 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

adachsoft/embedding-contracts

Composer 安装命令:

composer require adachsoft/embedding-contracts

包简介

Provider-agnostic contracts and DTOs for text embeddings in PHP.

README 文档

README

Provider-agnostic contracts and DTOs for text embeddings in PHP.

Installation

composer require adachsoft/embedding-contracts

Contracts

ElementDescription
Contract\\EmbedderInterfaceMain contract for embedding providers; exposes single and batch embed operations plus provider metadata.

DTOs

ClassDescription
Dto\\EmbeddingRequestDtoImmutable request describing a single embedding operation (input, model, input type, encoding format, dimensions, user).
Dto\\EmbeddingResponseDtoImmutable response carrying a single embedding vector together with token count, encoding format, model and original input.
Dto\\EmbedderCapabilitiesDtoDescribes provider capabilities such as maximum input tokens, vector dimensions, batch size and supported formats/input types.

Enums

EnumValuesDescription
Enum\\EmbeddingEncodingFormatEnumFloat, Base64Encoding of the embedding vector (float list or base64 string).
Enum\\EmbeddingInputTypeEnumQuery, DocumentSemantic role of the input text (search query vs indexed document).

Collections

ClassDescription
Collection\\EmbeddingResponseCollectionImmutable collection of EmbeddingResponseDto with helper totalTokenCount() summing tokens of all items.
Collection\\EmbeddingEncodingFormatCollectionImmutable collection of EmbeddingEncodingFormatEnum with helper supports(EmbeddingEncodingFormatEnum): bool.
Collection\\EmbeddingInputTypeCollectionImmutable collection of EmbeddingInputTypeEnum with helper supports(EmbeddingInputTypeEnum): bool.

Exceptions

ClassDescription
Exception\\EmbeddingExceptionBase class for all embedding-related domain exceptions.
Exception\\InputTooLongExceptionThrown when input text exceeds maximum supported length.
Exception\\UnsupportedModelExceptionThrown when a requested embedding model is not supported by the provider.
Exception\\UnsupportedEncodingFormatExceptionThrown when a requested encoding format is not supported; message is built from the requested format.
Exception\\RateLimitExceptionThrown when provider rate limits are exceeded.
Exception\\EmbeddingFailedExceptionThrown when an embedding operation fails for a non-recoverable reason.

Exception hierarchy

EmbeddingException (extends RuntimeException)
├── InputTooLongException
├── UnsupportedModelException
├── UnsupportedEncodingFormatException
├── RateLimitException
└── EmbeddingFailedException

Usage examples

Implementing a provider

<?php declare(strict_types=1);

use AdachSoft\\EmbeddingContracts\\Collection\\EmbeddingEncodingFormatCollection;
use AdachSoft\\EmbeddingContracts\\Collection\\EmbeddingInputTypeCollection;
use AdachSoft\\EmbeddingContracts\\Contract\\EmbedderInterface;
use AdachSoft\\EmbeddingContracts\\Dto\\EmbedderCapabilitiesDto;
use AdachSoft\\EmbeddingContracts\\Dto\\EmbeddingRequestDto;
use AdachSoft\\EmbeddingContracts\\Dto\\EmbeddingResponseDto;
use AdachSoft\\EmbeddingContracts\\Enum\\EmbeddingEncodingFormatEnum;
use AdachSoft\\EmbeddingContracts\\Enum\\EmbeddingInputTypeEnum;
use AdachSoft\\EmbeddingContracts\\Exception\\EmbeddingException;
use AdachSoft\\EmbeddingContracts\\Exception\\EmbeddingFailedException;
use AdachSoft\\EmbeddingContracts\\Exception\\InputTooLongException;
use AdachSoft\\EmbeddingContracts\\Exception\\RateLimitException;
use AdachSoft\\EmbeddingContracts\\Exception\\UnsupportedEncodingFormatException;
use AdachSoft\\EmbeddingContracts\\Exception\\UnsupportedModelException;

final class OpenAiEmbedder implements EmbedderInterface
{
    public function embed(EmbeddingRequestDto $request): EmbeddingResponseDto
    {
        // Call the underlying OpenAI client here and map the response.
        // The implementation is out of scope for this package.
        throw new EmbeddingFailedException('Not implemented');
    }

    public function embedBatch(EmbeddingRequestDto ...$requests): \AdachSoft\\EmbeddingContracts\\Collection\\EmbeddingResponseCollection
    {
        // Implement batch embedding using provider-specific API.
        throw new EmbeddingFailedException('Not implemented');
    }

    public function getProviderName(): string
    {
        return 'openai';
    }

    public function getCapabilities(): EmbedderCapabilitiesDto
    {
        return new EmbedderCapabilitiesDto(
            maxInputTokens: 8192,
            vectorDimensions: 1536,
            maxBatchSize: 100,
            supportedEncodingFormats: new EmbeddingEncodingFormatCollection([
                EmbeddingEncodingFormatEnum::Float,
            ]),
            supportedInputTypes: new EmbeddingInputTypeCollection([
                EmbeddingInputTypeEnum::Query,
                EmbeddingInputTypeEnum::Document,
            ]),
            supportsDimensionTruncation: true,
        );
    }
}

Consuming the contracts from application code

<?php declare(strict_types=1);

use AdachSoft\\EmbeddingContracts\\Contract\\EmbedderInterface;
use AdachSoft\\EmbeddingContracts\\Dto\\EmbeddingRequestDto;
use AdachSoft\\EmbeddingContracts\\Enum\\EmbeddingEncodingFormatEnum;
use AdachSoft\\EmbeddingContracts\\Enum\\EmbeddingInputTypeEnum;
use AdachSoft\\EmbeddingContracts\\Exception\\EmbeddingException;

final class SemanticSearchService
{
    public function __construct(private readonly EmbedderInterface $embedder)
    {
    }

    /**
     * @return list<float>
     *
     * @throws EmbeddingException
     */
    public function embedQuery(string $query, string $model): array
    {
        $request = new EmbeddingRequestDto(
            input: $query,
            model: $model,
            inputType: EmbeddingInputTypeEnum::Query,
            encodingFormat: EmbeddingEncodingFormatEnum::Float,
        );

        $response = $this->embedder->embed($request);

        if (!is_array($response->vector)) {
            throw new \RuntimeException('Expected float vector for query embeddings.');
        }

        return $response->vector;
    }
}

adachsoft/embedding-contracts 适用场景与选型建议

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

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

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

围绕 adachsoft/embedding-contracts 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-23