mirror of
https://github.com/james-m-jordan/openai-cookbook.git
synced 2025-05-09 19:32:38 +00:00
[Azure] add example snippet on how to refresh AAD tokens (#435)
* add how to refresh aad tokens example * remove accidental config
This commit is contained in:
parent
c6a104c0ca
commit
33dc207afb
@ -89,6 +89,45 @@
|
||||
"# openai.api_key = token.token"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import typing\n",
|
||||
"import time\n",
|
||||
"import requests\n",
|
||||
"if typing.TYPE_CHECKING:\n",
|
||||
" from azure.core.credentials import TokenCredential\n",
|
||||
"\n",
|
||||
"class TokenRefresh(requests.auth.AuthBase):\n",
|
||||
"\n",
|
||||
" def __init__(self, credential: \"TokenCredential\", scopes: typing.List[str]) -> None:\n",
|
||||
" self.credential = credential\n",
|
||||
" self.scopes = scopes\n",
|
||||
" self.cached_token: typing.Optional[str] = None\n",
|
||||
"\n",
|
||||
" def __call__(self, req):\n",
|
||||
" if not self.cached_token or self.cached_token.expires_on - time.time() < 300:\n",
|
||||
" self.cached_token = self.credential.get_token(*self.scopes)\n",
|
||||
" req.headers[\"Authorization\"] = f\"Bearer {self.cached_token.token}\"\n",
|
||||
" return req\n",
|
||||
"\n",
|
||||
"session = requests.Session()\n",
|
||||
"session.auth = TokenRefresh(default_credential, [\"https://cognitiveservices.azure.com/.default\"])\n",
|
||||
"\n",
|
||||
"openai.requestssession = session"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
|
@ -89,6 +89,45 @@
|
||||
"# openai.api_key = token.token"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import typing\n",
|
||||
"import time\n",
|
||||
"import requests\n",
|
||||
"if typing.TYPE_CHECKING:\n",
|
||||
" from azure.core.credentials import TokenCredential\n",
|
||||
"\n",
|
||||
"class TokenRefresh(requests.auth.AuthBase):\n",
|
||||
"\n",
|
||||
" def __init__(self, credential: \"TokenCredential\", scopes: typing.List[str]) -> None:\n",
|
||||
" self.credential = credential\n",
|
||||
" self.scopes = scopes\n",
|
||||
" self.cached_token: typing.Optional[str] = None\n",
|
||||
"\n",
|
||||
" def __call__(self, req):\n",
|
||||
" if not self.cached_token or self.cached_token.expires_on - time.time() < 300:\n",
|
||||
" self.cached_token = self.credential.get_token(*self.scopes)\n",
|
||||
" req.headers[\"Authorization\"] = f\"Bearer {self.cached_token.token}\"\n",
|
||||
" return req\n",
|
||||
"\n",
|
||||
"session = requests.Session()\n",
|
||||
"session.auth = TokenRefresh(default_credential, [\"https://cognitiveservices.azure.com/.default\"])\n",
|
||||
"\n",
|
||||
"openai.requestssession = session"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
|
@ -89,6 +89,45 @@
|
||||
"# openai.api_key = token.token"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import typing\n",
|
||||
"import time\n",
|
||||
"import requests\n",
|
||||
"if typing.TYPE_CHECKING:\n",
|
||||
" from azure.core.credentials import TokenCredential\n",
|
||||
"\n",
|
||||
"class TokenRefresh(requests.auth.AuthBase):\n",
|
||||
"\n",
|
||||
" def __init__(self, credential: \"TokenCredential\", scopes: typing.List[str]) -> None:\n",
|
||||
" self.credential = credential\n",
|
||||
" self.scopes = scopes\n",
|
||||
" self.cached_token: typing.Optional[str] = None\n",
|
||||
"\n",
|
||||
" def __call__(self, req):\n",
|
||||
" if not self.cached_token or self.cached_token.expires_on - time.time() < 300:\n",
|
||||
" self.cached_token = self.credential.get_token(*self.scopes)\n",
|
||||
" req.headers[\"Authorization\"] = f\"Bearer {self.cached_token.token}\"\n",
|
||||
" return req\n",
|
||||
"\n",
|
||||
"session = requests.Session()\n",
|
||||
"session.auth = TokenRefresh(default_credential, [\"https://cognitiveservices.azure.com/.default\"])\n",
|
||||
"\n",
|
||||
"openai.requestssession = session"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
|
Loading…
x
Reference in New Issue
Block a user