1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import os
import pytest
import openai
import httpx
from devtools_testutils import AzureRecordedTestCase, get_credential
from azure.identity import get_bearer_token_provider
from conftest import (
AZURE,
ENV_AZURE_OPENAI_ENDPOINT,
ENV_AZURE_OPENAI_KEY,
LATEST,
ENV_AZURE_OPENAI_CHAT_COMPLETIONS_NAME,
configure,
reload,
)
@pytest.mark.live_test_only
class TestClient(AzureRecordedTestCase):
"""Azure AD with token provider is missing here because it is tested per feature"""
@configure
@pytest.mark.parametrize("api_type, api_version", [(AZURE, LATEST)])
def test_chat_completion_bad_deployment(self, client, api_type, api_version, **kwargs):
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"}
]
with pytest.raises(openai.NotFoundError) as e:
client.chat.completions.create(messages=messages, model="bad_deployment")
assert e.value.status_code == 404
assert "The API deployment for this resource does not exist" in e.value.message
@configure
@pytest.mark.parametrize("api_type, api_version", [(AZURE, LATEST)])
def test_chat_completion_endpoint_deployment(self, client, api_type, api_version, **kwargs):
client = openai.AzureOpenAI(
azure_endpoint=os.getenv(ENV_AZURE_OPENAI_ENDPOINT),
azure_deployment=ENV_AZURE_OPENAI_CHAT_COMPLETIONS_NAME,
azure_ad_token_provider=get_bearer_token_provider(get_credential(), "https://cognitiveservices.azure.com/.default"),
api_version=LATEST,
)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"}
]
completion = client.chat.completions.create(messages=messages, model="placeholder")
assert completion.id
assert completion.object == "chat.completion"
assert completion.model
assert completion.created
assert completion.usage.completion_tokens is not None
assert completion.usage.prompt_tokens is not None
assert completion.usage.total_tokens == completion.usage.completion_tokens + completion.usage.prompt_tokens
assert len(completion.choices) == 1
assert completion.choices[0].finish_reason
assert completion.choices[0].index is not None
assert completion.choices[0].message.content is not None
assert completion.choices[0].message.role
# try to call some other feature not under the same deployment name
with pytest.raises(openai.BadRequestError) as e:
client.embeddings.create(input=["Hello world!"], model="placeholder")
assert e.value.status_code == 400
assert "The embeddings operation does not work with the specified model, " \
f"{ENV_AZURE_OPENAI_CHAT_COMPLETIONS_NAME}. Please choose different model and try again" in e.value.message
@configure
@pytest.mark.parametrize("api_type, api_version", [(AZURE, LATEST)])
def test_deployment_with_nondeployment_api(self, client, api_type, api_version, **kwargs):
client = openai.AzureOpenAI(
azure_endpoint=os.getenv(ENV_AZURE_OPENAI_ENDPOINT),
azure_deployment=ENV_AZURE_OPENAI_CHAT_COMPLETIONS_NAME,
azure_ad_token_provider=get_bearer_token_provider(get_credential(), "https://cognitiveservices.azure.com/.default"),
api_version=LATEST,
)
model = client.models.retrieve(**kwargs)
assert model
@configure
@pytest.mark.parametrize("api_type, api_version", [(AZURE, LATEST)])
def test_chat_completion_base_url(self, client, api_type, api_version, **kwargs):
client = openai.AzureOpenAI(
base_url=f"{os.getenv(ENV_AZURE_OPENAI_ENDPOINT)}/openai/deployments/{ENV_AZURE_OPENAI_CHAT_COMPLETIONS_NAME}",
azure_ad_token_provider=get_bearer_token_provider(get_credential(), "https://cognitiveservices.azure.com/.default"),
api_version=LATEST,
)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"}
]
completion = client.chat.completions.create(messages=messages, **kwargs)
assert completion.id
assert completion.object == "chat.completion"
assert completion.model
assert completion.created
assert completion.usage.completion_tokens is not None
assert completion.usage.prompt_tokens is not None
assert completion.usage.total_tokens == completion.usage.completion_tokens + completion.usage.prompt_tokens
assert len(completion.choices) == 1
assert completion.choices[0].finish_reason
assert completion.choices[0].index is not None
assert completion.choices[0].message.content is not None
assert completion.choices[0].message.role
@configure
@pytest.mark.parametrize("api_type, api_version", [(AZURE, LATEST)])
def test_client_str_token(self, client, api_type, api_version, **kwargs):
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"}
]
client = openai.AzureOpenAI(
azure_endpoint=os.getenv(ENV_AZURE_OPENAI_ENDPOINT),
azure_ad_token=get_credential().get_token("https://cognitiveservices.azure.com/.default").token,
api_version=LATEST,
)
completion = client.chat.completions.create(messages=messages, **kwargs)
assert completion.id
assert completion.object == "chat.completion"
assert completion.model
assert completion.created
assert completion.usage.completion_tokens is not None
assert completion.usage.prompt_tokens is not None
assert completion.usage.total_tokens == completion.usage.completion_tokens + completion.usage.prompt_tokens
assert len(completion.choices) == 1
assert completion.choices[0].finish_reason
assert completion.choices[0].index is not None
assert completion.choices[0].message.content is not None
assert completion.choices[0].message.role
@configure
@pytest.mark.parametrize("api_type, api_version", [(AZURE, LATEST)])
def test_client_no_api_key(self, client, api_type, api_version, **kwargs):
with pytest.raises(openai.OpenAIError) as e:
openai.AzureOpenAI(
azure_endpoint=os.getenv(ENV_AZURE_OPENAI_ENDPOINT),
api_key=None,
api_version=LATEST,
)
assert 'Missing credentials. Please pass one of `api_key`, `azure_ad_token`, `azure_ad_token_provider`, or the `AZURE_OPENAI_API_KEY` or `AZURE_OPENAI_AD_TOKEN` environment variables.' in str(e.value.args)
@configure
@pytest.mark.parametrize("api_type, api_version", [(AZURE, LATEST)])
def test_client_bad_token(self, client, api_type, api_version, **kwargs):
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"}
]
client = openai.AzureOpenAI(
azure_endpoint=os.getenv(ENV_AZURE_OPENAI_ENDPOINT),
azure_ad_token="None",
api_version=LATEST,
)
with pytest.raises(openai.AuthenticationError) as e:
client.chat.completions.create(messages=messages, **kwargs)
assert e.value.status_code == 401
@configure
@pytest.mark.parametrize("api_type, api_version", [(AZURE, LATEST)])
def test_client_bad_token_provider(self, client, api_type, api_version, **kwargs):
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"}
]
client = openai.AzureOpenAI(
azure_endpoint=os.getenv(ENV_AZURE_OPENAI_ENDPOINT),
azure_ad_token_provider=lambda: None,
api_version=LATEST,
)
with pytest.raises(ValueError) as e:
client.chat.completions.create(messages=messages, **kwargs)
assert "Expected `azure_ad_token_provider` argument to return a string but it returned None" in str(e.value.args)
@configure
@pytest.mark.parametrize("api_type, api_version", [(AZURE, LATEST)])
def test_client_env_vars_key(self, client, api_type, api_version, **kwargs):
with reload():
os.environ["AZURE_OPENAI_ENDPOINT"] = os.getenv(ENV_AZURE_OPENAI_ENDPOINT)
os.environ["OPENAI_API_VERSION"] = LATEST
os.environ["AZURE_OPENAI_API_KEY"] = os.getenv(ENV_AZURE_OPENAI_KEY)
try:
client = openai.AzureOpenAI()
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"}
]
completion = client.chat.completions.create(messages=messages, **kwargs)
assert completion.id
assert completion.object == "chat.completion"
assert completion.model
assert completion.created
assert completion.usage.completion_tokens is not None
assert completion.usage.prompt_tokens is not None
assert completion.usage.total_tokens == completion.usage.completion_tokens + completion.usage.prompt_tokens
assert len(completion.choices) == 1
assert completion.choices[0].finish_reason
assert completion.choices[0].index is not None
assert completion.choices[0].message.content is not None
assert completion.choices[0].message.role
finally:
del os.environ['AZURE_OPENAI_ENDPOINT']
del os.environ['AZURE_OPENAI_API_KEY']
del os.environ['OPENAI_API_VERSION']
@configure
@pytest.mark.parametrize("api_type, api_version", [(AZURE, LATEST)])
def test_client_env_vars_token(self, client, api_type, api_version, **kwargs):
with reload():
os.environ["AZURE_OPENAI_ENDPOINT"] = os.getenv(ENV_AZURE_OPENAI_ENDPOINT)
os.environ["OPENAI_API_VERSION"] = LATEST
os.environ["AZURE_OPENAI_AD_TOKEN"] = get_credential().get_token("https://cognitiveservices.azure.com/.default").token
try:
client = openai.AzureOpenAI()
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"}
]
completion = client.chat.completions.create(messages=messages, **kwargs)
assert completion.id
assert completion.object == "chat.completion"
assert completion.model
assert completion.created
assert completion.usage.completion_tokens is not None
assert completion.usage.prompt_tokens is not None
assert completion.usage.total_tokens == completion.usage.completion_tokens + completion.usage.prompt_tokens
assert len(completion.choices) == 1
assert completion.choices[0].finish_reason
assert completion.choices[0].index is not None
assert completion.choices[0].message.content is not None
assert completion.choices[0].message.role
finally:
del os.environ['AZURE_OPENAI_ENDPOINT']
del os.environ['AZURE_OPENAI_AD_TOKEN']
del os.environ['OPENAI_API_VERSION']
@pytest.mark.parametrize(
"headers,timeout",
[
({"retry-after-ms": "2000"}, 2.0),
({"retry-after-ms": "2", "retry-after": "1"}, 0.002),
({"Retry-After-Ms": "2", "Retry-After": "1"}, 0.002),
({"retry-after-ms": "invalid"}, ...),
({}, ...),
(None, ...),
],
)
def test_parse_retry_after_ms_header(self, headers, timeout, **kwargs):
client = openai.AzureOpenAI(
azure_endpoint=os.getenv(ENV_AZURE_OPENAI_ENDPOINT),
api_key="key",
api_version=LATEST,
)
response_headers = httpx.Headers(headers)
options = openai._models.FinalRequestOptions(method="post", url="/completions")
retry_timeout = client._calculate_retry_timeout(
remaining_retries=2,
options=options,
response_headers=response_headers
)
if headers is None or headers == {} or headers.get("retry-after-ms") == "invalid":
assert retry_timeout # uses the default implementation
else:
assert retry_timeout == timeout # uses retry-after-ms
|