main
  1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2
  3from __future__ import annotations
  4
  5import os
  6from typing import Any, cast
  7
  8import pytest
  9
 10from openai import OpenAI, AsyncOpenAI
 11from tests.utils import assert_matches_type
 12from openai.types import CreateEmbeddingResponse
 13
 14base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
 15
 16
 17class 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
 66class TestAsyncEmbeddings:
 67    parametrize = pytest.mark.parametrize(
 68        "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
 69    )
 70
 71    @parametrize
 72    async def test_method_create(self, async_client: AsyncOpenAI) -> None:
 73        embedding = await async_client.embeddings.create(
 74            input="The quick brown fox jumped over the lazy dog",
 75            model="text-embedding-3-small",
 76        )
 77        assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
 78
 79    @parametrize
 80    async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
 81        embedding = await async_client.embeddings.create(
 82            input="The quick brown fox jumped over the lazy dog",
 83            model="text-embedding-3-small",
 84            dimensions=1,
 85            encoding_format="float",
 86            user="user-1234",
 87        )
 88        assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
 89
 90    @parametrize
 91    async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
 92        response = await async_client.embeddings.with_raw_response.create(
 93            input="The quick brown fox jumped over the lazy dog",
 94            model="text-embedding-3-small",
 95        )
 96
 97        assert response.is_closed is True
 98        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
 99        embedding = response.parse()
100        assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
101
102    @parametrize
103    async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
104        async with async_client.embeddings.with_streaming_response.create(
105            input="The quick brown fox jumped over the lazy dog",
106            model="text-embedding-3-small",
107        ) as response:
108            assert not response.is_closed
109            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
110
111            embedding = await response.parse()
112            assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"])
113
114        assert cast(Any, response.is_closed) is True