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 ));
0 commit comments