Skip to content

Commit 3b83a23

Browse files
committed
Adding new features and examples
Adding support and examples for JSON mode Parallel Function Calling GPT4 with Vision DALL·E
1 parent a4baf34 commit 3b83a23

13 files changed

+497
-29
lines changed

+llms/+internal/callOpenAIChatAPI.m

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
% Ref: https://platform.openai.com/docs/guides/gpt/chat-completions-api
99
%
1010
% Currently, the supported NVP are, including the equivalent name in the API:
11-
% - FunctionCall (function_call)
11+
% - ToolChoice (tool_choice)
1212
% - ModelName (model)
1313
% - Temperature (temperature)
1414
% - TopProbabilityMass (top_p)
@@ -17,6 +17,8 @@
1717
% - MaxNumTokens (max_tokens)
1818
% - PresencePenalty (presence_penalty)
1919
% - FrequencyPenalty (frequence_penalty)
20+
% - ResponseFormat (response_format)
21+
% - Seed (seed)
2022
% - ApiKey
2123
% - TimeOut
2224
% - StreamFun
@@ -55,7 +57,7 @@
5557
arguments
5658
messages
5759
functions
58-
nvp.FunctionCall = []
60+
nvp.ToolChoice = []
5961
nvp.ModelName = "gpt-3.5-turbo"
6062
nvp.Temperature = 1
6163
nvp.TopProbabilityMass = 1
@@ -64,6 +66,8 @@
6466
nvp.MaxNumTokens = inf
6567
nvp.PresencePenalty = 0
6668
nvp.FrequencyPenalty = 0
69+
nvp.ResponseFormat = "text"
70+
nvp.Seed = []
6771
nvp.ApiKey = ""
6872
nvp.TimeOut = 10
6973
nvp.StreamFun = []
@@ -85,7 +89,7 @@
8589
message = struct("role", "assistant", ...
8690
"content", streamedText);
8791
end
88-
if isfield(message, "function_call")
92+
if isfield(message, "tool_choice")
8993
text = "";
9094
else
9195
text = string(message.content);
@@ -105,19 +109,33 @@
105109

106110
parameters.stream = ~isempty(nvp.StreamFun);
107111

108-
if ~isempty(functions)
109-
parameters.functions = functions;
112+
if ~isempty(functions) && ~strcmp(nvp.ModelName,'gpt-4-vision-preview')
113+
parameters.tools = functions;
110114
end
111115

112-
if ~isempty(nvp.FunctionCall)
113-
parameters.function_call = nvp.FunctionCall;
116+
if ~isempty(nvp.ToolChoice) && ~strcmp(nvp.ModelName,'gpt-4-vision-preview')
117+
parameters.tool_choice = nvp.ToolChoice;
118+
end
119+
120+
if ismember(nvp.ModelName,["gpt-3.5-turbo-1106","gpt-4-1106-preview"])
121+
if strcmp(nvp.ResponseFormat,"json")
122+
parameters.response_format = struct('type','json_object');
123+
end
124+
end
125+
126+
if ~isempty(nvp.Seed)
127+
parameters.seed = nvp.Seed;
114128
end
115129

116130
parameters.model = nvp.ModelName;
117131

118132
dict = mapNVPToParameters;
119133

120134
nvpOptions = keys(dict);
135+
if strcmp(nvp.ModelName,'gpt-4-vision-preview')
136+
nvpOptions(ismember(nvpOptions,["MaxNumTokens","StopSequences"])) = [];
137+
end
138+
121139
for i=1:length(nvpOptions)
122140
if isfield(nvp, nvpOptions(i))
123141
parameters.(dict(nvpOptions(i))) = nvp.(nvpOptions(i));

.gitattributes

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
*.fig binary
2+
*.mat binary
3+
*.mdl binary diff merge=mlAutoMerge
4+
*.mdlp binary
5+
*.mexa64 binary
6+
*.mexw64 binary
7+
*.mexmaci64 binary
8+
*.mlapp binary
9+
*.mldatx binary
10+
*.mlproj binary
11+
*.mlx binary
12+
*.p binary
13+
*.sfx binary
14+
*.sldd binary
15+
*.slreqx binary merge=mlAutoMerge
16+
*.slmx binary merge=mlAutoMerge
17+
*.sltx binary
18+
*.slxc binary
19+
*.slx binary merge=mlAutoMerge
20+
*.slxp binary
21+
22+
## Other common binary file types
23+
*.docx binary
24+
*.exe binary
25+
*.jpg binary
26+
*.pdf binary
27+
*.png binary
28+
*.xlsx binary

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.env
2+
*.asv

examples/Example_DALL·E.mlx

2.45 MB
Binary file not shown.

examples/Example_GPT4_Vision.mlx

1.17 MB
Binary file not shown.

examples/Example_JSON_Mode.mlx

4.75 KB
Binary file not shown.
4.47 KB
Binary file not shown.

examples/Example_Streaming.mlx

5.61 KB
Binary file not shown.

examples/images/bear.png

2.15 MB
Loading

examples/images/bear_mask.png

2.39 MB
Loading

0 commit comments

Comments
 (0)