Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,22 +391,22 @@ $response = $chain->call($messages);
LLM Chain supports document embedding and similarity search using vector stores like ChromaDB, Azure AI Search, MongoDB
Atlas Search, or Pinecone.

For populating a vector store, LLM Chain provides the service `Embedder`, which requires an instance of an
For populating a vector store, LLM Chain provides the service `Indexer`, which requires an instance of an
`EmbeddingsModel` and one of `StoreInterface`, and works with a collection of `Document` objects as input:

```php
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Embeddings;
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\PlatformFactory;
use PhpLlm\LlmChain\Store\Bridge\Pinecone\Store;
use PhpLlm\LlmChain\Store\Embedder;
use PhpLlm\LlmChain\Store\Indexer;
use Probots\Pinecone\Pinecone;

$embedder = new Embedder(
$indexer = new Indexer(
PlatformFactory::create($_ENV['OPENAI_API_KEY']),
new Embeddings(),
new Store(Pinecone::client($_ENV['PINECONE_API_KEY'], $_ENV['PINECONE_HOST']),
);
$embedder->embed($documents);
$indexer->index($documents);
```

The collection of `Document` instances is usually created by text input of your domain entities:
Expand Down
6 changes: 3 additions & 3 deletions examples/store/mongodb-similarity-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use PhpLlm\LlmChain\Store\Bridge\MongoDB\Store;
use PhpLlm\LlmChain\Store\Document\Metadata;
use PhpLlm\LlmChain\Store\Document\TextDocument;
use PhpLlm\LlmChain\Store\Embedder;
use PhpLlm\LlmChain\Store\Indexer;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\Uid\Uuid;

Expand Down Expand Up @@ -52,8 +52,8 @@

// create embeddings for documents
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']);
$embedder = new Embedder($platform, $embeddings = new Embeddings(), $store);
$embedder->embed($documents);
$indexer = new Indexer($platform, $embeddings = new Embeddings(), $store);
$indexer->index($documents);

// initialize the index
$store->initialize();
Expand Down
6 changes: 3 additions & 3 deletions examples/store/pinecone-similarity-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use PhpLlm\LlmChain\Store\Bridge\Pinecone\Store;
use PhpLlm\LlmChain\Store\Document\Metadata;
use PhpLlm\LlmChain\Store\Document\TextDocument;
use PhpLlm\LlmChain\Store\Embedder;
use PhpLlm\LlmChain\Store\Indexer;
use Probots\Pinecone\Pinecone;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\Uid\Uuid;
Expand Down Expand Up @@ -46,8 +46,8 @@

// create embeddings for documents
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']);
$embedder = new Embedder($platform, $embeddings = new Embeddings(), $store);
$embedder->embed($documents);
$indexer = new Indexer($platform, $embeddings = new Embeddings(), $store);
$indexer->index($documents);

$model = new GPT(GPT::GPT_4O_MINI);

