Skip to content

Commit

Permalink
Update document readers samples
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasVitale committed Jan 26, 2024
1 parent 2347203 commit b2dbb82
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 22 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,7 @@ Icon
Network Trash Folder
Temporary Items
.apdisk

# Project

**/embeddings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}
""");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<Document> documents = new ArrayList<>();

log.info("Loading JSON as Documents");
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
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;

@Value("classpath:documents/story2.pdf")
Resource pdfFile2;

public DocumentInitializer(SimpleVectorStore simpleVectorStore) {
this.simpleVectorStore = simpleVectorStore;
public DocumentInitializer(SimpleVectorStore vectorStore) {
this.vectorStore = vectorStore;
}

@PostConstruct
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
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;

@Value("classpath:documents/story2.txt")
Resource textFile2;

public DocumentInitializer(SimpleVectorStore simpleVectorStore) {
this.simpleVectorStore = simpleVectorStore;
public DocumentInitializer(SimpleVectorStore vectorStore) {
this.vectorStore = vectorStore;
}

@PostConstruct
Expand All @@ -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);
}

}

0 comments on commit b2dbb82

Please sign in to comment.