From 3bfb391b6d1c22ac6333e43a36a89052e816c76d Mon Sep 17 00:00:00 2001 From: 0007 <0007@qq.com> Date: Wed, 27 Aug 2025 19:57:31 +0800 Subject: [PATCH] Add File --- .../llm/qianfan/QianFanLlmUtil.java | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 agents-flex-llm/agents-flex-llm-qianfan/src/main/java/com/agentsflex/llm/qianfan/QianFanLlmUtil.java diff --git a/agents-flex-llm/agents-flex-llm-qianfan/src/main/java/com/agentsflex/llm/qianfan/QianFanLlmUtil.java b/agents-flex-llm/agents-flex-llm-qianfan/src/main/java/com/agentsflex/llm/qianfan/QianFanLlmUtil.java new file mode 100644 index 0000000..a4dcb87 --- /dev/null +++ b/agents-flex-llm/agents-flex-llm-qianfan/src/main/java/com/agentsflex/llm/qianfan/QianFanLlmUtil.java @@ -0,0 +1,102 @@ +package com.agentsflex.llm.qianfan; + +import com.agentsflex.core.document.Document; +import com.agentsflex.core.llm.ChatOptions; +import com.agentsflex.core.llm.embedding.EmbeddingOptions; +import com.agentsflex.core.llm.functions.Function; +import com.agentsflex.core.llm.functions.Parameter; +import com.agentsflex.core.message.HumanMessage; +import com.agentsflex.core.message.Message; +import com.agentsflex.core.parser.AiMessageParser; +import com.agentsflex.core.parser.impl.DefaultAiMessageParser; +import com.agentsflex.core.prompt.DefaultPromptFormat; +import com.agentsflex.core.prompt.Prompt; +import com.agentsflex.core.prompt.PromptFormat; +import com.agentsflex.core.util.Maps; +import com.agentsflex.core.util.MessageUtil; + +import java.util.*; + +public class QianFanLlmUtil { + private static final PromptFormat promptFormat = new DefaultPromptFormat() { + @Override + protected void buildFunctionJsonArray(List> functionsJsonArray, List functions) { + for (Function function : functions) { + Map functionRoot = new HashMap<>(); + functionRoot.put("type", "function"); + + Map functionObj = new HashMap<>(); + functionRoot.put("function", functionObj); + + functionObj.put("name", function.getName()); + functionObj.put("description", function.getDescription()); + + + Map parametersObj = new HashMap<>(); + functionObj.put("parameters", parametersObj); + + parametersObj.put("type", "object"); + + Map propertiesObj = new HashMap<>(); + parametersObj.put("properties", propertiesObj); + + List requiredProperties = new ArrayList<>(); + + for (Parameter parameter : function.getParameters()) { + Map parameterObj = new HashMap<>(); + parameterObj.put("type", parameter.getType()); + parameterObj.put("description", parameter.getDescription()); + if (parameter.getEnums().length > 0) { + parameterObj.put("enum", parameter.getEnums()); + } + if (parameter.isRequired()) { + requiredProperties.add(parameter.getName()); + } + + propertiesObj.put(parameter.getName(), parameterObj); + } + + if (!requiredProperties.isEmpty()) { + parametersObj.put("required", requiredProperties); + } + + functionsJsonArray.add(functionRoot); + } + } + }; + + + public static AiMessageParser getAiMessageParser(boolean isStream) { + return DefaultAiMessageParser.getChatGPTMessageParser(isStream); + } + + + public static String promptToEmbeddingsPayload(Document text, EmbeddingOptions options, QianFanLlmConfig config) { + List documents = new ArrayList<>(); + documents.add(text.getContent()); + return Maps.of("model", options.getModelOrDefault(config.getEmbeddingModel())) + .set("encoding_format", "float") + .set("input", documents) + .toJSON(); + } + + + public static String promptToPayload(Prompt prompt, QianFanLlmConfig config, ChatOptions options, boolean withStream) { + List messages = prompt.toMessages(); + HumanMessage message = MessageUtil.findLastHumanMessage(messages); + return Maps.of("model", Optional.ofNullable(options.getModel()).orElse(config.getModel())) + .set("messages", promptFormat.toMessagesJsonObject(messages)) + .setIf(withStream, "stream", true) + .setIfNotEmpty("tools", promptFormat.toFunctionsJsonObject(message)) + .setIfContainsKey("tools", "tool_choice", MessageUtil.getToolChoice(message)) + .setIfNotNull("top_p", options.getTopP()) + .setIfNotEmpty("stop", options.getStop()) + .setIf(map -> !map.containsKey("tools") && options.getTemperature() > 0, "temperature", options.getTemperature()) + .setIf(map -> !map.containsKey("tools") && options.getMaxTokens() != null, "max_tokens", options.getMaxTokens()) + .setIfNotEmpty(options.getExtra()) + .toJSON(); + } +} + + +