Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import type {
GoogleErrorResponse,
GoogleImageGenInstanceData,
GoogleImageGenResponse,
} from '@api/ai-providers/google/types';
import { generateInvalidProviderResponseError } from '@api/utils/ai-provider';
import type {
AIProviderFunctionConfig,
ResponseTransformFunction,
} from '@shared/types/ai-providers/config';
import { AIProvider } from '@shared/types/constants';
import { GoogleErrorResponseTransform } from './utils';
const transformParams = (
params: Record<string, unknown>,
): Record<string, unknown> => {
const config: Record<string, unknown> = {};
if (params.n) {
config.sampleCount = params.n;
}
if (params.quality) {
let quality: number;
if (typeof params.quality === 'number') {
quality = params.quality;
} else {
if (params.quality === 'hd') {
quality = 100;
} else {
quality = 75;
}
}
if (config.outputOptions) {
(
config.outputOptions as unknown as Record<string, unknown>
).compressionQuality = quality;
} else {
config.outputOptions = { compressionQuality: quality };
}
}
if (params.style) {
config.sampleImageStyle = params.style;
}
if (params.aspectRatio) {
config.aspectRatio = params.aspectRatio;
}
if (params.seed) {
config.seed = params.seed;
}
if (params.negativePrompt) {
config.negativePrompt = params.negativePrompt;
}
if (params.personGeneration) {
config.personGeneration = params.personGeneration;
}
if (params.safetySetting) {
config.safetySetting = params.safetySetting;
}
if (params.addWatermark) {
config.addWatermark = params.addWatermark;
}
if (params.mimeType) {
if (config.outputOptions) {
(config.outputOptions as unknown as Record<string, unknown>).mimeType =
params.mimeType;
} else {
config.outputOptions = { mimeType: params.mimeType };
}
}
return config;
};
export const googleImageGenConfig: AIProviderFunctionConfig = {
prompt: {
param: 'instances',
required: true,
transform: (params: Record<string, unknown>) => {
const instances: GoogleImageGenInstanceData[] = [];
if (Array.isArray(params.prompt)) {
params.prompt.forEach((text: string) => {
instances.push({
prompt: text,
});
});
} else {
instances.push({
prompt: params.prompt as string,
});
}
return instances as unknown as Record<string, unknown>[];
},
},
n: {
param: 'parameters',
min: 1,
max: 8,
transform: transformParams,
},
quality: {
param: 'parameters',
transform: transformParams,
},
style: {
param: 'parameters',
transform: transformParams,
},
aspectRatio: {
param: 'parameters',
transform: transformParams,
},
seed: {
param: 'parameters',
transform: transformParams,
},
negativePrompt: {
param: 'parameters',
transform: transformParams,
},
personGeneration: {
param: 'parameters',
transform: transformParams,
},
safetySetting: {
param: 'parameters',
transform: transformParams,
},
addWatermark: {
param: 'parameters',
transform: transformParams,
},
mimeType: {
param: 'parameters',
transform: transformParams,
},
};
export const vertexGoogleImageGenResponseTransform: ResponseTransformFunction =
(response, responseStatus) => {
const googleResponse = response as unknown as
| GoogleImageGenResponse
| GoogleErrorResponse;
if (responseStatus !== 200) {
const errorResposne = GoogleErrorResponseTransform(
googleResponse as unknown as GoogleErrorResponse,
);
if (errorResposne) return errorResposne;
}
if ('predictions' in googleResponse) {
return {
created: Math.floor(Date.now() / 1000),
data: googleResponse.predictions.map((generation) => ({
b64_json: generation.bytesBase64Encoded,
})),
provider: AIProvider.GOOGLE_VERTEX_AI,
};
}
return generateInvalidProviderResponseError(
response as unknown as Record<string, unknown>,
AIProvider.GOOGLE_VERTEX_AI,
);
};
|