implement auto generated thread titles

This commit is contained in:
shatfield4 2024-06-05 13:53:52 -07:00
parent d003613bb1
commit 433a823fd6
2 changed files with 60 additions and 0 deletions

View File

@ -15,6 +15,8 @@ const {
validWorkspaceSlug,
} = require("../utils/middleware/validWorkspace");
const { writeResponseChunk } = require("../utils/helpers/chat/responses");
const generateThreadTitle = require("../utils/threadNames");
const { WorkspaceThread } = require("../models/workspaceThread");
function chatEndpoints(app) {
if (!app) return;
@ -196,6 +198,34 @@ function chatEndpoints(app) {
user,
thread
);
// Check if first message in thread
const chatCount = await WorkspaceChats.count({
workspaceId: workspace.id,
user_id: user?.id || null,
thread_id: thread.id,
});
// Generate thread name
if (chatCount === 1) {
try {
const generatedTitle = await generateThreadTitle(message);
if (generatedTitle) {
const { thread: updatedThread } = await WorkspaceThread.update(
thread,
{
name: generatedTitle,
}
);
if (!updatedThread) {
console.log("Failed to update thread name");
}
}
} catch (e) {
console.log("Error generating thread title:", e);
}
}
await Telemetry.sendTelemetry("sent_chat", {
multiUserMode: multiUserMode(response),
LLMSelection: process.env.LLM_PROVIDER || "openai",

View File

@ -0,0 +1,30 @@
const { getLLMProvider } = require("../helpers");
async function generateThreadTitle(prompt) {
const systemPrompt =
"Listen to any instructions below and do not give any description or explanation when replying. Do not return anything else other than what is asked.";
const getTitlePrompt = `Take the message below and generate a short and concise title for a thread for it (max 22 characters or less). Do not return anything else.
Message:${prompt}\n\nTitle:`;
const LLMConnector = getLLMProvider();
const messages = await LLMConnector.compressMessages(
{
systemPrompt: systemPrompt,
userPrompt: getTitlePrompt,
},
[]
);
const title = await LLMConnector.getChatCompletion(messages, {
temperature: LLMConnector.defaultTemp,
});
// truncate title to 22 characters
const maxLength = 22;
const truncatedTitle =
title.length > maxLength ? title.slice(0, maxLength - 3) + "..." : title;
return truncatedTitle;
}
module.exports = generateThreadTitle;