openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/test_embeddings.py
110lines · modeblame
08b8179aDavid Schnurr2 years ago | 1 | # File generated from our OpenAPI spec by Stainless. |
| 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 | |
| 12 | from openai.types import CreateEmbeddingResponse | |
| 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", | |
| 24 | model="text-embedding-ada-002", | |
| 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", | |
| 32 | model="text-embedding-ada-002", | |
| 33 | encoding_format="float", | |
| 34 | user="user-1234", | |
| 35 | ) | |
| 36 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 37 | | |
| 38 | @parametrize | |
| 39 | def test_raw_response_create(self, client: OpenAI) -> None: | |
| 40 | response = client.embeddings.with_raw_response.create( | |
| 41 | input="The quick brown fox jumped over the lazy dog", | |
| 42 | model="text-embedding-ada-002", | |
| 43 | ) | |
86379b44Stainless Bot2 years ago | 44 | |
| 45 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 46 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 47 | embedding = response.parse() | |
| 48 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 49 | | |
86379b44Stainless Bot2 years ago | 50 | @parametrize |
| 51 | def test_streaming_response_create(self, client: OpenAI) -> None: | |
| 52 | with client.embeddings.with_streaming_response.create( | |
| 53 | input="The quick brown fox jumped over the lazy dog", | |
| 54 | model="text-embedding-ada-002", | |
| 55 | ) as response: | |
| 56 | assert not response.is_closed | |
| 57 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 58 | | |
| 59 | embedding = response.parse() | |
| 60 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 61 | | |
| 62 | assert cast(Any, response.is_closed) is True | |
| 63 | | |
08b8179aDavid Schnurr2 years ago | 64 | |
| 65 | class TestAsyncEmbeddings: | |
98d779fbStainless Bot2 years ago | 66 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
08b8179aDavid Schnurr2 years ago | 67 | |
| 68 | @parametrize | |
98d779fbStainless Bot2 years ago | 69 | async def test_method_create(self, async_client: AsyncOpenAI) -> None: |
| 70 | embedding = await async_client.embeddings.create( | |
08b8179aDavid Schnurr2 years ago | 71 | input="The quick brown fox jumped over the lazy dog", |
| 72 | model="text-embedding-ada-002", | |
| 73 | ) | |
| 74 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 75 | | |
| 76 | @parametrize | |
98d779fbStainless Bot2 years ago | 77 | async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 78 | embedding = await async_client.embeddings.create( | |
08b8179aDavid Schnurr2 years ago | 79 | input="The quick brown fox jumped over the lazy dog", |
| 80 | model="text-embedding-ada-002", | |
| 81 | encoding_format="float", | |
| 82 | user="user-1234", | |
| 83 | ) | |
| 84 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 85 | | |
| 86 | @parametrize | |
98d779fbStainless Bot2 years ago | 87 | async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None: |
| 88 | response = await async_client.embeddings.with_raw_response.create( | |
08b8179aDavid Schnurr2 years ago | 89 | input="The quick brown fox jumped over the lazy dog", |
| 90 | model="text-embedding-ada-002", | |
| 91 | ) | |
86379b44Stainless Bot2 years ago | 92 | |
| 93 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 94 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 95 | embedding = response.parse() | |
| 96 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
86379b44Stainless Bot2 years ago | 97 | |
| 98 | @parametrize | |
98d779fbStainless Bot2 years ago | 99 | async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None: |
| 100 | async with async_client.embeddings.with_streaming_response.create( | |
86379b44Stainless Bot2 years ago | 101 | input="The quick brown fox jumped over the lazy dog", |
| 102 | model="text-embedding-ada-002", | |
| 103 | ) as response: | |
| 104 | assert not response.is_closed | |
| 105 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 106 | | |
| 107 | embedding = await response.parse() | |
| 108 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 109 | | |
| 110 | assert cast(Any, response.is_closed) is True |