Streamline AI text formatting

This commit is contained in:
Sam Becker 2024-06-12 11:41:02 -05:00
parent cb14f3c8f9
commit ffeef657c3
4 changed files with 28 additions and 36 deletions

View File

@ -64,9 +64,8 @@ export const parseTitleAndCaption = (text: string) => {
};
};
export const cleanUpAiTextResponse = (text?: string) => text
? text
export const cleanUpAiTextResponse = (text: string) =>
text
.replaceAll('\n', ' ')
.replaceAll('"', '')
.replace(/\.$/, '')
: undefined;
.replace(/\.$/, '');

View File

@ -2,7 +2,6 @@ import { generateOpenAiImageQuery } from '@/services/openai';
import {
AI_IMAGE_QUERIES,
AiAutoGeneratedField,
cleanUpAiTextResponse,
parseTitleAndCaption,
} from '.';
@ -28,11 +27,10 @@ export const generateAiImageQueries = async (
textFieldsToGenerate.includes('title') &&
textFieldsToGenerate.includes('caption')
) {
const titleAndCaption = cleanUpAiTextResponse(
await generateOpenAiImageQuery(
imageBase64,
AI_IMAGE_QUERIES['title-and-caption'],
));
const titleAndCaption = await generateOpenAiImageQuery(
imageBase64,
AI_IMAGE_QUERIES['title-and-caption'],
);
if (titleAndCaption) {
const titleAndCaptionParsed = parseTitleAndCaption(titleAndCaption);
title = titleAndCaptionParsed.title;
@ -40,35 +38,31 @@ export const generateAiImageQueries = async (
}
} else {
if (textFieldsToGenerate.includes('title')) {
title = cleanUpAiTextResponse(
await generateOpenAiImageQuery(
imageBase64,
AI_IMAGE_QUERIES['title'],
));
title = await generateOpenAiImageQuery(
imageBase64,
AI_IMAGE_QUERIES['title'],
);
}
if (textFieldsToGenerate.includes('caption')) {
caption = cleanUpAiTextResponse(
await generateOpenAiImageQuery(
imageBase64,
AI_IMAGE_QUERIES['caption'],
));
caption = await generateOpenAiImageQuery(
imageBase64,
AI_IMAGE_QUERIES['caption'],
);
}
}
if (textFieldsToGenerate.includes('tags')) {
tags = cleanUpAiTextResponse(
await generateOpenAiImageQuery(
imageBase64,
AI_IMAGE_QUERIES['tags'],
));
tags = await generateOpenAiImageQuery(
imageBase64,
AI_IMAGE_QUERIES['tags'],
);
}
if (textFieldsToGenerate.includes('semantic')) {
semanticDescription = cleanUpAiTextResponse(
await generateOpenAiImageQuery(
imageBase64,
AI_IMAGE_QUERIES['description-small'],
));
semanticDescription = await generateOpenAiImageQuery(
imageBase64,
AI_IMAGE_QUERIES['description-small'],
);
}
}
} catch (e: any) {

View File

@ -1,7 +1,7 @@
import { useCallback, useState } from 'react';
import { streamAiImageQueryAction } from '../actions';
import { readStreamableValue } from 'ai/rsc';
import { AiImageQuery, cleanUpAiTextResponse } from '.';
import { AiImageQuery } from '.';
export default function useAiImageQuery(
imageBase64: string | undefined,
@ -21,9 +21,7 @@ export default function useAiImageQuery(
query,
);
for await (const text of readStreamableValue(textStream)) {
setText(current =>
cleanUpAiTextResponse(`${current}${text ?? ''}`) ?? ''
);
setText(current => `${current}${text ?? ''}`);
}
setIsLoading(false);
} catch (e) {

View File

@ -5,6 +5,7 @@ import { kv } from '@vercel/kv';
import { Ratelimit } from '@upstash/ratelimit';
import { AI_TEXT_GENERATION_ENABLED, HAS_VERCEL_KV } from '@/site/config';
import { removeBase64Prefix } from '@/utility/image';
import { cleanUpAiTextResponse } from '@/photo/ai';
const RATE_LIMIT_IDENTIFIER = 'openai-image-query';
const RATE_LIMIT_MAX_QUERIES_PER_HOUR = 100;
@ -73,7 +74,7 @@ export const streamOpenAiImageQuery = async (
(async () => {
const { textStream } = await streamText(args);
for await (const delta of textStream) {
stream.update(delta);
stream.update(cleanUpAiTextResponse(delta));
}
stream.done();
})();
@ -92,7 +93,7 @@ export const generateOpenAiImageQuery = async (
if (args) {
return generateText(args)
.then(({ text }) => text);
.then(({ text }) => cleanUpAiTextResponse(text));
}
};