openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/test_models.py
203lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. |
| 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 Model, ModelDeleted |
| 13 | from openai._client import OpenAI, AsyncOpenAI |
| 14 | from openai.pagination import SyncPage, AsyncPage |
| 15 | |
| 16 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 17 | api_key = "My API Key" |
| 18 | |
| 19 | |
| 20 | class TestModels: |
| 21 | strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 22 | loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 23 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 24 | |
| 25 | @parametrize |
| 26 | def test_method_retrieve(self, client: OpenAI) -> None: |
| 27 | model = client.models.retrieve( |
| 28 | "gpt-3.5-turbo", |
| 29 | ) |
| 30 | assert_matches_type(Model, model, path=["response"]) |
| 31 | |
| 32 | @parametrize |
| 33 | def test_raw_response_retrieve(self, client: OpenAI) -> None: |
| 34 | response = client.models.with_raw_response.retrieve( |
| 35 | "gpt-3.5-turbo", |
| 36 | ) |
| 37 | |
| 38 | assert response.is_closed is True |
| 39 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 40 | model = response.parse() |
| 41 | assert_matches_type(Model, model, path=["response"]) |
| 42 | |
| 43 | @parametrize |
| 44 | def test_streaming_response_retrieve(self, client: OpenAI) -> None: |
| 45 | with client.models.with_streaming_response.retrieve( |
| 46 | "gpt-3.5-turbo", |
| 47 | ) as response: |
| 48 | assert not response.is_closed |
| 49 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 50 | |
| 51 | model = response.parse() |
| 52 | assert_matches_type(Model, model, path=["response"]) |
| 53 | |
| 54 | assert cast(Any, response.is_closed) is True |
| 55 | |
| 56 | @parametrize |
| 57 | def test_method_list(self, client: OpenAI) -> None: |
| 58 | model = client.models.list() |
| 59 | assert_matches_type(SyncPage[Model], model, path=["response"]) |
| 60 | |
| 61 | @parametrize |
| 62 | def test_raw_response_list(self, client: OpenAI) -> None: |
| 63 | response = client.models.with_raw_response.list() |
| 64 | |
| 65 | assert response.is_closed is True |
| 66 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 67 | model = response.parse() |
| 68 | assert_matches_type(SyncPage[Model], model, path=["response"]) |
| 69 | |
| 70 | @parametrize |
| 71 | def test_streaming_response_list(self, client: OpenAI) -> None: |
| 72 | with client.models.with_streaming_response.list() as response: |
| 73 | assert not response.is_closed |
| 74 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 75 | |
| 76 | model = response.parse() |
| 77 | assert_matches_type(SyncPage[Model], model, path=["response"]) |
| 78 | |
| 79 | assert cast(Any, response.is_closed) is True |
| 80 | |
| 81 | @parametrize |
| 82 | def test_method_delete(self, client: OpenAI) -> None: |
| 83 | model = client.models.delete( |
| 84 | "ft:gpt-3.5-turbo:acemeco:suffix:abc123", |
| 85 | ) |
| 86 | assert_matches_type(ModelDeleted, model, path=["response"]) |
| 87 | |
| 88 | @parametrize |
| 89 | def test_raw_response_delete(self, client: OpenAI) -> None: |
| 90 | response = client.models.with_raw_response.delete( |
| 91 | "ft:gpt-3.5-turbo:acemeco:suffix:abc123", |
| 92 | ) |
| 93 | |
| 94 | assert response.is_closed is True |
| 95 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 96 | model = response.parse() |
| 97 | assert_matches_type(ModelDeleted, model, path=["response"]) |
| 98 | |
| 99 | @parametrize |
| 100 | def test_streaming_response_delete(self, client: OpenAI) -> None: |
| 101 | with client.models.with_streaming_response.delete( |
| 102 | "ft:gpt-3.5-turbo:acemeco:suffix:abc123", |
| 103 | ) as response: |
| 104 | assert not response.is_closed |
| 105 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 106 | |
| 107 | model = response.parse() |
| 108 | assert_matches_type(ModelDeleted, model, path=["response"]) |
| 109 | |
| 110 | assert cast(Any, response.is_closed) is True |
| 111 | |
| 112 | |
| 113 | class TestAsyncModels: |
| 114 | strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 115 | loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 116 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 117 | |
| 118 | @parametrize |
| 119 | async def test_method_retrieve(self, client: AsyncOpenAI) -> None: |
| 120 | model = await client.models.retrieve( |
| 121 | "gpt-3.5-turbo", |
| 122 | ) |
| 123 | assert_matches_type(Model, model, path=["response"]) |
| 124 | |
| 125 | @parametrize |
| 126 | async def test_raw_response_retrieve(self, client: AsyncOpenAI) -> None: |
| 127 | response = await client.models.with_raw_response.retrieve( |
| 128 | "gpt-3.5-turbo", |
| 129 | ) |
| 130 | |
| 131 | assert response.is_closed is True |
| 132 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 133 | model = response.parse() |
| 134 | assert_matches_type(Model, model, path=["response"]) |
| 135 | |
| 136 | @parametrize |
| 137 | async def test_streaming_response_retrieve(self, client: AsyncOpenAI) -> None: |
| 138 | async with client.models.with_streaming_response.retrieve( |
| 139 | "gpt-3.5-turbo", |
| 140 | ) as response: |
| 141 | assert not response.is_closed |
| 142 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 143 | |
| 144 | model = await response.parse() |
| 145 | assert_matches_type(Model, model, path=["response"]) |
| 146 | |
| 147 | assert cast(Any, response.is_closed) is True |
| 148 | |
| 149 | @parametrize |
| 150 | async def test_method_list(self, client: AsyncOpenAI) -> None: |
| 151 | model = await client.models.list() |
| 152 | assert_matches_type(AsyncPage[Model], model, path=["response"]) |
| 153 | |
| 154 | @parametrize |
| 155 | async def test_raw_response_list(self, client: AsyncOpenAI) -> None: |
| 156 | response = await client.models.with_raw_response.list() |
| 157 | |
| 158 | assert response.is_closed is True |
| 159 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 160 | model = response.parse() |
| 161 | assert_matches_type(AsyncPage[Model], model, path=["response"]) |
| 162 | |
| 163 | @parametrize |
| 164 | async def test_streaming_response_list(self, client: AsyncOpenAI) -> None: |
| 165 | async with client.models.with_streaming_response.list() as response: |
| 166 | assert not response.is_closed |
| 167 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 168 | |
| 169 | model = await response.parse() |
| 170 | assert_matches_type(AsyncPage[Model], model, path=["response"]) |
| 171 | |
| 172 | assert cast(Any, response.is_closed) is True |
| 173 | |
| 174 | @parametrize |
| 175 | async def test_method_delete(self, client: AsyncOpenAI) -> None: |
| 176 | model = await client.models.delete( |
| 177 | "ft:gpt-3.5-turbo:acemeco:suffix:abc123", |
| 178 | ) |
| 179 | assert_matches_type(ModelDeleted, model, path=["response"]) |
| 180 | |
| 181 | @parametrize |
| 182 | async def test_raw_response_delete(self, client: AsyncOpenAI) -> None: |
| 183 | response = await client.models.with_raw_response.delete( |
| 184 | "ft:gpt-3.5-turbo:acemeco:suffix:abc123", |
| 185 | ) |
| 186 | |
| 187 | assert response.is_closed is True |
| 188 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 189 | model = response.parse() |
| 190 | assert_matches_type(ModelDeleted, model, path=["response"]) |
| 191 | |
| 192 | @parametrize |
| 193 | async def test_streaming_response_delete(self, client: AsyncOpenAI) -> None: |
| 194 | async with client.models.with_streaming_response.delete( |
| 195 | "ft:gpt-3.5-turbo:acemeco:suffix:abc123", |
| 196 | ) as response: |
| 197 | assert not response.is_closed |
| 198 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 199 | |
| 200 | model = await response.parse() |
| 201 | assert_matches_type(ModelDeleted, model, path=["response"]) |
| 202 | |
| 203 | assert cast(Any, response.is_closed) is True |
| 204 | |