ADK-Java-SDK 自定义模型服务
Created|Updated
|Word Count:4.1k|Post Views:
ADK-Java-SDK 自定义模型服务
本文由 AI 参与创作
最近在做 Agent 构建相关的工作,而在 Agent 构建这个领域,Google 的 ADK 算是业界标杆之一。最近在学习 ADK 过程中发现,ADK 的 Python SDK 提供了 LiteLLM 快速接入第三方供应商的模型参考: https://adk.wiki/agents/models/#using-open-local-models-via-litellm ,但是 Java SDK 并未提供这样快速接入的能力,于是记录下我在 ADK Java SDK 中 proxy 第三方供应商模型的例子,以供参考。
ADK Java SDK 模型扩展机制
ADK Java SDK 通过 BaseLlm 抽象类提供了模型扩展能力。要接入第三方模型服务商,只需继承该类并实现核心方法即可。
BaseLlm 核心接口
1 2 3 4 5 6 7 8 9 10
| public abstract class BaseLlm { protected final String model; public abstract Flowable<LlmResponse> generateContent(LlmRequest request, boolean stream); public abstract BaseLlmConnection connect(LlmRequest request); }
|
关键在于实现 generateContent 方法,它接收一个 LlmRequest 请求对象,返回一个 Flowable<LlmResponse> 响应流。其中:
LlmRequest 包含:消息历史(contents)、系统指令(systemInstruction)、工具定义(tools)等
LlmResponse 包含:模型输出(content)、是否完成(turnComplete)、是否为部分响应(partial)等
stream 参数决定是否使用流式输出
实现 DashScope 适配器
下面以阿里云 DashScope(通义千问系列模型)为例,展示如何实现一个完整的 LLM 适配器。
整体架构
适配器的核心工作是做好两个方向的转换:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| ┌─────────────────────────────────────────────────────────────────┐ │ DashScopeLlm │ ├─────────────────────────────────────────────────────────────────┤ │ │ │ ADK Request ──────┬──────────────────────> DashScope Request │ │ (LlmRequest) │ (GenerationParam) │ │ │ │ │ • Content[] │ convertContentsToMessages() │ │ • systemInstruction extractSystemInstruction() │ │ • tools[] │ buildToolList() │ │ ▼ │ │ │ │ ADK Response <────┴────────────────────── DashScope Response │ │ (LlmResponse) (GenerationResult) │ │ │ │ • Content │ convertToLlmResponse() │ │ • turnComplete │ emitStreamResponse() │ │ • partial │ │ │ │ └─────────────────────────────────────────────────────────────────┘
|
核心代码实现
1. 类定义与构造方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class DashScopeLlm extends BaseLlm { private static final String ENV_API_KEY = "DASHSCOPE_API_KEY"; private final String apiKey; private final Generation generation;
public DashScopeLlm(String model, String apiKey) { super(model); this.apiKey = (apiKey != null) ? apiKey : getEnvApiKey(); this.generation = new Generation(); }
public DashScopeLlm(String model) { this(model, null); } }
|
构造方法支持两种方式传入 API Key:显式传入或从环境变量 DASHSCOPE_API_KEY 读取。
2. 实现 generateContent 方法
这是适配器的核心入口,负责协调请求转换、API 调用和响应转换:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| @Override public Flowable<LlmResponse> generateContent(LlmRequest request, boolean stream) { try { List<Message> messages = buildMessageList(request); List<ToolBase> tools = buildToolList(request); GenerationParam param = buildGenerationParam(request, messages, tools); return stream ? executeStreamCall(param) : executeBlockingCall(param); } catch (Exception e) { return Flowable.error(e); } }
|
3. 消息格式转换
ADK 使用 Content + Part 的消息结构,而 DashScope 使用 Message 结构。转换时需要处理三种类型的 Part:
- 文本:直接拼接到
content 字段
- 函数调用(FunctionCall):转换为
toolCalls 列表
- 函数响应(FunctionResponse):转换为
role=tool 的消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| private MessageParseResult parseContentParts(List<Part> parts) { MessageParseResult result = new MessageParseResult(); for (Part part : parts) { if (part.functionCall().isPresent()) { parseFunctionCall(part.functionCall().get(), result.toolCalls); } else if (part.functionResponse().isPresent()) { parseFunctionResponse(part.functionResponse().get(), result.toolMessages); } else { part.text().ifPresent(result.textBuilder::append); } } return result; }
|
4. 工具定义转换
ADK 的 FunctionDeclaration 需要转换为 DashScope 的 ToolFunction:
1 2 3 4 5 6 7 8 9 10 11
| private ToolBase convertToToolFunction(FunctionDeclaration declaration) { FunctionDefinition.FunctionDefinitionBuilder builder = FunctionDefinition.builder() .name(declaration.name().orElseThrow()) .description(declaration.description().orElse(""));
declaration.parameters().ifPresent(params -> builder.parameters(convertSchemaToJson(params)));
return ToolFunction.builder().function(builder.build()).build(); }
|
5. 流式响应处理
流式调用时,需要追踪已发送的文本长度,实现增量输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| private void emitStreamResponse(FlowableEmitter<LlmResponse> emitter, GenerationResult result, StreamContext context) { var choice = result.getOutput().getChoices().get(0); var message = choice.getMessage(); boolean isComplete = isFinishReasonComplete(choice.getFinishReason());
List<Part> parts = extractIncrementalParts(message, context, isComplete); if (parts.isEmpty()) return;
LlmResponse response = buildStreamResponse(parts, isComplete); emitter.onNext(response); }
private static class StreamContext { int emittedTextLength = 0; }
|
6. 响应格式转换
将 DashScope 的响应转换回 ADK 格式,包括文本和工具调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| private LlmResponse convertToLlmResponse(GenerationResult result) { var choice = result.getOutput().getChoices().get(0); var message = choice.getMessage(); List<Part> parts = extractResponseParts(message);
LlmResponse.Builder builder = LlmResponse.builder(); if (!parts.isEmpty()) { builder.content(Content.builder() .role("model") .parts(ImmutableList.copyOf(parts)) .build()); } if ("stop".equalsIgnoreCase(choice.getFinishReason())) { builder.turnComplete(true); } return builder.build(); }
|
使用示例
完成适配器后,就可以在 ADK 中使用通义千问模型了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public class Main { public static void main(String[] args) { LlmAgent agent = LlmAgent.builder() .name("weatherAgent") .description("天气查询助手") .instruction("你是一个天气查询助手,可以帮用户查询天气信息。") .model(new DashScopeLlm("qwen-plus")) .tools(FunctionTool.create(Main.class, "queryWeather")) .build();
InMemoryRunner runner = new InMemoryRunner(agent); Session session = runner.sessionService() .createSession(runner.appName(), "user1234") .blockingGet();
RunConfig runConfig = RunConfig.builder() .setStreamingMode(RunConfig.StreamingMode.SSE) .build();
runner.runAsync(session.userId(), session.id(), Content.fromParts(Part.fromText("查询下杭州天气")), runConfig ).blockingForEach(System.out::println); }
@Annotations.Schema(name = "queryWeather", description = "查询天气") public static Map<String, String> queryWeather( @Annotations.Schema(name = "city", description = "城市") String city) { return Map.of("city", city, "weather", "晴,温度 30"); } }
|
适配其他模型服务商
基于同样的思路,你可以轻松适配其他模型服务商:
| 服务商 |
SDK |
核心转换点 |
| OpenAI |
openai-java |
ChatCompletionRequest ↔ LlmRequest |
| Azure OpenAI |
azure-ai-openai |
同 OpenAI,增加 deployment 配置 |
| 百度文心 |
wenxin-sdk |
ChatRequest ↔ LlmRequest |
| 智谱 AI |
zhipu-sdk |
ChatCompletionRequest ↔ LlmRequest |
关键是理解 ADK 的数据结构(Content、Part、FunctionCall、FunctionResponse),并将其与目标 SDK 的数据结构进行正确映射。
总结
ADK Java SDK 虽然没有像 Python SDK 那样提供 LiteLLM 的开箱即用支持,但其 BaseLlm 抽象类设计良好,扩展起来并不复杂。核心工作就是做好两个方向的格式转换:
- 请求转换:ADK 的
LlmRequest → 目标 SDK 的请求格式
- 响应转换:目标 SDK 的响应格式 → ADK 的
LlmResponse
完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
| package top.fengye;
import com.alibaba.dashscope.aigc.generation.Generation; import com.alibaba.dashscope.aigc.generation.GenerationParam; import com.alibaba.dashscope.aigc.generation.GenerationResult; import com.alibaba.dashscope.common.Message; import com.alibaba.dashscope.common.Role; import com.alibaba.dashscope.tools.FunctionDefinition; import com.alibaba.dashscope.tools.ToolBase; import com.alibaba.dashscope.tools.ToolCallBase; import com.alibaba.dashscope.tools.ToolCallFunction; import com.alibaba.dashscope.tools.ToolFunction; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; import com.google.adk.models.BaseLlm; import com.google.adk.models.BaseLlmConnection; import com.google.adk.models.LlmRequest; import com.google.adk.models.LlmResponse; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.genai.types.Content; import com.google.genai.types.FunctionCall; import com.google.genai.types.GenerateContentConfig; import com.google.genai.types.Part; import com.google.genai.types.Schema; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import io.reactivex.rxjava3.core.BackpressureStrategy; import io.reactivex.rxjava3.core.Flowable; import io.reactivex.rxjava3.core.FlowableEmitter;
import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.stream.Collectors;
public class DashScopeLlm extends BaseLlm {
private static final String ENV_API_KEY = "DASHSCOPE_API_KEY";
private static final ObjectMapper MAPPER = new ObjectMapper().registerModule(new Jdk8Module());
private static final String ROLE_MODEL = "model";
private static final String FINISH_REASON_STOP = "stop";
private static final String FINISH_REASON_TOOL_CALLS = "tool_calls";
private static final String TOOL_CALL_JSON_TEMPLATE = "{\"id\":\"%s\",\"type\":\"function\",\"function\":{\"name\":\"%s\",\"arguments\":\"%s\"}}";
private final String apiKey;
private final Generation generation;
public DashScopeLlm(String model, String apiKey) { super(model); this.apiKey = (apiKey != null) ? apiKey : getEnvApiKey(); this.generation = new Generation(); }
public DashScopeLlm(String model) { this(model, null); }
@Override public Flowable<LlmResponse> generateContent(LlmRequest request, boolean stream) { try { List<Message> messages = buildMessageList(request); List<ToolBase> tools = buildToolList(request); GenerationParam param = buildGenerationParam(request, messages, tools);
return stream ? executeStreamCall(param) : executeBlockingCall(param); } catch (Exception e) { return Flowable.error(e); } }
@Override public BaseLlmConnection connect(LlmRequest request) { throw new UnsupportedOperationException("实时连接功能暂不支持"); }
private GenerationParam buildGenerationParam(LlmRequest request, List<Message> messages, List<ToolBase> tools) { GenerationParam.GenerationParamBuilder<?, ?> builder = GenerationParam.builder() .model(request.model().orElse(model())) .messages(messages) .apiKey(apiKey) .resultFormat(GenerationParam.ResultFormat.MESSAGE);
if (!tools.isEmpty()) { builder.tools(tools); }
return builder.build(); }
private List<Message> buildMessageList(LlmRequest request) { List<Message> messages = convertContentsToMessages(request.contents()); extractSystemInstruction(request).ifPresent(instruction -> messages.add(0, Message.builder() .role(Role.SYSTEM.getValue()) .content(instruction) .build()) ); return messages; }
private Optional<String> extractSystemInstruction(LlmRequest request) { return request.config() .flatMap(GenerateContentConfig::systemInstruction) .flatMap(Content::parts) .map(parts -> parts.stream() .map(part -> part.text().orElse("")) .filter(text -> !text.isEmpty()) .collect(Collectors.joining("\n"))) .filter(text -> !text.isEmpty()); }
private List<ToolBase> buildToolList(LlmRequest request) { return request.config() .flatMap(GenerateContentConfig::tools) .orElse(ImmutableList.of()) .stream() .filter(tool -> tool.functionDeclarations().isPresent()) .flatMap(tool -> tool.functionDeclarations().get().stream()) .map(this::convertToToolFunction) .collect(Collectors.toList()); }
private ToolBase convertToToolFunction(com.google.genai.types.FunctionDeclaration declaration) { FunctionDefinition.FunctionDefinitionBuilder builder = FunctionDefinition.builder() .name(declaration.name().orElseThrow(() -> new IllegalArgumentException("函数名称不能为空"))) .description(declaration.description().orElse(""));
declaration.parameters().ifPresent(params -> builder.parameters(convertSchemaToJson(params)));
return ToolFunction.builder().function(builder.build()).build(); }
private Flowable<LlmResponse> executeBlockingCall(GenerationParam param) throws Exception { GenerationResult result = generation.call(param); return Flowable.just(convertToLlmResponse(result)); }
private Flowable<LlmResponse> executeStreamCall(GenerationParam param) { param.setIncrementalOutput(false); StreamContext context = new StreamContext();
return Flowable.create(emitter -> generation.streamCall(param).subscribe( result -> emitStreamResponse(emitter, result, context), error -> emitter.onError((Throwable) error), emitter::onComplete ), BackpressureStrategy.BUFFER ); }
private static class StreamContext { int emittedTextLength = 0; }
private void emitStreamResponse(FlowableEmitter<LlmResponse> emitter, GenerationResult result, StreamContext context) { var output = result.getOutput();
if (output == null || output.getChoices() == null || output.getChoices().isEmpty()) { if (output != null && output.getText() != null) { emitter.onNext(buildPartialTextResponse(output.getText())); } return; }
var choice = output.getChoices().get(0); var message = choice.getMessage(); boolean isComplete = isFinishReasonComplete(choice.getFinishReason());
List<Part> parts = extractIncrementalParts(message, context, isComplete); if (parts.isEmpty()) { return; }
LlmResponse response = buildStreamResponse(parts, isComplete); emitter.onNext(response); }
private boolean isFinishReasonComplete(String reason) { return FINISH_REASON_STOP.equalsIgnoreCase(reason) || FINISH_REASON_TOOL_CALLS.equalsIgnoreCase(reason); }
private List<Part> extractIncrementalParts(Message message, StreamContext context, boolean isComplete) { List<Part> parts = new ArrayList<>();
String content = message.getContent(); if (content != null && content.length() > context.emittedTextLength) { String incrementalText = content.substring(context.emittedTextLength); parts.add(Part.fromText(incrementalText)); context.emittedTextLength = content.length(); }
if (isComplete && message.getToolCalls() != null) { message.getToolCalls().stream() .map(this::convertToFunctionCallPart) .filter(Optional::isPresent) .map(Optional::get) .forEach(parts::add); }
return parts; }
private LlmResponse buildPartialTextResponse(String text) { return LlmResponse.builder() .content(Content.builder() .role(ROLE_MODEL) .parts(Part.fromText(text)) .build()) .partial(true) .build(); }
private LlmResponse buildStreamResponse(List<Part> parts, boolean isComplete) { LlmResponse.Builder builder = LlmResponse.builder() .content(Content.builder() .role(ROLE_MODEL) .parts(ImmutableList.copyOf(parts)) .build());
return isComplete ? builder.turnComplete(true).build() : builder.partial(true).build(); }
private List<Message> convertContentsToMessages(List<Content> contents) { List<Message> messages = new ArrayList<>();
for (Content content : contents) { String role = content.role().orElse("user"); List<Part> parts = content.parts().orElse(ImmutableList.of());
MessageParseResult parseResult = parseContentParts(parts); addParsedMessages(messages, role, parseResult); }
return messages; }
private static class MessageParseResult { final StringBuilder textBuilder = new StringBuilder(); final List<ToolCallBase> toolCalls = new ArrayList<>(); final List<Message> toolMessages = new ArrayList<>(); }
private MessageParseResult parseContentParts(List<Part> parts) { MessageParseResult result = new MessageParseResult();
for (Part part : parts) { if (part.functionCall().isPresent()) { parseFunctionCall(part.functionCall().get(), result.toolCalls); } else if (part.functionResponse().isPresent()) { parseFunctionResponse(part.functionResponse().get(), result.toolMessages); } else { part.text().ifPresent(result.textBuilder::append); } }
return result; }
private void parseFunctionCall(FunctionCall functionCall, List<ToolCallBase> toolCalls) { String id = functionCall.id().orElse("call_" + System.currentTimeMillis()); String name = functionCall.name().orElse(""); String args = escapeJsonString(toJson(functionCall.args().orElse(ImmutableMap.of())));
try { String json = String.format(TOOL_CALL_JSON_TEMPLATE, id, name, args); ToolCallFunction toolCall = com.alibaba.dashscope.utils.JsonUtils.fromJson(json, ToolCallFunction.class); toolCalls.add(toolCall); } catch (Exception ignored) { } }
private void parseFunctionResponse(com.google.genai.types.FunctionResponse response, List<Message> toolMessages) { Message message = Message.builder() .role("tool") .content(response.response().map(this::toJson).orElse("{}")) .name(response.name().orElse("")) .toolCallId(response.id().orElse("")) .build(); toolMessages.add(message); }
private void addParsedMessages(List<Message> messages, String role, MessageParseResult result) { String text = result.textBuilder.toString();
if (!result.toolCalls.isEmpty()) { Message.MessageBuilder builder = Message.builder() .role(Role.ASSISTANT.getValue()) .toolCalls(result.toolCalls); if (!text.isEmpty()) { builder.content(text); } messages.add(builder.build()); } else if (!text.isEmpty()) { messages.add(Message.builder() .role(mapRole(role)) .content(text) .build()); }
messages.addAll(result.toolMessages); }
private String mapRole(String adkRole) { return switch (adkRole) { case "model", "assistant" -> Role.ASSISTANT.getValue(); case "system" -> Role.SYSTEM.getValue(); default -> Role.USER.getValue(); }; }
private LlmResponse convertToLlmResponse(GenerationResult result) { var output = result.getOutput();
if (output == null || output.getChoices() == null || output.getChoices().isEmpty()) { if (output != null && output.getText() != null) { return buildPartialTextResponse(output.getText()); } return LlmResponse.builder().build(); }
var choice = output.getChoices().get(0); var message = choice.getMessage(); List<Part> parts = extractResponseParts(message);
LlmResponse.Builder builder = LlmResponse.builder(); if (!parts.isEmpty()) { builder.content(Content.builder() .role(ROLE_MODEL) .parts(ImmutableList.copyOf(parts)) .build()); } if (FINISH_REASON_STOP.equalsIgnoreCase(choice.getFinishReason())) { builder.turnComplete(true); }
return builder.build(); }
private List<Part> extractResponseParts(Message message) { List<Part> parts = new ArrayList<>();
String content = message.getContent(); if (content != null && !content.isEmpty()) { parts.add(Part.fromText(content)); }
if (message.getToolCalls() != null) { message.getToolCalls().stream() .map(this::convertToFunctionCallPart) .filter(Optional::isPresent) .map(Optional::get) .forEach(parts::add); }
return parts; }
private Optional<Part> convertToFunctionCallPart(ToolCallBase toolCall) { if (!(toolCall instanceof ToolCallFunction functionCall) || functionCall.getFunction() == null) { return Optional.empty(); }
String name = functionCall.getFunction().getName(); String arguments = functionCall.getFunction().getArguments();
if (name == null || name.isEmpty()) { return Optional.empty(); }
return parseArguments(arguments).map(argsMap -> Part.builder() .functionCall(FunctionCall.builder() .id(toolCall.getId()) .name(name) .args(argsMap) .build()) .build() ); }
private Optional<Map<String, Object>> parseArguments(String arguments) { if (arguments == null || arguments.isEmpty()) { return Optional.of(ImmutableMap.of()); }
String trimmed = arguments.trim(); if (!trimmed.startsWith("{") || !trimmed.endsWith("}")) { return Optional.empty(); }
try { Map<String, Object> argsMap = MAPPER.readValue(arguments, new TypeReference<>() {}); return Optional.of(argsMap); } catch (Exception e) { return Optional.empty(); } }
private JsonObject convertSchemaToJson(Schema schema) { JsonObject json = new JsonObject();
schema.type().ifPresent(type -> json.addProperty("type", type.toString())); schema.description().ifPresent(desc -> json.addProperty("description", desc));
schema.properties().ifPresent(properties -> { JsonObject propsJson = new JsonObject(); properties.forEach((key, value) -> propsJson.add(key, convertSchemaToJson(value))); json.add("properties", propsJson); });
schema.required() .filter(required -> !required.isEmpty()) .ifPresent(required -> { JsonArray requiredArray = new JsonArray(); required.forEach(requiredArray::add); json.add("required", requiredArray); });
return json; }
private static String getEnvApiKey() { String key = System.getenv(ENV_API_KEY); if (key == null || key.isEmpty()) { throw new IllegalStateException("环境变量 " + ENV_API_KEY + " 未设置"); } return key; }
private String toJson(Map<String, Object> map) { try { return MAPPER.writeValueAsString(map); } catch (Exception e) { return "{}"; } }
private String escapeJsonString(String json) { return json.replace("\\", "\\\\").replace("\"", "\\\""); } }
|