Skip to content

Commit

Permalink
Fixes TheoKanning#469: Pass dimensions in EmbeddingRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhupant committed Feb 22, 2024
1 parent 48694ba commit 9a9ae7b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.theokanning.openai.embedding;

/**
* Enum to represent the available OpenAI embedding models
*/
public enum EmbeddingModel {

TEXT_EMBEDDING_ADA_002("text-embedding-ada-002", 2),
TEXT_EMBEDDING_V3_SMALL("text-embedding-3-small", 3),
TEXT_EMBEDDING_V3_LARGE("text-embedding-3-large", 3),
;

private final String modelName;
private final Integer generation;

/**
* Constructor.
*
* @param modelName the value of the enum
*/
EmbeddingModel(String modelName, Integer generation) {
this.modelName = modelName;
this.generation = generation;
}

/**
* Method to return enum from a string value.
*
* @param value the value to convert to an enum
* @return the enum value
*/
public static EmbeddingModel fromValue(String value) {
for (EmbeddingModel b : EmbeddingModel.values()) {
if (b.modelName.equals(value)) {
return b;
}
}
throw new IllegalArgumentException("Unexpected value '" + value + "'");
}

/**
* Method to return the value of the enum.
*
* @return the value of the enum
*/
public Integer getGeneration() {
return generation;
}

/**
* Method to return the value of the enum.
*
* @return the value of the enum
*/
@Override
public String toString() {
return modelName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,25 @@ public class EmbeddingRequest {
String user;

/**
* The number of dimensions for the embedding. Default value, is not provided, is 1536
* The number of dimensions to be used by the model to represent each input text as the feature
* vector.
*/
int dimensions = 1536;
@Setter(AccessLevel.NONE)
Integer dimensions = null;

/**
* Set the number of dimensions to be used by the model to represent each input text as the feature vector.
*
* @param dimensions The number of dimensions to be used by the model. Only supported for models from or after
* third generation.
*/
public void setDimensions(Integer dimensions) {
EmbeddingModel embeddingModel = EmbeddingModel.fromValue(this.model);

if (embeddingModel.getGeneration() > 2) {
this.dimensions = dimensions;
} else {
this.dimensions = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public interface OpenAiApi {
@POST("/v1/engines/{engine_id}/completions")
Single<CompletionResult> createCompletion(@Path("engine_id") String engineId, @Body CompletionRequest request);

@Deprecated
@POST("/v1/edits")
Single<EditResult> createEdit(@Body EditRequest request);

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GROUP=com.theokanning.openai-gpt3-java
VERSION_NAME=0.18.2
VERSION_NAME=0.18.3

POM_URL=https://github.com/theokanning/openai-java
POM_SCM_URL=https://github.com/theokanning/openai-java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static org.junit.jupiter.api.Assertions.assertNotNull;

@Deprecated
public class EditTest {

String token = System.getenv("OPENAI_TOKEN");
Expand Down

0 comments on commit 9a9ae7b

Please sign in to comment.