forked from openai/openai-python
-
Notifications
You must be signed in to change notification settings - Fork 0
[azure] only add deployment in url path for deployment-based APIs #8
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
kristapratico
wants to merge
3
commits into
main
Choose a base branch
from
azure-prepare-url
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Union | ||
from typing_extensions import Literal | ||
|
||
|
@@ -22,21 +24,6 @@ | |
) | ||
|
||
|
||
@pytest.mark.parametrize("client", [sync_client, async_client]) | ||
def test_implicit_deployment_path(client: Client) -> None: | ||
req = client._build_request( | ||
FinalRequestOptions.construct( | ||
method="post", | ||
url="/chat/completions", | ||
json_data={"model": "my-deployment-model"}, | ||
) | ||
) | ||
assert ( | ||
req.url | ||
== "https://example-resource.azure.openai.com/openai/deployments/my-deployment-model/chat/completions?api-version=2023-07-01" | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"client,method", | ||
[ | ||
|
@@ -64,3 +51,178 @@ def test_client_copying_override_options(client: Client) -> None: | |
api_version="2022-05-01", | ||
) | ||
assert copied._custom_query == {"api-version": "2022-05-01"} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"client,base_url,api_path,json_data,expected", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can come up with a couple of additional tests here - e.g. have a dns name with deployments in it, add a deployment called deployments, add a couple of deployments to the azure_endpoint url etc. |
||
[ | ||
( | ||
AzureOpenAI( | ||
api_version="2024-02-01", | ||
api_key="example API key", | ||
azure_endpoint="https://example-resource.azure.openai.com", | ||
), | ||
"https://example-resource.azure.openai.com/openai/", | ||
"/chat/completions", | ||
{"model": "my-deployment"}, | ||
"https://example-resource.azure.openai.com/openai/deployments/my-deployment/chat/completions?api-version=2024-02-01" | ||
), | ||
( | ||
AzureOpenAI( | ||
api_version="2024-02-01", | ||
api_key="example API key", | ||
azure_endpoint="https://example-resource.azure.openai.com", | ||
), | ||
"https://example-resource.azure.openai.com/openai/", | ||
"/models", | ||
kristapratico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{}, | ||
"https://example-resource.azure.openai.com/openai/models?api-version=2024-02-01" | ||
), | ||
( | ||
AzureOpenAI( | ||
api_version="2024-02-01", | ||
api_key="example API key", | ||
azure_endpoint="https://example-resource.azure.openai.com", | ||
azure_deployment="my-deployment" | ||
), | ||
"https://example-resource.azure.openai.com/openai/deployments/my-deployment/", | ||
"/chat/completions", | ||
{"model": "placeholder"}, | ||
"https://example-resource.azure.openai.com/openai/deployments/my-deployment/chat/completions?api-version=2024-02-01" | ||
), | ||
( | ||
AzureOpenAI( | ||
api_version="2024-02-01", | ||
api_key="example API key", | ||
azure_endpoint="https://example-resource.azure.openai.com", | ||
azure_deployment="my-deployment" | ||
), | ||
"https://example-resource.azure.openai.com/openai/deployments/my-deployment/", | ||
"/models", | ||
{}, | ||
"https://example-resource.azure.openai.com/openai/models?api-version=2024-02-01" | ||
), | ||
( | ||
AzureOpenAI( | ||
api_version="2024-02-01", | ||
api_key="example API key", | ||
base_url="https://example.azure-api.net/PTU/deployments/my-deployment/", | ||
), | ||
"https://example.azure-api.net/PTU/deployments/my-deployment/", | ||
"/chat/completions", | ||
{"model": "placeholder"}, | ||
"https://example.azure-api.net/PTU/deployments/my-deployment/chat/completions?api-version=2024-02-01" | ||
), | ||
( | ||
AzureOpenAI( | ||
api_version="2024-02-01", | ||
api_key="example API key", | ||
base_url="https://example.azure-api.net/PTU/", | ||
), | ||
"https://example.azure-api.net/PTU/", | ||
"/chat/completions", | ||
{"model": "my-deployment"}, | ||
"https://example.azure-api.net/PTU/deployments/my-deployment/chat/completions?api-version=2024-02-01" | ||
), | ||
( | ||
AzureOpenAI( | ||
api_version="2024-02-01", | ||
api_key="example API key", | ||
base_url="https://example.azure-api.net/PTU/deployments/my-deployment/", | ||
), | ||
"https://example.azure-api.net/PTU/deployments/my-deployment/", | ||
"/models", | ||
{}, | ||
"https://example.azure-api.net/PTU/models?api-version=2024-02-01" | ||
), | ||
( | ||
AsyncAzureOpenAI( | ||
api_version="2024-02-01", | ||
api_key="example API key", | ||
azure_endpoint="https://example-resource.azure.openai.com", | ||
), | ||
"https://example-resource.azure.openai.com/openai/", | ||
"/chat/completions", | ||
{"model": "my-deployment"}, | ||
"https://example-resource.azure.openai.com/openai/deployments/my-deployment/chat/completions?api-version=2024-02-01" | ||
), | ||
( | ||
AsyncAzureOpenAI( | ||
api_version="2024-02-01", | ||
api_key="example API key", | ||
azure_endpoint="https://example-resource.azure.openai.com", | ||
), | ||
"https://example-resource.azure.openai.com/openai/", | ||
"/models", | ||
{}, | ||
"https://example-resource.azure.openai.com/openai/models?api-version=2024-02-01" | ||
), | ||
( | ||
AsyncAzureOpenAI( | ||
api_version="2024-02-01", | ||
api_key="example API key", | ||
azure_endpoint="https://example-resource.azure.openai.com", | ||
azure_deployment="my-deployment" | ||
), | ||
"https://example-resource.azure.openai.com/openai/deployments/my-deployment/", | ||
"/chat/completions", | ||
{"model": "placeholder"}, | ||
"https://example-resource.azure.openai.com/openai/deployments/my-deployment/chat/completions?api-version=2024-02-01" | ||
), | ||
( | ||
AsyncAzureOpenAI( | ||
api_version="2024-02-01", | ||
api_key="example API key", | ||
azure_endpoint="https://example-resource.azure.openai.com", | ||
azure_deployment="my-deployment" | ||
), | ||
"https://example-resource.azure.openai.com/openai/deployments/my-deployment/", | ||
"/models", | ||
{}, | ||
"https://example-resource.azure.openai.com/openai/models?api-version=2024-02-01" | ||
), | ||
( | ||
AsyncAzureOpenAI( | ||
api_version="2024-02-01", | ||
api_key="example API key", | ||
base_url="https://example.azure-api.net/PTU/deployments/my-deployment/", | ||
), | ||
"https://example.azure-api.net/PTU/deployments/my-deployment/", | ||
"/chat/completions", | ||
{"model": "placeholder"}, | ||
"https://example.azure-api.net/PTU/deployments/my-deployment/chat/completions?api-version=2024-02-01" | ||
), | ||
( | ||
AsyncAzureOpenAI( | ||
api_version="2024-02-01", | ||
api_key="example API key", | ||
base_url="https://example.azure-api.net/PTU/", | ||
), | ||
"https://example.azure-api.net/PTU/", | ||
"/chat/completions", | ||
{"model": "my-deployment"}, | ||
"https://example.azure-api.net/PTU/deployments/my-deployment/chat/completions?api-version=2024-02-01" | ||
), | ||
( | ||
AsyncAzureOpenAI( | ||
api_version="2024-02-01", | ||
api_key="example API key", | ||
base_url="https://example.azure-api.net/PTU/deployments/my-deployment/", | ||
), | ||
"https://example.azure-api.net/PTU/deployments/my-deployment/", | ||
"/models", | ||
{}, | ||
"https://example.azure-api.net/PTU/models?api-version=2024-02-01" | ||
), | ||
], | ||
) | ||
def test_client_prepare_url(client: Client, base_url: str, api_path: str, json_data: dict[str, str], expected: str) -> None: | ||
req = client._build_request( | ||
FinalRequestOptions.construct( | ||
method="post", | ||
url=api_path, | ||
json_data=json_data, | ||
) | ||
) | ||
assert req.url == expected | ||
assert client.base_url == base_url |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than adding it and then removing it, did we consider adding it only when needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that would be the better way of dealing with it. However, we'd have to change the current value of
client.base_url
to take that approach which could break users.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we split on/find
'/deployments/'
? I guess it is unlikely that one would name a deployment"deployments"
(and we do a maxsplit=1 so it is only a problem if we have a deployments "to the right" of the intended split point).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, the base_url property enforces a trailing slash so it will always be there, regardless of whether the user added it.