Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions packages/components/nodes/chatmodels/ChatOpenAI/ChatOpenAI.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
jest.mock('@langchain/openai', () => ({
ChatOpenAI: jest.fn(),
ChatOpenAIFields: {}
}))

jest.mock('undici', () => ({
ProxyAgent: jest.fn().mockImplementation((url) => ({ proxyUrl: url }))
}))

jest.mock('../../../src/utils', () => ({
getBaseClasses: jest.fn().mockReturnValue(['BaseChatModel']),
getCredentialData: jest.fn(),
getCredentialParam: jest.fn(),
isReasoningModelOpenAI: jest.fn().mockReturnValue(false)
}))

jest.mock('../../../src/modelLoader', () => ({
MODEL_TYPE: { CHAT: 'chat' },
getModels: jest.fn()
}))

jest.mock('./FlowiseChatOpenAI', () => ({
ChatOpenAI: jest.fn().mockImplementation((id, fields) => ({
id,
fields,
setMultiModalOption: jest.fn()
}))
}))

import { ProxyAgent } from 'undici'
import { getCredentialData, getCredentialParam } from '../../../src/utils'

const { ChatOpenAI } = require('./FlowiseChatOpenAI')
const { nodeClass: ChatOpenAINode } = require('./ChatOpenAI')

describe('ChatOpenAI node', () => {
beforeEach(() => {
jest.clearAllMocks()
})

it('passes proxyUrl to the OpenAI client fetch dispatcher', async () => {
;(getCredentialData as jest.Mock).mockResolvedValue({ openAIApiKey: 'sk-test' })
;(getCredentialParam as jest.Mock).mockImplementation((key, credentialData) => credentialData[key])

const node = new ChatOpenAINode()
const nodeData = {
credential: 'cred-1',
inputs: {
modelName: 'gpt-4o-mini',
temperature: '0.2',
proxyUrl: 'http://corporate-proxy.example.com:3128',
baseOptions: {
'OpenAI-Beta': 'assistants=v2'
}
}
}

await node.init(
{
...nodeData,
id: 'chatOpenAI_0'
},
'',
{}
)
await node.init(
{
...nodeData,
id: 'chatOpenAI_1'
},
'',
{}
)

expect(ProxyAgent).toHaveBeenCalledWith('http://corporate-proxy.example.com:3128')
expect(ProxyAgent).toHaveBeenCalledTimes(1)
expect(ChatOpenAI).toHaveBeenCalledWith(
'chatOpenAI_0',
expect.objectContaining({
configuration: {
defaultHeaders: {
'OpenAI-Beta': 'assistants=v2'
},
fetchOptions: {
dispatcher: { proxyUrl: 'http://corporate-proxy.example.com:3128' }
}
}
})
)
})
})
32 changes: 30 additions & 2 deletions packages/components/nodes/chatmodels/ChatOpenAI/ChatOpenAI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ import { getBaseClasses, getCredentialData, getCredentialParam, isReasoningModel
import { ChatOpenAI } from './FlowiseChatOpenAI'
import { getModels, MODEL_TYPE } from '../../../src/modelLoader'
import { OpenAI as OpenAIClient } from 'openai'
import { ProxyAgent } from 'undici'

Comment on lines +8 to 9

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To prevent socket leaks and enable connection reuse (keep-alive) across multiple executions, we should cache and reuse ProxyAgent instances instead of creating a new one on every initialization. Let's declare a module-level cache map for the proxy agents.

import { ProxyAgent } from 'undici'

const proxyAgentCache = new Map<string, ProxyAgent>()

const proxyAgents = new Map<string, ProxyAgent>()

const getProxyAgent = (proxyUrl: string): ProxyAgent => {
let proxyAgent = proxyAgents.get(proxyUrl)
if (!proxyAgent) {
proxyAgent = new ProxyAgent(proxyUrl)
proxyAgents.set(proxyUrl, proxyAgent)
}
return proxyAgent
}

class ChatOpenAI_ChatModels implements INode {
label: string
Expand Down Expand Up @@ -200,6 +212,14 @@ class ChatOpenAI_ChatModels implements INode {
description: 'Override the default base URL for the API, e.g., "https://api.example.com/v2/',
additionalParams: true
},
{
label: 'Proxy Url',
name: 'proxyUrl',
type: 'string',
optional: true,
description: 'Proxy URL to use for OpenAI API requests, e.g., "http://proxy.example.com:3128"',
additionalParams: true
},
{
label: 'Base Options',
name: 'baseOptions',
Expand Down Expand Up @@ -230,6 +250,7 @@ class ChatOpenAI_ChatModels implements INode {
const streaming = nodeData.inputs?.streaming as boolean
const strictToolCalling = nodeData.inputs?.strictToolCalling as boolean
const basePath = nodeData.inputs?.basepath as string
const proxyUrl = nodeData.inputs?.proxyUrl as string
const baseOptions = nodeData.inputs?.baseOptions
const reasoningEffort = nodeData.inputs?.reasoningEffort as OpenAIClient.ReasoningEffort | null
const reasoningSummary = nodeData.inputs?.reasoningSummary as 'auto' | 'concise' | 'detailed' | null
Expand Down Expand Up @@ -286,10 +307,17 @@ class ChatOpenAI_ChatModels implements INode {
}
}

if (basePath || parsedBaseOptions) {
if (basePath || parsedBaseOptions || proxyUrl) {
obj.configuration = {
baseURL: basePath,
defaultHeaders: parsedBaseOptions
defaultHeaders: parsedBaseOptions,
...(proxyUrl
? {
fetchOptions: {
dispatcher: getProxyAgent(proxyUrl)
}
}
: {})
}
}
Comment on lines +310 to 322

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Retrieve the cached ProxyAgent instance for the given proxyUrl to benefit from connection pooling and avoid resource exhaustion.

        if (basePath || parsedBaseOptions || proxyUrl) {
            let dispatcher: ProxyAgent | undefined = undefined
            if (proxyUrl) {
                let agent = proxyAgentCache.get(proxyUrl)
                if (!agent) {
                    agent = new ProxyAgent(proxyUrl)
                    proxyAgentCache.set(proxyUrl, agent)
                }
                dispatcher = agent
            }

            obj.configuration = {
                baseURL: basePath,
                defaultHeaders: parsedBaseOptions,
                ...(dispatcher
                    ? {
                          fetchOptions: {
                              dispatcher
                          }
                      }
                    : {})
            }
        }


Expand Down
1 change: 1 addition & 0 deletions packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
"srt-parser-2": "^1.2.3",
"supergateway": "3.0.1",
"typeorm": "^0.3.6",
"undici": "6.23.0",
"uuid": "^10.0.0",
"vm2": "3.11.2",
"weaviate-client": "3.12.0",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.