Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

feat: add pinecone store #55

Merged
merged 1 commit into from
Sep 26, 2024
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Supported Stores
* [x] [ChromaDB](https://trychroma.com)
* [x] [Azure AI Search](https://azure.microsoft.com/en-us/products/ai-services/ai-search)
* [x] [MongoDB Atlas Search](https://mongodb.com/products/platform/atlas-vector-search)
* [ ] [Pinecone](https://pinecone.io)
* [x] [Pinecone](https://pinecone.io)

Provided Tools
--------------
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
"symfony/var-dumper": "^6.4 || ^7.1"
},
"suggest": {
"mongodb/mongodb": "For using MongoDB Atlas as retrieval vector store.",
"codewithkyrian/chromadb-php": "For using the ChromaDB as retrieval vector store.",
"mongodb/mongodb": "For using MongoDB Atlas as retrieval vector store.",
"probots-io/pinecone-php": "For using the Pinecone as retrieval vector store.",
"symfony/clock": "For using the clock tool.",
"symfony/css-selector": "For using the YouTube transcription tool.",
"symfony/dom-crawler": "For using the YouTube transcription tool."
Expand Down
2 changes: 1 addition & 1 deletion src/Document/Vector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class Vector
/**
* @param list<float> $data
*/
private function __construct(
public function __construct(
private readonly array $data,
private ?int $dimensions = null,
) {
Expand Down
2 changes: 1 addition & 1 deletion src/Store/MongoDB/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function query(Vector $vector, array $options = []): array

foreach ($results as $result) {
$documents[] = Document::fromVector(
Vector::create1536($result[$this->vectorFieldName]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the static one is still needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, just thought it is an unnecessary Detail and limitation here

new Vector($result[$this->vectorFieldName]),
$this->toUuid($result['_id']),
new Metadata($result['metadata'] ?? []),
);
Expand Down
80 changes: 80 additions & 0 deletions src/Store/Pinecone/Store.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Store\Pinecone;

use PhpLlm\LlmChain\Document\Document;
use PhpLlm\LlmChain\Document\Metadata;
use PhpLlm\LlmChain\Document\Vector;
use PhpLlm\LlmChain\Store\VectorStoreInterface;
use Probots\Pinecone\Client;
use Probots\Pinecone\Resources\Data\VectorResource;
use Psr\Log\LoggerInterface;
use Symfony\Component\Uid\Uuid;

final readonly class Store implements VectorStoreInterface
{
/**
* @param array<string, mixed> $filter
*/
public function __construct(
private Client $pinecone,
private LoggerInterface $logger,
private ?string $namespace = null,
private array $filter = [],
private int $topK = 3,
) {
}

public function addDocument(Document $document): void
{
$this->addDocuments([$document]);
}

public function addDocuments(array $documents): void
{
$vectors = [];
foreach ($documents as $document) {
if (!$document->hasVector()) {
$this->logger->warning('Document {id} does not have a vector', ['id' => $document->id]);
continue;
}

$vectors[] = [
'id' => (string) $document->id,
'values' => $document->vector->getData(),
'metadata' => $document->metadata->getArrayCopy(),
];
}

$this->getVectors()->upsert($vectors);
}

public function query(Vector $vector, array $options = []): array
{
$response = $this->getVectors()->query(
vector: $vector->getData(),
namespace: $options['namespace'] ?? $this->namespace,
filter: $options['filter'] ?? $this->filter,
topK: $options['topK'] ?? $this->topK,
includeValues: true,
);

$documents = [];
foreach ($response->json()['matches'] as $match) {
$documents[] = Document::fromVector(
new Vector($match['values']),
Uuid::fromString($match['id']),
new Metadata($match['metadata']),
);
}

return $documents;
}

private function getVectors(): VectorResource
{
return $this->pinecone->data()->vectors();
}
}