Spaces:
Running
Running
File size: 1,978 Bytes
0953d85 3e9f86e 2e28042 6434339 2e28042 0953d85 3e9f86e 0953d85 3e9f86e 2e28042 3e9f86e 0953d85 3e9f86e ffa4f55 3e9f86e e91b76c 0c4cf03 1eb0e2e 06ffd82 3e9f86e 2e28042 3e9f86e 06ffd82 3e9f86e 06ffd82 3e9f86e 06ffd82 3e9f86e |
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 64 65 66 67 68 69 70 71 72 73 74 75 |
import { RATE_LIMIT } from "$env/static/private";
import { buildPrompt } from "$lib/buildPrompt";
import { authCondition } from "$lib/server/auth";
import { collections } from "$lib/server/database";
import { generateFromDefaultEndpoint } from "$lib/server/generateFromDefaultEndpoint";
import { defaultModel } from "$lib/server/models";
import { ERROR_MESSAGES } from "$lib/stores/errors.js";
import { error } from "@sveltejs/kit";
import { ObjectId } from "mongodb";
export async function POST({ params, locals, getClientAddress }) {
const convId = new ObjectId(params.id);
const conversation = await collections.conversations.findOne({
_id: convId,
...authCondition(locals),
});
if (!conversation) {
throw error(404, "Conversation not found");
}
const userId = locals.user?._id ?? locals.sessionId;
await collections.messageEvents.insertOne({
userId: userId,
createdAt: new Date(),
ip: getClientAddress(),
});
const nEvents = Math.max(
await collections.messageEvents.countDocuments({ userId }),
await collections.messageEvents.countDocuments({ ip: getClientAddress() })
);
if (RATE_LIMIT != "" && nEvents > parseInt(RATE_LIMIT)) {
throw error(429, ERROR_MESSAGES.rateLimited);
}
const firstMessage = conversation.messages.find((m) => m.from === "user");
const userPrompt =
`Please summarize the following message as a single sentence of less than 5 words:\n` +
firstMessage?.content;
const prompt = await buildPrompt({
messages: [{ from: "user", content: userPrompt }],
model: defaultModel,
});
const generated_text = await generateFromDefaultEndpoint(prompt);
if (generated_text) {
await collections.conversations.updateOne(
{
_id: convId,
...authCondition(locals),
},
{
$set: { title: generated_text },
}
);
}
return new Response(
JSON.stringify(
generated_text
? {
title: generated_text,
}
: {}
),
{ headers: { "Content-Type": "application/json" } }
);
}
|