Skip to content

Commit d602904

Browse files
committed
rag methods
1 parent 2aeb9f3 commit d602904

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/RAG/RAG.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
class RAG extends Agent
1212
{
13+
/**
14+
* @var VectorStoreInterface
15+
*/
16+
protected VectorStoreInterface $store;
1317

1418
/**
1519
* The embeddings provider.
@@ -25,10 +29,6 @@ class RAG extends Agent
2529
*/
2630
protected ?string $instructions = "Use the following pieces of context to answer the question of the user. If you don't know the answer, just say that you don't know, don't try to make up an answer.\n\n{context}.";
2731

28-
public function __construct(protected VectorStoreInterface $store) {
29-
parent::__construct();
30-
}
31-
3232
public function answerQuestion(string $question, int $k = 4): Message
3333
{
3434
$this->notify('agent:vectorstore:searching', $question);
@@ -78,7 +78,7 @@ public function setSystemMessage(array $documents, int $k): self
7878
private function searchDocuments(string $question, int $k): array
7979
{
8080
$embedding = $this->embeddings()->embedText($question);
81-
$docs = $this->store->similaritySearch($embedding, $k);
81+
$docs = $this->vectorStore()->similaritySearch($embedding, $k);
8282

8383
$retrievedDocs = [];
8484

@@ -100,4 +100,14 @@ public function embeddings(): EmbeddingsProviderInterface
100100
{
101101
return $this->embeddingsProvider;
102102
}
103+
104+
public function setVectorStore(VectorStoreInterface $store)
105+
{
106+
$this->store = $store;
107+
}
108+
109+
public function vectorStore(): VectorStoreInterface
110+
{
111+
return $this->store;
112+
}
103113
}

src/RAG/VectorStore/PineconeVectorStore.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class PineconeVectorStore implements VectorStoreInterface
1212

1313
public function __construct(
1414
string $key,
15-
protected string $indexName,
15+
protected string $index,
1616
array $spec,
1717
string $version = '2025-01'
1818
) {
@@ -26,7 +26,7 @@ public function __construct(
2626
]
2727
]);
2828

29-
$response = $this->client->get("indexes/{$this->indexName}");
29+
$response = $this->client->get("indexes/{$this->index}");
3030

3131
if ($response->getStatusCode() === 200) {
3232
return;
@@ -35,7 +35,7 @@ public function __construct(
3535
// Create the index
3636
$this->client->post('indexes', [
3737
RequestOptions::JSON => [
38-
'name' => $indexName,
38+
'name' => $index,
3939
'spec' => $spec,
4040
]
4141
]);
@@ -48,7 +48,7 @@ public function addDocument(Document $document): void
4848

4949
public function addDocuments(array $documents): void
5050
{
51-
$this->client->post("indexes/{$this->indexName}/vectors/upsert", [
51+
$this->client->post("indexes/{$this->index}/vectors/upsert", [
5252
RequestOptions::JSON => array_map(function (Document $document) {
5353
return [
5454
'id' => $document->id??uniqid(),
@@ -60,7 +60,7 @@ public function addDocuments(array $documents): void
6060

6161
public function similaritySearch(array $embedding, int $k = 4): iterable
6262
{
63-
$result = $this->client->get("indexes/{$this->indexName}/query", [
63+
$result = $this->client->get("indexes/{$this->index}/query", [
6464
RequestOptions::QUERY => [
6565
'namespace' => '',
6666
'vector' => $embedding,

0 commit comments

Comments
 (0)