Browse Source

dev: Standardized OpenAI config format and updated documentation issue #1180 (#1195)

* Standardized OpenAI config format and updated documentation issue #1180

* Addressed review comments
Anuja Mishra 2 months ago
parent
commit
50817f601e

+ 27 - 6
src/backend/src/modules/puterai/OpenAICompletionService.js

@@ -52,15 +52,36 @@ class OpenAICompletionService extends BaseService {
     * @returns {Promise<void>} Resolves when initialization is complete
     * @private
     */
-    async _init () {
-        const sk_key =
-            this.config?.openai?.secret_key ??
-            this.global_config.openai?.secret_key;
 
+    //New Updated Code (with backward compatibility)
+    //issue: #1180
+    async _init () {
+        // 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,

+ 19 - 0
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..."
+    }
+  }
+}

+ 2 - 0
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).