Skip to content

Commit 492fefc

Browse files
committed
Short error messages for bad endpoints
Remove a very deep stack when presented with bad endpoints, including those with timeouts.
1 parent 95f2f13 commit 492fefc

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

azureChat.m

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
% CHAT = azureChat(endpoint, deploymentID) creates an azureChat object with the
55
% endpoint and deployment ID path parameters required by Azure to establish the connection.
66
%
7-
% CHAT = azureChat(__,systemPrompt) creates an azureChatobject with the
7+
% CHAT = azureChat(__,systemPrompt) creates an azureChat object with the
88
% specified system prompt.
99
%
1010
% CHAT = azureChat(__,Name=Value) specifies additional options
@@ -178,14 +178,25 @@
178178
end
179179

180180
toolChoice = convertToolChoice(this, nvp.ToolChoice);
181-
[text, message, response] = llms.internal.callAzureChatAPI(this.Endpoint, ...
182-
this.DeploymentID, messagesStruct, this.FunctionsStruct, ...
183-
ToolChoice=toolChoice, APIVersion = this.APIVersion, Temperature=this.Temperature, ...
184-
TopProbabilityMass=this.TopProbabilityMass, NumCompletions=nvp.NumCompletions,...
185-
StopSequences=this.StopSequences, MaxNumTokens=nvp.MaxNumTokens, ...
186-
PresencePenalty=this.PresencePenalty, FrequencyPenalty=this.FrequencyPenalty, ...
187-
ResponseFormat=this.ResponseFormat,Seed=nvp.Seed, ...
188-
APIKey=this.APIKey,TimeOut=this.TimeOut, StreamFun=this.StreamFun);
181+
try
182+
[text, message, response] = llms.internal.callAzureChatAPI(this.Endpoint, ...
183+
this.DeploymentID, messagesStruct, this.FunctionsStruct, ...
184+
ToolChoice=toolChoice, APIVersion = this.APIVersion, Temperature=this.Temperature, ...
185+
TopProbabilityMass=this.TopProbabilityMass, NumCompletions=nvp.NumCompletions,...
186+
StopSequences=this.StopSequences, MaxNumTokens=nvp.MaxNumTokens, ...
187+
PresencePenalty=this.PresencePenalty, FrequencyPenalty=this.FrequencyPenalty, ...
188+
ResponseFormat=this.ResponseFormat,Seed=nvp.Seed, ...
189+
APIKey=this.APIKey,TimeOut=this.TimeOut, StreamFun=this.StreamFun);
190+
catch ME
191+
if ismember(ME.identifier,...
192+
["MATLAB:webservices:UnknownHost","MATLAB:webservices:Timeout"])
193+
% throw(ME)would still print a long stack trace, from
194+
% ME.cause.stack. We cannot change ME.cause, so we
195+
% throw a new error:
196+
error(ME.identifier,ME.message);
197+
end
198+
rethrow(ME);
199+
end
189200

190201
if isfield(response.Body.Data,"error")
191202
err = response.Body.Data.error.message;

tests/tazureChat.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ function errorsWhenPassingToolChoiceWithEmptyTools(testCase)
104104
testCase.verifyError(@()generate(chat,"input", ToolChoice="bla"), "llms:mustSetFunctionsForCall");
105105
end
106106

107+
function shortErrorForBadEndpoint(testCase)
108+
chat = azureChat("https://nobodyhere.whatever/","deployment");
109+
caught = false;
110+
try
111+
generate(chat,"input");
112+
catch ME
113+
caught = ME;
114+
end
115+
testCase.assertClass(caught,"MException");
116+
testCase.verifyEqual(caught.identifier,'MATLAB:webservices:UnknownHost');
117+
testCase.verifyEmpty(caught.cause);
118+
end
119+
107120
function invalidInputsConstructor(testCase, InvalidConstructorInput)
108121
testCase.verifyError(@()azureChat(getenv("AZURE_OPENAI_ENDPOINT"), getenv("AZURE_OPENAI_DEPLOYMENT"), InvalidConstructorInput.Input{:}), InvalidConstructorInput.Error);
109122
end

0 commit comments

Comments
 (0)