001/*
002 *  Copyright (c) 2023-2026, Agents-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.agentsflex.core.model.chat;
017
018import com.agentsflex.core.model.client.ChatClient;
019import com.agentsflex.core.model.client.ChatRequestSpecBuilder;
020import com.agentsflex.core.model.client.OpenAIChatClient;
021import com.agentsflex.core.model.client.OpenAIChatRequestSpecBuilder;
022
023import java.util.List;
024
025public class OpenAICompatibleChatModel<T extends ChatConfig> extends BaseChatModel<T> {
026    /**
027     * 构造一个聊天模型实例,不使用实例级拦截器。
028     *
029     * @param config 聊天模型配置
030     */
031    public OpenAICompatibleChatModel(T config) {
032        super(config);
033    }
034
035    /**
036     * 构造一个聊天模型实例,并指定实例级拦截器。
037     * <p>
038     * 实例级拦截器会与全局拦截器(通过 {@link GlobalChatInterceptors} 注册)合并,
039     * 执行顺序为:可观测性拦截器 → 全局拦截器 → 实例拦截器。
040     *
041     * @param config           聊天模型配置
042     * @param userInterceptors 实例级拦截器列表
043     */
044    public OpenAICompatibleChatModel(T config, List<ChatInterceptor> userInterceptors) {
045        super(config, userInterceptors);
046    }
047
048    @Override
049    public ChatClient getChatClient() {
050        if (this.chatClient == null) {
051            this.chatClient = buildChatClient();
052        }
053        return this.chatClient;
054    }
055
056    @Override
057    public ChatRequestSpecBuilder getChatRequestSpecBuilder() {
058        if (this.chatRequestSpecBuilder == null) {
059            this.chatRequestSpecBuilder = buildChatRequestSpecBuilder();
060        }
061        return this.chatRequestSpecBuilder;
062    }
063
064    protected ChatRequestSpecBuilder buildChatRequestSpecBuilder() {
065        return new OpenAIChatRequestSpecBuilder();
066    }
067
068    protected ChatClient buildChatClient() {
069        return new OpenAIChatClient(this);
070    }
071}