99import javax .annotation .Nonnull ;
1010import org .springframework .ai .chat .metadata .DefaultUsage ;
1111import org .springframework .ai .document .Document ;
12+ import org .springframework .ai .document .MetadataMode ;
1213import org .springframework .ai .embedding .Embedding ;
1314import org .springframework .ai .embedding .EmbeddingModel ;
1415import org .springframework .ai .embedding .EmbeddingRequest ;
2627public class OpenAiSpringEmbeddingModel implements EmbeddingModel {
2728
2829 private final OpenAiClient client ;
30+ private final MetadataMode metadataMode ;
2931
3032 /**
3133 * Constructs an {@code OpenAiSpringEmbeddingModel} with the specified {@link OpenAiClient} of
@@ -34,7 +36,24 @@ public class OpenAiSpringEmbeddingModel implements EmbeddingModel {
3436 * @param client the OpenAI client
3537 */
3638 public OpenAiSpringEmbeddingModel (@ Nonnull final OpenAiClient client ) {
39+ this (client , MetadataMode .EMBED );
40+ }
41+
42+ /**
43+ * Constructs an {@code OpenAiSpringEmbeddingModel} with the specified {@link OpenAiClient} of
44+ * some model and metadata mode.
45+ *
46+ * <p>The metadata mode is used by content formatter to determine which metadata to include in the
47+ * resulting content. Currently, the formatter is only effective for calls to {@link
48+ * #embed(Document)}.
49+ *
50+ * @param client the OpenAI client
51+ * @param metadataMode the metadata mode
52+ */
53+ public OpenAiSpringEmbeddingModel (
54+ @ Nonnull final OpenAiClient client , @ Nonnull final MetadataMode metadataMode ) {
3755 this .client = client ;
56+ this .metadataMode = metadataMode ;
3857 }
3958
4059 /**
@@ -62,7 +81,9 @@ public EmbeddingResponse call(@Nonnull final EmbeddingRequest request)
6281 @ Nonnull
6382 public float [] embed (@ Nonnull final Document document ) throws UnsupportedOperationException {
6483 if (document .isText ()) {
65- return embed (Objects .requireNonNull (document .getText (), "Document is missing text content" ));
84+ return embed (
85+ Objects .requireNonNull (
86+ document .getFormattedContent (this .metadataMode ), "Document is missing text content" ));
6687 }
6788 throw new UnsupportedOperationException ("Only text type document supported for embedding" );
6889 }
0 commit comments