Skip to content

Commit 413a4d4

Browse files
feat(embeddings): add embedding dimensions request field (#317)
1 parent aaca8ad commit 413a4d4

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

openai-client/src/commonTest/kotlin/com/aallam/openai/client/TestEmbeddings.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ class TestEmbeddings : TestOpenAI() {
2323
assertEquals(0, embedding.index)
2424
}
2525

26+
@Test
27+
fun embeddingDimensions() = test {
28+
val response = openAI.embeddings(request = embeddingRequest {
29+
model = ModelId("text-embedding-3-small")
30+
input = listOf("The food was delicious and the waiter...")
31+
dimensions = 1024
32+
})
33+
assertTrue { response.embeddings.isNotEmpty() }
34+
val embedding = response.embeddings.first()
35+
assertTrue { embedding.embedding.isNotEmpty() }
36+
assertEquals(1024, embedding.embedding.size)
37+
assertEquals(0, embedding.index)
38+
}
39+
2640
@Test
2741
fun similarityEqual() = test {
2842
val embedding1 = Embedding(embedding = listOf(1.0, 2.0, 3.0, 4.0), index = 0)

openai-core/src/commonMain/kotlin/com.aallam.openai.api/embedding/EmbeddingRequest.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ public class EmbeddingRequest(
2626
*/
2727
@SerialName("input") public val input: List<String>,
2828

29+
/**
30+
* The number of dimensions the resulting output embeddings should have.
31+
*
32+
* Only supported in text-embedding-3 and later models.
33+
*/
34+
@SerialName("dimensions") public val dimensions: Int? = null,
35+
2936
/**
3037
* A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse.
3138
*/
@@ -59,6 +66,13 @@ public class EmbeddingRequestBuilder {
5966
*/
6067
public var input: List<String>? = null
6168

69+
/**
70+
* The number of dimensions the resulting output embeddings should have.
71+
*
72+
* Only supported in text-embedding-3 and later models.
73+
*/
74+
public var dimensions: Int? = null
75+
6276
/**
6377
* A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse.
6478
*/
@@ -70,6 +84,7 @@ public class EmbeddingRequestBuilder {
7084
public fun build(): EmbeddingRequest = EmbeddingRequest(
7185
model = requireNotNull(model) { "model is required" },
7286
input = requireNotNull(input) { "input is required" },
87+
dimensions = dimensions,
7388
user = user
7489
)
7590
}

0 commit comments

Comments
 (0)