Expand Down
8 changes: 4 additions & 4 deletions src/Store/Embedder.php → src/Store/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* @author Christopher Hertel <[email protected]>
*/
final readonly class Embedder
final readonly class Indexer
{
private ClockInterface $clock;

Expand All @@ -32,16 +32,16 @@ public function __construct(
}

/**
* @param TextDocument|TextDocument[] $documents
* @param TextDocument|iterable<TextDocument> $documents
*/
public function embed(TextDocument|array $documents, int $chunkSize = 0, int $sleep = 0): void
public function index(TextDocument|iterable $documents, int $chunkSize = 0, int $sleep = 0): void
{
if ($documents instanceof TextDocument) {
$documents = [$documents];
}

if ([] === $documents) {
$this->logger->debug('No documents to embed');
$this->logger->debug('No documents to index');

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace PhpLlm\LlmChain\Tests\Store\Document;
namespace PhpLlm\LlmChain\Tests\Platform\Vector;

use PhpLlm\LlmChain\Platform\Vector\NullVector;
use PhpLlm\LlmChain\Platform\Vector\VectorInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace PhpLlm\LlmChain\Tests\Store\Document;
namespace PhpLlm\LlmChain\Tests\Platform\Vector;

use PhpLlm\LlmChain\Platform\Vector\Vector;
use PhpLlm\LlmChain\Platform\Vector\VectorInterface;
Expand Down
24 changes: 12 additions & 12 deletions tests/Store/EmbedderTest.php → tests/Store/IndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use PhpLlm\LlmChain\Store\Document\Metadata;
use PhpLlm\LlmChain\Store\Document\TextDocument;
use PhpLlm\LlmChain\Store\Document\VectorDocument;
use PhpLlm\LlmChain\Store\Embedder;
use PhpLlm\LlmChain\Store\Indexer;
use PhpLlm\LlmChain\Tests\Double\PlatformTestHandler;
use PhpLlm\LlmChain\Tests\Double\TestStore;
use PHPUnit\Framework\Attributes\CoversClass;
Expand All @@ -26,7 +26,7 @@
use Symfony\Component\Clock\MockClock;
use Symfony\Component\Uid\Uuid;

#[CoversClass(Embedder::class)]
#[CoversClass(Indexer::class)]
#[Medium]
#[UsesClass(TextDocument::class)]
#[UsesClass(Vector::class)]
Expand All @@ -37,22 +37,22 @@
#[UsesClass(Platform::class)]
#[UsesClass(AsyncResponse::class)]
#[UsesClass(VectorResponse::class)]
final class EmbedderTest extends TestCase
final class IndexerTest extends TestCase
{
#[Test]
public function embedSingleDocument(): void
{
$document = new TextDocument($id = Uuid::v4(), 'Test content');
$vector = new Vector([0.1, 0.2, 0.3]);

$embedder = new Embedder(
$indexer = new Indexer(
PlatformTestHandler::createPlatform(new VectorResponse($vector)),
new Embeddings(),
$store = new TestStore(),
new MockClock(),
);

$embedder->embed($document);
$indexer->index($document);

self::assertCount(1, $store->documents);
self::assertInstanceOf(VectorDocument::class, $store->documents[0]);
Expand All @@ -64,17 +64,17 @@ public function embedSingleDocument(): void
public function embedEmptyDocumentList(): void
{
$logger = self::createMock(LoggerInterface::class);
$logger->expects(self::once())->method('debug')->with('No documents to embed');
$logger->expects(self::once())->method('debug')->with('No documents to index');

$embedder = new Embedder(
$indexer = new Indexer(
PlatformTestHandler::createPlatform(),
new Embeddings(),
$store = new TestStore(),
new MockClock(),
$logger,
);

$embedder->embed([]);
$indexer->index([]);

self::assertSame([], $store->documents);
}
Expand All @@ -86,14 +86,14 @@ public function embedDocumentWithMetadata(): void
$document = new TextDocument($id = Uuid::v4(), 'Test content', $metadata);
$vector = new Vector([0.1, 0.2, 0.3]);

$embedder = new Embedder(
$indexer = new Indexer(
PlatformTestHandler::createPlatform(new VectorResponse($vector)),
new Embeddings(),
$store = new TestStore(),
new MockClock(),
);

$embedder->embed($document);
$indexer->index($document);

self::assertSame(1, $store->addCalls);
self::assertCount(1, $store->documents);
Expand All @@ -112,14 +112,14 @@ public function embedWithSleep(): void
$document1 = new TextDocument(Uuid::v4(), 'Test content 1');
$document2 = new TextDocument(Uuid::v4(), 'Test content 2');

$embedder = new Embedder(
$indexer = new Indexer(
PlatformTestHandler::createPlatform(new VectorResponse($vector1, $vector2)),
new Embeddings(),
$store = new TestStore(),
$clock = new MockClock('2024-01-01 00:00:00'),
);

$embedder->embed(
$indexer->index(
documents: [$document1, $document2],
sleep: 3
);
Expand Down