openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/test_embeddings.py
112lines · modeblame
5cfb125aStainless Bot2 years ago | 1 | # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
08b8179aDavid Schnurr2 years ago | 2 | |
| 3 | from __future__ import annotations | |
| 4 | | |
| 5 | import os | |
86379b44Stainless Bot2 years ago | 6 | from typing import Any, cast |
08b8179aDavid Schnurr2 years ago | 7 | |
| 8 | import pytest | |
| 9 | | |
| 10 | from openai import OpenAI, AsyncOpenAI | |
| 11 | from tests.utils import assert_matches_type | |
4a0f0fa0Stainless Bot2 years ago | 12 | from openai.types import CreateEmbeddingResponse |
08b8179aDavid Schnurr2 years ago | 13 | |
| 14 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") | |
| 15 | | |
| 16 | | |
| 17 | class TestEmbeddings: | |
98d779fbStainless Bot2 years ago | 18 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
08b8179aDavid Schnurr2 years ago | 19 | |
| 20 | @parametrize | |
| 21 | def test_method_create(self, client: OpenAI) -> None: | |
| 22 | embedding = client.embeddings.create( | |
| 23 | input="The quick brown fox jumped over the lazy dog", | |
20d97ef0Stainless Bot2 years ago | 24 | model="text-embedding-3-small", |
08b8179aDavid Schnurr2 years ago | 25 | ) |
| 26 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 27 | | |
| 28 | @parametrize | |
| 29 | def test_method_create_with_all_params(self, client: OpenAI) -> None: | |
| 30 | embedding = client.embeddings.create( | |
| 31 | input="The quick brown fox jumped over the lazy dog", | |
20d97ef0Stainless Bot2 years ago | 32 | model="text-embedding-3-small", |
| 33 | dimensions=1, | |
08b8179aDavid Schnurr2 years ago | 34 | encoding_format="float", |
| 35 | user="user-1234", | |
| 36 | ) | |
| 37 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 38 | | |
| 39 | @parametrize | |
| 40 | def test_raw_response_create(self, client: OpenAI) -> None: | |
| 41 | response = client.embeddings.with_raw_response.create( | |
| 42 | input="The quick brown fox jumped over the lazy dog", | |
20d97ef0Stainless Bot2 years ago | 43 | model="text-embedding-3-small", |
08b8179aDavid Schnurr2 years ago | 44 | ) |
86379b44Stainless Bot2 years ago | 45 | |
| 46 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 47 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 48 | embedding = response.parse() | |
| 49 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 50 | | |
86379b44Stainless Bot2 years ago | 51 | @parametrize |
| 52 | def test_streaming_response_create(self, client: OpenAI) -> None: | |
| 53 | with client.embeddings.with_streaming_response.create( | |
| 54 | input="The quick brown fox jumped over the lazy dog", | |
20d97ef0Stainless Bot2 years ago | 55 | model="text-embedding-3-small", |
86379b44Stainless Bot2 years ago | 56 | ) as response: |
| 57 | assert not response.is_closed | |
| 58 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 59 | | |
| 60 | embedding = response.parse() | |
| 61 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 62 | | |
| 63 | assert cast(Any, response.is_closed) is True | |
| 64 | | |
08b8179aDavid Schnurr2 years ago | 65 | |
| 66 | class TestAsyncEmbeddings: | |
98d779fbStainless Bot2 years ago | 67 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
08b8179aDavid Schnurr2 years ago | 68 | |
| 69 | @parametrize | |
98d779fbStainless Bot2 years ago | 70 | async def test_method_create(self, async_client: AsyncOpenAI) -> None: |
| 71 | embedding = await async_client.embeddings.create( | |
08b8179aDavid Schnurr2 years ago | 72 | input="The quick brown fox jumped over the lazy dog", |
20d97ef0Stainless Bot2 years ago | 73 | model="text-embedding-3-small", |
08b8179aDavid Schnurr2 years ago | 74 | ) |
| 75 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 76 | | |
| 77 | @parametrize | |
98d779fbStainless Bot2 years ago | 78 | async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 79 | embedding = await async_client.embeddings.create( | |
08b8179aDavid Schnurr2 years ago | 80 | input="The quick brown fox jumped over the lazy dog", |
20d97ef0Stainless Bot2 years ago | 81 | model="text-embedding-3-small", |
| 82 | dimensions=1, | |
08b8179aDavid Schnurr2 years ago | 83 | encoding_format="float", |
| 84 | user="user-1234", | |
| 85 | ) | |
| 86 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 87 | | |
| 88 | @parametrize | |
98d779fbStainless Bot2 years ago | 89 | async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None: |
| 90 | response = await async_client.embeddings.with_raw_response.create( | |
08b8179aDavid Schnurr2 years ago | 91 | input="The quick brown fox jumped over the lazy dog", |
20d97ef0Stainless Bot2 years ago | 92 | model="text-embedding-3-small", |
08b8179aDavid Schnurr2 years ago | 93 | ) |
86379b44Stainless Bot2 years ago | 94 | |
| 95 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 96 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 97 | embedding = response.parse() | |
| 98 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
86379b44Stainless Bot2 years ago | 99 | |
| 100 | @parametrize | |
98d779fbStainless Bot2 years ago | 101 | async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None: |
| 102 | async with async_client.embeddings.with_streaming_response.create( | |
86379b44Stainless Bot2 years ago | 103 | input="The quick brown fox jumped over the lazy dog", |
20d97ef0Stainless Bot2 years ago | 104 | model="text-embedding-3-small", |
86379b44Stainless Bot2 years ago | 105 | ) as response: |
| 106 | assert not response.is_closed | |
| 107 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 108 | | |
| 109 | embedding = await response.parse() | |
| 110 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 111 | | |
| 112 | assert cast(Any, response.is_closed) is True |