1
1
/*
2
- * Copyright 2023-2024 the original author or authors.
2
+ * Copyright 2023-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
28
28
import org .springframework .ai .embedding .AbstractEmbeddingModel ;
29
29
import org .springframework .ai .embedding .Embedding ;
30
30
import org .springframework .ai .embedding .EmbeddingOptions ;
31
- import org .springframework .ai .embedding .EmbeddingOptionsBuilder ;
32
31
import org .springframework .ai .embedding .EmbeddingRequest ;
33
32
import org .springframework .ai .embedding .EmbeddingResponse ;
34
33
import org .springframework .ai .embedding .EmbeddingResponseMetadata ;
@@ -110,12 +109,16 @@ public MistralAiEmbeddingModel(MistralAiApi mistralAiApi, MetadataMode metadataM
110
109
111
110
@ Override
112
111
public EmbeddingResponse call (EmbeddingRequest request ) {
113
- var apiRequest = createRequest (request );
112
+ // Before moving any further, build the final request Prompt,
113
+ // merging runtime and default options.
114
+ EmbeddingRequest embeddingRequest = buildEmbeddingRequest (request );
115
+
116
+ var apiRequest = createRequest (embeddingRequest );
114
117
115
118
var observationContext = EmbeddingModelObservationContext .builder ()
116
119
.embeddingRequest (request )
117
120
.provider (MistralAiApi .PROVIDER_NAME )
118
- .requestOptions (buildRequestOptions ( apiRequest ))
121
+ .requestOptions (embeddingRequest . getOptions ( ))
119
122
.build ();
120
123
121
124
return EmbeddingModelObservationDocumentation .EMBEDDING_MODEL_OPERATION
@@ -146,20 +149,29 @@ public EmbeddingResponse call(EmbeddingRequest request) {
146
149
});
147
150
}
148
151
152
+ private EmbeddingRequest buildEmbeddingRequest (EmbeddingRequest embeddingRequest ) {
153
+ // Process runtime options
154
+ MistralAiEmbeddingOptions runtimeOptions = null ;
155
+ if (embeddingRequest .getOptions () != null ) {
156
+ runtimeOptions = ModelOptionsUtils .copyToTarget (embeddingRequest .getOptions (), EmbeddingOptions .class ,
157
+ MistralAiEmbeddingOptions .class );
158
+ }
159
+
160
+ // Define request options by merging runtime options and default options
161
+ MistralAiEmbeddingOptions requestOptions = ModelOptionsUtils .merge (runtimeOptions , this .defaultOptions ,
162
+ MistralAiEmbeddingOptions .class );
163
+
164
+ return new EmbeddingRequest (embeddingRequest .getInstructions (), requestOptions );
165
+ }
166
+
149
167
private DefaultUsage getDefaultUsage (MistralAiApi .Usage usage ) {
150
168
return new DefaultUsage (usage .promptTokens (), usage .completionTokens (), usage .totalTokens (), usage );
151
169
}
152
170
153
- @ SuppressWarnings ("unchecked" )
154
171
private MistralAiApi .EmbeddingRequest <List <String >> createRequest (EmbeddingRequest request ) {
155
- var embeddingRequest = new MistralAiApi .EmbeddingRequest <>(request .getInstructions (),
156
- this .defaultOptions .getModel (), this .defaultOptions .getEncodingFormat ());
157
-
158
- if (request .getOptions () != null ) {
159
- embeddingRequest = ModelOptionsUtils .merge (request .getOptions (), embeddingRequest ,
160
- MistralAiApi .EmbeddingRequest .class );
161
- }
162
- return embeddingRequest ;
172
+ MistralAiEmbeddingOptions requestOptions = (MistralAiEmbeddingOptions ) request .getOptions ();
173
+ return new MistralAiApi .EmbeddingRequest <>(request .getInstructions (), requestOptions .getModel (),
174
+ requestOptions .getEncodingFormat ());
163
175
}
164
176
165
177
@ Override
@@ -168,10 +180,6 @@ public float[] embed(Document document) {
168
180
return this .embed (document .getFormattedContent (this .metadataMode ));
169
181
}
170
182
171
- private EmbeddingOptions buildRequestOptions (MistralAiApi .EmbeddingRequest <List <String >> request ) {
172
- return EmbeddingOptionsBuilder .builder ().withModel (request .model ()).build ();
173
- }
174
-
175
183
/**
176
184
* Use the provided convention for reporting observation data
177
185
* @param observationConvention The provided convention
0 commit comments