mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 00:40:14 +01:00
* feat: add option to disable titling as well as decide what model to use for OpenAI titling refactor: truncate conversation text so it caps around 200 tokens for titling requests, optimize some of the title prompts * feat: disable bing titling with TITLE_CONVO as well
43 lines
1.4 KiB
JavaScript
43 lines
1.4 KiB
JavaScript
const { z } = require('zod');
|
|
const { langPrompt, createTitlePrompt } = require('../prompts');
|
|
const { escapeBraces, getSnippet } = require('../output_parsers');
|
|
const { createStructuredOutputChainFromZod } = require('langchain/chains/openai_functions');
|
|
|
|
const langSchema = z.object({
|
|
language: z.string().describe('The language of the input text (full noun, no abbreviations).'),
|
|
});
|
|
|
|
const createLanguageChain = ({ llm }) =>
|
|
createStructuredOutputChainFromZod(langSchema, {
|
|
prompt: langPrompt,
|
|
llm,
|
|
// verbose: true,
|
|
});
|
|
|
|
const titleSchema = z.object({
|
|
title: z.string().describe('The conversation title in title-case, in the given language.'),
|
|
});
|
|
const createTitleChain = ({ llm, convo }) => {
|
|
const titlePrompt = createTitlePrompt({ convo });
|
|
return createStructuredOutputChainFromZod(titleSchema, {
|
|
prompt: titlePrompt,
|
|
llm,
|
|
// verbose: true,
|
|
});
|
|
};
|
|
|
|
const runTitleChain = async ({ llm, text, convo }) => {
|
|
let snippet = text;
|
|
try {
|
|
snippet = getSnippet(text);
|
|
} catch (e) {
|
|
console.log('Error getting snippet of text for titleChain');
|
|
console.log(e);
|
|
}
|
|
const languageChain = createLanguageChain({ llm });
|
|
const titleChain = createTitleChain({ llm, convo: escapeBraces(convo) });
|
|
const { language } = await languageChain.run(snippet);
|
|
return (await titleChain.run(language)).title;
|
|
};
|
|
|
|
module.exports = runTitleChain;
|