Skip to content

Commit 76565a5

Browse files
committed
adding TimeOut option
1 parent f774734 commit 76565a5

File tree

4 files changed

+58
-20
lines changed

4 files changed

+58
-20
lines changed

+llms/+internal/callOpenAIChatAPI.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
% - PresencePenalty (presence_penalty)
1919
% - FrequencyPenalty (frequence_penalty)
2020
% - ApiKey
21+
% - TimeOut
2122
% More details on the parameters: https://platform.openai.com/docs/api-reference/chat/create
2223
%
2324
% Example
@@ -63,13 +64,14 @@
6364
nvp.PresencePenalty = 0
6465
nvp.FrequencyPenalty = 0
6566
nvp.ApiKey = ""
67+
nvp.TimeOut = 10
6668
end
6769

6870
END_POINT = "https://api.openai.com/v1/chat/completions";
6971

7072
parameters = buildParametersCall(messages, functions, nvp);
7173

72-
response = llms.internal.sendRequest(parameters,nvp.ApiKey, END_POINT);
74+
response = llms.internal.sendRequest(parameters,nvp.ApiKey, END_POINT, nvp.TimeOut);
7375

7476
% If call errors, "choices" will not be part of response.Body.Data, instead
7577
% we get response.Body.Data.error

+llms/+internal/sendRequest.m

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
1-
function response = sendRequest(parameters, token, endpoint)
1+
function response = sendRequest(parameters, token, endpoint, timeout)
22
% This function is undocumented and will change in a future release
33

4-
%sendRequest Sends a request to an ENDPOINT using PARAMETERS and
5-
% api key TOKEN.
4+
%sendRequest Sends a request to an ENDPOINT using PARAMETERS and
5+
% api key TOKEN. TIMEOUT is the nubmer of seconds to wait for initial
6+
% server connection.
67

78
% Copyright 2023 The MathWorks, Inc.
89

9-
% Define the headers for the API request
10-
11-
headers = [matlab.net.http.HeaderField('Content-Type', 'application/json')...
12-
matlab.net.http.HeaderField('Authorization', "Bearer " + token)];
13-
% Define the request message
14-
request = matlab.net.http.RequestMessage('post',headers,parameters);
15-
% Send the request and store the response
16-
response = send(request, matlab.net.URI(endpoint));
10+
arguments
11+
parameters
12+
token
13+
endpoint
14+
timeout
1715
end
1816

17+
% Define the headers for the API request
18+
19+
headers = [matlab.net.http.HeaderField('Content-Type', 'application/json')...
20+
matlab.net.http.HeaderField('Authorization', "Bearer " + token)];
21+
22+
% Define the request message
23+
request = matlab.net.http.RequestMessage('post',headers,parameters);
24+
25+
% Create a HTTPOptions object;
26+
httpOpts = matlab.net.http.HTTPOptions;
27+
% Set the ConnectTimeout option
28+
29+
httpOpts.ConnectTimeout = timeout;
30+
% Send the request and store the response
31+
response = send(request, matlab.net.URI(endpoint),httpOpts);
32+
end

openAIChat.m

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
%
5555
% FunctionNames - Names of the functions that the model can
5656
% request calls.
57+
%
58+
% TimeOut - Connection Timeout in seconds (default: 10 secs)
59+
%
5760

5861
% Copyright 2023 The MathWorks, Inc.
5962

@@ -71,10 +74,13 @@
7174
PresencePenalty
7275

7376
%FREQUENCYPENALTY Penalty for using a token that is frequent in the training data.
74-
FrequencyPenalty
77+
FrequencyPenalty
7578
end
7679

77-
properties(SetAccess=private)
80+
properties(SetAccess=private)
81+
%TIMEOUT Connection timeout in seconds (default 10 secs)
82+
TimeOut
83+
7884
%FUNCTIONNAMES Names of the functions that the model can request calls
7985
FunctionNames
8086

@@ -88,7 +94,7 @@
8894
properties(Access=private)
8995
Functions
9096
FunctionsStruct
91-
ApiKey
97+
ApiKey
9298
end
9399

