Skip to content

Commit 9d64168

Browse files
committed
Added embedding + QA examples using simple file system vector db
1 parent b0d73ae commit 9d64168

4 files changed

Lines changed: 82 additions & 0 deletions

File tree

data/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vectordb.json

data/Uranus_Neptune_moons.pdf

39.1 KB
Binary file not shown.

src/rag/ollama/embedding_file.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Embedding with Ollama (Llama 3)
4+
*/
5+
require dirname(dirname(dirname(__DIR__))) . '/vendor/autoload.php';
6+
7+
use LLPhant\Chat\OllamaChat;
8+
use LLPhant\Embeddings\DataReader\FileDataReader;
9+
use LLPhant\Embeddings\DocumentSplitter\DocumentSplitter;
10+
use LLPhant\Embeddings\EmbeddingGenerator\Ollama\OllamaEmbeddingGenerator;
11+
use LLPhant\Embeddings\VectorStores\FileSystem\FileSystemVectorStore;
12+
use LLPhant\OllamaConfig;
13+
14+
# You can run ollama locally and install LLama3 from here: https://ollama.com/library/llama3
15+
$config = new OllamaConfig();
16+
$config->model = 'llama3';
17+
$chat = new OllamaChat($config);
18+
19+
# Read PDF file
20+
printf ("- Reading the PDF files\n");
21+
$pdfPath = __DIR__ . '/../../../data/Uranus_Neptune_moons.pdf';
22+
$reader = new FileDataReader($pdfPath);
23+
$documents = $reader->getDocuments();
24+
printf("Number of PDF files: %d\n", count($documents));
25+
26+
# Document split
27+
printf("- Document split\n");
28+
$splitDocuments = DocumentSplitter::splitDocuments($documents, 1000);
29+
printf("Number of splitted documents (chunk): %d\n", count($splitDocuments));
30+
31+
# Embedding
32+
printf("- Embedding\n");
33+
$embeddingGenerator = new OllamaEmbeddingGenerator($config);
34+
$embeddedDocuments = $embeddingGenerator->embedDocuments($splitDocuments);
35+
36+
# File system vector store
37+
printf("- Index all the embeddings using the file system\n");
38+
$vectorStorePath = __DIR__ . '/../../../data/vectordb.json';
39+
$store = new FileSystemVectorStore($vectorStorePath);
40+
41+
$store->addDocuments($embeddedDocuments);
42+
43+
printf("Added %d documents in %s\n", count($embeddedDocuments), realpath($vectorStorePath));

src/rag/ollama/qa_file.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* RAG architecture with Ollama and Elasticsearch
4+
*/
5+
require dirname(dirname(dirname(__DIR__))) . '/vendor/autoload.php';
6+
7+
use LLPhant\Chat\OllamaChat;
8+
use LLPhant\Embeddings\EmbeddingGenerator\Ollama\OllamaEmbeddingGenerator;
9+
use LLPhant\Embeddings\VectorStores\FileSystem\FileSystemVectorStore;
10+
use LLPhant\OllamaConfig;
11+
use LLPhant\Query\SemanticSearch\QuestionAnswering;
12+
13+
# Ollama with Llama3
14+
$config = new OllamaConfig();
15+
$config->model = 'llama3';
16+
$chat = new OllamaChat($config);
17+
18+
# Embedding
19+
$embeddingGenerator = new OllamaEmbeddingGenerator($config);
20+
21+
# File system vector store
22+
$vectorStorePath = __DIR__ . '/../../../data/vectordb.json';
23+
$store = new FileSystemVectorStore($vectorStorePath);
24+
25+
# RAG
26+
$qa = new QuestionAnswering(
27+
$store,
28+
$embeddingGenerator,
29+
$chat
30+
);
31+
32+
$answer = $qa->answerQuestion('How many moons has Neptune?');
33+
printf("-- Answer:\n%s\n", $answer);
34+
35+
foreach ($qa->getRetrievedDocuments() as $doc) {
36+
printf("-- Document: %s\n", $doc->sourceName);
37+
printf("-- Content (%d characters): %s\n", strlen($doc->content), $doc->content);
38+
}

0 commit comments

Comments
 (0)