openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/test_completions.py
189lines · 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 Completion |
| 12 | from openai._client import OpenAI, AsyncOpenAI |
| 13 | |
| 14 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 15 | api_key = "My API Key" |
| 16 | |
| 17 | |
| 18 | class TestCompletions: |
| 19 | strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 20 | loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 21 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 22 | |
| 23 | @parametrize |
| 24 | def test_method_create_overload_1(self, client: OpenAI) -> None: |
| 25 | completion = client.completions.create( |
| 26 | model="string", |
| 27 | prompt="This is a test.", |
| 28 | ) |
| 29 | assert_matches_type(Completion, completion, path=["response"]) |
| 30 | |
| 31 | @parametrize |
| 32 | def test_method_create_with_all_params_overload_1(self, client: OpenAI) -> None: |
| 33 | completion = client.completions.create( |
| 34 | model="string", |
| 35 | prompt="This is a test.", |
| 36 | best_of=0, |
| 37 | echo=True, |
| 38 | frequency_penalty=-2, |
| 39 | logit_bias={"foo": 0}, |
| 40 | logprobs=0, |
| 41 | max_tokens=16, |
| 42 | n=1, |
| 43 | presence_penalty=-2, |
| 44 | seed=-9223372036854776000, |
| 45 | stop="\n", |
| 46 | stream=False, |
| 47 | suffix="test.", |
| 48 | temperature=1, |
| 49 | top_p=1, |
| 50 | user="user-1234", |
| 51 | ) |
| 52 | assert_matches_type(Completion, completion, path=["response"]) |
| 53 | |
| 54 | @parametrize |
| 55 | def test_raw_response_create_overload_1(self, client: OpenAI) -> None: |
| 56 | response = client.completions.with_raw_response.create( |
| 57 | model="string", |
| 58 | prompt="This is a test.", |
| 59 | ) |
| 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_method_create_overload_2(self, client: OpenAI) -> None: |
| 66 | client.completions.create( |
| 67 | model="string", |
| 68 | prompt="This is a test.", |
| 69 | stream=True, |
| 70 | ) |
| 71 | |
| 72 | @parametrize |
| 73 | def test_method_create_with_all_params_overload_2(self, client: OpenAI) -> None: |
| 74 | client.completions.create( |
| 75 | model="string", |
| 76 | prompt="This is a test.", |
| 77 | stream=True, |
| 78 | best_of=0, |
| 79 | echo=True, |
| 80 | frequency_penalty=-2, |
| 81 | logit_bias={"foo": 0}, |
| 82 | logprobs=0, |
| 83 | max_tokens=16, |
| 84 | n=1, |
| 85 | presence_penalty=-2, |
| 86 | seed=-9223372036854776000, |
| 87 | stop="\n", |
| 88 | suffix="test.", |
| 89 | temperature=1, |
| 90 | top_p=1, |
| 91 | user="user-1234", |
| 92 | ) |
| 93 | |
| 94 | @parametrize |
| 95 | def test_raw_response_create_overload_2(self, client: OpenAI) -> None: |
| 96 | response = client.completions.with_raw_response.create( |
| 97 | model="string", |
| 98 | prompt="This is a test.", |
| 99 | stream=True, |
| 100 | ) |
| 101 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 102 | response.parse() |
| 103 | |
| 104 | |
| 105 | class TestAsyncCompletions: |
| 106 | strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 107 | loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 108 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 109 | |
| 110 | @parametrize |
| 111 | async def test_method_create_overload_1(self, client: AsyncOpenAI) -> None: |
| 112 | completion = await client.completions.create( |
| 113 | model="string", |
| 114 | prompt="This is a test.", |
| 115 | ) |
| 116 | assert_matches_type(Completion, completion, path=["response"]) |
| 117 | |
| 118 | @parametrize |
| 119 | async def test_method_create_with_all_params_overload_1(self, client: AsyncOpenAI) -> None: |
| 120 | completion = await client.completions.create( |
| 121 | model="string", |
| 122 | prompt="This is a test.", |
| 123 | best_of=0, |
| 124 | echo=True, |
| 125 | frequency_penalty=-2, |
| 126 | logit_bias={"foo": 0}, |
| 127 | logprobs=0, |
| 128 | max_tokens=16, |
| 129 | n=1, |
| 130 | presence_penalty=-2, |
| 131 | seed=-9223372036854776000, |
| 132 | stop="\n", |
| 133 | stream=False, |
| 134 | suffix="test.", |
| 135 | temperature=1, |
| 136 | top_p=1, |
| 137 | user="user-1234", |
| 138 | ) |
| 139 | assert_matches_type(Completion, completion, path=["response"]) |
| 140 | |
| 141 | @parametrize |
| 142 | async def test_raw_response_create_overload_1(self, client: AsyncOpenAI) -> None: |
| 143 | response = await client.completions.with_raw_response.create( |
| 144 | model="string", |
| 145 | prompt="This is a test.", |
| 146 | ) |
| 147 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 148 | completion = response.parse() |
| 149 | assert_matches_type(Completion, completion, path=["response"]) |
| 150 | |
| 151 | @parametrize |
| 152 | async def test_method_create_overload_2(self, client: AsyncOpenAI) -> None: |
| 153 | await client.completions.create( |
| 154 | model="string", |
| 155 | prompt="This is a test.", |
| 156 | stream=True, |
| 157 | ) |
| 158 | |
| 159 | @parametrize |
| 160 | async def test_method_create_with_all_params_overload_2(self, client: AsyncOpenAI) -> None: |
| 161 | await client.completions.create( |
| 162 | model="string", |
| 163 | prompt="This is a test.", |
| 164 | stream=True, |
| 165 | best_of=0, |
| 166 | echo=True, |
| 167 | frequency_penalty=-2, |
| 168 | logit_bias={"foo": 0}, |
| 169 | logprobs=0, |
| 170 | max_tokens=16, |
| 171 | n=1, |
| 172 | presence_penalty=-2, |
| 173 | seed=-9223372036854776000, |
| 174 | stop="\n", |
| 175 | suffix="test.", |
| 176 | temperature=1, |
| 177 | top_p=1, |
| 178 | user="user-1234", |
| 179 | ) |
| 180 | |
| 181 | @parametrize |
| 182 | async def test_raw_response_create_overload_2(self, client: AsyncOpenAI) -> None: |
| 183 | response = await client.completions.with_raw_response.create( |
| 184 | model="string", |
| 185 | prompt="This is a test.", |
| 186 | stream=True, |
| 187 | ) |
| 188 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 189 | response.parse() |