feat(agents): update suggestion generator

This commit is contained in:
ItzCrazyKns
2025-11-23 19:23:18 +05:30
parent 74bc08d189
commit 0ac8569a9e
3 changed files with 35 additions and 36 deletions

View File

@@ -1,32 +1,39 @@
import ListLineOutputParser from '@/lib/outputParsers/listLineOutputParser';
import { ChatPromptTemplate, PromptTemplate } from '@langchain/core/prompts';
import formatChatHistoryAsString from '@/lib/utils/formatHistory';
import { BaseMessage, HumanMessage, SystemMessage } from '@langchain/core/messages';
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
import { suggestionGeneratorPrompt } from '@/lib/prompts/suggestions';
import { ChatTurnMessage } from '@/lib/types';
import z from 'zod';
import BaseLLM from '@/lib/models/base/llm';
import { i } from 'mathjs';
type SuggestionGeneratorInput = {
chatHistory: BaseMessage[];
chatHistory: ChatTurnMessage[];
};
const outputParser = new ListLineOutputParser({
key: 'suggestions',
const schema = z.object({
suggestions: z
.array(z.string())
.describe('List of suggested questions or prompts'),
});
const generateSuggestions = async (
input: SuggestionGeneratorInput,
llm: BaseChatModel,
llm: BaseLLM<any>,
) => {
const chatPrompt = await ChatPromptTemplate.fromMessages([
new SystemMessage(suggestionGeneratorPrompt),
new HumanMessage(`<conversation>${formatChatHistoryAsString(input.chatHistory)}</conversation>`)
]).formatMessages({})
const res = await llm.generateObject<z.infer<typeof schema>>({
messages: [
{
role: 'system',
content: suggestionGeneratorPrompt,
},
{
role: 'user',
content: `<chat_history>\n${formatChatHistoryAsString(input.chatHistory)}\n</chat_history>`,
},
],
schema,
});
const res = await llm.invoke(chatPrompt)
const suggestions = await outputParser.invoke(res)
return suggestions
return res.suggestions;
};
export default generateSuggestions;