openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/test_embeddings.py
83lines · modeblame
08b8179aDavid Schnurr2 years ago | 1 | # File generated from our OpenAPI spec by Stainless. |
| 2 | | |
| 3 | from __future__ import annotations | |
| 4 | | |
| 5 | import os | |
| 6 | | |
| 7 | import pytest | |
| 8 | | |
| 9 | from openai import OpenAI, AsyncOpenAI | |
| 10 | from tests.utils import assert_matches_type | |
| 11 | from openai.types import CreateEmbeddingResponse | |
| 12 | from openai._client import OpenAI, AsyncOpenAI | |
| 13 | | |
| 14 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") | |
| 15 | api_key = "My API Key" | |
| 16 | | |
| 17 | | |
| 18 | class TestEmbeddings: | |
| 19 | strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) | |
| 20 | loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) | |
| 21 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) | |
| 22 | | |
| 23 | @parametrize | |
| 24 | def test_method_create(self, client: OpenAI) -> None: | |
| 25 | embedding = client.embeddings.create( | |
| 26 | input="The quick brown fox jumped over the lazy dog", | |
| 27 | model="text-embedding-ada-002", | |
| 28 | ) | |
| 29 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 30 | | |
| 31 | @parametrize | |
| 32 | def test_method_create_with_all_params(self, client: OpenAI) -> None: | |
| 33 | embedding = client.embeddings.create( | |
| 34 | input="The quick brown fox jumped over the lazy dog", | |
| 35 | model="text-embedding-ada-002", | |
| 36 | encoding_format="float", | |
| 37 | user="user-1234", | |
| 38 | ) | |
| 39 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 40 | | |
| 41 | @parametrize | |
| 42 | def test_raw_response_create(self, client: OpenAI) -> None: | |
| 43 | response = client.embeddings.with_raw_response.create( | |
| 44 | input="The quick brown fox jumped over the lazy dog", | |
| 45 | model="text-embedding-ada-002", | |
| 46 | ) | |
| 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 | | |
| 52 | class TestAsyncEmbeddings: | |
| 53 | strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) | |
| 54 | loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) | |
| 55 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) | |
| 56 | | |
| 57 | @parametrize | |
| 58 | async def test_method_create(self, client: AsyncOpenAI) -> None: | |
| 59 | embedding = await client.embeddings.create( | |
| 60 | input="The quick brown fox jumped over the lazy dog", | |
| 61 | model="text-embedding-ada-002", | |
| 62 | ) | |
| 63 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 64 | | |
| 65 | @parametrize | |
| 66 | async def test_method_create_with_all_params(self, client: AsyncOpenAI) -> None: | |
| 67 | embedding = await client.embeddings.create( | |
| 68 | input="The quick brown fox jumped over the lazy dog", | |
| 69 | model="text-embedding-ada-002", | |
| 70 | encoding_format="float", | |
| 71 | user="user-1234", | |
| 72 | ) | |
| 73 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 74 | | |
| 75 | @parametrize | |
| 76 | async def test_raw_response_create(self, client: AsyncOpenAI) -> None: | |
| 77 | response = await client.embeddings.with_raw_response.create( | |
| 78 | input="The quick brown fox jumped over the lazy dog", | |
| 79 | model="text-embedding-ada-002", | |
| 80 | ) | |
| 81 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 82 | embedding = response.parse() | |
| 83 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) |