Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e3b59de

Browse files
committed
adding docstrings initial
1 parent 7a31602 commit e3b59de

File tree

1 file changed

+62
-16
lines changed

1 file changed

+62
-16
lines changed

openai/client.py

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,45 @@
11
import sys
22
import time
33

4-
from typing import Union, Dict, Any, cast, Iterable, BinaryIO
4+
from typing import (
5+
Union,
6+
Dict,
7+
Any,
8+
cast,
9+
Iterable,
10+
BinaryIO,
11+
Optional,
12+
TYPE_CHECKING
13+
)
514
if sys.version_info >= (3, 8):
615
from typing import Literal, AsyncIterable
716
else:
817
from typing_extensions import Literal, AsyncIterable
918

1019
import openai
1120

21+
if TYPE_CHECKING:
22+
from azure.core.credentials import TokenCredential
23+
24+
1225
LATEST_AZURE_API_VERSION = "2023-05-15"
1326

1427

1528
class AzureTokenAuth:
16-
"""Authentication using an Azure AD token.
29+
"""Authentication using an Azure Active Directory token.
1730
"""
1831

19-
def __repr__(self):
32+
def __repr__(self) -> str:
2033
return f"AzureTokenAuth({type(self._credential)})"
2134

22-
def __init__(self, *, credential=None):
23-
"""Create a new AzureTokenAuth instance. If no credential is passed,
24-
it will use ~azure.identity.DefaultAzureCredential
35+
def __init__(self, *, credential: Optional["TokenCredential"] = None) -> None:
36+
"""Create a new AzureTokenAuth instance. Requires the
37+
azure-identity package.
38+
39+
:keyword credential: A credential type from the azure.identity library.
40+
If no credential is passed, it will use ~azure.identity.DefaultAzureCredential.
41+
:paramtype credential: ~azure.core.credentials.TokenCredential or
42+
~azure.identity.DefaultAzureCredential
2543
"""
2644
if not credential:
2745
try:
@@ -35,6 +53,13 @@ def __init__(self, *, credential=None):
3553
self._cached_token = None
3654

3755
def get_token(self) -> str:
56+
"""Gets a valid AAD token to authenticate the request.
57+
58+
.. note::
59+
60+
Do not directly interact with this API, it will be called
61+
automatically when a token is needed for the request.
62+
"""
3863
if (
3964
self._cached_token is None
4065
or (self._cached_token.expires_on - time.time()) < 300
@@ -49,15 +74,25 @@ def get_token(self) -> str:
4974
class ApiKeyAuth:
5075
"""Authentication using an API key"""
5176

52-
def __repr__(self):
53-
return f"ApiKeyAuth(api_key=<redacted>)"
54-
55-
def __init__(self, key: str = ""):
56-
"""Create a new ApiKeyAuth instance. If no key is passed, it will use ~openai.api_key"""
77+
def __repr__(self) -> str:
78+
return "ApiKeyAuth(api_key=<redacted>)"
79+
80+
def __init__(self, key: str = "") -> None:
81+
"""Create a new ApiKeyAuth instance.
82+
83+
:param str key: The API key associated with your account.
84+
If no key is passed, it will use ~openai.api_key
85+
"""
5786
self.key = key or openai.api_key
5887

5988
def get_token(self) -> str:
60-
"""Get the API key"""
89+
"""Get the API key
90+
91+
.. note::
92+
93+
Do not directly interact with this API, it will be called
94+
automatically when a token is needed for the request.
95+
"""
6196
return self.key
6297

6398

@@ -75,12 +110,16 @@ def __init__(
75110
organization: str = "",
76111
):
77112
"""Create a new OpenAI client.
78-
113+
79114
:keyword str api_base: The base URL for the API. If not specified, based on ~opeanai.api_base
80-
:keyword auth: The authentication method or key to use. If the string value "azuredefault" is passed, it will use ~azure.identity.DefaultAzureCredential
115+
:keyword auth: The authentication method or key to use. If the string value "azuredefault" is passed,
116+
it will use ~azure.identity.DefaultAzureCredential
81117
:paramtype auth: str or ~openai.client.ApiKeyAuth or ~openai.client.AzureTokenAuth
82-
:keyword str api_version: The API version to use. If not specified, based on ~openai.api_version or ~openai.client.LATEST_AZURE_API_VERSION.
118+
:keyword str api_version: The API version to use. If not specified, based on ~openai.api_version
119+
or ~openai.client.LATEST_AZURE_API_VERSION.
83120
:keyword str backend: One of 'azure' or 'openai'. If not specified, inferred from the auth method or ~openai.api_type
121+
:keyword str organization: The identifier of the organization to use for API requests.
122+
If not specified, based on ~openai.organization.
84123
"""
85124

86125
#
@@ -119,7 +158,7 @@ def __init__(
119158
if self.backend == 'azure' and self.api_base == "https://api.openai.com/v1":
120159
raise ValueError("You are using the 'openai.com' endpoint with an Azure credential or API type. Please provide the endpoint to your Azure resource instead.")
121160

122-
def __repr__(self):
161+
def __repr__(self) -> str:
123162
constructor_args = [
124163
f"{name}={repr(value)}"
125164
for name, value in self.__dict__.items()
@@ -173,6 +212,13 @@ def _normalize_model(self, kwargs: Dict[str, Any]):
173212
pass
174213

175214
def completion(self, prompt: str, **kwargs) -> openai.Completion:
215+
"""Creates a completion for the provided prompt and parameters.
216+
217+
:param prompt: The prompt(s) to generate completions for,
218+
encoded as a string, array of strings, array of tokens,
219+
or array of token arrays.
220+
:type prompt: str or Iterable[str] or Iterable[float] or Iterable[Iterable[float]]
221+
"""
176222
self._populate_args(kwargs, prompt=prompt, stream=False)
177223
self._normalize_model(kwargs)
178224
return cast(openai.Completion, openai.Completion.create(**kwargs))

0 commit comments

Comments
 (0)