94100
methods
@@ -97,14 +103,15 @@
97103
systemPrompt {llms.utils.mustBeTextOrEmpty} = []
98104
nvp.Functions (1,:) {mustBeA(nvp.Functions, "openAIFunction")} = openAIFunction.empty
99105
nvp.ModelName (1,1) {mustBeMember(nvp.ModelName,["gpt-4", "gpt-4-0613", "gpt-4-32k", ...
100-
"gpt-3.5-turbo", "gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k",...
101-
"gpt-3.5-turbo-16k-0613", "gpt-4-1106-preview"])} = "gpt-3.5-turbo"
106+
"gpt-3.5-turbo", "gpt-3.5-turbo-16k",...
107+
"gpt-4-1106-preview","gpt-3.5-turbo-1106"])} = "gpt-3.5-turbo"
102108
nvp.Temperature {mustBeValidTemperature} = 1
103109
nvp.TopProbabilityMass {mustBeValidTopP} = 1
104110
nvp.StopSequences {mustBeValidStop} = {}
105111
nvp.ApiKey {mustBeNonzeroLengthTextScalar}
106112
nvp.PresencePenalty {mustBeValidPenalty} = 0
107113
nvp.FrequencyPenalty {mustBeValidPenalty} = 0
114+
nvp.TimeOut (1,1) {mustBeReal,mustBePositive} = 10
108115
end
109116

110117
if ~isempty(nvp.Functions)
@@ -130,6 +137,7 @@
130137
this.PresencePenalty = nvp.PresencePenalty;
131138
this.FrequencyPenalty = nvp.FrequencyPenalty;
132139
this.ApiKey = llms.internal.getApiKeyFromNvpOrEnv(nvp);
140+
this.TimeOut = nvp.TimeOut;
133141
end
134142

135143
function [text, message, response] = generate(this, messages, nvp)
@@ -174,7 +182,7 @@
174182
TopProbabilityMass=this.TopProbabilityMass, NumCompletions=nvp.NumCompletions,...
175183
StopSequences=this.StopSequences, MaxNumTokens=nvp.MaxNumTokens, ...
176184
PresencePenalty=this.PresencePenalty, FrequencyPenalty=this.FrequencyPenalty, ...
177-
ApiKey=this.ApiKey);
185+
ApiKey=this.ApiKey,TimeOut=this.TimeOut);
178186
end
179187

180188
function this = set.Temperature(this, temperature)

tests/topenAIChat.m

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ function constructChatWithAllNVP(testCase)
5151
presenceP = -2;
5252
frequenceP = 2;
5353
systemPrompt = "This is a system prompt";
54+
timeout = 3;
5455
chat = openAIChat(systemPrompt, Functions=functions, ModelName=modelName, ...
5556
Temperature=temperature, TopProbabilityMass=topP, StopSequences=stop, ApiKey=apiKey,...
56-
FrequencyPenalty=frequenceP, PresencePenalty=presenceP);
57+
FrequencyPenalty=frequenceP, PresencePenalty=presenceP, TimeOut=timeout);
5758
testCase.verifyEqual(chat.ModelName, modelName);
5859
testCase.verifyEqual(chat.Temperature, temperature);
5960
testCase.verifyEqual(chat.TopProbabilityMass, topP);
@@ -62,6 +63,11 @@ function constructChatWithAllNVP(testCase)
6263
testCase.verifyEqual(chat.PresencePenalty, presenceP);
6364
end
6465

66+
function verySmallTimeOutErrors(testCase)
67+
chat = openAIChat(TimeOut=0.0001, ApiKey="false-key");
68+
testCase.verifyError(@()generate(chat, "hi"), "MATLAB:webservices:Timeout")
69+
end
70+
6571
function errorsWhenPassingFunctionCallWithEmptyFunctions(testCase)
6672
chat = openAIChat(ApiKey="this-is-not-a-real-key");
6773
testCase.verifyError(@()generate(chat,"input", FunctionCall="bla"), "llms:mustSetFunctionsForCall");
@@ -90,7 +96,7 @@ function assignValueToProperty(property, value)
9096

9197
function invalidValuesSetters = iGetInvalidValuesSetters
9298

93-
invalidValuesSetters = struct( ...
99+
invalidValuesSetters = struct( ...
94100
"InvalidTemperatureType", struct( ...
95101
"Property", "Temperature", ...
96102
"Value", "2", ...
@@ -195,6 +201,14 @@ function assignValueToProperty(property, value)
195201
function invalidConstructorInput = iGetInvalidConstructorInput
196202
validFunction = openAIFunction("funName");
197203
invalidConstructorInput = struct( ...
204+
"InvalidTimeOutType", struct( ...
205+
"Input",{{"TimeOut", "2" }},...
206+
"Error", "MATLAB:validators:mustBeReal"), ...
207+
...
208+
"InvalidTimeOutSize", struct( ...
209+
"Input",{{"TimeOut", [1 1 1] }},...
210+
"Error", "MATLAB:validation:IncompatibleSize"), ...
211+
...
198212
"WrongTypeSystemPrompt",struct( ...
199213
"Input",{{ 123 }},...
200214
"Error","MATLAB:validators:mustBeTextScalar"),...

0 commit comments

Comments
 (0)