|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.adk.models; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkArgument; |
| 20 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 21 | + |
| 22 | +import com.google.adk.models.chat.ChatCompletionsClient; |
| 23 | +import com.google.adk.models.chat.ChatCompletionsHttpClient; |
| 24 | +import com.google.common.annotations.VisibleForTesting; |
| 25 | +import com.google.common.base.Strings; |
| 26 | +import com.google.genai.types.HttpOptions; |
| 27 | +import io.reactivex.rxjava3.core.Flowable; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Optional; |
| 30 | + |
| 31 | +/** |
| 32 | + * A {@link BaseLlm} for any endpoint implementing the OpenAI Chat Completions API format (Groq, |
| 33 | + * Ollama, OpenRouter, Azure OpenAI, vLLM, and others). |
| 34 | + * |
| 35 | + * <p>HTTP transport and JSON mapping are delegated entirely to ADK's native {@link |
| 36 | + * ChatCompletionsHttpClient}. Instances are immutable: the underlying client (including its {@code |
| 37 | + * Authorization} header) is built once at construction. |
| 38 | + * |
| 39 | + * <p>Instances are constructed with the <em>bare</em> model identifier, which is what the backend |
| 40 | + * receives as the wire-format {@code "model"} field — any routing prefix such as {@code "groq/"} |
| 41 | + * must be stripped by the caller before construction (see {@link ModelProvider#create(String)}). |
| 42 | + * |
| 43 | + * <p>Example, together with the {@link ModelProvider} SPI: |
| 44 | + * |
| 45 | + * <pre>{@code |
| 46 | + * public final class GroqModelProvider implements ModelProvider { |
| 47 | + * @Override |
| 48 | + * public String prefix() { |
| 49 | + * return "groq"; |
| 50 | + * } |
| 51 | + * |
| 52 | + * @Override |
| 53 | + * public BaseLlm createFromBareModelName(String bareModelName) { |
| 54 | + * return new OpenAiCompatibleLlm( |
| 55 | + * bareModelName, |
| 56 | + * "https://api.groq.com/openai/v1", |
| 57 | + * Optional.ofNullable(System.getenv("GROQ_API_KEY"))); |
| 58 | + * } |
| 59 | + * } |
| 60 | + * }</pre> |
| 61 | + * |
| 62 | + * <p>Live bidirectional connections are not supported; the Chat Completions API does not provide |
| 63 | + * this capability. |
| 64 | + */ |
| 65 | +public class OpenAiCompatibleLlm extends BaseLlm { |
| 66 | + |
| 67 | + private static final String CHAT_COMPLETIONS_PATH = "/chat/completions"; |
| 68 | + |
| 69 | + private final ChatCompletionsClient client; |
| 70 | + |
| 71 | + /** |
| 72 | + * Creates a new OpenAI-compatible LLM. |
| 73 | + * |
| 74 | + * @param modelName the bare model name, sent to the backend as the {@code "model"} field |
| 75 | + * @param apiUrl the URL of the chat-completions endpoint; a trailing {@code /chat/completions} |
| 76 | + * segment is accepted and stripped, since the client appends it internally |
| 77 | + * @param apiKey optional API key; if empty, no {@code Authorization} header is sent |
| 78 | + */ |
| 79 | + public OpenAiCompatibleLlm(String modelName, String apiUrl, Optional<String> apiKey) { |
| 80 | + this(modelName, createClient(apiUrl, apiKey)); |
| 81 | + } |
| 82 | + |
| 83 | + @VisibleForTesting |
| 84 | + OpenAiCompatibleLlm(String modelName, ChatCompletionsClient client) { |
| 85 | + super(requireNotBlank(modelName, "modelName cannot be blank")); |
| 86 | + this.client = checkNotNull(client, "client"); |
| 87 | + } |
| 88 | + |
| 89 | + private static ChatCompletionsHttpClient createClient(String apiUrl, Optional<String> apiKey) { |
| 90 | + requireNotBlank(apiUrl, "apiUrl cannot be blank"); |
| 91 | + HttpOptions.Builder optionsBuilder = HttpOptions.builder().baseUrl(normalizeBaseUrl(apiUrl)); |
| 92 | + apiKey.ifPresent(key -> optionsBuilder.headers(Map.of("Authorization", "Bearer " + key))); |
| 93 | + return new ChatCompletionsHttpClient(optionsBuilder.build()); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Strips a trailing {@code /chat/completions} segment, which {@link ChatCompletionsHttpClient} |
| 98 | + * appends internally. |
| 99 | + */ |
| 100 | + @VisibleForTesting |
| 101 | + static String normalizeBaseUrl(String apiUrl) { |
| 102 | + if (apiUrl.endsWith(CHAT_COMPLETIONS_PATH)) { |
| 103 | + return apiUrl.substring(0, apiUrl.length() - CHAT_COMPLETIONS_PATH.length()); |
| 104 | + } |
| 105 | + return apiUrl; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public Flowable<LlmResponse> generateContent(LlmRequest llmRequest, boolean stream) { |
| 110 | + return client.complete(llmRequest, stream); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public BaseLlmConnection connect(LlmRequest llmRequest) { |
| 115 | + throw new UnsupportedOperationException( |
| 116 | + "OpenAiCompatibleLlm does not support live bidirectional connections."); |
| 117 | + } |
| 118 | + |
| 119 | + private static String requireNotBlank(String s, String errorMessage) { |
| 120 | + checkArgument(!Strings.nullToEmpty(s).isBlank(), errorMessage); |
| 121 | + return s; |
| 122 | + } |
| 123 | +} |
0 commit comments