Skip to content

Commit

Permalink
adding tests and minor fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
debymf committed Jan 16, 2024
1 parent bd780e2 commit adbddc2
Show file tree
Hide file tree
Showing 22 changed files with 780 additions and 441 deletions.
4 changes: 1 addition & 3 deletions +llms/+internal/callOpenAIChatAPI.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
function [text, message, response] = callOpenAIChatAPI(messages, functions, nvp)
% This function is undocumented and will change in a future release

%callOpenAIChatAPI Calls the openAI chat completions API.
%
% MESSAGES and FUNCTIONS should be structs matching the json format
Expand Down Expand Up @@ -52,7 +50,7 @@
% % Send a request
% [text, message] = llms.internal.callOpenAIChatAPI(messages, functions, ApiKey=apiKey)

% Copyright 2023 The MathWorks, Inc.
% Copyright 2023-2024 The MathWorks, Inc.

arguments
messages
Expand Down
18 changes: 10 additions & 8 deletions +llms/+utils/errorMessageCatalog.m
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
classdef errorMessageCatalog
% This class is undocumented and will change in a future release

%errorMessageCatalog Stores the error messages from this repository

% Copyright 2023 The MathWorks, Inc.
% Copyright 2023-2024 The MathWorks, Inc.

properties(Constant)
%CATALOG dictionary mapping error ids to error msgs
Catalog = buildErrorMessageCatalog;
end

methods(Static)
function msg = getMessage(messageId, slot)
% This function is undocumented and will change in a future release

%getMessage returns error message given a messageID and a SLOT.
% The value in SLOT should be ordered, where the n-th element
% will replace the value "{n}".
Expand Down Expand Up @@ -47,7 +44,12 @@
catalog("llms:stopSequencesMustHaveMax4Elements") = "Number of elements must not be larger than 4.";
catalog("llms:keyMustBeSpecified") = "API key not found as environment variable OPENAI_API_KEY and not specified via ApiKey parameter.";
catalog("llms:mustHaveMessages") = "Value must contain at least one message in Messages.";
catalog("llms:mustSetFunctionsForCall") = "When no functions are defined, FunctionCall must not be specified.";
catalog("llms:mustSetFunctionsForCall") = "When no functions are defined, ToolChoice must not be specified.";
catalog("llms:mustBeMessagesOrTxt") = "Messages must be text with one or more characters or an openAIMessages objects.";
end

catalog("llms:invalidOptionAndValueForModel") = "'{1}' with value '{2}' is not supported for ModelName '{3}'";
catalog("llms:invalidOptionForModel") = "{1} is not supported for ModelName '{2}'";
catalog("llms:functionNotAvailableForModel") = "This function is not supported for ModelName '{1}'";
catalog("llms:promptLimitCharacter") = "Prompt must have a maximum length of {1} characters for ModelName '{2}'";
catalog("llms:pngExpected") = "Argument must be a PNG image.";
catalog("llms:warningJsonInstruction") = "When using JSON mode, you must also prompt the model to produce JSON yourself via a system or user message.";
end
Binary file added examples/ExampleDALLE.mlx
Binary file not shown.
Binary file modified examples/ExampleFunctionCalling.mlx
Binary file not shown.
Binary file added examples/ExampleGPT4Vision.mlx
Binary file not shown.
Binary file added examples/ExampleJSONMode.mlx
Binary file not shown.
Binary file added examples/ExampleParallelFunctionCalls.mlx
Binary file not shown.
Binary file added examples/ExampleStreaming.mlx
Binary file not shown.
Binary file removed examples/Example_DALL·E.mlx
Binary file not shown.
Binary file removed examples/Example_GPT4_Vision.mlx
Binary file not shown.
Binary file removed examples/Example_JSON_Mode.mlx
Binary file not shown.
Binary file removed examples/Example_Parallel_Function_Calls.mlx
Binary file not shown.
Binary file removed examples/Example_Streaming.mlx
Binary file not shown.
Binary file removed examples/images/bear_mask.png
Binary file not shown.
Binary file added examples/images/mask_bear.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 13 additions & 13 deletions openAIChat.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
% TimeOut - Connection Timeout in seconds (default: 10 secs)
%

% Copyright 2023 The MathWorks, Inc.
% Copyright 2023-2024 The MathWorks, Inc.

properties
%TEMPERATURE Temperature of generation.
Expand All @@ -83,10 +83,6 @@

%FREQUENCYPENALTY Penalty for using a token that is frequent in the training data.
FrequencyPenalty

%RESPONSEFORMAT Response format, text or json
ResponseFormat

end

properties(SetAccess=private)
Expand All @@ -101,6 +97,9 @@

%SYSTEMPROMPT System prompt.
SystemPrompt = []

%RESPONSEFORMAT Response format, text or json
ResponseFormat
end

properties(Access=private)
Expand Down Expand Up @@ -156,18 +155,19 @@
this.Temperature = nvp.Temperature;
this.TopProbabilityMass = nvp.TopProbabilityMass;
this.StopSequences = nvp.StopSequences;
% Response Format is supported in the latest models only
if strcmp(nvp.ResponseFormat,"json")

% ResponseFormat is only supported in the latest models only
if (nvp.ResponseFormat == "json")
if ismember(this.ModelName,["gpt-3.5-turbo-1106","gpt-4-1106-preview"])
if contains(this.SystemPrompt{1}.content,"designed to output to JSON")
this.ResponseFormat = nvp.ResponseFormat;
else
error("To get JSON output, add 'designed to output to JSON' to the system prompt.")
end
warning("llms:warningJsonInstruction", ...
llms.utils.errorMessageCatalog.getMessage("llms:warningJsonInstruction"))
else
mustBeMember(this.ModelName,["gpt-3.5-turbo-1106","gpt-4-1106-preview"])
error("llms:invalidOptionAndValueForModel", ...
llms.utils.errorMessageCatalog.getMessage("llms:invalidOptionAndValueForModel", "ResponseFormat", "json", this.ModelName));
end

end

this.PresencePenalty = nvp.PresencePenalty;
this.FrequencyPenalty = nvp.FrequencyPenalty;
this.ApiKey = llms.internal.getApiKeyFromNvpOrEnv(nvp);
Expand Down
Loading

0 comments on commit adbddc2

Please sign in to comment.