diff --git a/.gitignore b/.gitignore index 5c34e17..3338cba 100644 --- a/.gitignore +++ b/.gitignore @@ -92,3 +92,7 @@ Icon Network Trash Folder Temporary Items .apdisk + +# Project + +**/embeddings.json diff --git a/05-document-readers/document-readers-json-ollama/src/main/java/com/thomasvitale/ai/spring/ChatService.java b/05-document-readers/document-readers-json-ollama/src/main/java/com/thomasvitale/ai/spring/ChatService.java index 4dde576..96f4066 100644 --- a/05-document-readers/document-readers-json-ollama/src/main/java/com/thomasvitale/ai/spring/ChatService.java +++ b/05-document-readers/document-readers-json-ollama/src/main/java/com/thomasvitale/ai/spring/ChatService.java @@ -28,10 +28,9 @@ class ChatService { AssistantMessage chatWithDocument(String message) { var systemPromptTemplate = new SystemPromptTemplate(""" You're assisting with questions about products in a bicycle catalog. - Use the information from the DOCUMENTS section and no prior knowledge. - If unsure or if the answer isn't found in the DOCUMENTS section, simply state - that you don't know the answer. - + Answer questions given the context information below (DOCUMENTS section) and no prior knowledge, + but act as if you knew this information innately. If the answer is not found in the DOCUMENTS section, + simply state that you don't know the answer. DOCUMENTS: {documents} """); diff --git a/05-document-readers/document-readers-json-ollama/src/main/java/com/thomasvitale/ai/spring/DocumentInitializer.java b/05-document-readers/document-readers-json-ollama/src/main/java/com/thomasvitale/ai/spring/DocumentInitializer.java index 8cb64cb..5bc738f 100644 --- a/05-document-readers/document-readers-json-ollama/src/main/java/com/thomasvitale/ai/spring/DocumentInitializer.java +++ b/05-document-readers/document-readers-json-ollama/src/main/java/com/thomasvitale/ai/spring/DocumentInitializer.java @@ -11,6 +11,7 @@ import org.springframework.core.io.Resource; import org.springframework.stereotype.Component; +import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -19,7 +20,7 @@ public class DocumentInitializer { private static final Logger log = LoggerFactory.getLogger(DocumentInitializer.class); - private final SimpleVectorStore simpleVectorStore; + private final SimpleVectorStore vectorStore; @Value("classpath:documents/bikes-1.json") Resource jsonFile1; @@ -30,12 +31,19 @@ public class DocumentInitializer { @Value("classpath:documents/bikes-3.json") Resource jsonFile3; - public DocumentInitializer(SimpleVectorStore simpleVectorStore) { - this.simpleVectorStore = simpleVectorStore; + public DocumentInitializer(SimpleVectorStore vectorStore) { + this.vectorStore = vectorStore; } @PostConstruct public void run() { + File embeddingsStorage = new File("src/main/resources/vector-store/embeddings.json"); + if (embeddingsStorage.exists()) { + log.info("Loading Embeddings from file into vector store"); + vectorStore.load(embeddingsStorage); + return; + } + List documents = new ArrayList<>(); log.info("Loading JSON as Documents"); @@ -56,7 +64,10 @@ public void run() { documents.addAll(jsonReader3.get()); log.info("Creating and storing Embeddings from Documents"); - simpleVectorStore.add(documents); + vectorStore.add(documents); + + log.info("Persisting Embeddings to local storage file"); + vectorStore.save(embeddingsStorage); } static class BikeJsonMetadataGenerator implements JsonMetadataGenerator { diff --git a/05-document-readers/document-readers-pdf-ollama/src/main/java/com/thomasvitale/ai/spring/ChatService.java b/05-document-readers/document-readers-pdf-ollama/src/main/java/com/thomasvitale/ai/spring/ChatService.java index 6420e4b..42f9f69 100644 --- a/05-document-readers/document-readers-pdf-ollama/src/main/java/com/thomasvitale/ai/spring/ChatService.java +++ b/05-document-readers/document-readers-pdf-ollama/src/main/java/com/thomasvitale/ai/spring/ChatService.java @@ -27,9 +27,10 @@ class ChatService { AssistantMessage chatWithDocument(String message) { var systemPromptTemplate = new SystemPromptTemplate(""" - Answer questions given the context information below (DOCUMENTS section) and no prior knowledge. - If the answer is not found in the DOCUMENTS section, simply state that you don't know the answer. - In the answer, include the source file name from which the context information is extracted from. + Answer questions given the context information below (DOCUMENTS section) and no prior knowledge, + but act as if you knew this information innately. If the answer is not found in the DOCUMENTS section, + simply state that you don't know the answer. In the answer, include the source file name from which + the context information is extracted from. DOCUMENTS: {documents} diff --git a/05-document-readers/document-readers-pdf-ollama/src/main/java/com/thomasvitale/ai/spring/DocumentInitializer.java b/05-document-readers/document-readers-pdf-ollama/src/main/java/com/thomasvitale/ai/spring/DocumentInitializer.java index c948744..4f387aa 100644 --- a/05-document-readers/document-readers-pdf-ollama/src/main/java/com/thomasvitale/ai/spring/DocumentInitializer.java +++ b/05-document-readers/document-readers-pdf-ollama/src/main/java/com/thomasvitale/ai/spring/DocumentInitializer.java @@ -19,7 +19,7 @@ public class DocumentInitializer { private static final Logger log = LoggerFactory.getLogger(DocumentInitializer.class); - private final SimpleVectorStore simpleVectorStore; + private final SimpleVectorStore vectorStore; @Value("classpath:documents/story1.pdf") Resource pdfFile1; @@ -27,8 +27,8 @@ public class DocumentInitializer { @Value("classpath:documents/story2.pdf") Resource pdfFile2; - public DocumentInitializer(SimpleVectorStore simpleVectorStore) { - this.simpleVectorStore = simpleVectorStore; + public DocumentInitializer(SimpleVectorStore vectorStore) { + this.vectorStore = vectorStore; } @PostConstruct @@ -51,7 +51,7 @@ public void run() { documents.addAll(pdfReader2.get()); log.info("Creating and storing Embeddings from Documents"); - simpleVectorStore.add(documents); + vectorStore.add(documents); } } diff --git a/05-document-readers/document-readers-text-ollama/src/main/java/com/thomasvitale/ai/spring/ChatService.java b/05-document-readers/document-readers-text-ollama/src/main/java/com/thomasvitale/ai/spring/ChatService.java index 6420e4b..42f9f69 100644 --- a/05-document-readers/document-readers-text-ollama/src/main/java/com/thomasvitale/ai/spring/ChatService.java +++ b/05-document-readers/document-readers-text-ollama/src/main/java/com/thomasvitale/ai/spring/ChatService.java @@ -27,9 +27,10 @@ class ChatService { AssistantMessage chatWithDocument(String message) { var systemPromptTemplate = new SystemPromptTemplate(""" - Answer questions given the context information below (DOCUMENTS section) and no prior knowledge. - If the answer is not found in the DOCUMENTS section, simply state that you don't know the answer. - In the answer, include the source file name from which the context information is extracted from. + Answer questions given the context information below (DOCUMENTS section) and no prior knowledge, + but act as if you knew this information innately. If the answer is not found in the DOCUMENTS section, + simply state that you don't know the answer. In the answer, include the source file name from which + the context information is extracted from. DOCUMENTS: {documents} diff --git a/05-document-readers/document-readers-text-ollama/src/main/java/com/thomasvitale/ai/spring/DocumentInitializer.java b/05-document-readers/document-readers-text-ollama/src/main/java/com/thomasvitale/ai/spring/DocumentInitializer.java index e16038f..220af99 100644 --- a/05-document-readers/document-readers-text-ollama/src/main/java/com/thomasvitale/ai/spring/DocumentInitializer.java +++ b/05-document-readers/document-readers-text-ollama/src/main/java/com/thomasvitale/ai/spring/DocumentInitializer.java @@ -18,7 +18,7 @@ public class DocumentInitializer { private static final Logger log = LoggerFactory.getLogger(DocumentInitializer.class); - private final SimpleVectorStore simpleVectorStore; + private final SimpleVectorStore vectorStore; @Value("classpath:documents/story1.md") Resource textFile1; @@ -26,8 +26,8 @@ public class DocumentInitializer { @Value("classpath:documents/story2.txt") Resource textFile2; - public DocumentInitializer(SimpleVectorStore simpleVectorStore) { - this.simpleVectorStore = simpleVectorStore; + public DocumentInitializer(SimpleVectorStore vectorStore) { + this.vectorStore = vectorStore; } @PostConstruct @@ -47,7 +47,7 @@ public void run() { documents.addAll(textReader2.get()); log.info("Creating and storing Embeddings from Documents"); - simpleVectorStore.add(documents); + vectorStore.add(documents); } }