001/*
002 *  Copyright (c) 2023-2025, 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.llm.openai;
017
018import com.agentsflex.core.document.Document;
019import com.agentsflex.core.llm.ChatOptions;
020import com.agentsflex.core.llm.embedding.EmbeddingOptions;
021import com.agentsflex.core.message.HumanMessage;
022import com.agentsflex.core.message.Message;
023import com.agentsflex.core.parser.AiMessageParser;
024import com.agentsflex.core.parser.impl.DefaultAiMessageParser;
025import com.agentsflex.core.prompt.DefaultPromptFormat;
026import com.agentsflex.core.prompt.Prompt;
027import com.agentsflex.core.prompt.PromptFormat;
028import com.agentsflex.core.util.CollectionUtil;
029import com.agentsflex.core.util.Maps;
030
031import java.util.List;
032
033public class OpenAILlmUtil {
034
035    private static final PromptFormat promptFormat = new DefaultPromptFormat();
036
037    public static AiMessageParser getAiMessageParser(boolean isStream) {
038        return DefaultAiMessageParser.getChatGPTMessageParser(isStream);
039    }
040
041
042    public static String promptToEmbeddingsPayload(Document text, EmbeddingOptions options, OpenAILlmConfig config) {
043        // https://platform.openai.com/docs/api-reference/making-requests
044        return Maps.of("model", options.getModelOrDefault(config.getDefaultEmbeddingModel()))
045            .set("encoding_format", "float")
046            .set("input", text.getContent())
047            .toJSON();
048    }
049
050
051    public static String promptToPayload(Prompt prompt, OpenAILlmConfig config, ChatOptions options, boolean withStream) {
052        List<Message> messages = prompt.toMessages();
053        Message message = CollectionUtil.lastItem(messages);
054
055        String toolChoice = null;
056        if (message instanceof HumanMessage) {
057            toolChoice = ((HumanMessage) message).getToolChoice();
058        }
059
060        return Maps.of("model", config.getModel())
061            .set("messages", promptFormat.toMessagesJsonObject(messages))
062            .setIf(withStream, "stream", true)
063            .setIfNotEmpty("tools", promptFormat.toFunctionsJsonObject(message))
064            .setIfContainsKey("tools", "tool_choice", toolChoice)
065            .setIfNotNull("top_p", options.getTopP())
066            .setIfNotEmpty("stop", options.getStop())
067            .setIf(map -> !map.containsKey("tools") && options.getTemperature() > 0, "temperature", options.getTemperature())
068            .setIf(map -> !map.containsKey("tools") && options.getMaxTokens() != null, "max_tokens", options.getMaxTokens())
069            .toJSON();
070    }
071
072
073}