openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/test_embeddings.py
112lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import os |
| 6 | from typing import Any, cast |
| 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: |
| 18 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 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-3-small", |
| 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-3-small", |
| 33 | dimensions=1, |
| 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", |
| 43 | model="text-embedding-3-small", |
| 44 | ) |
| 45 | |
| 46 | assert response.is_closed is True |
| 47 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 48 | embedding = response.parse() |
| 49 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) |
| 50 | |
| 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", |
| 55 | model="text-embedding-3-small", |
| 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 | |
| 65 | |
| 66 | class TestAsyncEmbeddings: |
| 67 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 68 | |
| 69 | @parametrize |
| 70 | async def test_method_create(self, async_client: AsyncOpenAI) -> None: |
| 71 | embedding = await async_client.embeddings.create( |
| 72 | input="The quick brown fox jumped over the lazy dog", |
| 73 | model="text-embedding-3-small", |
| 74 | ) |
| 75 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) |
| 76 | |
| 77 | @parametrize |
| 78 | async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 79 | embedding = await async_client.embeddings.create( |
| 80 | input="The quick brown fox jumped over the lazy dog", |
| 81 | model="text-embedding-3-small", |
| 82 | dimensions=1, |
| 83 | encoding_format="float", |
| 84 | user="user-1234", |
| 85 | ) |
| 86 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) |
| 87 | |
| 88 | @parametrize |
| 89 | async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None: |
| 90 | response = await async_client.embeddings.with_raw_response.create( |
| 91 | input="The quick brown fox jumped over the lazy dog", |
| 92 | model="text-embedding-3-small", |
| 93 | ) |
| 94 | |
| 95 | assert response.is_closed is True |
| 96 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 97 | embedding = response.parse() |
| 98 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) |
| 99 | |
| 100 | @parametrize |
| 101 | async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None: |
| 102 | async with async_client.embeddings.with_streaming_response.create( |
| 103 | input="The quick brown fox jumped over the lazy dog", |
| 104 | model="text-embedding-3-small", |
| 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 |
| 113 | |