-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLlmClientFactory.java
More file actions
187 lines (164 loc) · 8.97 KB
/
Copy pathLlmClientFactory.java
File metadata and controls
187 lines (164 loc) · 8.97 KB
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
package dev.aceclaw.llm;
import dev.aceclaw.core.llm.LlmClient;
import dev.aceclaw.core.llm.ProviderCapabilities;
import dev.aceclaw.llm.anthropic.AnthropicClient;
import dev.aceclaw.llm.openai.CopilotRoutingClient;
import dev.aceclaw.llm.openai.CopilotTokenProvider;
import dev.aceclaw.llm.openai.OpenAiCodexTokenProvider;
import dev.aceclaw.llm.openai.OpenAICompatClient;
import dev.aceclaw.llm.openai.OpenAIRoutingClient;
import dev.aceclaw.llm.openai.OpenAIResponsesClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
/**
* Factory for creating {@link LlmClient} instances based on provider name.
*
* <p>Supports Anthropic Claude (native) and any OpenAI-compatible API
* (OpenAI, Groq, Together, Mistral, GitHub Copilot, Ollama, etc.).
*/
public final class LlmClientFactory {
private static final Logger log = LoggerFactory.getLogger(LlmClientFactory.class);
/** Default base URLs for known providers (without trailing slash). */
private static final Map<String, String> DEFAULT_BASE_URLS = Map.of(
"openai", "https://api.openai.com",
"openai-codex", "https://chatgpt.com/backend-api/codex",
"groq", "https://api.groq.com/openai",
"together", "https://api.together.xyz",
"mistral", "https://api.mistral.ai",
"copilot", "https://api.githubcopilot.com",
"ollama", "http://localhost:11434"
);
/** Default model identifiers for known providers. */
private static final Map<String, String> DEFAULT_MODELS = Map.of(
"openai", "gpt-4o",
"openai-codex", "gpt-5-codex",
"groq", "llama-3.3-70b-versatile",
"together", "meta-llama/Llama-3.3-70B-Instruct-Turbo",
"mistral", "mistral-large-latest",
"copilot", "gpt-5.2-codex",
"ollama", "qwen3:4b"
);
/** Providers that support image input. */
private static final Map<String, ProviderCapabilities> PROVIDER_CAPABILITIES = Map.of(
"openai", ProviderCapabilities.OPENAI,
"openai-codex", ProviderCapabilities.CODEX,
"groq", ProviderCapabilities.OPENAI_COMPAT,
"together", ProviderCapabilities.OPENAI_COMPAT,
"mistral", ProviderCapabilities.OPENAI_COMPAT,
"copilot", ProviderCapabilities.OPENAI,
"ollama", ProviderCapabilities.OPENAI_COMPAT
);
private LlmClientFactory() {}
/**
* Returns the hardcoded default model for a given provider.
* Falls back to {@code "claude-sonnet-4-5-20250929"} for Anthropic and unknown providers.
*/
public static String getDefaultModel(String provider) {
if (provider == null || "anthropic".equals(provider)) {
return "claude-sonnet-4-5-20250929";
}
return DEFAULT_MODELS.getOrDefault(provider, "gpt-4o");
}
/**
* Creates an LLM client for the given provider.
*
* @param provider provider name (e.g. "anthropic", "openai", "groq", "ollama")
* @param apiKey the API key or access token
* @param refreshToken OAuth refresh token (Anthropic only, may be null)
* @param baseUrl custom base URL override (null = use provider default)
* @param model model to use (null = use provider default from {@link #getDefaultModel(String)})
* @return a configured LlmClient instance
* @throws IllegalArgumentException if the provider is unknown
*/
public static LlmClient create(String provider, String apiKey,
String refreshToken, String baseUrl,
String model) {
if (provider == null || provider.isBlank()) {
provider = "anthropic";
}
log.info("Creating LLM client: provider={}, model={}, baseUrl={}", provider,
model != null ? model : "(default)",
baseUrl != null ? baseUrl : "(default)");
return switch (provider) {
case "anthropic" -> createAnthropicClient(apiKey, refreshToken, baseUrl);
case "copilot" -> createCopilotClient(apiKey, baseUrl, model);
case "openai" -> createOpenAiClient(apiKey, baseUrl, model);
case "openai-codex" -> createOpenAiCodexClient(apiKey, baseUrl, model);
case "groq", "together", "mistral", "ollama" -> {
String resolvedBaseUrl = baseUrl != null ? baseUrl : DEFAULT_BASE_URLS.get(provider);
String resolvedModel = model != null ? model : DEFAULT_MODELS.getOrDefault(provider, "gpt-4o");
ProviderCapabilities caps = PROVIDER_CAPABILITIES.getOrDefault(
provider, ProviderCapabilities.OPENAI_COMPAT);
yield new OpenAICompatClient(apiKey, resolvedBaseUrl, provider, resolvedModel, caps);
}
default -> throw new IllegalArgumentException(
"Unknown provider: " + provider
+ ". Supported: anthropic, openai, openai-codex, groq, together, mistral, copilot, ollama");
};
}
/** Copilot-specific headers required by the GitHub Copilot API. */
private static final Map<String, String> COPILOT_API_HEADERS = Map.of(
"copilot-integration-id", "vscode-chat",
"editor-version", "vscode/1.95.0",
"editor-plugin-version", "copilot-chat/0.26.7",
"User-Agent", "GitHubCopilotChat/0.26.7",
"openai-intent", "conversation-panel",
"x-github-api-version", "2025-04-01"
);
private static LlmClient createCopilotClient(String githubToken, String baseUrl, String model) {
var tokenProvider = new CopilotTokenProvider(githubToken);
String resolvedBaseUrl = baseUrl != null ? baseUrl : DEFAULT_BASE_URLS.get("copilot");
String resolvedModel = model != null ? model : DEFAULT_MODELS.getOrDefault("copilot", "claude-sonnet-4.5");
// Always create both clients so runtime model switching works.
// The routing client dispatches to the correct endpoint based on model name:
// - Codex models (e.g. gpt-5.2-codex) → Responses API (/responses)
// - All other models → Chat Completions API (/chat/completions)
var chatClient = new OpenAICompatClient(
tokenProvider, resolvedBaseUrl, "/chat/completions",
"copilot", resolvedModel, ProviderCapabilities.OPENAI,
COPILOT_API_HEADERS);
var responsesClient = new OpenAIResponsesClient(
tokenProvider, resolvedBaseUrl, "/responses",
"copilot", resolvedModel, ProviderCapabilities.CODEX,
COPILOT_API_HEADERS);
return new CopilotRoutingClient(chatClient, responsesClient, resolvedModel);
}
private static LlmClient createOpenAiClient(String apiKey, String baseUrl, String model) {
String resolvedBaseUrl = baseUrl != null ? baseUrl : DEFAULT_BASE_URLS.get("openai");
String resolvedModel = model != null ? model : DEFAULT_MODELS.getOrDefault("openai", "gpt-4o");
var chatClient = new OpenAICompatClient(
() -> apiKey, resolvedBaseUrl, "/v1/chat/completions",
"openai", resolvedModel, ProviderCapabilities.OPENAI, Map.of());
var responsesClient = new OpenAIResponsesClient(
() -> apiKey, resolvedBaseUrl, "/v1/responses",
"openai", resolvedModel, ProviderCapabilities.CODEX, Map.of());
return new OpenAIRoutingClient(chatClient, responsesClient, resolvedModel);
}
private static LlmClient createOpenAiCodexClient(String apiKey, String baseUrl, String model) {
var tokenProvider = new OpenAiCodexTokenProvider(apiKey);
String resolvedBaseUrl = baseUrl != null ? baseUrl : DEFAULT_BASE_URLS.get("openai-codex");
String resolvedModel = model != null ? model : DEFAULT_MODELS.getOrDefault("openai-codex", "gpt-5-codex");
var chatClient = new OpenAICompatClient(
tokenProvider, resolvedBaseUrl, "/v1/chat/completions",
"openai-codex", resolvedModel, ProviderCapabilities.OPENAI, Map.of());
var responsesClient = new OpenAIResponsesClient(
tokenProvider, resolvedBaseUrl, "/v1/responses",
"openai-codex", resolvedModel, ProviderCapabilities.CODEX, Map.of());
return new OpenAIRoutingClient(chatClient, responsesClient, resolvedModel, "openai-codex");
}
private static LlmClient createAnthropicClient(String apiKey, String refreshToken, String baseUrl) {
if (apiKey != null && apiKey.startsWith("sk-ant-oat") && refreshToken != null) {
if (baseUrl != null) {
return new AnthropicClient(apiKey, refreshToken, baseUrl,
java.time.Duration.ofSeconds(120));
}
return new AnthropicClient(apiKey, refreshToken);
}
if (baseUrl != null) {
return new AnthropicClient(apiKey, null, baseUrl,
java.time.Duration.ofSeconds(120));
}
return new AnthropicClient(apiKey);
}
}