Skip to content

Commit 20d97ef

Browse files
committed
feat(api): add text embeddings dimensions param (openai#1103)
1 parent 62cd463 commit 20d97ef

File tree

5 files changed

+62
-18
lines changed

5 files changed

+62
-18
lines changed

src/openai/resources/chat/completions.py

+28-6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ def create(
4646
model: Union[
4747
str,
4848
Literal[
49+
"gpt-4-0125-preview",
50+
"gpt-4-turbo-preview",
4951
"gpt-4-1106-preview",
5052
"gpt-4-vision-preview",
5153
"gpt-4",
@@ -152,7 +154,8 @@ def create(
152154
[See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details)
153155
154156
response_format: An object specifying the format that the model must output. Compatible with
155-
`gpt-4-1106-preview` and `gpt-3.5-turbo-1106`.
157+
[GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and
158+
`gpt-3.5-turbo-1106`.
156159
157160
Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the
158161
message the model generates is valid JSON.
@@ -232,6 +235,8 @@ def create(
232235
model: Union[
233236
str,
234237
Literal[
238+
"gpt-4-0125-preview",
239+
"gpt-4-turbo-preview",
235240
"gpt-4-1106-preview",
236241
"gpt-4-vision-preview",
237242
"gpt-4",
@@ -345,7 +350,8 @@ def create(
345350
[See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details)
346351
347352
response_format: An object specifying the format that the model must output. Compatible with
348-
`gpt-4-1106-preview` and `gpt-3.5-turbo-1106`.
353+
[GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and
354+
`gpt-3.5-turbo-1106`.
349355
350356
Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the
351357
message the model generates is valid JSON.
@@ -418,6 +424,8 @@ def create(
418424
model: Union[
419425
str,
420426
Literal[
427+
"gpt-4-0125-preview",
428+
"gpt-4-turbo-preview",
421429
"gpt-4-1106-preview",
422430
"gpt-4-vision-preview",
423431
"gpt-4",
@@ -531,7 +539,8 @@ def create(
531539
[See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details)
532540
533541
response_format: An object specifying the format that the model must output. Compatible with
534-
`gpt-4-1106-preview` and `gpt-3.5-turbo-1106`.
542+
[GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and
543+
`gpt-3.5-turbo-1106`.
535544
536545
Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the
537546
message the model generates is valid JSON.
@@ -604,6 +613,8 @@ def create(
604613
model: Union[
605614
str,
606615
Literal[
616+
"gpt-4-0125-preview",
617+
"gpt-4-turbo-preview",
607618
"gpt-4-1106-preview",
608619
"gpt-4-vision-preview",
609620
"gpt-4",
@@ -698,6 +709,8 @@ async def create(
698709
model: Union[
699710
str,
700711
Literal[
712+
"gpt-4-0125-preview",
713+
"gpt-4-turbo-preview",
701714
"gpt-4-1106-preview",
702715
"gpt-4-vision-preview",
703716
"gpt-4",
@@ -804,7 +817,8 @@ async def create(
804817
[See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details)
805818
806819
response_format: An object specifying the format that the model must output. Compatible with
807-
`gpt-4-1106-preview` and `gpt-3.5-turbo-1106`.
820+
[GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and
821+
`gpt-3.5-turbo-1106`.
808822
809823
Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the
810824
message the model generates is valid JSON.
@@ -884,6 +898,8 @@ async def create(
884898
model: Union[
885899
str,
886900
Literal[
901+
"gpt-4-0125-preview",
902+
"gpt-4-turbo-preview",
887903
"gpt-4-1106-preview",
888904
"gpt-4-vision-preview",
889905
"gpt-4",
@@ -997,7 +1013,8 @@ async def create(
9971013
[See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details)
9981014
9991015
response_format: An object specifying the format that the model must output. Compatible with
1000-
`gpt-4-1106-preview` and `gpt-3.5-turbo-1106`.
1016+
[GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and
1017+
`gpt-3.5-turbo-1106`.
10011018
10021019
Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the
10031020
message the model generates is valid JSON.
@@ -1070,6 +1087,8 @@ async def create(
10701087
model: Union[
10711088
str,
10721089
Literal[
1090+
"gpt-4-0125-preview",
1091+
"gpt-4-turbo-preview",
10731092
"gpt-4-1106-preview",
10741093
"gpt-4-vision-preview",
10751094
"gpt-4",
@@ -1183,7 +1202,8 @@ async def create(
11831202
[See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details)
11841203
11851204
response_format: An object specifying the format that the model must output. Compatible with
1186-
`gpt-4-1106-preview` and `gpt-3.5-turbo-1106`.
1205+
[GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and
1206+
`gpt-3.5-turbo-1106`.
11871207
11881208
Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the
11891209
message the model generates is valid JSON.
@@ -1256,6 +1276,8 @@ async def create(
12561276
model: Union[
12571277
str,
12581278
Literal[
1279+
"gpt-4-0125-preview",
1280+
"gpt-4-turbo-preview",
12591281
"gpt-4-1106-preview",
12601282
"gpt-4-vision-preview",
12611283
"gpt-4",

src/openai/resources/embeddings.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def create(
3636
self,
3737
*,
3838
input: Union[str, List[str], List[int], List[List[int]]],
39-
model: Union[str, Literal["text-embedding-ada-002"]],
39+
model: Union[str, Literal["text-embedding-ada-002", "text-embedding-3-small", "text-embedding-3-large"]],
40+
dimensions: int | NotGiven = NOT_GIVEN,
4041
encoding_format: Literal["float", "base64"] | NotGiven = NOT_GIVEN,
4142
user: str | NotGiven = NOT_GIVEN,
4243
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
@@ -64,6 +65,9 @@ def create(
6465
[Model overview](https://platform.openai.com/docs/models/overview) for
6566
descriptions of them.
6667
68+
dimensions: The number of dimensions the resulting output embeddings should have. Only
69+
supported in `text-embedding-3` and later models.
70+
6771
encoding_format: The format to return the embeddings in. Can be either `float` or
6872
[`base64`](https://pypi.org/project/pybase64/).
6973
@@ -83,6 +87,7 @@ def create(
8387
"input": input,
8488
"model": model,
8589
"user": user,
90+
"dimensions": dimensions,
8691
"encoding_format": encoding_format,
8792
}
8893
if not is_given(encoding_format) and has_numpy():
@@ -132,7 +137,8 @@ async def create(
132137
self,
133138
*,
134139
input: Union[str, List[str], List[int], List[List[int]]],
135-
model: Union[str, Literal["text-embedding-ada-002"]],
140+
model: Union[str, Literal["text-embedding-ada-002", "text-embedding-3-small", "text-embedding-3-large"]],
141+
dimensions: int | NotGiven = NOT_GIVEN,
136142
encoding_format: Literal["float", "base64"] | NotGiven = NOT_GIVEN,
137143
user: str | NotGiven = NOT_GIVEN,
138144
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
@@ -160,6 +166,9 @@ async def create(
160166
[Model overview](https://platform.openai.com/docs/models/overview) for
161167
descriptions of them.
162168
169+
dimensions: The number of dimensions the resulting output embeddings should have. Only
170+
supported in `text-embedding-3` and later models.
171+
163172
encoding_format: The format to return the embeddings in. Can be either `float` or
164173
[`base64`](https://pypi.org/project/pybase64/).
165174
@@ -179,6 +188,7 @@ async def create(
179188
"input": input,
180189
"model": model,
181190
"user": user,
191+
"dimensions": dimensions,
182192
"encoding_format": encoding_format,
183193
}
184194
if not is_given(encoding_format) and has_numpy():

src/openai/types/chat/completion_create_params.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class CompletionCreateParamsBase(TypedDict, total=False):
3232
Union[
3333
str,
3434
Literal[
35+
"gpt-4-0125-preview",
36+
"gpt-4-turbo-preview",
3537
"gpt-4-1106-preview",
3638
"gpt-4-vision-preview",
3739
"gpt-4",
@@ -133,7 +135,9 @@ class CompletionCreateParamsBase(TypedDict, total=False):
133135
response_format: ResponseFormat
134136
"""An object specifying the format that the model must output.
135137
136-
Compatible with `gpt-4-1106-preview` and `gpt-3.5-turbo-1106`.
138+
Compatible with
139+
[GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and
140+
`gpt-3.5-turbo-1106`.
137141
138142
Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the
139143
message the model generates is valid JSON.

src/openai/types/embedding_create_params.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class EmbeddingCreateParams(TypedDict, total=False):
2020
for counting tokens.
2121
"""
2222

23-
model: Required[Union[str, Literal["text-embedding-ada-002"]]]
23+
model: Required[Union[str, Literal["text-embedding-ada-002", "text-embedding-3-small", "text-embedding-3-large"]]]
2424
"""ID of the model to use.
2525
2626
You can use the
@@ -30,6 +30,12 @@ class EmbeddingCreateParams(TypedDict, total=False):
3030
descriptions of them.
3131
"""
3232

33+
dimensions: int
34+
"""The number of dimensions the resulting output embeddings should have.
35+
36+
Only supported in `text-embedding-3` and later models.
37+
"""
38+
3339
encoding_format: Literal["float", "base64"]
3440
"""The format to return the embeddings in.
3541

tests/api_resources/test_embeddings.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ class TestEmbeddings:
2121
def test_method_create(self, client: OpenAI) -> None:
2222
embedding = client.embeddings.create(
2323
input="The quick brown fox jumped over the lazy dog",
24-
model="text-embedding-ada-002",
24+
model="text-embedding-3-small",
2525
)
2626
assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
2727

2828
@parametrize
2929
def test_method_create_with_all_params(self, client: OpenAI) -> None:
3030
embedding = client.embeddings.create(
3131
input="The quick brown fox jumped over the lazy dog",
32-
model="text-embedding-ada-002",
32+
model="text-embedding-3-small",
33+
dimensions=1,
3334
encoding_format="float",
3435
user="user-1234",
3536
)
@@ -39,7 +40,7 @@ def test_method_create_with_all_params(self, client: OpenAI) -> None:
3940
def test_raw_response_create(self, client: OpenAI) -> None:
4041
response = client.embeddings.with_raw_response.create(
4142
input="The quick brown fox jumped over the lazy dog",
42-
model="text-embedding-ada-002",
43+
model="text-embedding-3-small",
4344
)
4445

4546
assert response.is_closed is True
@@ -51,7 +52,7 @@ def test_raw_response_create(self, client: OpenAI) -> None:
5152
def test_streaming_response_create(self, client: OpenAI) -> None:
5253
with client.embeddings.with_streaming_response.create(
5354
input="The quick brown fox jumped over the lazy dog",
54-
model="text-embedding-ada-002",
55+
model="text-embedding-3-small",
5556
) as response:
5657
assert not response.is_closed
5758
assert response.http_request.headers.get("X-Stainless-Lang") == "python"
@@ -69,15 +70,16 @@ class TestAsyncEmbeddings:
6970
async def test_method_create(self, async_client: AsyncOpenAI) -> None:
7071
embedding = await async_client.embeddings.create(
7172
input="The quick brown fox jumped over the lazy dog",
72-
model="text-embedding-ada-002",
73+
model="text-embedding-3-small",
7374
)
7475
assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
7576

7677
@parametrize
7778
async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
7879
embedding = await async_client.embeddings.create(
7980
input="The quick brown fox jumped over the lazy dog",
80-
model="text-embedding-ada-002",
81+
model="text-embedding-3-small",
82+
dimensions=1,
8183
encoding_format="float",
8284
user="user-1234",
8385
)
@@ -87,7 +89,7 @@ async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) ->
8789
async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
8890
response = await async_client.embeddings.with_raw_response.create(
8991
input="The quick brown fox jumped over the lazy dog",
90-
model="text-embedding-ada-002",
92+
model="text-embedding-3-small",
9193
)
9294

9395
assert response.is_closed is True
@@ -99,7 +101,7 @@ async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
99101
async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
100102
async with async_client.embeddings.with_streaming_response.create(
101103
input="The quick brown fox jumped over the lazy dog",
102-
model="text-embedding-ada-002",
104+
model="text-embedding-3-small",
103105
) as response:
104106
assert not response.is_closed
105107
assert response.http_request.headers.get("X-Stainless-Lang") == "python"

0 commit comments

Comments
 (0)