Add AI query constructor

This commit is contained in:
Sam Becker 2025-09-23 17:11:58 -05:00
parent e9d3c19c40
commit 76639a4195
2 changed files with 33 additions and 2 deletions

View File

@ -80,6 +80,37 @@ export const getAiImageQuery = (
}
};
const getAiImageQueryForField = (
field: AiAutoGeneratedField,
existingTags: Tags = [],
existingTitle?: string,
) => {
switch(field) {
case 'title': return `TITLE: ${getAiImageQuery('title', existingTags, existingTitle)}`;
case 'caption': return `CAPTION: ${getAiImageQuery('caption', existingTags, existingTitle)}`;
case 'tags': return `TAGS: ${getAiImageQuery('tags', existingTags, existingTitle)}`;
case 'semantic': return `SEMANTIC: ${getAiImageQuery('description-small', existingTags, existingTitle)}`;
}
};
export const getAiImageryQuerySet = (
fields: AiAutoGeneratedField[],
existingTags: Tags = [],
existingTitle?: string,
) => {
const query = ['Generate a suite of structured meta content for the attached image:' + '\n'];
fields.forEach(field => {
query.push(getAiImageQueryForField(field, existingTags, existingTitle));
});
query.push(`\nRespond with a valid JSON object with the following format: { ${fields.map(field => `${field}: 'string'`).join(', ')} }`);
console.log(query.join('\n'));
return query.join('\n');
};
export const parseTitleAndCaption = (text: string) => {
const matches = text.includes('Title')
? text.match(/^[`'"]*Title: ["']*(.*?)["']*[ ]*Caption: ["']*(.*?)\.*["']*[`'"]*$/)

View File

@ -119,7 +119,7 @@ export const getColorFieldsForPhotoForm = async (
export const getColorFromAI = async (
_url: string,
useBatch?: boolean,
isBatch?: boolean,
) => {
const url = getOptimizedPhotoUrlForManipulation(_url, IS_PREVIEW);
const image = await getImageBase64FromUrl(url);
@ -128,7 +128,7 @@ export const getColorFromAI = async (
If yes, what is the approximate hex color of the subject.
If not, what is the approximate hex color of the background?
Respond only with a hex color value:
`, useBatch);
`, isBatch);
const hex = hexColor?.match(/#*([a-f0-9]{6})/i)?.[1];
if (hex) {
return convertHexToOklch(`#${hex}`);