more fixes for annoying gemini API design that allows arrays or single objects for contents parts

This commit is contained in:
nai-degen 2024-10-09 17:11:53 -05:00
parent b57627e69b
commit ba4532b38d
1 changed files with 31 additions and 19 deletions

View File

@ -6,27 +6,39 @@ import {
import { APIFormatTransformer } from "./index";
const GoogleAIV1ContentSchema = z.object({
parts: z.array(z.object({ text: z.string() })), // TODO: add other media types
parts: z
.union([
z.array(z.object({ text: z.string() })),
z.object({ text: z.string() }),
])
// Google allows parts to be an array or a single object, which is really
// annoying for downstream code. We will coerce it to an array here.
.transform((val) => (Array.isArray(val) ? val : [val])),
// TODO: add other media types
role: z.enum(["user", "model"]).optional(),
});
const SafetySettingsSchema = z.array(z.object({
category: z.enum([
"HARM_CATEGORY_HARASSMENT",
"HARM_CATEGORY_HATE_SPEECH",
"HARM_CATEGORY_SEXUALLY_EXPLICIT",
"HARM_CATEGORY_DANGEROUS_CONTENT",
"HARM_CATEGORY_CIVIC_INTEGRITY",
]),
threshold: z.enum([
"OFF",
"BLOCK_NONE",
"BLOCK_ONLY_HIGH",
"BLOCK_MEDIUM_AND_ABOVE",
"BLOCK_LOW_AND_ABOVE",
"HARM_BLOCK_THRESHOLD_UNSPECIFIED",
]),
})).optional();
const SafetySettingsSchema = z
.array(
z.object({
category: z.enum([
"HARM_CATEGORY_HARASSMENT",
"HARM_CATEGORY_HATE_SPEECH",
"HARM_CATEGORY_SEXUALLY_EXPLICIT",
"HARM_CATEGORY_DANGEROUS_CONTENT",
"HARM_CATEGORY_CIVIC_INTEGRITY",
]),
threshold: z.enum([
"OFF",
"BLOCK_NONE",
"BLOCK_ONLY_HIGH",
"BLOCK_MEDIUM_AND_ABOVE",
"BLOCK_LOW_AND_ABOVE",
"HARM_BLOCK_THRESHOLD_UNSPECIFIED",
]),
})
)
.optional();
// https://developers.generativeai.google/api/rest/generativelanguage/models/generateContent
export const GoogleAIV1GenerateContentSchema = z
@ -40,7 +52,7 @@ export const GoogleAIV1GenerateContentSchema = z
// quick fix for SillyTavern, which uses camel case field names for everything
// except for system_instruction where it randomly uses snake case.
// google api evidently accepts either case.
"system_instruction": GoogleAIV1ContentSchema.optional(),
system_instruction: GoogleAIV1ContentSchema.optional(),
generationConfig: z
.object({
temperature: z.number().min(0).max(2).optional(),