Skip to content

Commit 0d122bb

Browse files
ilayaperumalgmarkpollack
authored andcommitted
Remove embedding from Document
- This PR removes the reference of `embedding` from the `Document` object. - The Document is designed to contain the content and its metadata but not the embedding representation of its content. This provides clear separation of concern. - Prior to this change, all the vector stores are updated by not depending on the embedding from Document. Instead, use the embedding values returned by the Embedding Models directly: #1826 Resolves #1781
1 parent bfc0b82 commit 0d122bb

File tree

2 files changed

+8
-56
lines changed

2 files changed

+8
-56
lines changed

spring-ai-core/src/main/java/org/springframework/ai/document/Document.java

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@
3434

3535
/**
3636
* A document is a container for the content and metadata of a document. It also contains
37-
* the document's unique ID and an optional embedding.
37+
* the document's unique ID.
3838
*
39-
* A Document can hold either text content or media content, but not both. This ensures
40-
* clear content type handling and processing.
39+
* A Document can hold either text content or media content, but not both.
4140
*
4241
* It is intended to be used to take data from external sources as part of spring-ai's ETL
4342
* pipeline and create an embedding for the text or media and store that embedding in a
@@ -122,12 +121,6 @@ public class Document {
122121
@Nullable
123122
private final Double score;
124123

125-
/**
126-
* Embedding of the document. Note: ephemeral field.
127-
*/
128-
@JsonProperty(index = 100)
129-
private float[] embedding = new float[0];
130-
131124
/**
132125
* Mutable, ephemeral, content to text formatter. Defaults to Document text.
133126
*/
@@ -260,22 +253,6 @@ public Double getScore() {
260253
return this.score;
261254
}
262255

263-
/**
264-
* Return the embedding that were calculated.
265-
* @deprecated We are considering getting rid of this, please comment on
266-
* https://github.com/spring-projects/spring-ai/issues/1781
267-
* @return the embeddings
268-
*/
269-
@Deprecated(since = "1.0.0-M4")
270-
public float[] getEmbedding() {
271-
return this.embedding;
272-
}
273-
274-
public void setEmbedding(float[] embedding) {
275-
Assert.notNull(embedding, "embedding must not be null");
276-
this.embedding = embedding;
277-
}
278-
279256
/**
280257
* Returns the content formatter associated with this document.
281258
* @deprecated We are considering getting rid of this, please comment on
@@ -332,8 +309,6 @@ public static class Builder {
332309

333310
private Map<String, Object> metadata = new HashMap<>();
334311

335-
private float[] embedding = new float[0];
336-
337312
@Nullable
338313
private Double score;
339314

@@ -406,12 +381,6 @@ public Builder metadata(String key, Object value) {
406381
return this;
407382
}
408383

409-
public Builder embedding(float[] embedding) {
410-
Assert.notNull(embedding, "embedding cannot be null");
411-
this.embedding = embedding;
412-
return this;
413-
}
414-
415384
/**
416385
* Sets a score value for this document.
417386
* <p>
@@ -436,9 +405,7 @@ public Document build() {
436405
if (!StringUtils.hasText(this.id)) {
437406
this.id = this.idGenerator.generateId(this.text, this.metadata);
438407
}
439-
var document = new Document(this.id, this.text, this.media, this.metadata, this.score);
440-
document.setEmbedding(this.embedding);
441-
return document;
408+
return new Document(this.id, this.text, this.media, this.metadata, this.score);
442409
}
443410

444411
}

spring-ai-core/src/test/java/org/springframework/ai/document/DocumentTests.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@
1616

1717
package org.springframework.ai.document;
1818

19+
import java.net.MalformedURLException;
20+
import java.net.URL;
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
1924
import org.junit.jupiter.api.Test;
2025

2126
import org.springframework.ai.document.id.IdGenerator;
2227
import org.springframework.ai.model.Media;
2328
import org.springframework.util.MimeTypeUtils;
2429

25-
import java.net.MalformedURLException;
26-
import java.net.URL;
27-
import java.util.HashMap;
28-
import java.util.List;
29-
import java.util.Map;
30-
3130
import static org.assertj.core.api.Assertions.assertThat;
3231
import static org.junit.jupiter.api.Assertions.assertThrows;
3332

@@ -211,20 +210,6 @@ void testMetadataKeyValueAddition() {
211210
assertThat(document.getMetadata()).containsEntry("key1", "value1").containsEntry("key2", "value2");
212211
}
213212

214-
@Test
215-
void testEmbeddingOperations() {
216-
float[] embedding = new float[] { 0.1f, 0.2f, 0.3f };
217-
218-
Document document = Document.builder().text("test").embedding(embedding).build();
219-
220-
assertThat(document.getEmbedding()).isEqualTo(embedding);
221-
}
222-
223-
@Test
224-
void testNullEmbeddingThrowsException() {
225-
assertThrows(IllegalArgumentException.class, () -> Document.builder().text("test").embedding(null).build());
226-
}
227-
228213
private static Media getMedia() {
229214
try {
230215
URL mediaUrl1 = new URL("http://type1");

0 commit comments

Comments
 (0)