openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/test_models.py
116lines · modecode
| 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 Model, ModelDeleted |
| 12 | from openai._client import OpenAI, AsyncOpenAI |
| 13 | from openai.pagination import SyncPage, AsyncPage |
| 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 TestModels: |
| 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_retrieve(self, client: OpenAI) -> None: |
| 26 | model = client.models.retrieve( |
| 27 | "gpt-3.5-turbo", |
| 28 | ) |
| 29 | assert_matches_type(Model, model, path=["response"]) |
| 30 | |
| 31 | @parametrize |
| 32 | def test_raw_response_retrieve(self, client: OpenAI) -> None: |
| 33 | response = client.models.with_raw_response.retrieve( |
| 34 | "gpt-3.5-turbo", |
| 35 | ) |
| 36 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 37 | model = response.parse() |
| 38 | assert_matches_type(Model, model, path=["response"]) |
| 39 | |
| 40 | @parametrize |
| 41 | def test_method_list(self, client: OpenAI) -> None: |
| 42 | model = client.models.list() |
| 43 | assert_matches_type(SyncPage[Model], model, path=["response"]) |
| 44 | |
| 45 | @parametrize |
| 46 | def test_raw_response_list(self, client: OpenAI) -> None: |
| 47 | response = client.models.with_raw_response.list() |
| 48 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 49 | model = response.parse() |
| 50 | assert_matches_type(SyncPage[Model], model, path=["response"]) |
| 51 | |
| 52 | @parametrize |
| 53 | def test_method_delete(self, client: OpenAI) -> None: |
| 54 | model = client.models.delete( |
| 55 | "ft:gpt-3.5-turbo:acemeco:suffix:abc123", |
| 56 | ) |
| 57 | assert_matches_type(ModelDeleted, model, path=["response"]) |
| 58 | |
| 59 | @parametrize |
| 60 | def test_raw_response_delete(self, client: OpenAI) -> None: |
| 61 | response = client.models.with_raw_response.delete( |
| 62 | "ft:gpt-3.5-turbo:acemeco:suffix:abc123", |
| 63 | ) |
| 64 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 65 | model = response.parse() |
| 66 | assert_matches_type(ModelDeleted, model, path=["response"]) |
| 67 | |
| 68 | |
| 69 | class TestAsyncModels: |
| 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_retrieve(self, client: AsyncOpenAI) -> None: |
| 76 | model = await client.models.retrieve( |
| 77 | "gpt-3.5-turbo", |
| 78 | ) |
| 79 | assert_matches_type(Model, model, path=["response"]) |
| 80 | |
| 81 | @parametrize |
| 82 | async def test_raw_response_retrieve(self, client: AsyncOpenAI) -> None: |
| 83 | response = await client.models.with_raw_response.retrieve( |
| 84 | "gpt-3.5-turbo", |
| 85 | ) |
| 86 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 87 | model = response.parse() |
| 88 | assert_matches_type(Model, model, path=["response"]) |
| 89 | |
| 90 | @parametrize |
| 91 | async def test_method_list(self, client: AsyncOpenAI) -> None: |
| 92 | model = await client.models.list() |
| 93 | assert_matches_type(AsyncPage[Model], model, path=["response"]) |
| 94 | |
| 95 | @parametrize |
| 96 | async def test_raw_response_list(self, client: AsyncOpenAI) -> None: |
| 97 | response = await client.models.with_raw_response.list() |
| 98 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 99 | model = response.parse() |
| 100 | assert_matches_type(AsyncPage[Model], model, path=["response"]) |
| 101 | |
| 102 | @parametrize |
| 103 | async def test_method_delete(self, client: AsyncOpenAI) -> None: |
| 104 | model = await client.models.delete( |
| 105 | "ft:gpt-3.5-turbo:acemeco:suffix:abc123", |
| 106 | ) |
| 107 | assert_matches_type(ModelDeleted, model, path=["response"]) |
| 108 | |
| 109 | @parametrize |
| 110 | async def test_raw_response_delete(self, client: AsyncOpenAI) -> None: |
| 111 | response = await client.models.with_raw_response.delete( |
| 112 | "ft:gpt-3.5-turbo:acemeco:suffix:abc123", |
| 113 | ) |
| 114 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 115 | model = response.parse() |
| 116 | assert_matches_type(ModelDeleted, model, path=["response"]) |