Streamline AI text formatting
This commit is contained in:
parent
cb14f3c8f9
commit
ffeef657c3
@ -64,9 +64,8 @@ export const parseTitleAndCaption = (text: string) => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const cleanUpAiTextResponse = (text?: string) => text
|
export const cleanUpAiTextResponse = (text: string) =>
|
||||||
? text
|
text
|
||||||
.replaceAll('\n', ' ')
|
.replaceAll('\n', ' ')
|
||||||
.replaceAll('"', '')
|
.replaceAll('"', '')
|
||||||
.replace(/\.$/, '')
|
.replace(/\.$/, '');
|
||||||
: undefined;
|
|
||||||
|
|||||||
@ -2,7 +2,6 @@ import { generateOpenAiImageQuery } from '@/services/openai';
|
|||||||
import {
|
import {
|
||||||
AI_IMAGE_QUERIES,
|
AI_IMAGE_QUERIES,
|
||||||
AiAutoGeneratedField,
|
AiAutoGeneratedField,
|
||||||
cleanUpAiTextResponse,
|
|
||||||
parseTitleAndCaption,
|
parseTitleAndCaption,
|
||||||
} from '.';
|
} from '.';
|
||||||
|
|
||||||
@ -28,11 +27,10 @@ export const generateAiImageQueries = async (
|
|||||||
textFieldsToGenerate.includes('title') &&
|
textFieldsToGenerate.includes('title') &&
|
||||||
textFieldsToGenerate.includes('caption')
|
textFieldsToGenerate.includes('caption')
|
||||||
) {
|
) {
|
||||||
const titleAndCaption = cleanUpAiTextResponse(
|
const titleAndCaption = await generateOpenAiImageQuery(
|
||||||
await generateOpenAiImageQuery(
|
imageBase64,
|
||||||
imageBase64,
|
AI_IMAGE_QUERIES['title-and-caption'],
|
||||||
AI_IMAGE_QUERIES['title-and-caption'],
|
);
|
||||||
));
|
|
||||||
if (titleAndCaption) {
|
if (titleAndCaption) {
|
||||||
const titleAndCaptionParsed = parseTitleAndCaption(titleAndCaption);
|
const titleAndCaptionParsed = parseTitleAndCaption(titleAndCaption);
|
||||||
title = titleAndCaptionParsed.title;
|
title = titleAndCaptionParsed.title;
|
||||||
@ -40,35 +38,31 @@ export const generateAiImageQueries = async (
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (textFieldsToGenerate.includes('title')) {
|
if (textFieldsToGenerate.includes('title')) {
|
||||||
title = cleanUpAiTextResponse(
|
title = await generateOpenAiImageQuery(
|
||||||
await generateOpenAiImageQuery(
|
imageBase64,
|
||||||
imageBase64,
|
AI_IMAGE_QUERIES['title'],
|
||||||
AI_IMAGE_QUERIES['title'],
|
);
|
||||||
));
|
|
||||||
}
|
}
|
||||||
if (textFieldsToGenerate.includes('caption')) {
|
if (textFieldsToGenerate.includes('caption')) {
|
||||||
caption = cleanUpAiTextResponse(
|
caption = await generateOpenAiImageQuery(
|
||||||
await generateOpenAiImageQuery(
|
imageBase64,
|
||||||
imageBase64,
|
AI_IMAGE_QUERIES['caption'],
|
||||||
AI_IMAGE_QUERIES['caption'],
|
);
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (textFieldsToGenerate.includes('tags')) {
|
if (textFieldsToGenerate.includes('tags')) {
|
||||||
tags = cleanUpAiTextResponse(
|
tags = await generateOpenAiImageQuery(
|
||||||
await generateOpenAiImageQuery(
|
imageBase64,
|
||||||
imageBase64,
|
AI_IMAGE_QUERIES['tags'],
|
||||||
AI_IMAGE_QUERIES['tags'],
|
);
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (textFieldsToGenerate.includes('semantic')) {
|
if (textFieldsToGenerate.includes('semantic')) {
|
||||||
semanticDescription = cleanUpAiTextResponse(
|
semanticDescription = await generateOpenAiImageQuery(
|
||||||
await generateOpenAiImageQuery(
|
imageBase64,
|
||||||
imageBase64,
|
AI_IMAGE_QUERIES['description-small'],
|
||||||
AI_IMAGE_QUERIES['description-small'],
|
);
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { useCallback, useState } from 'react';
|
import { useCallback, useState } from 'react';
|
||||||
import { streamAiImageQueryAction } from '../actions';
|
import { streamAiImageQueryAction } from '../actions';
|
||||||
import { readStreamableValue } from 'ai/rsc';
|
import { readStreamableValue } from 'ai/rsc';
|
||||||
import { AiImageQuery, cleanUpAiTextResponse } from '.';
|
import { AiImageQuery } from '.';
|
||||||
|
|
||||||
export default function useAiImageQuery(
|
export default function useAiImageQuery(
|
||||||
imageBase64: string | undefined,
|
imageBase64: string | undefined,
|
||||||
@ -21,9 +21,7 @@ export default function useAiImageQuery(
|
|||||||
query,
|
query,
|
||||||
);
|
);
|
||||||
for await (const text of readStreamableValue(textStream)) {
|
for await (const text of readStreamableValue(textStream)) {
|
||||||
setText(current =>
|
setText(current => `${current}${text ?? ''}`);
|
||||||
cleanUpAiTextResponse(`${current}${text ?? ''}`) ?? ''
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import { kv } from '@vercel/kv';
|
|||||||
import { Ratelimit } from '@upstash/ratelimit';
|
import { Ratelimit } from '@upstash/ratelimit';
|
||||||
import { AI_TEXT_GENERATION_ENABLED, HAS_VERCEL_KV } from '@/site/config';
|
import { AI_TEXT_GENERATION_ENABLED, HAS_VERCEL_KV } from '@/site/config';
|
||||||
import { removeBase64Prefix } from '@/utility/image';
|
import { removeBase64Prefix } from '@/utility/image';
|
||||||
|
import { cleanUpAiTextResponse } from '@/photo/ai';
|
||||||
|
|
||||||
const RATE_LIMIT_IDENTIFIER = 'openai-image-query';
|
const RATE_LIMIT_IDENTIFIER = 'openai-image-query';
|
||||||
const RATE_LIMIT_MAX_QUERIES_PER_HOUR = 100;
|
const RATE_LIMIT_MAX_QUERIES_PER_HOUR = 100;
|
||||||
@ -73,7 +74,7 @@ export const streamOpenAiImageQuery = async (
|
|||||||
(async () => {
|
(async () => {
|
||||||
const { textStream } = await streamText(args);
|
const { textStream } = await streamText(args);
|
||||||
for await (const delta of textStream) {
|
for await (const delta of textStream) {
|
||||||
stream.update(delta);
|
stream.update(cleanUpAiTextResponse(delta));
|
||||||
}
|
}
|
||||||
stream.done();
|
stream.done();
|
||||||
})();
|
})();
|
||||||
@ -92,7 +93,7 @@ export const generateOpenAiImageQuery = async (
|
|||||||
|
|
||||||
if (args) {
|
if (args) {
|
||||||
return generateText(args)
|
return generateText(args)
|
||||||
.then(({ text }) => text);
|
.then(({ text }) => cleanUpAiTextResponse(text));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user