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