|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2024 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { expect, use } from "chai"; |
| 19 | +import * as sinonChai from "sinon-chai"; |
| 20 | +import { Content } from "../../types"; |
| 21 | +import { formatGenerateContentInput } from "./request-helpers"; |
| 22 | + |
| 23 | +use(sinonChai); |
| 24 | + |
| 25 | +describe("request formatting methods", () => { |
| 26 | + describe("formatGenerateContentInput", () => { |
| 27 | + it("formats a text string into a request", () => { |
| 28 | + const result = formatGenerateContentInput("some text content"); |
| 29 | + expect(result).to.deep.equal({ |
| 30 | + contents: [ |
| 31 | + { |
| 32 | + role: "user", |
| 33 | + parts: [{ text: "some text content" }], |
| 34 | + }, |
| 35 | + ], |
| 36 | + }); |
| 37 | + }); |
| 38 | + it("formats an array of strings into a request", () => { |
| 39 | + const result = formatGenerateContentInput(["txt1", "txt2"]); |
| 40 | + expect(result).to.deep.equal({ |
| 41 | + contents: [ |
| 42 | + { |
| 43 | + role: "user", |
| 44 | + parts: [{ text: "txt1" }, { text: "txt2" }], |
| 45 | + }, |
| 46 | + ], |
| 47 | + }); |
| 48 | + }); |
| 49 | + it("formats an array of Parts into a request", () => { |
| 50 | + const result = formatGenerateContentInput([ |
| 51 | + { text: "txt1" }, |
| 52 | + { text: "txtB" }, |
| 53 | + ]); |
| 54 | + expect(result).to.deep.equal({ |
| 55 | + contents: [ |
| 56 | + { |
| 57 | + role: "user", |
| 58 | + parts: [{ text: "txt1" }, { text: "txtB" }], |
| 59 | + }, |
| 60 | + ], |
| 61 | + }); |
| 62 | + }); |
| 63 | + it("formats a mixed array into a request", () => { |
| 64 | + const result = formatGenerateContentInput(["txtA", { text: "txtB" }]); |
| 65 | + expect(result).to.deep.equal({ |
| 66 | + contents: [ |
| 67 | + { |
| 68 | + role: "user", |
| 69 | + parts: [{ text: "txtA" }, { text: "txtB" }], |
| 70 | + }, |
| 71 | + ], |
| 72 | + }); |
| 73 | + }); |
| 74 | + it("preserves other properties of request", () => { |
| 75 | + const result = formatGenerateContentInput({ |
| 76 | + contents: [ |
| 77 | + { |
| 78 | + role: "user", |
| 79 | + parts: [{ text: "txtA" }], |
| 80 | + }, |
| 81 | + ], |
| 82 | + generationConfig: { topK: 100 }, |
| 83 | + }); |
| 84 | + expect(result).to.deep.equal({ |
| 85 | + contents: [ |
| 86 | + { |
| 87 | + role: "user", |
| 88 | + parts: [{ text: "txtA" }], |
| 89 | + }, |
| 90 | + ], |
| 91 | + generationConfig: { topK: 100 }, |
| 92 | + }); |
| 93 | + }); |
| 94 | + it("formats systemInstructions if provided as text", () => { |
| 95 | + const result = formatGenerateContentInput({ |
| 96 | + contents: [ |
| 97 | + { |
| 98 | + role: "user", |
| 99 | + parts: [{ text: "txtA" }], |
| 100 | + }, |
| 101 | + ], |
| 102 | + systemInstruction: "be excited", |
| 103 | + }); |
| 104 | + expect(result).to.deep.equal({ |
| 105 | + contents: [ |
| 106 | + { |
| 107 | + role: "user", |
| 108 | + parts: [{ text: "txtA" }], |
| 109 | + }, |
| 110 | + ], |
| 111 | + systemInstruction: { role: "system", parts: [{ text: "be excited" }] }, |
| 112 | + }); |
| 113 | + }); |
| 114 | + it("formats systemInstructions if provided as Part", () => { |
| 115 | + const result = formatGenerateContentInput({ |
| 116 | + contents: [ |
| 117 | + { |
| 118 | + role: "user", |
| 119 | + parts: [{ text: "txtA" }], |
| 120 | + }, |
| 121 | + ], |
| 122 | + systemInstruction: { text: "be excited" }, |
| 123 | + }); |
| 124 | + expect(result).to.deep.equal({ |
| 125 | + contents: [ |
| 126 | + { |
| 127 | + role: "user", |
| 128 | + parts: [{ text: "txtA" }], |
| 129 | + }, |
| 130 | + ], |
| 131 | + systemInstruction: { role: "system", parts: [{ text: "be excited" }] }, |
| 132 | + }); |
| 133 | + }); |
| 134 | + it("formats systemInstructions if provided as Content (no role)", () => { |
| 135 | + const result = formatGenerateContentInput({ |
| 136 | + contents: [ |
| 137 | + { |
| 138 | + role: "user", |
| 139 | + parts: [{ text: "txtA" }], |
| 140 | + }, |
| 141 | + ], |
| 142 | + systemInstruction: { parts: [{ text: "be excited" }] } as Content, |
| 143 | + }); |
| 144 | + expect(result).to.deep.equal({ |
| 145 | + contents: [ |
| 146 | + { |
| 147 | + role: "user", |
| 148 | + parts: [{ text: "txtA" }], |
| 149 | + }, |
| 150 | + ], |
| 151 | + systemInstruction: { role: "system", parts: [{ text: "be excited" }] }, |
| 152 | + }); |
| 153 | + }); |
| 154 | + it("passes thru systemInstructions if provided as Content", () => { |
| 155 | + const result = formatGenerateContentInput({ |
| 156 | + contents: [ |
| 157 | + { |
| 158 | + role: "user", |
| 159 | + parts: [{ text: "txtA" }], |
| 160 | + }, |
| 161 | + ], |
| 162 | + systemInstruction: { role: "system", parts: [{ text: "be excited" }] }, |
| 163 | + }); |
| 164 | + expect(result).to.deep.equal({ |
| 165 | + contents: [ |
| 166 | + { |
| 167 | + role: "user", |
| 168 | + parts: [{ text: "txtA" }], |
| 169 | + }, |
| 170 | + ], |
| 171 | + systemInstruction: { role: "system", parts: [{ text: "be excited" }] }, |
| 172 | + }); |
| 173 | + }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments