Any way to use OpenAI Chat Model with default open AI tool: web_search_preview

Describe the problem/error/question

I would like to use the OpenAI Chat Model with the default tool provided by OpenAI for web searches: web_search_preview

As I understand it, this would allow the requests to the Open AI Chat Model to use web search’s to supplement the response similar to what is shown on the ChatGPT site.

The OpenAI documentation shows how to use this through their API: https://platform.openai.com/docs/guides/tools-web-search?api-mode=responses

import OpenAI from "openai";
const client = new OpenAI();

const response = await client.responses.create({
    model: "gpt-4o",
    tools: [ { type: "web_search_preview" } ],
    input: "What was a positive news story from today?",
});

I looked into LangChain and it appears that this is supported in their API as well as a pass through. See: ChatOpenAI | 🦜️🔗 LangChain

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o-mini")

tool = {"type": "web_search_preview"}
llm_with_tools = llm.bind_tools([tool])

response = llm_with_tools.invoke("What was a positive news story from today?")

So I tracked down the n8n node code to see if there was any way to pass this in through a configuration. From what I can tell there is not. See: n8n/packages/@n8n/nodes-langchain/nodes/llms/LMChatOpenAi/LmChatOpenAi.node.ts at master · n8n-io/n8n · GitHub

const model = new ChatOpenAI({
			openAIApiKey: credentials.apiKey as string,
			modelName,
			...options,
			timeout: options.timeout ?? 60000,
			maxRetries: options.maxRetries ?? 2,
			configuration,
			callbacks: [new N8nLlmTracing(this)],
			modelKwargs,
			onFailedAttempt: makeN8nLlmFailedAttemptHandler(this, openAiFailedAttemptHandler),
});

I think it should be pretty easy for someone that knows the codebase well to add another option to allow passing in the names of some default tools or something similar. Unfortunately I don’t know n8n well enough to run it in development or how all of this works. But I thought I would ask here to see if I am missing something or if anyone that does know would be willing to give it a try.

2 Likes

I tried implementing it myself and added this PR: Idea/Question: Initial idea for allowing OpenAI default tools by abierbaum · Pull Request #14329 · n8n-io/n8n · GitHub

It doesn’t work, but wanted to put up an attempt so someone else can tell me what would really need to be done.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.