import { RunnableSequence, RunnableMap } from '@langchain/core/runnables'; import ListLineOutputParser from '../lib/outputParsers/listLineOutputParser'; import { PromptTemplate } from '@langchain/core/prompts'; import formatChatHistoryAsString from '../utils/formatHistory'; import { BaseMessage } from '@langchain/core/messages'; import { BaseChatModel } from '@langchain/core/language_models/chat_models'; import { ChatOpenAI } from '@langchain/openai'; const suggestionGeneratorPrompt = ` You are an AI suggestion generator for an AI-powered search engine. Based on the conversation provided, generate 4-5 relevant suggestions that the user can use to ask the chat model for more information. - Ensure each suggestion is relevant to the conversation and provides value to the user. - The suggestions should be medium-length and informative, offering a path for the user to explore further. Provide the suggestions in the following format, separated by newlines between the XML tags and : Suggestion 1 Suggestion 2 Suggestion 3 Conversation: {chat_history} `; type SuggestionGeneratorInput = { chat_history: BaseMessage[]; }; const outputParser = new ListLineOutputParser({ key: 'suggestions', }); const createSuggestionGeneratorChain = (llm: BaseChatModel) => { return RunnableSequence.from([ RunnableMap.from({ chat_history: (input: SuggestionGeneratorInput) => formatChatHistoryAsString(input.chat_history), }), PromptTemplate.fromTemplate(suggestionGeneratorPrompt), llm, outputParser, ]); }; const generateSuggestions = ( input: SuggestionGeneratorInput, llm: BaseChatModel, ) => { (llm as unknown as ChatOpenAI).temperature = 0; const suggestionGeneratorChain = createSuggestionGeneratorChain(llm); return suggestionGeneratorChain.invoke(input); }; export default generateSuggestions;