-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNeuronPHPVector.php
More file actions
83 lines (72 loc) · 2.29 KB
/
NeuronPHPVector.php
File metadata and controls
83 lines (72 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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);
}
}