I encountered an issue when using the OpenAI Chat Model node with the model gpt-4o-search-preview. I get the following error:
Bad request - please check your parameters
Model incompatible request arguments supplied: frequency_penalty, n, presence_penalty, temperature, top_p
However, I’m not providing any of these parameters in my node. I suspect that this model does not support some of the default arguments.
Therefore, I would like to create my own custom Language Model using the LangChain Code node in order to have better control over the parameters. Could someone please provide me with a complete code example for calling the gpt4o (or gpt-4o-search-preview) model via the LangChain Code node?
I need to know exactly what to include in the code of my LangChain Code node to use it correctly.
This setup allows you to bypass the issues with default parameters and gives you more control over the request sent to the OpenAI API. I hope this helps anyone facing similar issues!
I have tested the solution with the LangChain Code node for using the “gpt-4o-search-preview” model, but it doesn’t work either. I’m sharing the solution that works with other models.
Example: Using “LangChain Code” with gpt-4-mini
In your LangChain Code node, click on Add Code and select Execute.
Paste the following code:
const { PromptTemplate } = require('@langchain/core/prompts');
const { ChatOpenAI } = require('@langchain/openai');
const query = 'Tell me a joke';
const prompt = PromptTemplate.fromTemplate(query);
// Create an instance of ChatOpenAI with the GPT-4o-mini model
const llm = new ChatOpenAI({
modelName: "gpt-4o-mini", // replace with your desired model
temperature: 0.7,
openAIApiKey: 'YOUR API KEY',
});
let chain = prompt.pipe(llm);
const output = await chain.invoke();
return [ { json: { output } } ];
In your node’s Input and Output section, add “Main” and leave the default values.
Execute the code; it should work as expected with the gpt-4-mini model.
This example should help those looking to integrate custom language models using the LangChain Code node in n8n, even though the “gpt-4o-search-preview” model is currently not supported with this method.