-
-
Notifications
You must be signed in to change notification settings - Fork 24.5k
fix(chatopenai): honor proxy URL #6504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
luochen211
wants to merge
1
commit into
FlowiseAI:main
Choose a base branch
from
luochen211:fix-chatopenai-proxy-url
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+125
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
packages/components/nodes/chatmodels/ChatOpenAI/ChatOpenAI.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' } | ||
| } | ||
| } | ||
| }) | ||
| ) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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' | ||
|
|
||
| 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 | ||
|
|
@@ -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', | ||
|
|
@@ -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 | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Retrieve the cached 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
}
}
: {})
}
} |
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To prevent socket leaks and enable connection reuse (keep-alive) across multiple executions, we should cache and reuse
ProxyAgentinstances instead of creating a new one on every initialization. Let's declare a module-level cache map for the proxy agents.