com.agentsflex.llm.openai.OpenAiLlm Maven / Gradle / Ivy
/*
* Copyright (c) 2023-2025, Agents-Flex ([email protected]).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.agentsflex.llm.openai;
import com.agentsflex.core.document.Document;
import com.agentsflex.core.llm.BaseLlm;
import com.agentsflex.core.llm.ChatOptions;
import com.agentsflex.core.llm.MessageResponse;
import com.agentsflex.core.llm.StreamResponseListener;
import com.agentsflex.core.llm.client.BaseLlmClientListener;
import com.agentsflex.core.llm.client.HttpClient;
import com.agentsflex.core.llm.client.LlmClient;
import com.agentsflex.core.llm.client.LlmClientListener;
import com.agentsflex.core.llm.client.impl.SseClient;
import com.agentsflex.core.llm.embedding.EmbeddingOptions;
import com.agentsflex.core.llm.response.AbstractBaseMessageResponse;
import com.agentsflex.core.llm.response.AiMessageResponse;
import com.agentsflex.core.llm.response.FunctionMessageResponse;
import com.agentsflex.core.parser.AiMessageParser;
import com.agentsflex.core.parser.FunctionMessageParser;
import com.agentsflex.core.prompt.FunctionPrompt;
import com.agentsflex.core.prompt.Prompt;
import com.agentsflex.core.store.VectorData;
import com.agentsflex.core.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
public class OpenAiLlm extends BaseLlm {
private final HttpClient httpClient = new HttpClient();
public AiMessageParser aiMessageParser = OpenAiLLmUtil.getAiMessageParser(false);
public AiMessageParser streamMessageParser = OpenAiLLmUtil.getAiMessageParser(true);
public FunctionMessageParser functionMessageParser = OpenAiLLmUtil.getFunctionMessageParser();
public static OpenAiLlm of(String apiKey) {
OpenAiLlmConfig config = new OpenAiLlmConfig();
config.setApiKey(apiKey);
return new OpenAiLlm(config);
}
public static OpenAiLlm of(String apiKey, String endpoint) {
OpenAiLlmConfig config = new OpenAiLlmConfig();
config.setApiKey(apiKey);
config.setEndpoint(endpoint);
return new OpenAiLlm(config);
}
public OpenAiLlm(OpenAiLlmConfig config) {
super(config);
}
@Override
public > R chat(Prompt prompt, ChatOptions options) {
Map headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer " + getConfig().getApiKey());
Consumer