adachsoft / embedding-openai
OpenAI Embeddings provider implementation for adachsoft/embedding-contracts.
Requires
- adachsoft/embedding-contracts: ^0.1
- guzzlehttp/guzzle: ^7.10
Requires (Dev)
- adachsoft/php-code-style: ^0.4
- friendsofphp/php-cs-fixer: ^3.94
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^13.0
- rector/rector: ^2.3
- vlucas/phpdotenv: ^5.6
This package is not auto-updated.
Last update: 2026-03-24 14:43:36 UTC
README
Provider-specific implementation of adachsoft/embedding-contracts for the OpenAI Embeddings API.
This library provides a production-ready implementation of EmbedderInterface that talks to the official OpenAI HTTP API using Guzzle and exposes a simple, DTO-based contract for generating text embeddings.
Installation
composer require adachsoft/embedding-openai
This will also install adachsoft/embedding-contracts and guzzlehttp/guzzle as dependencies.
Usage
<?php declare(strict_types=1);
use AdachSoft\EmbeddingContracts\Dto\EmbeddingRequestDto;
use AdachSoft\EmbeddingContracts\Enum\EmbeddingEncodingFormatEnum;
use AdachSoft\EmbeddingContracts\Enum\EmbeddingInputTypeEnum;
use AdachSoft\EmbeddingOpenai\OpenAiEmbedder;
use GuzzleHttp\Client;
$client = new Client();
$apiKey = $_ENV['OPENAI_API_KEY'] ?? getenv('OPENAI_API_KEY');
$embedder = new OpenAiEmbedder($client, (string) $apiKey);
$request = new EmbeddingRequestDto(
input: 'hello world',
model: 'text-embedding-3-small',
inputType: EmbeddingInputTypeEnum::Query,
encodingFormat: EmbeddingEncodingFormatEnum::Float,
);
$response = $embedder->embed($request);
// $response->vector now contains the embedding for "hello world".
Testing
The project ships with a production integration test that talks to the real OpenAI API.
To run it, you must provide a valid API key via OPENAI_API_KEY in your environment (or .env file) and then execute:
vendor/bin/phpunit --group production
By default, this production test group is excluded from the regular test suite.