diff --git a/api/server/services/Endpoints/custom/initializeClient.js b/api/server/services/Endpoints/custom/initializeClient.js index 18ca8c4455..a80f5efaa7 100644 --- a/api/server/services/Endpoints/custom/initializeClient.js +++ b/api/server/services/Endpoints/custom/initializeClient.js @@ -60,8 +60,8 @@ const initializeClient = async ({ req, res, endpointOption }) => { } } - let apiKey = userProvidesKey ? userValues.apiKey : CUSTOM_API_KEY; - let baseURL = userProvidesURL ? userValues.baseURL : CUSTOM_BASE_URL; + let apiKey = userProvidesKey ? userValues?.apiKey : CUSTOM_API_KEY; + let baseURL = userProvidesURL ? userValues?.baseURL : CUSTOM_BASE_URL; if (!apiKey) { throw new Error(`${endpoint} API key not provided.`); diff --git a/api/server/services/Endpoints/gptPlugins/initializeClient.js b/api/server/services/Endpoints/gptPlugins/initializeClient.js index a596c7820b..2920a58917 100644 --- a/api/server/services/Endpoints/gptPlugins/initializeClient.js +++ b/api/server/services/Endpoints/gptPlugins/initializeClient.js @@ -63,8 +63,8 @@ const initializeClient = async ({ req, res, endpointOption }) => { } } - let apiKey = userProvidesKey ? userValues.apiKey : credentials[endpoint]; - let baseURL = userProvidesURL ? userValues.baseURL : baseURLOptions[endpoint]; + let apiKey = userProvidesKey ? userValues?.apiKey : credentials[endpoint]; + let baseURL = userProvidesURL ? userValues?.baseURL : baseURLOptions[endpoint]; const clientOptions = { contextStrategy, diff --git a/api/server/services/Endpoints/gptPlugins/initializeClient.spec.js b/api/server/services/Endpoints/gptPlugins/initializeClient.spec.js index 01718c31d2..1b7147d9f7 100644 --- a/api/server/services/Endpoints/gptPlugins/initializeClient.spec.js +++ b/api/server/services/Endpoints/gptPlugins/initializeClient.spec.js @@ -340,9 +340,10 @@ describe('gptPlugins/initializeClient', () => { test('should initialize client with default options when certain env vars are not set', async () => { delete process.env.DEBUG_OPENAI; delete process.env.OPENAI_SUMMARIZE; + process.env.OPENAI_API_KEY = 'some-api-key'; const req = { - body: { key: null, endpoint: 'openAI' }, + body: { key: null, endpoint: EModelEndpoint.gptPlugins }, user: { id: '123' }, app, }; diff --git a/api/server/services/Endpoints/openAI/initializeClient.js b/api/server/services/Endpoints/openAI/initializeClient.js index 06dad5963a..9dd9765dd0 100644 --- a/api/server/services/Endpoints/openAI/initializeClient.js +++ b/api/server/services/Endpoints/openAI/initializeClient.js @@ -50,8 +50,8 @@ const initializeClient = async ({ req, res, endpointOption }) => { } } - let apiKey = userProvidesKey ? userValues.apiKey : credentials[endpoint]; - let baseURL = userProvidesURL ? userValues.baseURL : baseURLOptions[endpoint]; + let apiKey = userProvidesKey ? userValues?.apiKey : credentials[endpoint]; + let baseURL = userProvidesURL ? userValues?.baseURL : baseURLOptions[endpoint]; const clientOptions = { debug: isEnabled(DEBUG_OPENAI), diff --git a/api/server/services/Endpoints/openAI/initializeClient.spec.js b/api/server/services/Endpoints/openAI/initializeClient.spec.js index bf31d2d9bf..1a53f95b3d 100644 --- a/api/server/services/Endpoints/openAI/initializeClient.spec.js +++ b/api/server/services/Endpoints/openAI/initializeClient.spec.js @@ -94,7 +94,7 @@ describe('initializeClient', () => { process.env.OPENAI_SUMMARIZE = 'false'; const req = { - body: { key: null, endpoint: 'openAI' }, + body: { key: null, endpoint: EModelEndpoint.openAI }, user: { id: '123' }, app, }; @@ -137,7 +137,7 @@ describe('initializeClient', () => { process.env.DEBUG_OPENAI = 'true'; const req = { - body: { key: null, endpoint: 'openAI' }, + body: { key: null, endpoint: EModelEndpoint.openAI }, user: { id: '123' }, app, }; @@ -154,7 +154,7 @@ describe('initializeClient', () => { process.env.OPENAI_SUMMARIZE = 'true'; const req = { - body: { key: null, endpoint: 'openAI' }, + body: { key: null, endpoint: EModelEndpoint.openAI }, user: { id: '123' }, app, }; @@ -172,7 +172,7 @@ describe('initializeClient', () => { process.env.PROXY = 'http://proxy'; const req = { - body: { key: null, endpoint: 'openAI' }, + body: { key: null, endpoint: EModelEndpoint.openAI }, user: { id: '123' }, app, }; @@ -193,7 +193,7 @@ describe('initializeClient', () => { const expiresAt = new Date(Date.now() - 10000).toISOString(); // Expired const req = { - body: { key: expiresAt, endpoint: 'openAI' }, + body: { key: expiresAt, endpoint: EModelEndpoint.openAI }, user: { id: '123' }, app, }; @@ -209,7 +209,7 @@ describe('initializeClient', () => { delete process.env.AZURE_API_KEY; const req = { - body: { key: null, endpoint: 'openAI' }, + body: { key: null, endpoint: EModelEndpoint.openAI }, user: { id: '123' }, app, }; @@ -226,7 +226,7 @@ describe('initializeClient', () => { const req = { body: { key: new Date(Date.now() + 10000).toISOString(), - endpoint: 'openAI', + endpoint: EModelEndpoint.openAI, }, user: { id: '123', @@ -253,7 +253,7 @@ describe('initializeClient', () => { test('should throw an error if the user-provided key is invalid', async () => { const invalidKey = new Date(Date.now() - 100000).toISOString(); const req = { - body: { key: invalidKey, endpoint: 'openAI' }, + body: { key: invalidKey, endpoint: EModelEndpoint.openAI }, user: { id: '123' }, app, }; @@ -272,7 +272,7 @@ describe('initializeClient', () => { test('should throw an error when user-provided values are not valid JSON', async () => { process.env.OPENAI_API_KEY = 'user_provided'; const req = { - body: { key: new Date(Date.now() + 10000).toISOString(), endpoint: 'openAI' }, + body: { key: new Date(Date.now() + 10000).toISOString(), endpoint: EModelEndpoint.openAI }, user: { id: '123' }, app, }; @@ -315,9 +315,10 @@ describe('initializeClient', () => { test('should initialize client with default options when certain env vars are not set', async () => { delete process.env.DEBUG_OPENAI; delete process.env.OPENAI_SUMMARIZE; + process.env.OPENAI_API_KEY = 'some-api-key'; const req = { - body: { key: null, endpoint: 'openAI' }, + body: { key: null, endpoint: EModelEndpoint.openAI }, user: { id: '123' }, app, }; @@ -336,7 +337,7 @@ describe('initializeClient', () => { const req = { body: { key: new Date(Date.now() + 10000).toISOString(), - endpoint: 'openAI', + endpoint: EModelEndpoint.openAI, }, user: { id: '123',