Spaces:
Sleeping
Sleeping
File size: 1,760 Bytes
05c5ed5 | 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 | import { smoothStream, streamText } from "ai";
import { customModelProvider } from "lib/ai/models";
import { CREATE_THREAD_TITLE_PROMPT } from "lib/ai/prompts";
import globalLogger from "logger";
import { ChatModel } from "app-types/chat";
import { chatRepository } from "lib/db/repository";
import { getSession } from "auth/server";
import { colorize } from "consola/utils";
import { handleError } from "../shared.chat";
const logger = globalLogger.withDefaults({
message: colorize("blackBright", `Title API: `),
});
export async function POST(request: Request) {
try {
const json = await request.json();
const {
chatModel,
message = "hello",
threadId,
} = json as {
chatModel?: ChatModel;
message: string;
threadId: string;
};
const session = await getSession();
if (!session) {
return new Response("Unauthorized", { status: 401 });
}
logger.info(
`chatModel: ${chatModel?.provider}/${chatModel?.model}, threadId: ${threadId}`,
);
const thread = await chatRepository.selectThreadDetails(threadId);
const result = streamText({
model: customModelProvider.getDynamicModel(chatModel, thread?.userPreferences),
system: CREATE_THREAD_TITLE_PROMPT,
experimental_transform: smoothStream({ chunking: "word" }),
prompt: message,
abortSignal: request.signal,
onFinish: (ctx) => {
chatRepository
.upsertThread({
id: threadId,
title: ctx.text,
userId: session.user.id,
})
.catch((err) => logger.error(err));
},
});
return result.toUIMessageStreamResponse();
} catch (err) {
return new Response(handleError(err), { status: 500 });
}
}
|