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
| Element | Description |
|---|---|
Contract\\EmbedderInterface | Main contract for embedding providers; exposes single and batch embed operations plus provider metadata. |
DTOs
| Class | Description |
|---|---|
Dto\\EmbeddingRequestDto | Immutable request describing a single embedding operation (input, model, input type, encoding format, dimensions, user). |
Dto\\EmbeddingResponseDto | Immutable response carrying a single embedding vector together with token count, encoding format, model and original input. |
Dto\\EmbedderCapabilitiesDto | Describes provider capabilities such as maximum input tokens, vector dimensions, batch size and supported formats/input types. |
Enums
| Enum | Values | Description |
|---|---|---|
Enum\\EmbeddingEncodingFormatEnum | Float, Base64 | Encoding of the embedding vector (float list or base64 string). |
Enum\\EmbeddingInputTypeEnum | Query, Document | Semantic role of the input text (search query vs indexed document). |
Collections
| Class | Description |
|---|---|
Collection\\EmbeddingResponseCollection | Immutable collection of EmbeddingResponseDto with helper totalTokenCount() summing tokens of all items. |
Collection\\EmbeddingEncodingFormatCollection | Immutable collection of EmbeddingEncodingFormatEnum with helper supports(EmbeddingEncodingFormatEnum): bool. |
Collection\\EmbeddingInputTypeCollection | Immutable collection of EmbeddingInputTypeEnum with helper supports(EmbeddingInputTypeEnum): bool. |
Exceptions
| Class | Description |
|---|---|
Exception\\EmbeddingException | Base class for all embedding-related domain exceptions. |
Exception\\InputTooLongException | Thrown when input text exceeds maximum supported length. |
Exception\\UnsupportedModelException | Thrown when a requested embedding model is not supported by the provider. |
Exception\\UnsupportedEncodingFormatException | Thrown when a requested encoding format is not supported; message is built from the requested format. |
Exception\\RateLimitException | Thrown when provider rate limits are exceeded. |
Exception\\EmbeddingFailedException | Thrown 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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 adachsoft/embedding-contracts 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Interface for Babel Street Text Analytics
PHP library for the MUMSYS project
A simple library that allows transform any kind of data to native php data or whatever
This package provides type-safe extension of the laravel collection, forcing a single type of object.
Doctrine Collections adapter for Rekapager pagination library
Library to mimics generic collections
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 33
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-23