Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 4,303 Bytes
a8a9533 43606a3 564e576 486ffa7 564e576 bf75aa7 6655689 7aa951e 564e576 ee5c213 564e576 aa1ef18 2bae046 ee1ec85 6655689 5b1a9aa 564e576 43606a3 564e576 43606a3 bf75aa7 43606a3 486ffa7 43606a3 7aa951e |
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
import { env } from "$env/dynamic/private";
import { generateFromDefaultEndpoint } from "$lib/server/generateFromDefaultEndpoint";
import type { EndpointMessage } from "../endpoints/endpoints";
import { logger } from "$lib/server/logger";
import { MessageUpdateType, type MessageUpdate } from "$lib/types/MessageUpdate";
import type { Conversation } from "$lib/types/Conversation";
import { getReturnFromGenerator } from "$lib/utils/getReturnFromGenerator";
import { smallModel } from "../models";
import type { Tool } from "$lib/types/Tool";
import { getToolOutput } from "../tools/getToolOutput";
export async function* generateTitleForConversation(
conv: Conversation
): AsyncGenerator<MessageUpdate, undefined, undefined> {
try {
const userMessage = conv.messages.find((m) => m.from === "user");
// HACK: detect if the conversation is new
if (conv.title !== "New Chat" || !userMessage) return;
const prompt = userMessage.content;
const title = (await generateTitle(prompt)) ?? "New Chat";
yield {
type: MessageUpdateType.Title,
title,
};
} catch (cause) {
logger.error(Error("Failed whilte generating title for conversation", { cause }));
}
}
export async function generateTitle(prompt: string) {
if (env.LLM_SUMMARIZATION !== "true") {
return prompt.split(/\s+/g).slice(0, 5).join(" ");
}
if (smallModel.tools) {
const titleTool = {
name: "title",
description:
"Submit a title for the conversation so far. Do not try to answer the user question or the tool will fail.",
inputs: [
{
name: "title",
type: "str",
description:
"The title for the conversation. It should be a single short sentence of four words or less and start with a unicode emoji relevant to the conversation.",
},
],
} as unknown as Tool;
const endpoint = await smallModel.getEndpoint();
const title = await getToolOutput({
messages: [
{
from: "user" as const,
content: prompt,
},
],
preprompt:
"You are a summarization AI. You'll never answer a user's question directly, but instead summarize the user's request into a single short sentence.",
tool: titleTool,
endpoint,
});
if (title) {
if (!/\p{Emoji}/u.test(title.slice(0, 3))) {
return "💬 " + title;
}
return title;
}
}
const messages: Array<EndpointMessage> = [
{
from: "system",
content:
"You are a summarization AI. You'll never answer a user's question directly, but instead summarize the user's request into a single short sentence of four words or less. Always start your answer with an emoji relevant to the summary",
},
{ from: "user", content: "Who is the president of Gabon?" },
{ from: "assistant", content: "🇬🇦 President of Gabon" },
{ from: "user", content: "Who is Julien Chaumond?" },
{ from: "assistant", content: "🧑 Julien Chaumond" },
{ from: "user", content: "what is 1 + 1?" },
{ from: "assistant", content: "🔢 Simple math operation" },
{ from: "user", content: "What are the latest news?" },
{ from: "assistant", content: "📰 Latest news" },
{ from: "user", content: "How to make a great cheesecake?" },
{ from: "assistant", content: "🍰 Cheesecake recipe" },
{ from: "user", content: "what is your favorite movie? do a short answer." },
{ from: "assistant", content: "🎥 Favorite movie" },
{ from: "user", content: "Explain the concept of artificial intelligence in one sentence" },
{ from: "assistant", content: "🤖 AI definition" },
{ from: "user", content: "Draw a cute cat" },
{ from: "assistant", content: "🐱 Cute cat drawing" },
{ from: "user", content: prompt },
];
return await getReturnFromGenerator(
generateFromDefaultEndpoint({
messages,
preprompt:
"You are a summarization AI. Summarize the user's request into a single short sentence of four words or less. Do not try to answer it, only summarize the user's query. Always start your answer with an emoji relevant to the summary",
generateSettings: {
max_new_tokens: 15,
},
})
)
.then((summary) => {
// add an emoji if none is found in the first three characters
if (!/\p{Emoji}/u.test(summary.slice(0, 3))) {
return "💬 " + summary;
}
return summary;
})
.catch((e) => {
logger.error(e);
return null;
});
}
|