Skip to content

Commit

Permalink
feat(api): add text embeddings dimensions param (#1103)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Jan 25, 2024
1 parent 62cd463 commit 20d97ef
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 18 deletions.
34 changes: 28 additions & 6 deletions src/openai/resources/chat/completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def create(
model: Union[
str,
Literal[
"gpt-4-0125-preview",
"gpt-4-turbo-preview",
"gpt-4-1106-preview",
"gpt-4-vision-preview",
"gpt-4",
Expand Down Expand Up @@ -152,7 +154,8 @@ def create(
[See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details)
response_format: An object specifying the format that the model must output. Compatible with
`gpt-4-1106-preview` and `gpt-3.5-turbo-1106`.
[GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and
`gpt-3.5-turbo-1106`.
Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the
message the model generates is valid JSON.
Expand Down Expand Up @@ -232,6 +235,8 @@ def create(
model: Union[
str,
Literal[
"gpt-4-0125-preview",
"gpt-4-turbo-preview",
"gpt-4-1106-preview",
"gpt-4-vision-preview",
"gpt-4",
Expand Down Expand Up @@ -345,7 +350,8 @@ def create(
[See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details)
response_format: An object specifying the format that the model must output. Compatible with
`gpt-4-1106-preview` and `gpt-3.5-turbo-1106`.
[GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and
`gpt-3.5-turbo-1106`.
Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the
message the model generates is valid JSON.
Expand Down Expand Up @@ -418,6 +424,8 @@ def create(
model: Union[
str,
Literal[
"gpt-4-0125-preview",
"gpt-4-turbo-preview",
"gpt-4-1106-preview",
"gpt-4-vision-preview",
"gpt-4",
Expand Down Expand Up @@ -531,7 +539,8 @@ def create(
[See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details)
response_format: An object specifying the format that the model must output. Compatible with
`gpt-4-1106-preview` and `gpt-3.5-turbo-1106`.
[GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and
`gpt-3.5-turbo-1106`.
Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the
message the model generates is valid JSON.
Expand Down Expand Up @@ -604,6 +613,8 @@ def create(
model: Union[
str,
Literal[
"gpt-4-0125-preview",
"gpt-4-turbo-preview",
"gpt-4-1106-preview",
"gpt-4-vision-preview",
"gpt-4",
Expand Down Expand Up @@ -698,6 +709,8 @@ async def create(
model: Union[
str,
Literal[
"gpt-4-0125-preview",
"gpt-4-turbo-preview",
"gpt-4-1106-preview",
"gpt-4-vision-preview",
"gpt-4",
Expand Down Expand Up @@ -804,7 +817,8 @@ async def create(
[See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details)
response_format: An object specifying the format that the model must output. Compatible with
`gpt-4-1106-preview` and `gpt-3.5-turbo-1106`.
[GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and
`gpt-3.5-turbo-1106`.
Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the
message the model generates is valid JSON.
Expand Down Expand Up @@ -884,6 +898,8 @@ async def create(
model: Union[
str,
Literal[
"gpt-4-0125-preview",
"gpt-4-turbo-preview",
"gpt-4-1106-preview",
"gpt-4-vision-preview",
"gpt-4",
Expand Down Expand Up @@ -997,7 +1013,8 @@ async def create(
[See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details)
response_format: An object specifying the format that the model must output. Compatible with
`gpt-4-1106-preview` and `gpt-3.5-turbo-1106`.
[GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and
`gpt-3.5-turbo-1106`.
Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the
message the model generates is valid JSON.
Expand Down Expand Up @@ -1070,6 +1087,8 @@ async def create(
model: Union[
str,
Literal[
"gpt-4-0125-preview",
"gpt-4-turbo-preview",
"gpt-4-1106-preview",
"gpt-4-vision-preview",
"gpt-4",
Expand Down Expand Up @@ -1183,7 +1202,8 @@ async def create(
[See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details)
response_format: An object specifying the format that the model must output. Compatible with
`gpt-4-1106-preview` and `gpt-3.5-turbo-1106`.
[GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and
`gpt-3.5-turbo-1106`.
Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the
message the model generates is valid JSON.
Expand Down Expand Up @@ -1256,6 +1276,8 @@ async def create(
model: Union[
str,
Literal[
"gpt-4-0125-preview",
"gpt-4-turbo-preview",
"gpt-4-1106-preview",
"gpt-4-vision-preview",
"gpt-4",
Expand Down
14 changes: 12 additions & 2 deletions src/openai/resources/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def create(
self,
*,
input: Union[str, List[str], List[int], List[List[int]]],
model: Union[str, Literal["text-embedding-ada-002"]],
model: Union[str, Literal["text-embedding-ada-002", "text-embedding-3-small", "text-embedding-3-large"]],
dimensions: int | NotGiven = NOT_GIVEN,
encoding_format: Literal["float", "base64"] | NotGiven = NOT_GIVEN,
user: str | NotGiven = NOT_GIVEN,
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
Expand Down Expand Up @@ -64,6 +65,9 @@ def create(
[Model overview](https://platform.openai.com/docs/models/overview) for
descriptions of them.
dimensions: The number of dimensions the resulting output embeddings should have. Only
supported in `text-embedding-3` and later models.
encoding_format: The format to return the embeddings in. Can be either `float` or
[`base64`](https://pypi.org/project/pybase64/).
Expand All @@ -83,6 +87,7 @@ def create(
"input": input,
"model": model,
"user": user,
"dimensions": dimensions,
"encoding_format": encoding_format,
}
if not is_given(encoding_format) and has_numpy():
Expand Down Expand Up @@ -132,7 +137,8 @@ async def create(
self,
*,
input: Union[str, List[str], List[int], List[List[int]]],
model: Union[str, Literal["text-embedding-ada-002"]],
model: Union[str, Literal["text-embedding-ada-002", "text-embedding-3-small", "text-embedding-3-large"]],
dimensions: int | NotGiven = NOT_GIVEN,
encoding_format: Literal["float", "base64"] | NotGiven = NOT_GIVEN,
user: str | NotGiven = NOT_GIVEN,
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
Expand Down Expand Up @@ -160,6 +166,9 @@ async def create(
[Model overview](https://platform.openai.com/docs/models/overview) for
descriptions of them.
dimensions: The number of dimensions the resulting output embeddings should have. Only
supported in `text-embedding-3` and later models.
encoding_format: The format to return the embeddings in. Can be either `float` or
[`base64`](https://pypi.org/project/pybase64/).
Expand All @@ -179,6 +188,7 @@ async def create(
"input": input,
"model": model,
"user": user,
"dimensions": dimensions,
"encoding_format": encoding_format,
}
if not is_given(encoding_format) and has_numpy():
Expand Down
6 changes: 5 additions & 1 deletion src/openai/types/chat/completion_create_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class CompletionCreateParamsBase(TypedDict, total=False):
Union[
str,
Literal[
"gpt-4-0125-preview",
"gpt-4-turbo-preview",
"gpt-4-1106-preview",
"gpt-4-vision-preview",
"gpt-4",
Expand Down Expand Up @@ -133,7 +135,9 @@ class CompletionCreateParamsBase(TypedDict, total=False):
response_format: ResponseFormat
"""An object specifying the format that the model must output.
Compatible with `gpt-4-1106-preview` and `gpt-3.5-turbo-1106`.
Compatible with
[GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and
`gpt-3.5-turbo-1106`.
Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the
message the model generates is valid JSON.
Expand Down
8 changes: 7 additions & 1 deletion src/openai/types/embedding_create_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class EmbeddingCreateParams(TypedDict, total=False):
for counting tokens.
"""

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

dimensions: int
"""The number of dimensions the resulting output embeddings should have.
Only supported in `text-embedding-3` and later models.
"""

encoding_format: Literal["float", "base64"]
"""The format to return the embeddings in.
Expand Down
18 changes: 10 additions & 8 deletions tests/api_resources/test_embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ class TestEmbeddings:
def test_method_create(self, client: OpenAI) -> None:
embedding = client.embeddings.create(
input="The quick brown fox jumped over the lazy dog",
model="text-embedding-ada-002",
model="text-embedding-3-small",
)
assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])

@parametrize
def test_method_create_with_all_params(self, client: OpenAI) -> None:
embedding = client.embeddings.create(
input="The quick brown fox jumped over the lazy dog",
model="text-embedding-ada-002",
model="text-embedding-3-small",
dimensions=1,
encoding_format="float",
user="user-1234",
)
Expand All @@ -39,7 +40,7 @@ def test_method_create_with_all_params(self, client: OpenAI) -> None:
def test_raw_response_create(self, client: OpenAI) -> None:
response = client.embeddings.with_raw_response.create(
input="The quick brown fox jumped over the lazy dog",
model="text-embedding-ada-002",
model="text-embedding-3-small",
)

assert response.is_closed is True
Expand All @@ -51,7 +52,7 @@ def test_raw_response_create(self, client: OpenAI) -> None:
def test_streaming_response_create(self, client: OpenAI) -> None:
with client.embeddings.with_streaming_response.create(
input="The quick brown fox jumped over the lazy dog",
model="text-embedding-ada-002",
model="text-embedding-3-small",
) as response:
assert not response.is_closed
assert response.http_request.headers.get("X-Stainless-Lang") == "python"
Expand All @@ -69,15 +70,16 @@ class TestAsyncEmbeddings:
async def test_method_create(self, async_client: AsyncOpenAI) -> None:
embedding = await async_client.embeddings.create(
input="The quick brown fox jumped over the lazy dog",
model="text-embedding-ada-002",
model="text-embedding-3-small",
)
assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])

@parametrize
async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
embedding = await async_client.embeddings.create(
input="The quick brown fox jumped over the lazy dog",
model="text-embedding-ada-002",
model="text-embedding-3-small",
dimensions=1,
encoding_format="float",
user="user-1234",
)
Expand All @@ -87,7 +89,7 @@ async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) ->
async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
response = await async_client.embeddings.with_raw_response.create(
input="The quick brown fox jumped over the lazy dog",
model="text-embedding-ada-002",
model="text-embedding-3-small",
)

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

0 comments on commit 20d97ef

Please sign in to comment.