001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.ai; 029 030import org.opencms.main.CmsLog; 031import org.opencms.main.OpenCms; 032import org.opencms.security.I_CmsSecretStore; 033 034import java.io.File; 035import java.util.function.Function; 036 037import org.apache.commons.logging.Log; 038 039import com.fasterxml.jackson.annotation.JsonCreator; 040import com.fasterxml.jackson.annotation.JsonProperty; 041import com.fasterxml.jackson.databind.ObjectMapper; 042 043/** 044 * Simple configuration bean for AI provider access settings.<p> 045 * 046 * @since 21.0.0 047 */ 048public class CmsAiProviderConfig { 049 050 /** Logger instance for this class. */ 051 private static final Log LOG = CmsLog.getLog(CmsAiProviderConfig.class); 052 053 public static final String SECRET_PREFIX = "contenteditor.translation.ai"; 054 055 /** The API key used to access the provider. */ 056 private String m_apiKey; 057 058 /** The base URL of the provider endpoint. */ 059 private String m_providerUrl; 060 061 /** The model name to use for requests. */ 062 private String m_modelName; 063 064 /** 065 * Creates a new provider configuration.<p> 066 * 067 * @param apiKey the API key to use 068 * @param providerUrl the provider base URL 069 * @param modelName the model name 070 */ 071 @JsonCreator 072 public CmsAiProviderConfig( 073 @JsonProperty(value = "apiKey", required = true) String apiKey, 074 @JsonProperty(value = "providerUrl", required = true) String providerUrl, 075 @JsonProperty(value = "modelName", required = true) String modelName) { 076 077 m_apiKey = apiKey; 078 m_providerUrl = providerUrl; 079 m_modelName = modelName; 080 } 081 082 /** 083 * Loads the provider configuration from a JSON file in the RFS. 084 * 085 * @param path the path of the config file 086 * @return the provider configuration 087 * @throws Exception if loading the file fails 088 */ 089 public static CmsAiProviderConfig loadFromJsonFile(String path) throws Exception { 090 091 ObjectMapper mapper = new ObjectMapper(); 092 return mapper.readValue(new File(path), CmsAiProviderConfig.class); 093 } 094 095 /** 096 * Loads the configuration from the secret store. 097 * 098 * <p>Returns null if one or more of the credentials could not be read from the secret store. 099 * 100 * @return the loaded configuration 101 * @throws Exception if something goes wrong 102 */ 103 public static CmsAiProviderConfig loadFromSecretStore() { 104 105 I_CmsSecretStore secrets = OpenCms.getSecretStore(); 106 Function<String, String> getSecret = key -> { 107 String result = secrets.getSecret(key); 108 if (result == null) { 109 LOG.error("Could not read credentials for AI translation:" + key); 110 } 111 return result; 112 }; 113 String apiKey = getSecret.apply(SECRET_PREFIX + ".apiKey"); 114 String providerUrl = getSecret.apply(SECRET_PREFIX + ".providerUrl"); 115 String modelName = getSecret.apply(SECRET_PREFIX + ".modelName"); 116 if ((apiKey == null) || (providerUrl == null) || (modelName == null)) { 117 return null; 118 } 119 return new CmsAiProviderConfig(apiKey, providerUrl, modelName); 120 121 } 122 123 /** 124 * Returns the API key used to access the provider.<p> 125 * 126 * @return the API key 127 */ 128 public String getApiKey() { 129 130 return m_apiKey; 131 } 132 133 /** 134 * Returns the model name used for requests.<p> 135 * 136 * @return the model name 137 */ 138 public String getModelName() { 139 140 return m_modelName; 141 } 142 143 /** 144 * Returns the provider base URL.<p> 145 * 146 * @return the provider URL 147 */ 148 public String getProviderUrl() { 149 150 return m_providerUrl; 151 } 152}