1616
1717package org .springframework .ai .openai .api ;
1818
19+ import java .util .Arrays ;
1920import java .util .List ;
2021import java .util .Map ;
22+ import java .util .Objects ;
2123import java .util .concurrent .atomic .AtomicBoolean ;
2224import java .util .function .Consumer ;
2325import java .util .function .Predicate ;
@@ -85,6 +87,12 @@ public static Builder builder() {
8587
8688 private static final Predicate <String > SSE_DONE_PREDICATE = "[DONE]" ::equals ;
8789
90+ private static final String REQUEST_BODY_NULL_MESSAGE = "The request body can not be null." ;
91+
92+ private static final String STREAM_FALSE_MESSAGE = "Request must set the stream property to false." ;
93+
94+ private static final String ADDITIONAL_HEADERS_NULL_MESSAGE = "The additional HTTP headers can not be null." ;
95+
8896 // Store config fields for mutate/copy
8997 private final String baseUrl ;
9098
@@ -183,9 +191,9 @@ public ResponseEntity<ChatCompletion> chatCompletionEntity(ChatCompletionRequest
183191 public ResponseEntity <ChatCompletion > chatCompletionEntity (ChatCompletionRequest chatRequest ,
184192 MultiValueMap <String , String > additionalHttpHeader ) {
185193
186- Assert .notNull (chatRequest , "The request body can not be null." );
187- Assert .isTrue (!chatRequest .stream (), "Request must set the stream property to false." );
188- Assert .notNull (additionalHttpHeader , "The additional HTTP headers can not be null." );
194+ Assert .notNull (chatRequest , REQUEST_BODY_NULL_MESSAGE );
195+ Assert .isTrue (!chatRequest .stream (), STREAM_FALSE_MESSAGE );
196+ Assert .notNull (additionalHttpHeader , ADDITIONAL_HEADERS_NULL_MESSAGE );
189197
190198 // @formatter:off
191199 return this .restClient .post ()
@@ -221,7 +229,7 @@ public Flux<ChatCompletionChunk> chatCompletionStream(ChatCompletionRequest chat
221229 public Flux <ChatCompletionChunk > chatCompletionStream (ChatCompletionRequest chatRequest ,
222230 MultiValueMap <String , String > additionalHttpHeader ) {
223231
224- Assert .notNull (chatRequest , "The request body can not be null." );
232+ Assert .notNull (chatRequest , REQUEST_BODY_NULL_MESSAGE );
225233 Assert .isTrue (chatRequest .stream (), "Request must set the stream property to true." );
226234
227235 AtomicBoolean isInsideTool = new AtomicBoolean (false );
@@ -284,7 +292,7 @@ public Flux<ChatCompletionChunk> chatCompletionStream(ChatCompletionRequest chat
284292 */
285293 public <T > ResponseEntity <EmbeddingList <Embedding >> embeddings (EmbeddingRequest <T > embeddingRequest ) {
286294
287- Assert .notNull (embeddingRequest , "The request body can not be null." );
295+ Assert .notNull (embeddingRequest , REQUEST_BODY_NULL_MESSAGE );
288296
289297 // Input text to embed, encoded as a string or array of tokens. To embed multiple
290298 // inputs in a single
@@ -296,7 +304,7 @@ public <T> ResponseEntity<EmbeddingList<Embedding>> embeddings(EmbeddingRequest<
296304 // The input must not exceed the max input tokens for the model (8192 tokens for
297305 // text-embedding-ada-002), cannot
298306 // be an empty string, and any array must be 2048 dimensions or less.
299- if (embeddingRequest .input () instanceof List list ) {
307+ if (embeddingRequest .input () instanceof List <?> list ) {
300308 Assert .isTrue (!CollectionUtils .isEmpty (list ), "The input list can not be empty." );
301309 Assert .isTrue (list .size () <= 2048 , "The list must be 2048 dimensions or less" );
302310 Assert .isTrue (
@@ -1218,7 +1226,7 @@ public static class ToolChoiceBuilder {
12181226 /**
12191227 * Specifying a particular function forces the model to call that function.
12201228 */
1221- public static Object FUNCTION (String functionName ) {
1229+ public static Object function (String functionName ) {
12221230 return Map .of ("type" , "function" , "function" , Map .of ("name" , functionName ));
12231231 }
12241232 }
@@ -1877,7 +1885,9 @@ public record ChunkChoice(// @formatter:off
18771885 @ JsonIgnoreProperties (ignoreUnknown = true )
18781886 public record Embedding (// @formatter:off
18791887 @ JsonProperty ("index" ) Integer index ,
1880- @ JsonProperty ("embedding" ) @ JsonDeserialize (using = OpenAiEmbeddingDeserializer .class ) float [] embedding ,
1888+ @ JsonProperty ("embedding" )
1889+ @ JsonDeserialize (using = OpenAiEmbeddingDeserializer .class )
1890+ float [] embedding ,
18811891 @ JsonProperty ("object" ) String object ) { // @formatter:on
18821892
18831893 /**
@@ -1891,6 +1901,25 @@ public Embedding(Integer index, float[] embedding) {
18911901 this (index , embedding , "embedding" );
18921902 }
18931903
1904+ @ Override
1905+ public boolean equals (Object o ) {
1906+ if (this == o ) {
1907+ return true ;
1908+ }
1909+ if (!(o instanceof Embedding other )) {
1910+ return false ;
1911+ }
1912+ return Objects .equals (this .index , other .index ) && Arrays .equals (this .embedding , other .embedding )
1913+ && Objects .equals (this .object , other .object );
1914+ }
1915+
1916+ @ Override
1917+ public int hashCode () {
1918+ int result = Objects .hash (this .index , this .object );
1919+ result = 31 * result + Arrays .hashCode (this .embedding );
1920+ return result ;
1921+ }
1922+
18941923 }
18951924
18961925 /**
0 commit comments