LibreChat/api/app/clients/chains/runTitleChain.js
Danny Avila 1d3e336e1c
feat: Add Option to Disable Titling, Config Titling Model, and Title Prompt Improvements (#977)
* 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
2023-09-20 18:45:56 -04:00

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;