|
14 | 14 | * See the License for the specific language governing permissions and
|
15 | 15 | * limitations under the License.
|
16 | 16 | */
|
17 |
| -import { expect } from 'chai'; |
| 17 | +import { use, expect } from 'chai'; |
18 | 18 | import { GenerativeModel } from './generative-model';
|
19 |
| -import { VertexAI } from '../public-types'; |
| 19 | +import { FunctionCallingMode, VertexAI } from '../public-types'; |
| 20 | +import * as request from '../requests/request'; |
| 21 | +import { match, restore, stub } from 'sinon'; |
| 22 | +import { getMockResponse } from '../../test-utils/mock-response'; |
| 23 | +import sinonChai from 'sinon-chai'; |
| 24 | + |
| 25 | +use(sinonChai); |
20 | 26 |
|
21 | 27 | const fakeVertexAI: VertexAI = {
|
22 | 28 | app: {
|
@@ -53,4 +59,157 @@ describe('GenerativeModel', () => {
|
53 | 59 | });
|
54 | 60 | expect(genModel.model).to.equal('tunedModels/my-model');
|
55 | 61 | });
|
| 62 | + it('passes params through to generateContent', async () => { |
| 63 | + const genModel = new GenerativeModel(fakeVertexAI, { |
| 64 | + model: 'my-model', |
| 65 | + tools: [{ functionDeclarations: [{ name: 'myfunc' }] }], |
| 66 | + toolConfig: { functionCallingConfig: { mode: FunctionCallingMode.NONE } }, |
| 67 | + systemInstruction: { role: 'system', parts: [{ text: 'be friendly' }] } |
| 68 | + }); |
| 69 | + expect(genModel.tools?.length).to.equal(1); |
| 70 | + expect(genModel.toolConfig?.functionCallingConfig.mode).to.equal( |
| 71 | + FunctionCallingMode.NONE |
| 72 | + ); |
| 73 | + expect(genModel.systemInstruction?.parts[0].text).to.equal('be friendly'); |
| 74 | + const mockResponse = getMockResponse( |
| 75 | + 'unary-success-basic-reply-short.json' |
| 76 | + ); |
| 77 | + const makeRequestStub = stub(request, 'makeRequest').resolves( |
| 78 | + mockResponse as Response |
| 79 | + ); |
| 80 | + await genModel.generateContent('hello'); |
| 81 | + expect(makeRequestStub).to.be.calledWith( |
| 82 | + 'publishers/google/models/my-model', |
| 83 | + request.Task.GENERATE_CONTENT, |
| 84 | + match.any, |
| 85 | + false, |
| 86 | + match((value: string) => { |
| 87 | + return ( |
| 88 | + value.includes('myfunc') && |
| 89 | + value.includes(FunctionCallingMode.NONE) && |
| 90 | + value.includes('be friendly') |
| 91 | + ); |
| 92 | + }), |
| 93 | + {} |
| 94 | + ); |
| 95 | + restore(); |
| 96 | + }); |
| 97 | + it('generateContent overrides model values', async () => { |
| 98 | + const genModel = new GenerativeModel(fakeVertexAI, { |
| 99 | + model: 'my-model', |
| 100 | + tools: [{ functionDeclarations: [{ name: 'myfunc' }] }], |
| 101 | + toolConfig: { functionCallingConfig: { mode: FunctionCallingMode.NONE } }, |
| 102 | + systemInstruction: { role: 'system', parts: [{ text: 'be friendly' }] } |
| 103 | + }); |
| 104 | + expect(genModel.tools?.length).to.equal(1); |
| 105 | + expect(genModel.toolConfig?.functionCallingConfig.mode).to.equal( |
| 106 | + FunctionCallingMode.NONE |
| 107 | + ); |
| 108 | + expect(genModel.systemInstruction?.parts[0].text).to.equal('be friendly'); |
| 109 | + const mockResponse = getMockResponse( |
| 110 | + 'unary-success-basic-reply-short.json' |
| 111 | + ); |
| 112 | + const makeRequestStub = stub(request, 'makeRequest').resolves( |
| 113 | + mockResponse as Response |
| 114 | + ); |
| 115 | + await genModel.generateContent({ |
| 116 | + contents: [{ role: 'user', parts: [{ text: 'hello' }] }], |
| 117 | + tools: [{ functionDeclarations: [{ name: 'otherfunc' }] }], |
| 118 | + toolConfig: { functionCallingConfig: { mode: FunctionCallingMode.AUTO } }, |
| 119 | + systemInstruction: { role: 'system', parts: [{ text: 'be formal' }] } |
| 120 | + }); |
| 121 | + expect(makeRequestStub).to.be.calledWith( |
| 122 | + 'publishers/google/models/my-model', |
| 123 | + request.Task.GENERATE_CONTENT, |
| 124 | + match.any, |
| 125 | + false, |
| 126 | + match((value: string) => { |
| 127 | + return ( |
| 128 | + value.includes('otherfunc') && |
| 129 | + value.includes(FunctionCallingMode.AUTO) && |
| 130 | + value.includes('be formal') |
| 131 | + ); |
| 132 | + }), |
| 133 | + {} |
| 134 | + ); |
| 135 | + restore(); |
| 136 | + }); |
| 137 | + it('passes params through to chat.sendMessage', async () => { |
| 138 | + const genModel = new GenerativeModel(fakeVertexAI, { |
| 139 | + model: 'my-model', |
| 140 | + tools: [{ functionDeclarations: [{ name: 'myfunc' }] }], |
| 141 | + toolConfig: { functionCallingConfig: { mode: FunctionCallingMode.NONE } }, |
| 142 | + systemInstruction: { role: 'system', parts: [{ text: 'be friendly' }] } |
| 143 | + }); |
| 144 | + expect(genModel.tools?.length).to.equal(1); |
| 145 | + expect(genModel.toolConfig?.functionCallingConfig.mode).to.equal( |
| 146 | + FunctionCallingMode.NONE |
| 147 | + ); |
| 148 | + expect(genModel.systemInstruction?.parts[0].text).to.equal('be friendly'); |
| 149 | + const mockResponse = getMockResponse( |
| 150 | + 'unary-success-basic-reply-short.json' |
| 151 | + ); |
| 152 | + const makeRequestStub = stub(request, 'makeRequest').resolves( |
| 153 | + mockResponse as Response |
| 154 | + ); |
| 155 | + await genModel.startChat().sendMessage('hello'); |
| 156 | + expect(makeRequestStub).to.be.calledWith( |
| 157 | + 'publishers/google/models/my-model', |
| 158 | + request.Task.GENERATE_CONTENT, |
| 159 | + match.any, |
| 160 | + false, |
| 161 | + match((value: string) => { |
| 162 | + return ( |
| 163 | + value.includes('myfunc') && |
| 164 | + value.includes(FunctionCallingMode.NONE) && |
| 165 | + value.includes('be friendly') |
| 166 | + ); |
| 167 | + }), |
| 168 | + {} |
| 169 | + ); |
| 170 | + restore(); |
| 171 | + }); |
| 172 | + it('startChat overrides model values', async () => { |
| 173 | + const genModel = new GenerativeModel(fakeVertexAI, { |
| 174 | + model: 'my-model', |
| 175 | + tools: [{ functionDeclarations: [{ name: 'myfunc' }] }], |
| 176 | + toolConfig: { functionCallingConfig: { mode: FunctionCallingMode.NONE } }, |
| 177 | + systemInstruction: { role: 'system', parts: [{ text: 'be friendly' }] } |
| 178 | + }); |
| 179 | + expect(genModel.tools?.length).to.equal(1); |
| 180 | + expect(genModel.toolConfig?.functionCallingConfig.mode).to.equal( |
| 181 | + FunctionCallingMode.NONE |
| 182 | + ); |
| 183 | + expect(genModel.systemInstruction?.parts[0].text).to.equal('be friendly'); |
| 184 | + const mockResponse = getMockResponse( |
| 185 | + 'unary-success-basic-reply-short.json' |
| 186 | + ); |
| 187 | + const makeRequestStub = stub(request, 'makeRequest').resolves( |
| 188 | + mockResponse as Response |
| 189 | + ); |
| 190 | + await genModel |
| 191 | + .startChat({ |
| 192 | + tools: [{ functionDeclarations: [{ name: 'otherfunc' }] }], |
| 193 | + toolConfig: { |
| 194 | + functionCallingConfig: { mode: FunctionCallingMode.AUTO } |
| 195 | + }, |
| 196 | + systemInstruction: { role: 'system', parts: [{ text: 'be formal' }] } |
| 197 | + }) |
| 198 | + .sendMessage('hello'); |
| 199 | + expect(makeRequestStub).to.be.calledWith( |
| 200 | + 'publishers/google/models/my-model', |
| 201 | + request.Task.GENERATE_CONTENT, |
| 202 | + match.any, |
| 203 | + false, |
| 204 | + match((value: string) => { |
| 205 | + return ( |
| 206 | + value.includes('otherfunc') && |
| 207 | + value.includes(FunctionCallingMode.AUTO) && |
| 208 | + value.includes('be formal') |
| 209 | + ); |
| 210 | + }), |
| 211 | + {} |
| 212 | + ); |
| 213 | + restore(); |
| 214 | + }); |
56 | 215 | });
|
0 commit comments