openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/test_embeddings.py
116lines · 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 | from openai._client import OpenAI, AsyncOpenAI | |
| 14 | | |
| 15 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") | |
| 16 | api_key = "My API Key" | |
| 17 | | |
| 18 | | |
| 19 | class TestEmbeddings: | |
| 20 | strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) | |
| 21 | loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) | |
| 22 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) | |
| 23 | | |
| 24 | @parametrize | |
| 25 | def test_method_create(self, client: OpenAI) -> None: | |
| 26 | embedding = client.embeddings.create( | |
| 27 | input="The quick brown fox jumped over the lazy dog", | |
| 28 | model="text-embedding-ada-002", | |
| 29 | ) | |
| 30 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 31 | | |
| 32 | @parametrize | |
| 33 | def test_method_create_with_all_params(self, client: OpenAI) -> None: | |
| 34 | embedding = client.embeddings.create( | |
| 35 | input="The quick brown fox jumped over the lazy dog", | |
| 36 | model="text-embedding-ada-002", | |
| 37 | encoding_format="float", | |
| 38 | user="user-1234", | |
| 39 | ) | |
| 40 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 41 | | |
| 42 | @parametrize | |
| 43 | def test_raw_response_create(self, client: OpenAI) -> None: | |
| 44 | response = client.embeddings.with_raw_response.create( | |
| 45 | input="The quick brown fox jumped over the lazy dog", | |
| 46 | model="text-embedding-ada-002", | |
| 47 | ) | |
86379b44Stainless Bot2 years ago | 48 | |
| 49 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 50 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 51 | embedding = response.parse() | |
| 52 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 53 | | |
86379b44Stainless Bot2 years ago | 54 | @parametrize |
| 55 | def test_streaming_response_create(self, client: OpenAI) -> None: | |
| 56 | with client.embeddings.with_streaming_response.create( | |
| 57 | input="The quick brown fox jumped over the lazy dog", | |
| 58 | model="text-embedding-ada-002", | |
| 59 | ) as response: | |
| 60 | assert not response.is_closed | |
| 61 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 62 | | |
| 63 | embedding = response.parse() | |
| 64 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 65 | | |
| 66 | assert cast(Any, response.is_closed) is True | |
| 67 | | |
08b8179aDavid Schnurr2 years ago | 68 | |
| 69 | class TestAsyncEmbeddings: | |
| 70 | strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) | |
| 71 | loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) | |
| 72 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) | |
| 73 | | |
| 74 | @parametrize | |
| 75 | async def test_method_create(self, client: AsyncOpenAI) -> None: | |
| 76 | embedding = await client.embeddings.create( | |
| 77 | input="The quick brown fox jumped over the lazy dog", | |
| 78 | model="text-embedding-ada-002", | |
| 79 | ) | |
| 80 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 81 | | |
| 82 | @parametrize | |
| 83 | async def test_method_create_with_all_params(self, client: AsyncOpenAI) -> None: | |
| 84 | embedding = await client.embeddings.create( | |
| 85 | input="The quick brown fox jumped over the lazy dog", | |
| 86 | model="text-embedding-ada-002", | |
| 87 | encoding_format="float", | |
| 88 | user="user-1234", | |
| 89 | ) | |
| 90 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 91 | | |
| 92 | @parametrize | |
| 93 | async def test_raw_response_create(self, client: AsyncOpenAI) -> None: | |
| 94 | response = await client.embeddings.with_raw_response.create( | |
| 95 | input="The quick brown fox jumped over the lazy dog", | |
| 96 | model="text-embedding-ada-002", | |
| 97 | ) | |
86379b44Stainless Bot2 years ago | 98 | |
| 99 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 100 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 101 | embedding = response.parse() | |
| 102 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
86379b44Stainless Bot2 years ago | 103 | |
| 104 | @parametrize | |
| 105 | async def test_streaming_response_create(self, client: AsyncOpenAI) -> None: | |
| 106 | async with client.embeddings.with_streaming_response.create( | |
| 107 | input="The quick brown fox jumped over the lazy dog", | |
| 108 | model="text-embedding-ada-002", | |
| 109 | ) as response: | |
| 110 | assert not response.is_closed | |
| 111 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 112 | | |
| 113 | embedding = await response.parse() | |
| 114 | assert_matches_type(CreateEmbeddingResponse, embedding, path=["response"]) | |
| 115 | | |
| 116 | assert cast(Any, response.is_closed) is True |