Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
DeepSeekConfig.java
Go to the documentation of this file.
1package dev.rafex.ether.ai.deepseek.config;
2
3/*-
4 * #%L
5 * ether-ai-deepseek
6 * %%
7 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
8 * %%
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 * #L%
27 */
28
29import java.net.URI;
30import java.time.Duration;
31import java.util.Map;
32import java.util.Objects;
33
34/**
35 * Immutable configuration for connecting to the DeepSeek chat completions API.
36 *
37 * <p>Validates and normalises all fields at construction time: the API key must
38 * be non-blank, {@code baseUri} defaults to {@value #DEFAULT_BASE_URI} and
39 * {@code timeout} defaults to {@value #DEFAULT_TIMEOUT} seconds.
40 *
41 * @param apiKey non-blank DeepSeek API key used for Bearer authentication
42 * @param baseUri base URI of the API (trailing slash is normalised automatically)
43 * @param timeout connection and read timeout applied to every HTTP request
44 * @param defaultHeaders additional HTTP headers sent with every request (unmodifiable)
45 */
46public record DeepSeekConfig(String apiKey, URI baseUri, Duration timeout, Map<String, String> defaultHeaders) {
47
48 /** Default base URI pointing to the public DeepSeek API. */
49 private static final URI DEFAULT_BASE_URI = URI.create("https://api.deepseek.com/");
50
51 /** Default connection timeout in seconds. */
52 private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(30);
53
54 /**
55 * Compact constructor that validates the API key and applies defaults for
56 * nullable fields.
57 *
58 * @throws IllegalArgumentException if {@code apiKey} is blank
59 */
60 public DeepSeekConfig {
61 Objects.requireNonNull(apiKey, "apiKey");
62 if (apiKey.isBlank()) {
63 throw new IllegalArgumentException("apiKey must not be blank");
64 }
65 baseUri = normalizeBaseUri(baseUri == null ? DEFAULT_BASE_URI : baseUri);
66 timeout = timeout == null ? DEFAULT_TIMEOUT : timeout;
67 defaultHeaders = defaultHeaders == null ? Map.of() : Map.copyOf(defaultHeaders);
68 }
69
70 public static DeepSeekConfig of(final String apiKey) {
71 return new DeepSeekConfig(apiKey, DEFAULT_BASE_URI, DEFAULT_TIMEOUT, Map.of());
72 }
73
74 public URI chatCompletionsUri() {
75 return baseUri.resolve("chat/completions");
76 }
77
78 private static URI normalizeBaseUri(final URI baseUri) {
79 final String value = baseUri.toString();
80 return URI.create(value.endsWith("/") ? value : value + "/");
81 }
82}
record DeepSeekConfig(String apiKey, URI baseUri, Duration timeout, Map< String, String > defaultHeaders)
Immutable configuration for connecting to the DeepSeek chat completions API.