|
1 | | -classdef (Abstract) htoolCalls < matlab.mock.TestCase |
| 1 | +classdef (Abstract) htoolCalls < hmockSendRequest |
2 | 2 | % Tests for backends with tool calls |
3 | 3 |
|
4 | 4 | % Copyright 2023-2025 The MathWorks, Inc. |
5 | 5 | properties(Abstract) |
6 | 6 | constructor |
7 | 7 | defaultModel |
8 | 8 | end |
| 9 | + |
| 10 | + properties(TestParameter) |
| 11 | + ToolsForChatConstruction = {openAIFunction.empty, openAIFunction("SomeToolNameForChat")} |
| 12 | + end |
9 | 13 |
|
10 | 14 | methods (Test) % calling the server, end-to-end tests |
11 | 15 | function generateWithToolsAndStreamFunc(testCase) |
@@ -84,4 +88,78 @@ function generateWithToolsAndStreamFunc(testCase) |
84 | 88 | testCase.verifyThat(data,HasField("explanation")); |
85 | 89 | end |
86 | 90 | end |
| 91 | + |
| 92 | + methods (Test) % generate Tools tests without calling the server |
| 93 | + |
| 94 | + function generateWithEmptyToolsSwitchesOffChatTools(testCase) |
| 95 | + import matlab.unittest.constraints.HasField |
| 96 | + |
| 97 | + sendRequestMock = testCase.setUpSendRequestMock; |
| 98 | + |
| 99 | + chat = testCase.constructor("You are a helpful assistant", Tools=openAIFunction("SomeToolNameForChat")); |
| 100 | + chat.sendRequestFcn = @(varargin) sendRequestMock.sendRequest(varargin{:}); |
| 101 | + |
| 102 | + emptyTools = openAIFunction.empty; |
| 103 | + testCase.verifyWarningFree(@() generate(chat,"Hi",Tools=emptyTools)); |
| 104 | + |
| 105 | + calls = testCase.getMockHistory(sendRequestMock); |
| 106 | + |
| 107 | + testCase.verifySize(calls,[1,1]); |
| 108 | + sentHistory = calls.Inputs{2}; |
| 109 | + testCase.verifyFalse(isfield(sentHistory,'tools')) |
| 110 | + end |
| 111 | + |
| 112 | + function generateSendsOverriddenTools(testCase, ToolsForChatConstruction) |
| 113 | + import matlab.unittest.constraints.HasField |
| 114 | + |
| 115 | + sendRequestMock = testCase.setUpSendRequestMock; |
| 116 | + |
| 117 | + chat = testCase.constructor("You are a helpful assistant", Tools=ToolsForChatConstruction); |
| 118 | + chat.sendRequestFcn = @(varargin) sendRequestMock.sendRequest(varargin{:}); |
| 119 | + |
| 120 | + tools = openAIFunction("ToolForGenerate"); |
| 121 | + response = testCase.verifyWarningFree(@() generate(chat,"Hi",Tools=tools)); |
| 122 | + |
| 123 | + calls = testCase.getMockHistory(sendRequestMock); |
| 124 | + |
| 125 | + testCase.verifySize(calls,[1,1]); |
| 126 | + sentHistory = calls.Inputs{2}; |
| 127 | + testCase.verifyThat(sentHistory,HasField("tools")); |
| 128 | + expectedTool = struct('type', 'function', ... |
| 129 | + 'function', struct('name', "ToolForGenerate", ... |
| 130 | + 'parameters', struct('type', "object", ... |
| 131 | + 'properties', struct()))); |
| 132 | + testCase.verifySize(sentHistory.tools, [1,1]); |
| 133 | + testCase.verifyEqual(sentHistory.tools{1}, expectedTool); |
| 134 | + testCase.verifyEqual(response,"Hello"); |
| 135 | + end |
| 136 | + |
| 137 | + function generateWithNoToolsDoesNotOverrideTopLevelTools(testCase) |
| 138 | + import matlab.unittest.constraints.HasField |
| 139 | + |
| 140 | + sendRequestMock = testCase.setUpSendRequestMock; |
| 141 | + |
| 142 | + tools = openAIFunction("funNameAtTopLevel"); |
| 143 | + chat = testCase.constructor("You are a helpful assistant", Tools=tools); |
| 144 | + chat.sendRequestFcn = @(varargin) sendRequestMock.sendRequest(varargin{:}); |
| 145 | + |
| 146 | + response = testCase.verifyWarningFree(@() generate(chat,"Hi")); |
| 147 | + |
| 148 | + calls = testCase.getMockHistory(sendRequestMock); |
| 149 | + |
| 150 | + testCase.verifySize(calls,[1,1]); |
| 151 | + sentHistory = calls.Inputs{2}; |
| 152 | + testCase.verifyThat(sentHistory,HasField("tools")); |
| 153 | + expectedTool = struct( ... |
| 154 | + 'type', 'function', ... |
| 155 | + 'function', struct( ... |
| 156 | + 'name', "funNameAtTopLevel", ... |
| 157 | + 'parameters', struct( ... |
| 158 | + 'type', "object", ... |
| 159 | + 'properties', struct()))); |
| 160 | + testCase.verifySize(sentHistory.tools, [1,1]); |
| 161 | + testCase.verifyEqual(sentHistory.tools{1}, expectedTool); |
| 162 | + testCase.verifyEqual(response,"Hello"); |
| 163 | + end |
| 164 | + end |
87 | 165 | end |
0 commit comments