Use findLast to avoid reversing

This commit is contained in:
Tat Dat Duong
2025-03-02 19:09:59 +01:00
parent a15a9104b4
commit b0f0784a6b

View File

@@ -32,10 +32,9 @@ async function router(
const prompt = `You're a highly helpful AI assistant, tasked with routing the user's query to the appropriate tool. const prompt = `You're a highly helpful AI assistant, tasked with routing the user's query to the appropriate tool.
You should analyze the user's input, and choose the appropriate tool to use.`; You should analyze the user's input, and choose the appropriate tool to use.`;
const messagesCopy = state.messages; const recentHumanMessage = state.messages.findLast(
const recentHumanMessage = messagesCopy (m) => m.getType() === "human"
.reverse() );
.find((m) => m.getType() === "human");
if (!recentHumanMessage) { if (!recentHumanMessage) {
throw new Error("No human message found in state"); throw new Error("No human message found in state");
@@ -66,11 +65,7 @@ function handleRoute(
async function handleGeneralInput(state: GenerativeUIState) { async function handleGeneralInput(state: GenerativeUIState) {
const llm = new ChatOpenAI({ model: "gpt-4o-mini", temperature: 0 }); const llm = new ChatOpenAI({ model: "gpt-4o-mini", temperature: 0 });
const messagesCopy = state.messages; const response = await llm.invoke(state.messages);
messagesCopy.reverse();
console.log("messagesCopy", messagesCopy);
const response = await llm.invoke(messagesCopy);
return { return {
messages: [response], messages: [response],