Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
vendor/
composer.lock
.claude
.idea

# PHPUnit
.phpunit.result.cache
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
},
"require-dev": {
"phpunit/phpunit": "^11.0",
"phpstan/phpstan": "^2.1"
"phpstan/phpstan": "^2.1",
"neuron-core/neuron-ai": "^3.0"
},
"autoload": {
"psr-4": {
Expand Down
83 changes: 83 additions & 0 deletions src/Integrations/NeuronPHPVector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace PHPVector\Integrations;

use NeuronAI\Exceptions\VectorStoreException;
use NeuronAI\RAG\Document as NeuronDocument;
use NeuronAI\RAG\VectorStore\VectorStoreInterface;
use NeuronAI\StaticConstructor;
use PHPVector\Document;
use PHPVector\SearchResult;
use PHPVector\VectorDatabase;

class NeuronPHPVector implements VectorStoreInterface
{
use StaticConstructor;

public function __construct(
protected VectorDatabase $database,
protected int $topK = 5,
){
}

public function addDocument(NeuronDocument $document): VectorStoreInterface
{
$this->addDocuments([$document]);
return $this;
}

/**
* @param NeuronDocument[] $documents
*/
public function addDocuments(array $documents): VectorStoreInterface
{
$this->database->addDocuments(
array_map(fn (NeuronDocument $document): Document => new Document(
id: $document->id,
vector: $document->embedding,
text: $document->content,
metadata: $document->metadata,
), $documents)
);

return $this;
}

/**
* @throws VectorStoreException
*/
public function deleteBy(string $sourceType, ?string $sourceName = null): VectorStoreInterface
{
throw new VectorStoreException('Deletion not supported.');
}

/**
* @throws VectorStoreException
*/
public function deleteBySource(string $sourceType, string $sourceName): VectorStoreInterface
{
$this->deleteBy($sourceType, $sourceName);
return $this;
}

/**
* @param array<float> $embedding
* @return iterable<NeuronDocument>
*/
public function similaritySearch(array $embedding): iterable
{
$results = $this->database->vectorSearch(
vector: $embedding,
k: $this->topK,
);

return array_map(function (SearchResult $result): NeuronDocument {
$document = new NeuronDocument($result->document->text);
$document->id = $result->document->id;
$document->embedding = $result->document->vector;
$document->metadata = $result->document->metadata;
$document->score = $result->score;
return $document;
}, $results);
}
}
Loading