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 dev.langchain4j.data.message.SystemMessage;
031import dev.langchain4j.model.chat.ChatModel;
032import dev.langchain4j.model.chat.StreamingChatModel;
033import dev.langchain4j.model.chat.request.ResponseFormat;
034import dev.langchain4j.model.openai.OpenAiChatModel;
035import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
036
037/**
038 * Provides a wrapper for an AI model with provider configuration.
039 *
040 * @since 21.0.0
041 */
042public class CmsAiModel {
043
044    /** The AI provider configuration. */
045    private CmsAiProviderConfig m_config;
046
047    /** The AI model. */
048    private ChatModel m_model;
049
050    /** The streaming AI model. */
051    private StreamingChatModel m_streamingModel;
052
053    /** The basic prompt for the LLM. */
054    private SystemMessage m_llmPrompt;
055
056    /** The (optional) LLM response format. */
057    private ResponseFormat m_llmResponseFormat;
058
059    /**
060     * Generate a new AI model wrapper.
061     *
062     * @param config the AI provider configuration
063     */
064    public CmsAiModel(CmsAiProviderConfig config) {
065
066        m_config = config;
067    }
068
069    /**
070     * Returns a chat model initialized with the provider configuration.<p>
071     *
072     * @return  a chat model initialized with the provider configuration
073     */
074    public ChatModel getChatModel() {
075
076        if (m_model == null) {
077            m_model = OpenAiChatModel.builder().apiKey(m_config.getApiKey()).baseUrl(
078                m_config.getProviderUrl()).modelName(m_config.getModelName()).build();
079        }
080        return m_model;
081    }
082
083    /**
084     * Returns a streaming chat model initialized with the provider configuration.<p>
085     *
086     * @return a streaming chat model initialized with the provider configuration
087     */
088    public StreamingChatModel getStreamingChatModel() {
089
090        if (m_streamingModel == null) {
091            m_streamingModel = OpenAiStreamingChatModel.builder().apiKey(m_config.getApiKey()).baseUrl(
092                m_config.getProviderUrl()).modelName(m_config.getModelName()).build();
093        }
094        return m_streamingModel;
095    }
096}