Skip to content

Commit 72ae907

Browse files
authored
Azure AI Inference SDK - Changes for beta 4 release (#36669)
1 parent 5055a13 commit 72ae907

File tree

55 files changed

+1533
-1169
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1533
-1169
lines changed

.vscode/cspell.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,8 @@
13051305
"mros",
13061306
"Nify",
13071307
"ctxt",
1308-
"wday"
1308+
"wday",
1309+
"dtype"
13091310
]
13101311
},
13111312
{

sdk/ai/azure-ai-inference/CHANGELOG.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
# Release History
22

3-
## 1.0.0b4 (Unreleased)
3+
## 1.0.0b4 (2024-08-30)
44

55
### Features Added
66

7+
* Support chat completion streaming response with function arguments (tool calls). Add new classes
8+
`StreamingChatResponseMessageUpdate` and `StreamingChatResponseToolCallUpdate`.
9+
* Support text embeddings result in base64 encoded string format.
10+
* Nicely formated print of chat completions and embeddings result objects.
11+
712
### Breaking Changes
813

14+
* Classes `ChatCompletionsToolSelectionPreset`, `ChatCompletionsNamedToolSelection` and `ChatCompletionsFunctionToolSelection` renamed to `ChatCompletionsToolChoicePreset` `ChatCompletionsNamedToolChoice` and `ChatCompletionsNamedToolChoiceFunction` respectively.
15+
* Update the object type of `embeddings` property on `EmbeddingsResult`, from `embedding: List[float]` to `embedding: Union[str, List[float]]`.
16+
* Instead of base class `ChatCompletionsToolCall` and derived class `ChatCompletionsFunctionToolCall`, we now have a flat representation of only one class `ChatCompletionsToolCall` that that represents a function tool. This is because the only support tool is a function call.
17+
918
### Bugs Fixed
1019

11-
### Other Changes
20+
* Fix setting of chat completions response format, to allow response in JSON format. See classes `ChatCompletionsResponseFormat` (base class) and
21+
derived classes `ChatCompletionsResponseFormatJSON` and `ChatCompletionsResponseFormatText`.
1222

1323
## 1.0.0b3 (2024-07-31)
1424

sdk/ai/azure-ai-inference/README.md

Lines changed: 72 additions & 28 deletions
Large diffs are not rendered by default.

sdk/ai/azure-ai-inference/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "python",
44
"TagPrefix": "python/ai/azure-ai-inference",
5-
"Tag": "python/ai/azure-ai-inference_a43207800c"
5+
"Tag": "python/ai/azure-ai-inference_498e85cbfd"
66
}

sdk/ai/azure-ai-inference/azure/ai/inference/_client.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ class ChatCompletionsClient(ChatCompletionsClientOperationsMixin): # pylint: di
3737
3838
:param endpoint: Service host. Required.
3939
:type endpoint: str
40-
:param credential: Credential used to authenticate requests to the service. Is either a
41-
AzureKeyCredential type or a TokenCredential type. Required.
40+
:param credential: Credential used to authenticate requests to the service. Is one of the
41+
following types: AzureKeyCredential, AzureKeyCredential, TokenCredential Required.
4242
:type credential: ~azure.core.credentials.AzureKeyCredential or
43-
~azure.core.credentials.TokenCredential
43+
~azure.core.credentials.AzureKeyCredential or ~azure.core.credentials.TokenCredential
4444
:keyword api_version: The API version to use for this operation. Default value is
4545
"2024-05-01-preview". Note that overriding this default value may result in unsupported
4646
behavior.
@@ -115,10 +115,10 @@ class EmbeddingsClient(EmbeddingsClientOperationsMixin): # pylint: disable=clie
115115
116116
:param endpoint: Service host. Required.
117117
:type endpoint: str
118-
:param credential: Credential used to authenticate requests to the service. Is either a
119-
AzureKeyCredential type or a TokenCredential type. Required.
118+
:param credential: Credential used to authenticate requests to the service. Is one of the
119+
following types: AzureKeyCredential, AzureKeyCredential, TokenCredential Required.
120120
:type credential: ~azure.core.credentials.AzureKeyCredential or
121-
~azure.core.credentials.TokenCredential
121+
~azure.core.credentials.AzureKeyCredential or ~azure.core.credentials.TokenCredential
122122
:keyword api_version: The API version to use for this operation. Default value is
123123
"2024-05-01-preview". Note that overriding this default value may result in unsupported
124124
behavior.
@@ -193,10 +193,10 @@ class ImageEmbeddingsClient(ImageEmbeddingsClientOperationsMixin): # pylint: di
193193
194194
:param endpoint: Service host. Required.
195195
:type endpoint: str
196-
:param credential: Credential used to authenticate requests to the service. Is either a
197-
AzureKeyCredential type or a TokenCredential type. Required.
196+
:param credential: Credential used to authenticate requests to the service. Is one of the
197+
following types: AzureKeyCredential, AzureKeyCredential, TokenCredential Required.
198198
:type credential: ~azure.core.credentials.AzureKeyCredential or
199-
~azure.core.credentials.TokenCredential
199+
~azure.core.credentials.AzureKeyCredential or ~azure.core.credentials.TokenCredential
200200
:keyword api_version: The API version to use for this operation. Default value is
201201
"2024-05-01-preview". Note that overriding this default value may result in unsupported
202202
behavior.

