Skip to content

_prepare_request option #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: v1_azure
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions src/openai/azure/_async_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing_extensions import Literal, override
from typing import Any, Callable, cast, List, Mapping, Dict, Optional, overload, Type, Union
import time
import json

import httpx

Expand Down Expand Up @@ -378,6 +379,25 @@ def auth_headers(self) -> Dict[str, str]:
return { 'Authorization': f'Bearer {self.credential.get_token()}'}
return {"api-key": self.api_key}

def _prepare_request(self, request: httpx.Request) -> None:
# TODO: need confirmation that it is okay to override this
url = request.url
if url.path.startswith("/audio"):
model_name = request.stream.fields[0].value # is this a robust way to extract model name?
request.url = url.copy_with(path=f"/openai/deployments/{model_name}{url.path}")
return
Comment on lines +385 to +388
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't get model out of the content here b/c I don't want to call read() on the request before I send it..... it's probably not a a robust way to extract the model name from the httpx._multipart.MultipartStream. Need to re-think this approach...


try:
content = json.loads(request.content)
except json.JSONDecodeError:
return

if content.get("dataSources"):
request.url = url.copy_with(path=f"/openai/deployments/{content['model']}/extensions{url.path}")
elif request.url.path == "/images/generations":
request.url = url.copy_with(path="/openai/images/generations:submit")
elif content.get("model"):
request.url = url.copy_with(path=f"/openai/deployments/{content['model']}{url.path}")

def _check_polling_response(self, response: httpx.Response, predicate: Callable[[httpx.Response], bool]) -> bool:
if not predicate(response):
Expand Down Expand Up @@ -416,21 +436,14 @@ async def _poll(

# NOTE: We override the internal method because `@overrid`ing `@overload`ed methods and keeping typing happy is a pain. Most typing tools are lacking...
async def _request(self, cast_to: Type[ResponseT], options: FinalRequestOptions, **kwargs: Any) -> Any:
if options.url == "/images/generations":
options.url = "openai/images/generations:submit"
response = await super()._request(cast_to=cast_to, options=options, **kwargs)
response = await super()._request(cast_to=cast_to, options=options, **kwargs)
if isinstance(response, ImagesResponse):
model_extra = cast(Mapping[str, Any], getattr(response, 'model_extra')) or {}
operation_id = cast(str, model_extra['id'])
return await self._poll(
"get", f"openai/operations/images/{operation_id}",
until=lambda response: response.json()["status"] in ["succeeded"],
failed=lambda response: response.json()["status"] in ["failed"],
)
if isinstance(options.json_data, Mapping):
model = cast(str, options.json_data["model"])
if not options.url.startswith(f'openai/deployments/{model}'):
if options.extra_json and options.extra_json.get("dataSources"):
options.url = f'openai/deployments/{model}/extensions' + options.url
else:
options.url = f'openai/deployments/{model}' + options.url
if model_extra.get("id"):
operation_id = cast(str, model_extra['id'])
return await self._poll(
"get", f"openai/operations/images/{operation_id}",
until=lambda response: response.json()["status"] in ["succeeded"],
failed=lambda response: response.json()["status"] in ["failed"],
)
return await super()._request(cast_to=cast_to, options=options, **kwargs)
49 changes: 32 additions & 17 deletions src/openai/azure/_sync_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing_extensions import Literal, override
from typing import Any, Callable, cast, List, Mapping, Dict, Optional, overload, Union
import time
import json

import httpx

Expand Down Expand Up @@ -380,26 +381,40 @@ def auth_headers(self) -> Dict[str, str]:
return { 'Authorization': f'Bearer {self.credential.get_token()}'}
return {"api-key": self.api_key}

def _prepare_request(self, request: httpx.Request) -> None:
# TODO: need confirmation that it is okay to override this
url = request.url
if url.path.startswith("/audio"):
model_name = request.stream.fields[0].value # is this a robust way to extract model name?
request.url = url.copy_with(path=f"/openai/deployments/{model_name}{url.path}")
return

try:
content = json.loads(request.content)
except json.JSONDecodeError:
return

if content.get("dataSources"):
request.url = url.copy_with(path=f"/openai/deployments/{content['model']}/extensions{url.path}")
elif request.url.path == "/images/generations":
request.url = url.copy_with(path="/openai/images/generations:submit")
elif content.get("model"):
request.url = url.copy_with(path=f"/openai/deployments/{content['model']}{url.path}")

# NOTE: We override the internal method because `@overrid`ing `@overload`ed methods and keeping typing happy is a pain. Most typing tools are lacking...
def _request(self, *, options: FinalRequestOptions, **kwargs: Any) -> Any:
if options.url == "/images/generations":
options.url = "openai/images/generations:submit"
response = super()._request(options=options, **kwargs)
response = super()._request(options=options, **kwargs)
if isinstance(response, ImagesResponse):
model_extra = cast(Mapping[str, Any], getattr(response, 'model_extra')) or {}
operation_id = cast(str, model_extra['id'])
return self._poll(
"get", f"openai/operations/images/{operation_id}",
until=lambda response: response.json()["status"] in ["succeeded"],
failed=lambda response: response.json()["status"] in ["failed"],
)
if isinstance(options.json_data, Mapping):
model = cast(str, options.json_data["model"])
if not options.url.startswith(f'openai/deployments/{model}'):
if options.extra_json and options.extra_json.get("dataSources"):
options.url = f'openai/deployments/{model}/extensions' + options.url
else:
options.url = f'openai/deployments/{model}' + options.url
return super()._request(options=options, **kwargs)
if model_extra.get("id"):
operation_id = cast(str, model_extra['id'])
return self._poll(
"get", f"openai/operations/images/{operation_id}",
until=lambda response: response.json()["status"] in ["succeeded"],
failed=lambda response: response.json()["status"] in ["failed"],
)

return response

# Internal azure specific "helper" methods
def _check_polling_response(self, response: httpx.Response, predicate: Callable[[httpx.Response], bool]) -> bool:
Expand Down