File size: 1,811 Bytes
3e9f86e
 
 
 
06ffd82
3e9f86e
 
 
c38ef3b
3e9f86e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06ffd82
 
 
 
 
 
 
 
 
 
3e9f86e
06ffd82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e9f86e
06ffd82
3e9f86e
06ffd82
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
76
77
78
79
80
81
import { HF_TOKEN } from "$env/static/private";
import { PUBLIC_MODEL_ENDPOINT } from "$env/static/public";
import { buildPrompt } from "$lib/buildPrompt";
import { collections } from "$lib/server/database.js";
import { textGeneration } from "@huggingface/inference";
import { error } from "@sveltejs/kit";
import { ObjectId } from "mongodb";

export async function POST({ params, locals, fetch }) {
	const convId = new ObjectId(params.id);

	const conversation = await collections.conversations.findOne({
		_id: convId,
		sessionId: locals.sessionId,
	});

	if (!conversation) {
		throw error(404, "Conversation not found");
	}

	const firstMessage = conversation.messages.find((m) => m.from === "user");

	const userPrompt =
		`You are a summarizing assistant. Please summarize the following message as a single sentence of less than 5 words:\n` +
		firstMessage?.content;

	const prompt = buildPrompt([{ from: "user", content: userPrompt }]);

	const parameters = {
		temperature: 0.9,
		top_p: 0.95,
		repetition_penalty: 1.2,
		top_k: 50,
		watermark: false,
		max_new_tokens: 1024,
		stop: ["<|endoftext|>"],
		return_full_text: false,
	};

	const { generated_text } = await textGeneration(
		{
			model: PUBLIC_MODEL_ENDPOINT,
			inputs: prompt,
			parameters,
		},
		{
			fetch: (url, options) =>
				fetch(url, {
					...options,
					headers: {
						...options?.headers,
						Authorization: `Basic ${HF_TOKEN}`,
					},
				}),
		}
	);

	if (generated_text) {
		await collections.conversations.updateOne(
			{
				_id: convId,
				sessionId: locals.sessionId,
			},
			{
				$set: { title: generated_text },
			}
		);
	}

	return new Response(
		JSON.stringify(
			generated_text
				? {
						title: generated_text,
				  }
				: {}
		),
		{ headers: { "Content-Type": "application/json" } }
	);
}