Yesterday, I made a trivial LangChain mistake that led to more than an hour wasted in debugging.
While googling this error, I did not find any useful articles, so hope this post will save someone else's time.
I was working on a code like the one below (mixed with some other stuff):
import { ChatOpenAI } from "@langchain/openai";
import { PromptTemplate } from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";
import * as dotenv from "dotenv";
dotenv.config();
const prompt = PromptTemplate.fromTemplate(
`Answer the question below:
Question: {question}`
);
const model = new ChatOpenAI({ temperature: 0 });
const chain = model
.pipe(model)
.pipe(new StringOutputParser());
const answer = await chain.invoke({
question: 'What is the speed of light?'
});
console.log(answer);
When running the example, I was getting this error:
TypeError: promptValue.toChatMessages is not a function
const promptMessages = promptValues.map((promptValue) => promptValue.toChatMessages());
The reason for this error was how I was building the LCEL chain.
// āļø TypeError: promptValue.toChatMessages is not a function
const chain = model
.pipe(prompt)
.pipe(new StringOutputParser());
const answer = await chain.invoke({
question: 'What is the speed of light?'
});
It turns out that I was passing the output of the model to the prompt š¤¦āāļø.
It needs to be the other way around. The output of the prompt needs to be passed in as input for the model.
// š this will work
const chain = prompt
.pipe(model)
.pipe(new StringOutputParser());
const answer = await chain.invoke({
question: 'What is the speed of light?'
});
There you have it! This was the solution in my case. Hope it helps!
š Build a full trivia game app with LangChain
Learn by doing with this FREE ebook! This 35-page guide walks you through every step of building your first fully functional AI-powered app using JavaScript and LangChain.js
š Build a full trivia game app with LangChain
Learn by doing with this FREE ebook! This 35-page guide walks you through every step of building your first fully functional AI-powered app using JavaScript and LangChain.js