Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"developerMessage": { "index": 6, "kind": "parameter", "displayName": "Developer Message", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.openai.OpenAIConfiguration", "configurationField": "configuration", "description": "Developer message to prepend before user messages" },
"dimensions": { "index": 7, "kind": "parameter", "displayName": "Dimensions", "group": "producer", "label": "", "required": false, "type": "integer", "javaType": "java.lang.Integer", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.openai.OpenAIConfiguration", "configurationField": "configuration", "description": "Number of dimensions for the embedding output. Only supported by text-embedding-3 models. Reducing dimensions can lower costs and improve performance without significant quality loss." },
"embeddingModel": { "index": 8, "kind": "parameter", "displayName": "Embedding Model", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.openai.OpenAIConfiguration", "configurationField": "configuration", "description": "The model to use for embeddings" },
"encodingFormat": { "index": 9, "kind": "parameter", "displayName": "Encoding Format", "group": "producer", "label": "", "required": false, "type": "enum", "javaType": "java.lang.String", "enum": [ "float", "base64" ], "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "defaultValue": "float", "configurationClass": "org.apache.camel.component.openai.OpenAIConfiguration", "configurationField": "configuration", "description": "The format for embedding output: 'float' for list of floats, 'base64' for compressed format" },
"encodingFormat": { "index": 9, "kind": "parameter", "displayName": "Encoding Format", "group": "producer", "label": "", "required": false, "type": "enum", "javaType": "java.lang.String", "enum": [ "float", "base64" ], "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "defaultValue": "base64", "configurationClass": "org.apache.camel.component.openai.OpenAIConfiguration", "configurationField": "configuration", "description": "The format for embedding output: 'float' for list of floats, 'base64' for compressed format" },
"jsonSchema": { "index": 10, "kind": "parameter", "displayName": "Json Schema", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "supportFileReference": true, "configurationClass": "org.apache.camel.component.openai.OpenAIConfiguration", "configurationField": "configuration", "description": "JSON schema for structured output validation" },
"maxTokens": { "index": 11, "kind": "parameter", "displayName": "Max Tokens", "group": "producer", "label": "", "required": false, "type": "integer", "javaType": "java.lang.Integer", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.openai.OpenAIConfiguration", "configurationField": "configuration", "description": "Maximum number of tokens to generate" },
"model": { "index": 12, "kind": "parameter", "displayName": "Model", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.openai.OpenAIConfiguration", "configurationField": "configuration", "description": "The model to use for chat completion" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"developerMessage": { "index": 6, "kind": "parameter", "displayName": "Developer Message", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.openai.OpenAIConfiguration", "configurationField": "configuration", "description": "Developer message to prepend before user messages" },
"dimensions": { "index": 7, "kind": "parameter", "displayName": "Dimensions", "group": "producer", "label": "", "required": false, "type": "integer", "javaType": "java.lang.Integer", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.openai.OpenAIConfiguration", "configurationField": "configuration", "description": "Number of dimensions for the embedding output. Only supported by text-embedding-3 models. Reducing dimensions can lower costs and improve performance without significant quality loss." },
"embeddingModel": { "index": 8, "kind": "parameter", "displayName": "Embedding Model", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.openai.OpenAIConfiguration", "configurationField": "configuration", "description": "The model to use for embeddings" },
"encodingFormat": { "index": 9, "kind": "parameter", "displayName": "Encoding Format", "group": "producer", "label": "", "required": false, "type": "enum", "javaType": "java.lang.String", "enum": [ "float", "base64" ], "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "defaultValue": "float", "configurationClass": "org.apache.camel.component.openai.OpenAIConfiguration", "configurationField": "configuration", "description": "The format for embedding output: 'float' for list of floats, 'base64' for compressed format" },
"encodingFormat": { "index": 9, "kind": "parameter", "displayName": "Encoding Format", "group": "producer", "label": "", "required": false, "type": "enum", "javaType": "java.lang.String", "enum": [ "float", "base64" ], "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "defaultValue": "base64", "configurationClass": "org.apache.camel.component.openai.OpenAIConfiguration", "configurationField": "configuration", "description": "The format for embedding output: 'float' for list of floats, 'base64' for compressed format" },
"jsonSchema": { "index": 10, "kind": "parameter", "displayName": "Json Schema", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "supportFileReference": true, "configurationClass": "org.apache.camel.component.openai.OpenAIConfiguration", "configurationField": "configuration", "description": "JSON schema for structured output validation" },
"maxTokens": { "index": 11, "kind": "parameter", "displayName": "Max Tokens", "group": "producer", "label": "", "required": false, "type": "integer", "javaType": "java.lang.Integer", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.openai.OpenAIConfiguration", "configurationField": "configuration", "description": "Maximum number of tokens to generate" },
"model": { "index": 12, "kind": "parameter", "displayName": "Model", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.openai.OpenAIConfiguration", "configurationField": "configuration", "description": "The model to use for chat completion" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ public class OpenAIConfiguration implements Cloneable {
"Reducing dimensions can lower costs and improve performance without significant quality loss.")
private Integer dimensions;

@UriParam(enums = "float,base64", defaultValue = "float")
@UriParam(enums = "float,base64", defaultValue = "base64")
@Metadata(description = "The format for embedding output: 'float' for list of floats, 'base64' for compressed format")
private String encodingFormat = "float";
private String encodingFormat = "base64";

public String getApiKey() {
return apiKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.openai.models.embeddings.CreateEmbeddingResponse;
import com.openai.models.embeddings.Embedding;
import com.openai.models.embeddings.EmbeddingCreateParams;
import com.openai.models.embeddings.EmbeddingCreateParams.EncodingFormat;
import org.apache.camel.AsyncCallback;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
Expand Down Expand Up @@ -92,6 +93,10 @@ private void processInternal(Exchange exchange) throws Exception {
paramsBuilder.dimensions(dimensions.longValue());
}

if (ObjectHelper.isNotEmpty(config.getEncodingFormat())) {
paramsBuilder.encodingFormat(EncodingFormat.of(config.getEncodingFormat()));
}

EmbeddingCreateParams params = paramsBuilder.build();

// Execute request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public void configure() {
from("direct:embedding")
.to("openai:embeddings")
.to("mock:response");

from("direct:embeddingWithEncodingFormatFloat")
.to("openai:embeddings?encodingFormat=float")
.to("mock:responseEncodingFormatFloat");
}
};
}
Expand Down Expand Up @@ -116,6 +120,33 @@ public void testSingleTextEmbedding() throws Exception {
.isEqualTo("Apache Camel is an integration framework");
}

@Test
public void testSingleTextEmbeddingWithFloatEncodingFormat() throws Exception {
MockEndpoint mockResponse = getMockEndpoint("mock:responseEncodingFormatFloat");
mockResponse.expectedMessageCount(1);

Exchange result = template.request("direct:embeddingWithEncodingFormatFloat",
e -> e.getIn().setBody("Apache Camel is an integration framework"));

mockResponse.assertIsSatisfied();

assertThat(result).isNotNull();
Object body = result.getMessage().getBody();
assertThat(body).isNotNull();
assertThat(body).isInstanceOf(List.class);

@SuppressWarnings("unchecked")
List<Float> embedding = (List<Float>) body;
assertThat(embedding).isNotEmpty();

// Verify headers
assertThat(result.getMessage().getHeader(OpenAIConstants.EMBEDDING_COUNT)).isEqualTo(1);
assertThat(result.getMessage().getHeader(OpenAIConstants.EMBEDDING_VECTOR_SIZE, Integer.class))
.isGreaterThan(0);
assertThat(result.getMessage().getHeader(OpenAIConstants.ORIGINAL_TEXT))
.isEqualTo("Apache Camel is an integration framework");
}

@Test
public void testBatchTextEmbeddings() throws Exception {
MockEndpoint mockResponse = getMockEndpoint("mock:response");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## ---------------------------------------------------------------------------
## Licensed to the Apache Software Foundation (ASF) under one or more
## contributor license agreements. See the NOTICE file distributed with
## this work for additional information regarding copyright ownership.
## The ASF licenses this file to You under the Apache License, Version 2.0
## (the "License"); you may not use this file except in compliance with
## the License. You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
## ---------------------------------------------------------------------------

appender.file.type = File
appender.file.name = file
appender.file.fileName = target/camel-openai-tests.log
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n

appender.out.type = Console
appender.out.name = out
appender.out.layout.type = PatternLayout
appender.out.layout.pattern = [%30.30t] %-30.30c{1} %-5p %m%n

rootLogger.level = INFO
rootLogger.appenderRef.file.ref = file
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ default OpenAIEndpointBuilder embeddingModel(String embeddingModel) {
*
* The option is a: <code>java.lang.String</code> type.
*
* Default: float
* Default: base64
* Group: producer
*
* @param encodingFormat the value to set
Expand Down