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.message.AiMessage;
019import com.agentsflex.core.model.exception.ModelException;
020import com.agentsflex.core.model.chat.response.AbstractBaseMessageResponse;
021import com.agentsflex.core.model.chat.response.AiMessageResponse;
022import com.agentsflex.core.prompt.Prompt;
023import com.agentsflex.core.prompt.SimplePrompt;
024
025public interface ChatModel {
026
027    default String chat(String prompt) {
028        return chat(prompt, new ChatOptions());
029    }
030
031    default String chat(String prompt, ChatOptions options) {
032        AbstractBaseMessageResponse<AiMessage> response = chat(new SimplePrompt(prompt), options);
033        if (response != null && response.isError()) {
034            throw new ModelException(response.getErrorMessage());
035        }
036        return response != null && response.getMessage() != null ? response.getMessage().getContent() : null;
037    }
038
039    default AiMessageResponse chat(Prompt prompt) {
040        return chat(prompt, new ChatOptions());
041    }
042
043    AiMessageResponse chat(Prompt prompt, ChatOptions options);
044
045    default void chatStream(String prompt, StreamResponseListener listener) {
046        this.chatStream(new SimplePrompt(prompt), listener, new ChatOptions());
047    }
048
049    default void chatStream(String prompt, StreamResponseListener listener, ChatOptions options) {
050        this.chatStream(new SimplePrompt(prompt), listener, options);
051    }
052
053    //chatStream
054    default void chatStream(Prompt prompt, StreamResponseListener listener) {
055        this.chatStream(prompt, listener, new ChatOptions());
056    }
057
058    void chatStream(Prompt prompt, StreamResponseListener listener, ChatOptions options);
059
060}