openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/test_completions.py
254lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 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 Completion |
| 13 | |
| 14 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 15 | |
| 16 | |
| 17 | class TestCompletions: |
| 18 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 19 | |
| 20 | @parametrize |
| 21 | def test_method_create_overload_1(self, client: OpenAI) -> None: |
| 22 | completion = client.completions.create( |
| 23 | model="string", |
| 24 | prompt="This is a test.", |
| 25 | ) |
| 26 | assert_matches_type(Completion, completion, path=["response"]) |
| 27 | |
| 28 | @parametrize |
| 29 | def test_method_create_with_all_params_overload_1(self, client: OpenAI) -> None: |
| 30 | completion = client.completions.create( |
| 31 | model="string", |
| 32 | prompt="This is a test.", |
| 33 | best_of=0, |
| 34 | echo=True, |
| 35 | frequency_penalty=-2, |
| 36 | logit_bias={"foo": 0}, |
| 37 | logprobs=0, |
| 38 | max_tokens=16, |
| 39 | n=1, |
| 40 | presence_penalty=-2, |
| 41 | seed=-9223372036854776000, |
| 42 | stop="\n", |
| 43 | stream=False, |
| 44 | suffix="test.", |
| 45 | temperature=1, |
| 46 | top_p=1, |
| 47 | user="user-1234", |
| 48 | ) |
| 49 | assert_matches_type(Completion, completion, path=["response"]) |
| 50 | |
| 51 | @parametrize |
| 52 | def test_raw_response_create_overload_1(self, client: OpenAI) -> None: |
| 53 | response = client.completions.with_raw_response.create( |
| 54 | model="string", |
| 55 | prompt="This is a test.", |
| 56 | ) |
| 57 | |
| 58 | assert response.is_closed is True |
| 59 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 60 | completion = response.parse() |
| 61 | assert_matches_type(Completion, completion, path=["response"]) |
| 62 | |
| 63 | @parametrize |
| 64 | def test_streaming_response_create_overload_1(self, client: OpenAI) -> None: |
| 65 | with client.completions.with_streaming_response.create( |
| 66 | model="string", |
| 67 | prompt="This is a test.", |
| 68 | ) as response: |
| 69 | assert not response.is_closed |
| 70 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 71 | |
| 72 | completion = response.parse() |
| 73 | assert_matches_type(Completion, completion, path=["response"]) |
| 74 | |
| 75 | assert cast(Any, response.is_closed) is True |
| 76 | |
| 77 | @parametrize |
| 78 | def test_method_create_overload_2(self, client: OpenAI) -> None: |
| 79 | completion_stream = client.completions.create( |
| 80 | model="string", |
| 81 | prompt="This is a test.", |
| 82 | stream=True, |
| 83 | ) |
| 84 | completion_stream.response.close() |
| 85 | |
| 86 | @parametrize |
| 87 | def test_method_create_with_all_params_overload_2(self, client: OpenAI) -> None: |
| 88 | completion_stream = client.completions.create( |
| 89 | model="string", |
| 90 | prompt="This is a test.", |
| 91 | stream=True, |
| 92 | best_of=0, |
| 93 | echo=True, |
| 94 | frequency_penalty=-2, |
| 95 | logit_bias={"foo": 0}, |
| 96 | logprobs=0, |
| 97 | max_tokens=16, |
| 98 | n=1, |
| 99 | presence_penalty=-2, |
| 100 | seed=-9223372036854776000, |
| 101 | stop="\n", |
| 102 | suffix="test.", |
| 103 | temperature=1, |
| 104 | top_p=1, |
| 105 | user="user-1234", |
| 106 | ) |
| 107 | completion_stream.response.close() |
| 108 | |
| 109 | @parametrize |
| 110 | def test_raw_response_create_overload_2(self, client: OpenAI) -> None: |
| 111 | response = client.completions.with_raw_response.create( |
| 112 | model="string", |
| 113 | prompt="This is a test.", |
| 114 | stream=True, |
| 115 | ) |
| 116 | |
| 117 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 118 | stream = response.parse() |
| 119 | stream.close() |
| 120 | |
| 121 | @parametrize |
| 122 | def test_streaming_response_create_overload_2(self, client: OpenAI) -> None: |
| 123 | with client.completions.with_streaming_response.create( |
| 124 | model="string", |
| 125 | prompt="This is a test.", |
| 126 | stream=True, |
| 127 | ) as response: |
| 128 | assert not response.is_closed |
| 129 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 130 | |
| 131 | stream = response.parse() |
| 132 | stream.close() |
| 133 | |
| 134 | assert cast(Any, response.is_closed) is True |
| 135 | |
| 136 | |
| 137 | class TestAsyncCompletions: |
| 138 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 139 | |
| 140 | @parametrize |
| 141 | async def test_method_create_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 142 | completion = await async_client.completions.create( |
| 143 | model="string", |
| 144 | prompt="This is a test.", |
| 145 | ) |
| 146 | assert_matches_type(Completion, completion, path=["response"]) |
| 147 | |
| 148 | @parametrize |
| 149 | async def test_method_create_with_all_params_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 150 | completion = await async_client.completions.create( |
| 151 | model="string", |
| 152 | prompt="This is a test.", |
| 153 | best_of=0, |
| 154 | echo=True, |
| 155 | frequency_penalty=-2, |
| 156 | logit_bias={"foo": 0}, |
| 157 | logprobs=0, |
| 158 | max_tokens=16, |
| 159 | n=1, |
| 160 | presence_penalty=-2, |
| 161 | seed=-9223372036854776000, |
| 162 | stop="\n", |
| 163 | stream=False, |
| 164 | suffix="test.", |
| 165 | temperature=1, |
| 166 | top_p=1, |
| 167 | user="user-1234", |
| 168 | ) |
| 169 | assert_matches_type(Completion, completion, path=["response"]) |
| 170 | |
| 171 | @parametrize |
| 172 | async def test_raw_response_create_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 173 | response = await async_client.completions.with_raw_response.create( |
| 174 | model="string", |
| 175 | prompt="This is a test.", |
| 176 | ) |
| 177 | |
| 178 | assert response.is_closed is True |
| 179 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 180 | completion = response.parse() |
| 181 | assert_matches_type(Completion, completion, path=["response"]) |
| 182 | |
| 183 | @parametrize |
| 184 | async def test_streaming_response_create_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 185 | async with async_client.completions.with_streaming_response.create( |
| 186 | model="string", |
| 187 | prompt="This is a test.", |
| 188 | ) as response: |
| 189 | assert not response.is_closed |
| 190 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 191 | |
| 192 | completion = await response.parse() |
| 193 | assert_matches_type(Completion, completion, path=["response"]) |
| 194 | |
| 195 | assert cast(Any, response.is_closed) is True |
| 196 | |
| 197 | @parametrize |
| 198 | async def test_method_create_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 199 | completion_stream = await async_client.completions.create( |
| 200 | model="string", |
| 201 | prompt="This is a test.", |
| 202 | stream=True, |
| 203 | ) |
| 204 | await completion_stream.response.aclose() |
| 205 | |
| 206 | @parametrize |
| 207 | async def test_method_create_with_all_params_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 208 | completion_stream = await async_client.completions.create( |
| 209 | model="string", |
| 210 | prompt="This is a test.", |
| 211 | stream=True, |
| 212 | best_of=0, |
| 213 | echo=True, |
| 214 | frequency_penalty=-2, |
| 215 | logit_bias={"foo": 0}, |
| 216 | logprobs=0, |
| 217 | max_tokens=16, |
| 218 | n=1, |
| 219 | presence_penalty=-2, |
| 220 | seed=-9223372036854776000, |
| 221 | stop="\n", |
| 222 | suffix="test.", |
| 223 | temperature=1, |
| 224 | top_p=1, |
| 225 | user="user-1234", |
| 226 | ) |
| 227 | await completion_stream.response.aclose() |
| 228 | |
| 229 | @parametrize |
| 230 | async def test_raw_response_create_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 231 | response = await async_client.completions.with_raw_response.create( |
| 232 | model="string", |
| 233 | prompt="This is a test.", |
| 234 | stream=True, |
| 235 | ) |
| 236 | |
| 237 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 238 | stream = response.parse() |
| 239 | await stream.close() |
| 240 | |
| 241 | @parametrize |
| 242 | async def test_streaming_response_create_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 243 | async with async_client.completions.with_streaming_response.create( |
| 244 | model="string", |
| 245 | prompt="This is a test.", |
| 246 | stream=True, |
| 247 | ) as response: |
| 248 | assert not response.is_closed |
| 249 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 250 | |
| 251 | stream = await response.parse() |
| 252 | await stream.close() |
| 253 | |
| 254 | assert cast(Any, response.is_closed) is True |
| 255 | |