Skip to content

Commit

Permalink
Added addToolMessage to openAIMessages
Browse files Browse the repository at this point in the history
AddFunctionMessage was rennamed to addToolMessage, added a new example to the existing MLX and error catalog was also updated.
  • Loading branch information
toshiakit committed Jan 17, 2024
1 parent adbddc2 commit fbfa569
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 20 deletions.
3 changes: 2 additions & 1 deletion +llms/+utils/errorMessageCatalog.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
catalog("llms:parameterMustBeUnique") = "A parameter name equivalent to '{1}' already exists in Parameters. Redefining a parameter is not allowed.";
catalog("llms:mustBeAssistantCall") = "Input struct must contain field 'role' with value 'assistant', and field 'content'.";
catalog("llms:mustBeAssistantWithContent") = "Input struct must contain field 'content' containing text with one or more characters.";
catalog("llms:mustBeAssistantWithNameAndArguments") = "Field 'function_call' must be a struct with fields 'name' and 'arguments'.";
catalog("llms:mustBeAssistantWithIdAndFunction") = "Field 'tool_call' must be a struct with fields 'id' and 'function'.";
catalog("llms:mustBeAssistantWithNameAndArguments") = "Field 'function' must be a struct with fields 'name' and 'arguments'.";
catalog("llms:assistantMustHaveTextNameAndArguments") = "Fields 'name' and 'arguments' must be text with one or more characters.";
catalog("llms:mustBeValidIndex") = "Value is larger than the number of elements in Messages ({1}).";
catalog("llms:stopSequencesMustHaveMax4Elements") = "Number of elements must not be larger than 4.";
Expand Down
Binary file modified examples/ExampleChatBot.mlx
Binary file not shown.
Binary file modified examples/ExampleParallelFunctionCalls.mlx
Binary file not shown.
59 changes: 40 additions & 19 deletions openAIMessages.m
Original file line number Diff line number Diff line change
Expand Up @@ -139,28 +139,31 @@

end

function this = addFunctionMessage(this, name, content)
%addFunctionMessage Add function message.
function this = addToolMessage(this, id, name, content)
%addToolMessage Add Tool message.
%
% MESSAGES = addFunctionMessage(MESSES, NAME, CONTENT) adds a function
% message with the specified name and content. NAME and
% CONTENT must be text scalars.
% MESSAGES = addFunctionMessage(MESSAGES, ID, NAME, CONTENT)
% adds a tool message with the specified id, name and content.
% ID, NAME and CONTENT must be text scalars.
%
% Example:
% % Create messages object
% messages = openAIMessages;
%
% % Add function message, containing the result of
% % calling strcat("Hello", " World")
% messages = addFunctionMessage(messages, "strcat", "Hello World");
% messages = addToolMessage(messages, "call_123", "strcat", "Hello World");

arguments
this (1,1) openAIMessages
id {mustBeNonzeroLengthTextScalar}
name {mustBeNonzeroLengthTextScalar}
content {mustBeNonzeroLengthTextScalar}

end

newMessage = struct("role", "function", "name", string(name), "content", string(content));
newMessage = struct("tool_call_id", id, "role", "tool", ...
"name", string(name), "content", string(content));
this.Messages{end+1} = newMessage;
end

Expand Down Expand Up @@ -201,9 +204,9 @@

% Assistant is asking for function call
if isfield(messageStruct, "tool_calls")
toolCall = messageStruct.tool_calls{1};
validateAssistantWithFunctionCall(toolCall.function)
this = addAssistantMessage(this, messageStruct.content, toolCall);
toolCalls = messageStruct.tool_calls;
validateAssistantWithToolCalls(toolCalls)
this = addAssistantMessage(this, messageStruct.content, toolCalls);
else
% Simple assistant response
validateRegularAssistant(messageStruct.content);
Expand Down Expand Up @@ -254,10 +257,19 @@
newMessage = struct("role", "assistant", "content", content);
else
% tool_calls message
functionCall = struct("name", toolCalls.function.name, "arguments", toolCalls.function.arguments);
toolsStruct = struct("id", toolCalls.id, "type", toolCalls.type, "function", functionCall);
toolsStruct = repmat(struct("id",[],"type",[],"function",[]),size(toolCalls));
for i = 1:numel(toolCalls)
toolsStruct(i).id = toolCalls(i).id;
toolsStruct(i).type = toolCalls(i).type;
toolsStruct(i).function = struct( ...
"name", toolCalls(i).function.name, ...
"arguments", toolCalls(i).function.arguments);
end

newMessage = struct("role", "assistant", "content", content, "tool_calls", toolsStruct);
newMessage.tool_calls = {newMessage.tool_calls};
if numel(newMessage.tool_calls) == 1
newMessage.tool_calls = {newMessage.tool_calls};
end
end

if isempty(this.Messages)
Expand All @@ -283,17 +295,26 @@ function validateRegularAssistant(content)
end
end

function validateAssistantWithFunctionCall(functionCallStruct)
if ~isstruct(functionCallStruct)||~isfield(functionCallStruct, "name")||~isfield(functionCallStruct, "arguments")
function validateAssistantWithToolCalls(toolCallStruct)
if ~isstruct(toolCallStruct)||~isfield(toolCallStruct, "id")||~isfield(toolCallStruct, "function")
error("llms:mustBeAssistantWithIdAndFunction", ...
llms.utils.errorMessageCatalog.getMessage("llms:mustBeAssistantWithIdAndFunction"))
else
functionCallStruct = [toolCallStruct.function];
end

if ~isfield(functionCallStruct, "name")||~isfield(functionCallStruct, "arguments")
error("llms:mustBeAssistantWithNameAndArguments", ...
llms.utils.errorMessageCatalog.getMessage("llms:mustBeAssistantWithNameAndArguments"))
end

try
mustBeNonzeroLengthText(functionCallStruct.name)
mustBeTextScalar(functionCallStruct.name)
mustBeNonzeroLengthText(functionCallStruct.arguments)
mustBeTextScalar(functionCallStruct.arguments)
for i = 1:numel(functionCallStruct)
mustBeNonzeroLengthText(functionCallStruct(i).name)
mustBeTextScalar(functionCallStruct(i).name)
mustBeNonzeroLengthText(functionCallStruct(i).arguments)
mustBeTextScalar(functionCallStruct(i).arguments)
end
catch ME
error("llms:assistantMustHaveTextNameAndArguments", ...
llms.utils.errorMessageCatalog.getMessage("llms:assistantMustHaveTextNameAndArguments"))
Expand Down
Loading

0 comments on commit fbfa569

Please sign in to comment.