mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2024-11-11 01:10:11 +01:00
implement auto generated thread titles
This commit is contained in:
parent
d003613bb1
commit
433a823fd6
@ -15,6 +15,8 @@ const {
|
|||||||
validWorkspaceSlug,
|
validWorkspaceSlug,
|
||||||
} = require("../utils/middleware/validWorkspace");
|
} = require("../utils/middleware/validWorkspace");
|
||||||
const { writeResponseChunk } = require("../utils/helpers/chat/responses");
|
const { writeResponseChunk } = require("../utils/helpers/chat/responses");
|
||||||
|
const generateThreadTitle = require("../utils/threadNames");
|
||||||
|
const { WorkspaceThread } = require("../models/workspaceThread");
|
||||||
|
|
||||||
function chatEndpoints(app) {
|
function chatEndpoints(app) {
|
||||||
if (!app) return;
|
if (!app) return;
|
||||||
@ -196,6 +198,34 @@ function chatEndpoints(app) {
|
|||||||
user,
|
user,
|
||||||
thread
|
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", {
|
await Telemetry.sendTelemetry("sent_chat", {
|
||||||
multiUserMode: multiUserMode(response),
|
multiUserMode: multiUserMode(response),
|
||||||
LLMSelection: process.env.LLM_PROVIDER || "openai",
|
LLMSelection: process.env.LLM_PROVIDER || "openai",
|
||||||
|
30
server/utils/threadNames/index.js
Normal file
30
server/utils/threadNames/index.js
Normal 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;
|
Loading…
Reference in New Issue
Block a user