Skip to content
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

refactor: Move OpenAI Base URL option to credentials #12175

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class EmbeddingsOpenAi implements INodeType {
},
],
group: ['transform'],
version: [1, 1.1],
version: [1, 1.1, 1.2],
description: 'Use Embeddings OpenAI',
defaults: {
name: 'Embeddings OpenAI',
Expand All @@ -106,7 +106,7 @@ export class EmbeddingsOpenAi implements INodeType {
requestDefaults: {
ignoreHttpStatusErrors: true,
baseURL:
'={{ $parameter.options?.baseURL?.split("/").slice(0,-1).join("/") || "https://api.openai.com" }}',
'={{ $parameter.options?.baseURL?.split("/").slice(0,-1).join("/") || $credentials.url?.split("/").slice(0,-1).join("/") || "https://api.openai.com" }}',
},
properties: [
getConnectionHintNoticeField([NodeConnectionType.AiVectorStore]),
Expand Down Expand Up @@ -171,6 +171,11 @@ export class EmbeddingsOpenAi implements INodeType {
default: 'https://api.openai.com/v1',
description: 'Override the default base URL for the API',
type: 'string',
displayOptions: {
hide: {
'@version': [{ _cnd: { gte: 1.2 } }],
},
},
},
{
displayName: 'Batch Size',
Expand Down Expand Up @@ -219,6 +224,8 @@ export class EmbeddingsOpenAi implements INodeType {
const configuration: ClientOptions = {};
if (options.baseURL) {
configuration.baseURL = options.baseURL;
} else if (credentials.url) {
configuration.baseURL = credentials.url as string;
}

const embeddings = new OpenAIEmbeddings(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class LmChatOpenAi implements INodeType {
name: 'lmChatOpenAi',
icon: { light: 'file:openAiLight.svg', dark: 'file:openAiLight.dark.svg' },
group: ['transform'],
version: 1,
version: [1, 1.1],
description: 'For advanced usage with an AI chain',
defaults: {
name: 'OpenAI Chat Model',
Expand Down Expand Up @@ -54,7 +54,7 @@ export class LmChatOpenAi implements INodeType {
requestDefaults: {
ignoreHttpStatusErrors: true,
baseURL:
'={{ $parameter.options?.baseURL?.split("/").slice(0,-1).join("/") || "https://api.openai.com" }}',
'={{ $parameter.options?.baseURL?.split("/").slice(0,-1).join("/") || $credentials?.url?.split("/").slice(0,-1).join("/") || "https://api.openai.com" }}',
},
properties: [
getConnectionHintNoticeField([NodeConnectionType.AiChain, NodeConnectionType.AiAgent]),
Expand All @@ -81,7 +81,7 @@ export class LmChatOpenAi implements INodeType {
routing: {
request: {
method: 'GET',
url: '={{ $parameter.options?.baseURL?.split("/").slice(-1).pop() || "v1" }}/models',
url: '={{ $parameter.options?.baseURL?.split("/").slice(-1).pop() || $credentials?.url?.split("/").slice(-1).pop() || "v1" }}/models',
},
output: {
postReceive: [
Expand All @@ -97,6 +97,7 @@ export class LmChatOpenAi implements INodeType {
// If the baseURL is not set or is set to api.openai.com, include only chat models
pass: `={{
($parameter.options?.baseURL && !$parameter.options?.baseURL?.includes('api.openai.com')) ||
($credentials?.url && !$credentials.url.includes('api.openai.com')) ||
$responseItem.id.startsWith('ft:') ||
$responseItem.id.startsWith('o1') ||
($responseItem.id.startsWith('gpt-') && !$responseItem.id.includes('instruct'))
Expand Down Expand Up @@ -155,6 +156,11 @@ export class LmChatOpenAi implements INodeType {
default: 'https://api.openai.com/v1',
description: 'Override the default base URL for the API',
type: 'string',
displayOptions: {
hide: {
'@version': [{ _cnd: { gte: 1.1 } }],
},
},
},
{
displayName: 'Frequency Penalty',
Expand Down Expand Up @@ -260,6 +266,8 @@ export class LmChatOpenAi implements INodeType {
const configuration: ClientOptions = {};
if (options.baseURL) {
configuration.baseURL = options.baseURL;
} else if (credentials.url) {
configuration.baseURL = credentials.url as string;
}

const model = new ChatOpenAI({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ const properties: INodeProperties[] = [
default: 'https://api.openai.com/v1',
description: 'Override the default base URL for the API',
type: 'string',
displayOptions: {
hide: {
'@version': [{ _cnd: { gte: 1.8 } }],
},
},
},
{
displayName: 'Max Retries',
Expand Down Expand Up @@ -181,11 +186,13 @@ export async function execute(this: IExecuteFunctions, i: number): Promise<INode
preserveOriginalTools?: boolean;
};

const baseURL = (options.baseURL ?? credentials.url) as string;

const client = new OpenAIClient({
apiKey: credentials.apiKey as string,
maxRetries: options.maxRetries ?? 2,
timeout: options.timeout ?? 10000,
baseURL: options.baseURL,
baseURL,
});

const agent = new OpenAIAssistantRunnable({ assistantId, client, asAgent: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const versionDescription: INodeTypeDescription = {
name: 'openAi',
icon: { light: 'file:openAi.svg', dark: 'file:openAi.dark.svg' },
group: ['transform'],
version: [1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7],
version: [1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8],
subtitle: `={{(${prettifyOperation})($parameter.resource, $parameter.operation)}}`,
description: 'Message an assistant or GPT, analyze images, generate audio, etc.',
defaults: {
Expand Down
11 changes: 9 additions & 2 deletions packages/nodes-base/credentials/OpenAiApi.credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ export class OpenAiApi implements ICredentialType {
description:
"For users who belong to multiple organizations, you can set which organization is used for an API request. Usage from these API requests will count against the specified organization's subscription quota.",
},
{
displayName: 'Base URL',
name: 'url',
type: 'string',
default: 'https://api.openai.com/v1',
description: 'Override the default base URL for the API',
},
];

authenticate: IAuthenticateGeneric = {
Expand All @@ -44,8 +51,8 @@ export class OpenAiApi implements ICredentialType {

test: ICredentialTestRequest = {
request: {
baseURL: 'https://api.openai.com',
url: '/v1/models',
baseURL: '={{$credentials?.url}}',
url: '/models',
},
};
}
3 changes: 2 additions & 1 deletion packages/nodes-base/nodes/OpenAi/OpenAi.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export class OpenAi implements INodeType {
],
requestDefaults: {
ignoreHttpStatusErrors: true,
baseURL: 'https://api.openai.com',
baseURL:
'={{ $credentials.url?.split("/").slice(0,-1).join("/") || https://api.openai.com }}',
},
properties: [
oldVersionNotice,
Expand Down
Loading