Skip to content

retaining the input document id if provided in the split documents #1197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Map;
import java.util.stream.Collectors;

import io.micrometer.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.document.ContentFormatter;
Expand Down Expand Up @@ -61,18 +62,20 @@ private List<Document> doSplitDocuments(List<Document> documents) {
List<String> texts = new ArrayList<>();
List<Map<String, Object>> metadataList = new ArrayList<>();
List<ContentFormatter> formatters = new ArrayList<>();
List<String> ids = new ArrayList<>();

for (Document doc : documents) {
texts.add(doc.getContent());
metadataList.add(doc.getMetadata());
formatters.add(doc.getContentFormatter());
ids.add(doc.getId());
}

return createDocuments(texts, formatters, metadataList);
return createDocuments(texts, formatters, metadataList, ids);
}

private List<Document> createDocuments(List<String> texts, List<ContentFormatter> formatters,
List<Map<String, Object>> metadataList) {
List<Map<String, Object>> metadataList, List<String> ids) {

// Process the data in a column oriented way and recreate the Document
List<Document> documents = new ArrayList<>();
Expand All @@ -84,12 +87,13 @@ private List<Document> createDocuments(List<String> texts, List<ContentFormatter
if (chunks.size() > 1) {
logger.info("Splitting up document into " + chunks.size() + " chunks.");
}
String id = ids.get(i);
for (String chunk : chunks) {
// only primitive values are in here -
Map<String, Object> metadataCopy = metadata.entrySet()
.stream()
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
Document newDoc = new Document(chunk, metadataCopy);
Document newDoc = StringUtils.isEmpty(id) ? new Document(chunk, metadataCopy) : new Document(id, chunk, metadataCopy);

if (this.copyContentFormatter) {
// Transfer the content-formatter of the parent to the chunked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,83 @@ public void pageWithChunkSplit() {
() -> assertThat(splitedDocument.get(3).getMetadata().get("page_number")).isEqualTo(3));
}

@Test
public void testInputAndOutputDocumentIdShouldRemainSameAfterSplitting() {
// given

var doc1 = new Document("1","1In the end, writing arises when man realizes that memory is not enough."
+ "1The most oppressive thing about the labyrinth is that you are constantly "
+ "1being forced to choose. It isn’t the lack of an exit, but the abundance of exits that is so disorienting.",
Map.of("file_name", "sample1.pdf", "page_number", 1));

var doc2 = new Document("2",
"levels, their care providers, legal representatives and families get the right home and \n"
+ " community-based support and services at the right time, in the right place. Please click here to \n"
+ " go to Community Living Connections. \n"
+ "\n"
+ " I am trying to register as a consumer, but Carina will not recognize me or my \n"
+ " information. What should I do? \n"
+ "\n"
+ " Please double check your form entries including the spelling of your name and your \n"
+ " ProviderOne number, or last four digits of your social security number and date of birth. Please \n"
+ " use the name you have on file with the Department of Social and Health Services (DSHS). Also \n"
+ " make sure you have a current or pending assessment with DSHS. \n"
+ "\n"
+ " If you are having trouble registering, please contact us or call us at 1-855-796-0605. \n"
+ "\n"
+ " The Home Care Referral Registry has been absorbed by Consumer Direct Care \n"
+ " Network Washington (CDWA). Who can help me find care on Carina? \n"
+ "\n"
+ " Consumer Direct Care Network Washington (CDWA) has taken over from the Home Care \n"
+ " Referral Registry (HCRR). CDWA is responsible for assisting consumers and Individual Providers \n"
+ " (IPs) to use Carina to find matches. CDWA staff are available across the state to assist \n"
+ " consumers to sign up in the Carina system and help IPs get (re)contracted or hired to work. \n"
+ "\n"
+ " What are some good interview questions I should ask providers? \n"
+ "\n"
+ " Your approach to the interview is important, you are offering a job to someone who is looking \n"
+ " for work. The person you interview may be nervous. Put them at ease, call them by their first \n"
+ " name, maintain eye contact and tell them a little about yourself. Read more tips and specific \n"
+ " interview questions in our blog: What to Ask Potential Providers. \n"
+ "\n"
+ " I am ready to hire a home care provider! \n"
+ "\n"
+ " You found an Individual Provider (IP) that you would like to hire? That is exciting! In order for \n"
+ " them to start working, contact Consumer Direct Care Network Washington (CDWA) and request \n"
+ " authorization. They cannot start work before you have received an Okay to Work from CDWA. \n"
+ "\n"
+ " Consumers should continue to work with their case manager, who will help you create a Plan of \n"
+ " Care and access needed services.\n"
+ "Once you have decided on an IP to work with, they should\n" + "\n",
Map.of("file_name", "sample1.pdf", "page_number", 2));

var doc3 = new Document("3","In the end, writing arises when man realizes that memory is not enough."
+ "3The most oppressive thing about the labyrinth is that you are constantly "
+ "3being forced to choose. It isn’t the lack of an exit, but the abundance of exits that is so disorienting.",
Map.of("file_name", "sample1.pdf", "page_number", 3));
var doc4 = new Document("In the end, writing arises when man realizes that memory is not enough."
+ "3The most oppressive thing about the labyrinth is that you are constantly "
+ "3being forced to choose. It isn’t the lack of an exit, but the abundance of exits that is so disorienting.",
Map.of("file_name", "sample1.pdf", "page_number", 4));

var tokenTextSplitter = new TokenTextSplitter();

// when
List<Document> splitedDocument = tokenTextSplitter.apply(List.of(doc1, doc2, doc3, doc4));

// then
assertAll(() -> assertThat(splitedDocument).isNotNull(), () -> assertThat(splitedDocument).isNotEmpty(),
() -> assertThat(splitedDocument).hasSize(5),
() -> assertThat(splitedDocument.get(0).getMetadata().get("page_number")).isEqualTo(1),
() -> assertThat(splitedDocument.get(1).getMetadata().get("page_number")).isEqualTo(2),
() -> assertThat(splitedDocument.get(2).getMetadata().get("page_number")).isEqualTo(2),
() -> assertThat(splitedDocument.get(3).getMetadata().get("page_number")).isEqualTo(3),
() -> assertThat(splitedDocument.get(4).getMetadata().get("page_number")).isEqualTo(4),
() -> assertThat(splitedDocument.get(0).getId()).isEqualTo("1"),
() -> assertThat(splitedDocument.get(1).getId()).isEqualTo("2"),
() -> assertThat(splitedDocument.get(2).getId()).isEqualTo("2"),
() -> assertThat(splitedDocument.get(3).getId()).isEqualTo("3"),
() -> assertThat(splitedDocument.get(4).getId()).isNotNull());
}

}