Skip to content

Commit 9ce1693

Browse files
hjh320ccreutzi
authored andcommitted
Make Azure URI formatting more robust
1 parent 8e53511 commit 9ce1693

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

+llms/+internal/callAzureChatAPI.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@
6161
nvp.sendRequestFcn
6262
end
6363

64-
URL = endpoint + "openai/deployments/" + deploymentID + "/chat/completions?api-version=" + nvp.APIVersion;
64+
URL = matlab.net.URI(endpoint);
65+
URL.Path = [URL.Path, "openai", "deployments", deploymentID, "chat", "completions"];
66+
URL.Query = matlab.net.QueryParameter("api-version", nvp.APIVersion);
6567

6668
parameters = buildParametersCall(messages, functions, nvp);
6769

68-
[response, streamedText] = nvp.sendRequestFcn(parameters,nvp.APIKey, URL, nvp.TimeOut, nvp.StreamFun);
70+
[response, streamedText] = nvp.sendRequestFcn(parameters,nvp.APIKey, URL.EncodedURI, nvp.TimeOut, nvp.StreamFun);
6971

7072
% For old models like GPT-3.5, we may have to change the request sent a
7173
% little. Since we cannot detect the model used other than trying to send a
@@ -75,7 +77,7 @@
7577
isfield(response.Body.Data.error,"message") && ...
7678
response.Body.Data.error.message == "Unrecognized request argument supplied: max_completion_tokens"
7779
parameters = renameStructField(parameters,'max_completion_tokens','max_tokens');
78-
[response, streamedText] = nvp.sendRequestFcn(parameters,nvp.APIKey, URL, nvp.TimeOut, nvp.StreamFun);
80+
[response, streamedText] = nvp.sendRequestFcn(parameters,nvp.APIKey, URL.EncodedURI, nvp.TimeOut, nvp.StreamFun);
7981
end
8082

8183
% If call errors, "choices" will not be part of response.Body.Data, instead

tests/tazureChat.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,31 @@ function sendsSystemPrompt(testCase)
6565
});
6666
testCase.verifyEqual(response,"Hello");
6767
end
68+
69+
function formatsURLCorrectly(testCase)
70+
[sendRequestMock,sendRequestBehaviour] = ...
71+
createMock(testCase, AddedMethods="sendRequest");
72+
testCase.assignOutputsWhen( ...
73+
withAnyInputs(sendRequestBehaviour.sendRequest),...
74+
testCase.responseMessage("Hello"),"This output is unused with Stream=false");
75+
76+
endpoint = "https://some.domain/without.final.forward.slash";
77+
deploymentID = "someDeploymentID";
78+
apiVersion = "2025-04-01-preview";
79+
chat = testCase.constructor("You are a helpful assistant", ...
80+
Endpoint=endpoint, ...
81+
DeploymentID=deploymentID, ...
82+
APIVersion=apiVersion);
83+
chat.sendRequestFcn = @(varargin) sendRequestMock.sendRequest(varargin{:});
84+
85+
testCase.verifyWarningFree(@() generate(chat,"Hi"));
86+
87+
calls = testCase.getMockHistory(sendRequestMock);
88+
URI = calls.Inputs{4};
89+
expectedURI = endpoint + "/openai/deployments/" + deploymentID + ...
90+
"/chat/completions?api-version=" + apiVersion;
91+
testCase.verifyEqual(URI, expectedURI);
92+
end
6893

6994
function endpointNotFound(testCase)
7095
% to verify the error, we need to unset the environment variable

0 commit comments

Comments
 (0)