|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +const {expect} = require('chai'); |
| 20 | +const {afterEach, describe, it} = require('mocha'); |
| 21 | +const sinon = require('sinon'); |
| 22 | +const gemma2PredictGpu = require('../gemma2PredictGpu.js'); |
| 23 | +const gemma2PredictTpu = require('../gemma2PredictTpu.js'); |
| 24 | + |
| 25 | +const gpuResponse = `The sky appears blue due to a phenomenon called **Rayleigh scattering**. |
| 26 | +**Here's how it works:** |
| 27 | +1. **Sunlight:** Sunlight is composed of all the colors of the rainbow. |
| 28 | +2. **Earth's Atmosphere:** When sunlight enters the Earth's atmosphere, it collides with tiny particles like nitrogen and oxygen molecules. |
| 29 | +3. **Scattering:** These particles scatter the sunlight in all directions. However, blue light (which has a shorter wavelength) is scattered more effectively than other colors. |
| 30 | +4. **Our Perception:** As a result, we see a blue sky because the scattered blue light reaches our eyes from all directions. |
| 31 | +**Why not other colors?** |
| 32 | +* **Violet light** has an even shorter wavelength than blue and is scattered even more. However, our eyes are less sensitive to violet light, so we perceive the sky as blue. |
| 33 | +* **Longer wavelengths** like red, orange, and yellow are scattered less and travel more directly through the atmosphere. This is why we see these colors during sunrise and sunset, when sunlight has to travel through more of the atmosphere. |
| 34 | +`; |
| 35 | + |
| 36 | +const tpuResponse = |
| 37 | + 'The sky appears blue due to a phenomenon called **Rayleigh scattering**.'; |
| 38 | + |
| 39 | +describe('Gemma2 predictions', async () => { |
| 40 | + const gemma2Endpoint = |
| 41 | + 'projects/your-project-id/locations/your-vertex-endpoint-region/endpoints/your-vertex-endpoint-id'; |
| 42 | + const configValues = { |
| 43 | + maxOutputTokens: {kind: 'numberValue', numberValue: 1024}, |
| 44 | + temperature: {kind: 'numberValue', numberValue: 0.9}, |
| 45 | + topP: {kind: 'numberValue', numberValue: 1}, |
| 46 | + topK: {kind: 'numberValue', numberValue: 1}, |
| 47 | + }; |
| 48 | + const prompt = 'Why is the sky blue?'; |
| 49 | + const predictionServiceClientMock = { |
| 50 | + predict: sinon.stub().resolves([]), |
| 51 | + }; |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + sinon.reset(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should run interference with GPU', async () => { |
| 58 | + const expectedGpuRequest = { |
| 59 | + endpoint: gemma2Endpoint, |
| 60 | + instances: [ |
| 61 | + { |
| 62 | + kind: 'structValue', |
| 63 | + structValue: { |
| 64 | + fields: { |
| 65 | + inputs: { |
| 66 | + kind: 'stringValue', |
| 67 | + stringValue: prompt, |
| 68 | + }, |
| 69 | + parameters: { |
| 70 | + kind: 'structValue', |
| 71 | + structValue: { |
| 72 | + fields: configValues, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + ], |
| 79 | + }; |
| 80 | + |
| 81 | + predictionServiceClientMock.predict.resolves([ |
| 82 | + { |
| 83 | + predictions: [ |
| 84 | + { |
| 85 | + stringValue: gpuResponse, |
| 86 | + }, |
| 87 | + ], |
| 88 | + }, |
| 89 | + ]); |
| 90 | + |
| 91 | + const output = await gemma2PredictGpu(predictionServiceClientMock); |
| 92 | + |
| 93 | + expect(output).include('Rayleigh scattering'); |
| 94 | + expect(predictionServiceClientMock.predict.calledOnce).to.be.true; |
| 95 | + expect(predictionServiceClientMock.predict.calledWith(expectedGpuRequest)) |
| 96 | + .to.be.true; |
| 97 | + }); |
| 98 | + |
| 99 | + it('should run interference with TPU', async () => { |
| 100 | + const expectedTpuRequest = { |
| 101 | + endpoint: gemma2Endpoint, |
| 102 | + instances: [ |
| 103 | + { |
| 104 | + kind: 'structValue', |
| 105 | + structValue: { |
| 106 | + fields: { |
| 107 | + ...configValues, |
| 108 | + prompt: { |
| 109 | + kind: 'stringValue', |
| 110 | + stringValue: prompt, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + ], |
| 116 | + }; |
| 117 | + |
| 118 | + predictionServiceClientMock.predict.resolves([ |
| 119 | + { |
| 120 | + predictions: [ |
| 121 | + { |
| 122 | + stringValue: tpuResponse, |
| 123 | + }, |
| 124 | + ], |
| 125 | + }, |
| 126 | + ]); |
| 127 | + |
| 128 | + const output = await gemma2PredictTpu(predictionServiceClientMock); |
| 129 | + |
| 130 | + expect(output).include('Rayleigh scattering'); |
| 131 | + expect(predictionServiceClientMock.predict.calledOnce).to.be.true; |
| 132 | + expect(predictionServiceClientMock.predict.calledWith(expectedTpuRequest)) |
| 133 | + .to.be.true; |
| 134 | + }); |
| 135 | +}); |
0 commit comments