From 50817f601eb23c4f01f5df49019b79aad910d37c Mon Sep 17 00:00:00 2001 From: Anuja Mishra <109236275+anuja12mishra@users.noreply.github.com> Date: Thu, 20 Mar 2025 22:20:51 +0530 Subject: [PATCH] dev: Standardized OpenAI config format and updated documentation issue #1180 (#1195) * Standardized OpenAI config format and updated documentation issue #1180 * Addressed review comments --- .../puterai/OpenAICompletionService.js | 33 +++++++++++++++---- .../modules/puterai/doc/ai-services-config.md | 19 +++++++++++ src/backend/src/modules/puterai/doc/config.md | 2 ++ 3 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 src/backend/src/modules/puterai/doc/ai-services-config.md create mode 100644 src/backend/src/modules/puterai/doc/config.md diff --git a/src/backend/src/modules/puterai/OpenAICompletionService.js b/src/backend/src/modules/puterai/OpenAICompletionService.js index c32d9df7b..74f07e9c6 100644 --- a/src/backend/src/modules/puterai/OpenAICompletionService.js +++ b/src/backend/src/modules/puterai/OpenAICompletionService.js @@ -52,15 +52,36 @@ class OpenAICompletionService extends BaseService { * @returns {Promise} Resolves when initialization is complete * @private */ + + //New Updated Code (with backward compatibility) + //issue: #1180 async _init () { - const sk_key = - this.config?.openai?.secret_key ?? - this.global_config.openai?.secret_key; - + // Check for the new format under `services.openai.apiKey` + let apiKey = + this.config?.services?.openai?.apiKey ?? + this.global_config?.services?.openai?.apiKey; + + // Fallback to the old format for backward compatibility + if (!apiKey) { + apiKey = + this.config?.openai?.secret_key ?? + this.global_config?.openai?.secret_key; + + // Log a warning to inform users about the deprecated format + this.log.warn( + 'The `openai.secret_key` configuration format is deprecated. ' + + 'Please use `services.openai.apiKey` instead.' + ); + } + + if (!apiKey) { + throw new Error('OpenAI API key is missing in configuration.'); + } + this.openai = new this.modules.openai.OpenAI({ - apiKey: sk_key + apiKey: apiKey }); - + const svc_aiChat = this.services.get('ai-chat'); svc_aiChat.register_provider({ service_name: this.service_name, diff --git a/src/backend/src/modules/puterai/doc/ai-services-config.md b/src/backend/src/modules/puterai/doc/ai-services-config.md new file mode 100644 index 000000000..ab648442a --- /dev/null +++ b/src/backend/src/modules/puterai/doc/ai-services-config.md @@ -0,0 +1,19 @@ +# Configuring AI Services + +AI services are configured under the `services` block in the configuration file. Each service requires an `apiKey` to authenticate requests. + +## Example Configuration +```json +{ + "services": { + "openai": { + "apiKey": "sk-abcdefg..." + }, + "deepseek": { + "apiKey": "sk-xyz123..." + }, + "other-ai-service": { + "apiKey": "sk-hijklmn..." + } + } +} diff --git a/src/backend/src/modules/puterai/doc/config.md b/src/backend/src/modules/puterai/doc/config.md new file mode 100644 index 000000000..1828882da --- /dev/null +++ b/src/backend/src/modules/puterai/doc/config.md @@ -0,0 +1,2 @@ +## AI Services Configuration +For details on configuring AI services, see [AI Services Configuration](ai-services-config.md). \ No newline at end of file