|
1 | | -function [text, message, response] = callOllamaChatAPI(model, messages, nvp) |
| 1 | +function [text, message, response] = callOllamaChatAPI(model, messages, functions, nvp) |
2 | 2 | % This function is undocumented and will change in a future release |
3 | 3 |
|
4 | 4 | %callOllamaChatAPI Calls the Ollama™ chat completions API. |
|
22 | 22 | % % Send a request |
23 | 23 | % [text, message] = llms.internal.callOllamaChatAPI(model, messages) |
24 | 24 |
|
25 | | -% Copyright 2023-2024 The MathWorks, Inc. |
| 25 | +% Copyright 2023-2025 The MathWorks, Inc. |
26 | 26 |
|
27 | 27 | arguments |
28 | 28 | model |
29 | 29 | messages |
| 30 | + functions |
| 31 | + nvp.ToolChoice |
30 | 32 | nvp.Temperature |
31 | 33 | nvp.TopP |
32 | 34 | nvp.MinP |
|
52 | 54 | nvp.StopSequences = [nvp.StopSequences, nvp.StopSequences]; |
53 | 55 | end |
54 | 56 |
|
55 | | -parameters = buildParametersCall(model, messages, nvp); |
| 57 | +parameters = buildParametersCall(model, messages, functions, nvp); |
56 | 58 |
|
57 | 59 | [response, streamedText] = llms.internal.sendRequestWrapper(parameters,[],URL,nvp.TimeOut,nvp.StreamFun); |
58 | 60 |
|
|
61 | 63 | if response.StatusCode=="OK" |
62 | 64 | % Outputs the first generation |
63 | 65 | if isempty(nvp.StreamFun) |
64 | | - message = response.Body.Data.message; |
| 66 | + if iscell(response.Body.Data) |
| 67 | + message = response.Body.Data{1}.message; |
| 68 | + else |
| 69 | + message = response.Body.Data.message; |
| 70 | + end |
65 | 71 | else |
66 | 72 | message = struct("role", "assistant", ... |
67 | 73 | "content", streamedText); |
|
73 | 79 | end |
74 | 80 | end |
75 | 81 |
|
76 | | -function parameters = buildParametersCall(model, messages, nvp) |
| 82 | +function parameters = buildParametersCall(model, messages, functions, nvp) |
77 | 83 | % Builds a struct in the format that is expected by the API, combining |
78 | 84 | % MESSAGES, FUNCTIONS and parameters in NVP. |
79 | 85 |
|
|
83 | 89 |
|
84 | 90 | parameters.stream = ~isempty(nvp.StreamFun); |
85 | 91 |
|
| 92 | +if ~isempty(functions) |
| 93 | + parameters.tools = functions; |
| 94 | +end |
| 95 | + |
| 96 | +if ~isempty(nvp.ToolChoice) |
| 97 | + parameters.tool_choice = nvp.ToolChoice; |
| 98 | +end |
| 99 | + |
86 | 100 | options = struct; |
87 | 101 |
|
88 | 102 | if strcmp(nvp.ResponseFormat,"json") |
|
0 commit comments