chatbot-template/app/(chat)/actions.ts

30 lines
805 B
TypeScript
Raw Normal View History

'use server';
2024-11-15 12:18:17 -05:00
import { CoreMessage, type CoreUserMessage, generateText } from 'ai';
import { cookies } from 'next/headers';
2024-11-05 17:15:51 +03:00
import { customModel } from '@/ai';
2024-10-30 16:01:24 +05:30
export async function saveModelId(model: string) {
const cookieStore = await cookies();
2024-10-30 16:01:24 +05:30
cookieStore.set('model-id', model);
}
2024-11-05 17:15:51 +03:00
export async function generateTitleFromUserMessage({
message,
}: {
message: CoreUserMessage;
}) {
const { text: title } = await generateText({
2024-11-15 10:15:49 -05:00
model: customModel('gpt-4o-mini'),
2024-11-05 17:15:51 +03:00
system: `\n
- you will generate a short title based on the first message a user begins a conversation with
- ensure it is not more than 80 characters long
- the title should be a summary of the user's message
- do not use quotes or colons`,
prompt: JSON.stringify(message),
});
return title;
}