Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
OpenAiConfig.java
Go to the documentation of this file.
1package dev.rafex.ether.ai.openai.config;
2
3/*-
4 * #%L
5 * ether-ai-openai
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 * Configuración para conectarse a la API de OpenAI.
36 *
37 * @param apiKey La clave de API para autenticar las solicitudes.
38 * @param baseUri La URI base de la API.
39 * @param timeout El tiempo de espera para las solicitudes HTTP.
40 * @param organization El identificador de la organización (opcional).
41 * @param project El identificador del proyecto (opcional).
42 * @param defaultHeaders Cabeceras HTTP por defecto para todas las solicitudes.
43 */
44public record OpenAiConfig(String apiKey, URI baseUri, Duration timeout, String organization, String project,
45 Map<String, String> defaultHeaders) {
46
47 private static final URI DEFAULT_BASE_URI = URI.create("https://api.openai.com/v1/");
48 private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(30);
49
50 /**
51 * Crea una nueva configuración y valida los parámetros.
52 *
53 * @param apiKey La clave de API para autenticar las solicitudes.
54 * @param baseUri La URI base de la API.
55 * @param timeout El tiempo de espera para las solicitudes HTTP.
56 * @param organization El identificador de la organización (opcional).
57 * @param project El identificador del proyecto (opcional).
58 * @param defaultHeaders Cabeceras HTTP por defecto para todas las solicitudes.
59 * @throws NullPointerException si la apiKey es nula.
60 * @throws IllegalArgumentException si la apiKey está en blanco.
61 */
62 public OpenAiConfig {
63 Objects.requireNonNull(apiKey, "apiKey");
64 if (apiKey.isBlank()) {
65 throw new IllegalArgumentException("apiKey must not be blank");
66 }
67 baseUri = normalizeBaseUri(baseUri == null ? DEFAULT_BASE_URI : baseUri);
68 timeout = timeout == null ? DEFAULT_TIMEOUT : timeout;
69 organization = organization == null ? "" : organization;
70 project = project == null ? "" : project;
71 defaultHeaders = defaultHeaders == null ? Map.of() : Map.copyOf(defaultHeaders);
72 }
73
74 /**
75 * Crea una configuración con valores por defecto.
76 *
77 * @param apiKey La clave de API.
78 * @return La configuración creada.
79 */
80 public static OpenAiConfig of(final String apiKey) {
81 return new OpenAiConfig(apiKey, DEFAULT_BASE_URI, DEFAULT_TIMEOUT, "", "", Map.of());
82 }
83
84 /**
85 * Devuelve la URI para generar respuestas de chat.
86 *
87 * @return La URI de completions de chat.
88 */
89 public URI chatCompletionsUri() {
90 return baseUri.resolve("chat/completions");
91 }
92
93 private static URI normalizeBaseUri(final URI baseUri) {
94 final String value = baseUri.toString();
95 return URI.create(value.endsWith("/") ? value : value + "/");
96 }
97}
record OpenAiConfig(String apiKey, URI baseUri, Duration timeout, String organization, String project, Map< String, String > defaultHeaders)
Configuración para conectarse a la API de OpenAI.