Skip to content

Commit f909676

Browse files
hjh320ccreutzi
authored andcommitted
Fix ollamaChat.models for non-homogeneous responses
1 parent 90d6c66 commit f909676

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

ollamaChat.m

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@
300300
end
301301
end
302302

303-
methods(Static)
303+
methods (Static)
304304
function mdls = models
305305
%ollamaChat.models - return models available on Ollama server
306306
% MDLS = ollamaChat.models returns a string vector MDLS
@@ -312,7 +312,17 @@
312312
% "phi".
313313
endpoint = "http://localhost:11434/api/tags";
314314
response = webread(endpoint);
315-
mdls = string({response.models.name}).';
315+
mdls = ollamaChat.extractModelNames(response.models);
316+
end
317+
end
318+
319+
methods (Static, Access=?tollamaChat)
320+
function mdls = extractModelNames(models)
321+
if isstruct(models)
322+
mdls = string({models.name}).';
323+
else
324+
mdls = cellfun(@(md)string(md.name), models);
325+
end
316326
baseMdls = unique(extractBefore(mdls,":latest"));
317327
% remove all those "mistral:latest", iff those are the only
318328
% model entries pointing at some model

tests/tollamaChat.m

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
InvalidValuesSetters = iGetInvalidValuesSetters;
1010
ValidValuesSetters = iGetValidValuesSetters;
1111
StringInputs = struct('string',{"hi"},'char',{'hi'},'cellstr',{{'hi'}});
12+
ModelDataFromTagsResponse = iGetModelDataFromTagsResponse
1213
end
1314

1415
properties
@@ -396,6 +397,11 @@ function queryModels(testCase)
396397
testCase.verifyThat(models, ...
397398
matlab.unittest.constraints.IsSupersetOf("mistral-nemo"));
398399
end
400+
401+
function extractModelNamesFromTagsResponse(testCase, ModelDataFromTagsResponse)
402+
mdlNames = ollamaChat.extractModelNames(ModelDataFromTagsResponse);
403+
testCase.verifyEqual(mdlNames, ["model1"; "model1:latest"; "model1:old"; "model2"]);
404+
end
399405
end
400406

401407
methods
@@ -609,3 +615,26 @@ function queryModels(testCase)
609615
"Error","MATLAB:validators:mustBePositive"));
610616
end
611617

618+
function modelData = iGetModelDataFromTagsResponse
619+
modelData = struct();
620+
modelData.HomogeneousModelData = [ ...
621+
struct( ...
622+
"name", "model1:old", ...
623+
"size", 123); ...
624+
struct( ...
625+
"name", "model1:latest", ...
626+
"size", 456); ...
627+
struct( ...
628+
"name", "model2:latest", ...
629+
"size", 789)];
630+
modelData.NonHomogeneousModelData = { ...
631+
struct( ...
632+
"name", "model1:old", ...
633+
"someName", 123); ...
634+
struct( ...
635+
"name", "model1:latest", ...
636+
"someOtherName", 456); ...
637+
struct( ...
638+
"name", "model2:latest", ...
639+
"yetAnotherName", 789)};
640+
end

0 commit comments

Comments
 (0)