Metadata: deprecated potentially confusing add/from/metadata(String, Object) methods (#413)

This commit is contained in:
LangChain4j 2023-12-22 15:15:32 +01:00 committed by GitHub
parent 0d5da6ef92
commit d64f02fe24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 18 deletions

View File

@ -44,7 +44,7 @@ public class Document {
}
public TextSegment toTextSegment() {
return TextSegment.from(text, metadata.copy().add("index", 0));
return TextSegment.from(text, metadata.copy().add("index", "0"));
}
@Override

View File

@ -30,11 +30,19 @@ public class Metadata {
return metadata.get(key);
}
public Metadata add(String key, Object value) {
this.metadata.put(key, value.toString());
public Metadata add(String key, String value) {
this.metadata.put(key, value);
return this;
}
/**
* Use {@link #add(String, String)} instead
*/
@Deprecated
public Metadata add(String key, Object value) {
return add(key, value.toString());
}
public Metadata remove(String key) {
this.metadata.remove(key);
return this;
@ -68,6 +76,14 @@ public class Metadata {
" }";
}
public static Metadata from(String key, String value) {
return new Metadata().add(key, value);
}
/**
* Use {@link #from(String, String)} instead
*/
@Deprecated
public static Metadata from(String key, Object value) {
return new Metadata().add(key, value);
}
@ -76,6 +92,14 @@ public class Metadata {
return new Metadata(metadata);
}
public static Metadata metadata(String key, String value) {
return from(key, value);
}
/**
* Use {@link #metadata(String, String)} instead
*/
@Deprecated
public static Metadata metadata(String key, Object value) {
return from(key, value);
}

View File

@ -31,8 +31,8 @@ public class FileSystemSource implements DocumentSource {
@Override
public Metadata metadata() {
return new Metadata()
.add(FILE_NAME, path.getFileName())
.add(ABSOLUTE_DIRECTORY_PATH, path.getParent().toAbsolutePath());
.add(FILE_NAME, path.getFileName().toString())
.add(ABSOLUTE_DIRECTORY_PATH, path.getParent().toAbsolutePath().toString());
}
public static FileSystemSource from(Path filePath) {

View File

@ -29,7 +29,7 @@ public class UrlSource implements DocumentSource {
@Override
public Metadata metadata() {
return Metadata.from(Document.URL, url);
return Metadata.from(Document.URL, url.toString());
}
public static UrlSource from(String url) {

View File

@ -138,7 +138,7 @@ public abstract class HierarchicalDocumentSplitter implements DocumentSplitter {
}
private static TextSegment createSegment(String text, Document document, int index) {
Metadata metadata = document.metadata().copy().add(INDEX, index);
Metadata metadata = document.metadata().copy().add(INDEX, String.valueOf(index));
return TextSegment.from(text, metadata);
}
}

View File

@ -160,17 +160,17 @@ class DocumentBySentenceSplitterTest {
segments.forEach(segment ->
assertThat(tokenizer.estimateTokenCountInText(segment.text())).isLessThanOrEqualTo(maxSegmentSize));
assertThat(segments).containsExactly(
textSegment(s1 + " " + s2, metadata("index", 0).add("document", "0")),
textSegment(s3 + " " + s4, metadata("index", 1).add("document", "0")),
textSegment(s5p1, metadata("index", 2).add("document", "0")),
textSegment(s5p2, metadata("index", 3).add("document", "0")),
textSegment(s6, metadata("index", 4).add("document", "0")),
textSegment(s7, metadata("index", 5).add("document", "0")),
textSegment(s8 + " " + s9, metadata("index", 6).add("document", "0")),
textSegment(s10, metadata("index", 7).add("document", "0")),
textSegment(s11 + " " + s12 + " " + s13 + " " + s14, metadata("index", 8).add("document", "0")),
textSegment(s15 + " " + s16 + " " + s17, metadata("index", 9).add("document", "0")),
textSegment(s18, metadata("index", 10).add("document", "0"))
textSegment(s1 + " " + s2, metadata("index", "0").add("document", "0")),
textSegment(s3 + " " + s4, metadata("index", "1").add("document", "0")),
textSegment(s5p1, metadata("index", "2").add("document", "0")),
textSegment(s5p2, metadata("index", "3").add("document", "0")),
textSegment(s6, metadata("index", "4").add("document", "0")),
textSegment(s7, metadata("index", "5").add("document", "0")),
textSegment(s8 + " " + s9, metadata("index", "6").add("document", "0")),
textSegment(s10, metadata("index", "7").add("document", "0")),
textSegment(s11 + " " + s12 + " " + s13 + " " + s14, metadata("index", "8").add("document", "0")),
textSegment(s15 + " " + s16 + " " + s17, metadata("index", "9").add("document", "0")),
textSegment(s18, metadata("index", "10").add("document", "0"))
);
}
}