sdk/ai/azure-ai-inference/azure/ai/inference/_configuration.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ class ChatCompletionsClientConfiguration: # pylint: disable=too-many-instance-a
2626
2727
:param endpoint: Service host. Required.
2828
:type endpoint: str
29-
:param credential: Credential used to authenticate requests to the service. Is either a
30-
AzureKeyCredential type or a TokenCredential type. Required.
29+
:param credential: Credential used to authenticate requests to the service. Is one of the
30+
following types: AzureKeyCredential, AzureKeyCredential, TokenCredential Required.
3131
:type credential: ~azure.core.credentials.AzureKeyCredential or
32-
~azure.core.credentials.TokenCredential
32+
~azure.core.credentials.AzureKeyCredential or ~azure.core.credentials.TokenCredential
3333
:keyword api_version: The API version to use for this operation. Default value is
3434
"2024-05-01-preview". Note that overriding this default value may result in unsupported
3535
behavior.
@@ -55,6 +55,8 @@ def __init__(self, endpoint: str, credential: Union[AzureKeyCredential, "TokenCr
5555
def _infer_policy(self, **kwargs):
5656
if isinstance(self.credential, AzureKeyCredential):
5757
return policies.AzureKeyCredentialPolicy(self.credential, "Authorization", prefix="Bearer", **kwargs)
58+
if isinstance(self.credential, AzureKeyCredential):
59+
return policies.AzureKeyCredentialPolicy(self.credential, "api-key", **kwargs)
5860
if hasattr(self.credential, "get_token"):
5961
return policies.BearerTokenCredentialPolicy(self.credential, *self.credential_scopes, **kwargs)
6062
raise TypeError(f"Unsupported credential: {self.credential}")
@@ -81,10 +83,10 @@ class EmbeddingsClientConfiguration: # pylint: disable=too-many-instance-attrib
8183
8284
:param endpoint: Service host. Required.
8385
:type endpoint: str
84-
:param credential: Credential used to authenticate requests to the service. Is either a
85-
AzureKeyCredential type or a TokenCredential type. Required.
86+
:param credential: Credential used to authenticate requests to the service. Is one of the
87+
following types: AzureKeyCredential, AzureKeyCredential, TokenCredential Required.
8688
:type credential: ~azure.core.credentials.AzureKeyCredential or
87-
~azure.core.credentials.TokenCredential
89+
~azure.core.credentials.AzureKeyCredential or ~azure.core.credentials.TokenCredential
8890
:keyword api_version: The API version to use for this operation. Default value is
8991
"2024-05-01-preview". Note that overriding this default value may result in unsupported
9092
behavior.
@@ -110,6 +112,8 @@ def __init__(self, endpoint: str, credential: Union[AzureKeyCredential, "TokenCr
110112
def _infer_policy(self, **kwargs):
111113
if isinstance(self.credential, AzureKeyCredential):
112114
return policies.AzureKeyCredentialPolicy(self.credential, "Authorization", prefix="Bearer", **kwargs)
115+
if isinstance(self.credential, AzureKeyCredential):
116+
return policies.AzureKeyCredentialPolicy(self.credential, "api-key", **kwargs)
113117
if hasattr(self.credential, "get_token"):
114118
return policies.BearerTokenCredentialPolicy(self.credential, *self.credential_scopes, **kwargs)
115119
raise TypeError(f"Unsupported credential: {self.credential}")
@@ -136,10 +140,10 @@ class ImageEmbeddingsClientConfiguration: # pylint: disable=too-many-instance-a
136140
137141
:param endpoint: Service host. Required.
138142
:type endpoint: str
139-
:param credential: Credential used to authenticate requests to the service. Is either a
140-
AzureKeyCredential type or a TokenCredential type. Required.
143+
:param credential: Credential used to authenticate requests to the service. Is one of the
144+
following types: AzureKeyCredential, AzureKeyCredential, TokenCredential Required.
141145
:type credential: ~azure.core.credentials.AzureKeyCredential or
142-
~azure.core.credentials.TokenCredential
146+
~azure.core.credentials.AzureKeyCredential or ~azure.core.credentials.TokenCredential
143147
:keyword api_version: The API version to use for this operation. Default value is
144148
"2024-05-01-preview". Note that overriding this default value may result in unsupported
145149
behavior.
@@ -165,6 +169,8 @@ def __init__(self, endpoint: str, credential: Union[AzureKeyCredential, "TokenCr
165169
def _infer_policy(self, **kwargs):
166170
if isinstance(self.credential, AzureKeyCredential):
167171
return policies.AzureKeyCredentialPolicy(self.credential, "Authorization", prefix="Bearer", **kwargs)
172+
if isinstance(self.credential, AzureKeyCredential):
173+
return policies.AzureKeyCredentialPolicy(self.credential, "api-key", **kwargs)
168174
if hasattr(self.credential, "get_token"):
169175
return policies.BearerTokenCredentialPolicy(self.credential, *self.credential_scopes, **kwargs)
170176
raise TypeError(f"Unsupported credential: {self.credential}")

sdk/ai/azure-ai-inference/azure/ai/inference/_model_base.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,9 @@ def _create_value(rf: typing.Optional["_RestField"], value: typing.Any) -> typin
476476

477477
class Model(_MyMutableMapping):
478478
_is_model = True
479+
# label whether current class's _attr_to_rest_field has been calculated
480+
# could not see _attr_to_rest_field directly because subclass inherits it from parent class
481+
_calculated: typing.Set[str] = set()
479482

480483
def __init__(self, *args: typing.Any, **kwargs: typing.Any) -> None:
481484
class_name = self.__class__.__name__
@@ -508,24 +511,27 @@ def copy(self) -> "Model":
508511
return Model(self.__dict__)
509512

510513
def __new__(cls, *args: typing.Any, **kwargs: typing.Any) -> Self: # pylint: disable=unused-argument
511-
# we know the last three classes in mro are going to be 'Model', 'dict', and 'object'
512-
mros = cls.__mro__[:-3][::-1] # ignore model, dict, and object parents, and reverse the mro order
513-
attr_to_rest_field: typing.Dict[str, _RestField] = { # map attribute name to rest_field property
514-
k: v for mro_class in mros for k, v in mro_class.__dict__.items() if k[0] != "_" and hasattr(v, "_type")
515-
}
516-
annotations = {
517-
k: v
518-
for mro_class in mros
519-
if hasattr(mro_class, "__annotations__") # pylint: disable=no-member
520-
for k, v in mro_class.__annotations__.items() # pylint: disable=no-member
521-
}
522-
for attr, rf in attr_to_rest_field.items():
523-
rf._module = cls.__module__
524-
if not rf._type:
525-
rf._type = rf._get_deserialize_callable_from_annotation(annotations.get(attr, None))
526-
if not rf._rest_name_input:
527-
rf._rest_name_input = attr
528-
cls._attr_to_rest_field: typing.Dict[str, _RestField] = dict(attr_to_rest_field.items())
514+
if f"{cls.__module__}.{cls.__qualname__}" not in cls._calculated:
515+
# we know the last nine classes in mro are going to be 'Model', '_MyMutableMapping', 'MutableMapping',
516+
# 'Mapping', 'Collection', 'Sized', 'Iterable', 'Container' and 'object'
517+
mros = cls.__mro__[:-9][::-1] # ignore parents, and reverse the mro order
518+
attr_to_rest_field: typing.Dict[str, _RestField] = { # map attribute name to rest_field property
519+
k: v for mro_class in mros for k, v in mro_class.__dict__.items() if k[0] != "_" and hasattr(v, "_type")
520+
}
521+
annotations = {
522+
k: v
523+
for mro_class in mros
524+
if hasattr(mro_class, "__annotations__") # pylint: disable=no-member
525+
for k, v in mro_class.__annotations__.items() # pylint: disable=no-member
526+
}
527+
for attr, rf in attr_to_rest_field.items():
528+
rf._module = cls.__module__
529+
if not rf._type:
530+
rf._type = rf._get_deserialize_callable_from_annotation(annotations.get(attr, None))
531+
if not rf._rest_name_input:
532+
rf._rest_name_input = attr
533+
cls._attr_to_rest_field: typing.Dict[str, _RestField] = dict(attr_to_rest_field.items())
534+
cls._calculated.add(f"{cls.__module__}.{cls.__qualname__}")
529535

530536
return super().__new__(cls) # pylint: disable=no-value-for-parameter
531537

@@ -563,6 +569,7 @@ def as_dict(self, *, exclude_readonly: bool = False) -> typing.Dict[str, typing.
563569
"""
564570

565571
result = {}
572+
readonly_props = []
566573
if exclude_readonly:
567574
readonly_props = [p._rest_name for p in self._attr_to_rest_field.values() if _is_readonly(p)]
568575
for k, v in self.items():

0 commit comments

Comments
 (0)