|
1 | 1 | function [text, message, response] = callOpenAIChatAPI(messages, functions, nvp)
|
2 |
| -% This function is undocumented and will change in a future release |
3 |
| - |
4 | 2 | %callOpenAIChatAPI Calls the openAI chat completions API.
|
5 | 3 | %
|
6 | 4 | % MESSAGES and FUNCTIONS should be structs matching the json format
|
7 | 5 | % required by the OpenAI Chat Completions API.
|
8 | 6 | % Ref: https://platform.openai.com/docs/guides/gpt/chat-completions-api
|
9 | 7 | %
|
10 | 8 | % Currently, the supported NVP are, including the equivalent name in the API:
|
11 |
| -% - FunctionCall (function_call) |
| 9 | +% - ToolChoice (tool_choice) |
12 | 10 | % - ModelName (model)
|
13 | 11 | % - Temperature (temperature)
|
14 | 12 | % - TopProbabilityMass (top_p)
|
|
17 | 15 | % - MaxNumTokens (max_tokens)
|
18 | 16 | % - PresencePenalty (presence_penalty)
|
19 | 17 | % - FrequencyPenalty (frequence_penalty)
|
| 18 | +% - ResponseFormat (response_format) |
| 19 | +% - Seed (seed) |
20 | 20 | % - ApiKey
|
21 | 21 | % - TimeOut
|
22 | 22 | % - StreamFun
|
|
50 | 50 | % % Send a request
|
51 | 51 | % [text, message] = llms.internal.callOpenAIChatAPI(messages, functions, ApiKey=apiKey)
|
52 | 52 |
|
53 |
| -% Copyright 2023 The MathWorks, Inc. |
| 53 | +% Copyright 2023-2024 The MathWorks, Inc. |
54 | 54 |
|
55 | 55 | arguments
|
56 | 56 | messages
|
57 | 57 | functions
|
58 |
| - nvp.FunctionCall = [] |
| 58 | + nvp.ToolChoice = [] |
59 | 59 | nvp.ModelName = "gpt-3.5-turbo"
|
60 | 60 | nvp.Temperature = 1
|
61 | 61 | nvp.TopProbabilityMass = 1
|
|
64 | 64 | nvp.MaxNumTokens = inf
|
65 | 65 | nvp.PresencePenalty = 0
|
66 | 66 | nvp.FrequencyPenalty = 0
|
| 67 | + nvp.ResponseFormat = "text" |
| 68 | + nvp.Seed = [] |
67 | 69 | nvp.ApiKey = ""
|
68 | 70 | nvp.TimeOut = 10
|
69 | 71 | nvp.StreamFun = []
|
|
85 | 87 | message = struct("role", "assistant", ...
|
86 | 88 | "content", streamedText);
|
87 | 89 | end
|
88 |
| - if isfield(message, "function_call") |
| 90 | + if isfield(message, "tool_choice") |
89 | 91 | text = "";
|
90 | 92 | else
|
91 | 93 | text = string(message.content);
|
|
105 | 107 |
|
106 | 108 | parameters.stream = ~isempty(nvp.StreamFun);
|
107 | 109 |
|
108 |
| -if ~isempty(functions) |
109 |
| - parameters.functions = functions; |
| 110 | +if ~isempty(functions) && ~strcmp(nvp.ModelName,'gpt-4-vision-preview') |
| 111 | + parameters.tools = functions; |
| 112 | +end |
| 113 | + |
| 114 | +if ~isempty(nvp.ToolChoice) && ~strcmp(nvp.ModelName,'gpt-4-vision-preview') |
| 115 | + parameters.tool_choice = nvp.ToolChoice; |
| 116 | +end |
| 117 | + |
| 118 | +if ismember(nvp.ModelName,["gpt-3.5-turbo-1106","gpt-4-1106-preview"]) |
| 119 | + if strcmp(nvp.ResponseFormat,"json") |
| 120 | + parameters.response_format = struct('type','json_object'); |
| 121 | + end |
110 | 122 | end
|
111 | 123 |
|
112 |
| -if ~isempty(nvp.FunctionCall) |
113 |
| - parameters.function_call = nvp.FunctionCall; |
| 124 | +if ~isempty(nvp.Seed) |
| 125 | + parameters.seed = nvp.Seed; |
114 | 126 | end
|
115 | 127 |
|
116 | 128 | parameters.model = nvp.ModelName;
|
117 | 129 |
|
118 | 130 | dict = mapNVPToParameters;
|
119 | 131 |
|
120 | 132 | nvpOptions = keys(dict);
|
121 |
| -for i=1:length(nvpOptions) |
122 |
| - if isfield(nvp, nvpOptions(i)) |
123 |
| - parameters.(dict(nvpOptions(i))) = nvp.(nvpOptions(i)); |
| 133 | +if strcmp(nvp.ModelName,'gpt-4-vision-preview') |
| 134 | + nvpOptions(ismember(nvpOptions,["MaxNumTokens","StopSequences"])) = []; |
| 135 | +end |
| 136 | + |
| 137 | +for opt = nvpOptions.' |
| 138 | + if isfield(nvp, opt) |
| 139 | + parameters.(dict(opt)) = nvp.(opt); |
124 | 140 | end
|
125 | 141 | end
|
126 | 142 | end
|
|
0 commit comments