Spaces:
Paused
Paused
File size: 5,725 Bytes
8c741f6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | /**
* @license
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
Content,
CountTokensRequest,
EmbedContentRequest,
GenerateContentRequest,
ModelParams,
Part,
_CountTokensRequestInternal,
_GenerateContentRequestInternal,
} from "../../types";
import {
GoogleGenerativeAIError,
GoogleGenerativeAIRequestInputError,
} from "../errors";
export function formatSystemInstruction(
input?: string | Part | Content,
): Content | undefined {
// null or undefined
if (input == null) {
return undefined;
} else if (typeof input === "string") {
return { role: "system", parts: [{ text: input }] } as Content;
} else if ((input as Part).text) {
return { role: "system", parts: [input as Part] };
} else if ((input as Content).parts) {
if (!(input as Content).role) {
return { role: "system", parts: (input as Content).parts };
} else {
return input as Content;
}
}
}
export function formatNewContent(
request: string | Array<string | Part>,
): Content {
let newParts: Part[] = [];
if (typeof request === "string") {
newParts = [{ text: request }];
} else {
for (const partOrString of request) {
if (typeof partOrString === "string") {
newParts.push({ text: partOrString });
} else {
newParts.push(partOrString);
}
}
}
return assignRoleToPartsAndValidateSendMessageRequest(newParts);
}
/**
* When multiple Part types (i.e. FunctionResponsePart and TextPart) are
* passed in a single Part array, we may need to assign different roles to each
* part. Currently only FunctionResponsePart requires a role other than 'user'.
* @private
* @param parts Array of parts to pass to the model
* @returns Array of content items
*/
function assignRoleToPartsAndValidateSendMessageRequest(
parts: Part[],
): Content {
const userContent: Content = { role: "user", parts: [] };
const functionContent: Content = { role: "function", parts: [] };
let hasUserContent = false;
let hasFunctionContent = false;
for (const part of parts) {
if ("functionResponse" in part) {
functionContent.parts.push(part);
hasFunctionContent = true;
} else {
userContent.parts.push(part);
hasUserContent = true;
}
}
if (hasUserContent && hasFunctionContent) {
throw new GoogleGenerativeAIError(
"Within a single message, FunctionResponse cannot be mixed with other type of part in the request for sending chat message.",
);
}
if (!hasUserContent && !hasFunctionContent) {
throw new GoogleGenerativeAIError(
"No content is provided for sending chat message.",
);
}
if (hasUserContent) {
return userContent;
}
return functionContent;
}
export function formatCountTokensInput(
params: CountTokensRequest | string | Array<string | Part>,
modelParams?: ModelParams,
): _CountTokensRequestInternal {
let formattedGenerateContentRequest: _GenerateContentRequestInternal = {
model: modelParams?.model,
generationConfig: modelParams?.generationConfig,
safetySettings: modelParams?.safetySettings,
tools: modelParams?.tools,
toolConfig: modelParams?.toolConfig,
systemInstruction: modelParams?.systemInstruction,
cachedContent: modelParams?.cachedContent?.name,
contents: [],
};
const containsGenerateContentRequest =
(params as CountTokensRequest).generateContentRequest != null;
if ((params as CountTokensRequest).contents) {
if (containsGenerateContentRequest) {
throw new GoogleGenerativeAIRequestInputError(
"CountTokensRequest must have one of contents or generateContentRequest, not both.",
);
}
formattedGenerateContentRequest.contents = (
params as CountTokensRequest
).contents;
} else if (containsGenerateContentRequest) {
formattedGenerateContentRequest = {
...formattedGenerateContentRequest,
...(params as CountTokensRequest).generateContentRequest,
};
} else {
// Array or string
const content = formatNewContent(params as string | Array<string | Part>);
formattedGenerateContentRequest.contents = [content];
}
return { generateContentRequest: formattedGenerateContentRequest };
}
export function formatGenerateContentInput(
params: GenerateContentRequest | string | Array<string | Part>,
): GenerateContentRequest {
let formattedRequest: GenerateContentRequest;
if ((params as GenerateContentRequest).contents) {
formattedRequest = params as GenerateContentRequest;
} else {
// Array or string
const content = formatNewContent(params as string | Array<string | Part>);
formattedRequest = { contents: [content] };
}
if ((params as GenerateContentRequest).systemInstruction) {
formattedRequest.systemInstruction = formatSystemInstruction(
(params as GenerateContentRequest).systemInstruction,
);
}
return formattedRequest;
}
export function formatEmbedContentInput(
params: EmbedContentRequest | string | Array<string | Part>,
): EmbedContentRequest {
if (typeof params === "string" || Array.isArray(params)) {
const content = formatNewContent(params);
return { content };
}
return params;
}
|