feat: Implement Ollama OAI Bridge with core functionality
- Add BridgeProperties class for configuration management. - Create GatewayProxyConfiguration to define routing and proxying logic. - Implement LlamaServerClient for communication with the upstream Llama server. - Develop OllamaDiscoveryController to handle API requests and responses. - Introduce OllamaDiscoveryService for business logic and data processing. - Set up OllamaOaiBridgeApplication as the main entry point for the application. - Add RequestBodyCachingFilter to cache request bodies for processing. - Create application.properties for configuration settings. - Implement unit tests for the application to ensure functionality. - Add start.sh script for easy application startup.
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package de.extio.ollama_oai_bridge;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@ConfigurationProperties(prefix = "bridge")
|
||||
public class BridgeProperties {
|
||||
|
||||
private URI upstreamBaseUrl = URI.create("http://localhost:5001");
|
||||
|
||||
private String version = "0.24.0";
|
||||
|
||||
public URI getUpstreamBaseUrl() {
|
||||
return this.upstreamBaseUrl;
|
||||
}
|
||||
|
||||
public void setUpstreamBaseUrl(final URI upstreamBaseUrl) {
|
||||
this.upstreamBaseUrl = upstreamBaseUrl;
|
||||
}
|
||||
|
||||
public String getVersion() {
|
||||
return this.version;
|
||||
}
|
||||
|
||||
public void setVersion(final String version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package de.extio.ollama_oai_bridge;
|
||||
|
||||
import static org.springframework.web.servlet.function.RequestPredicates.GET;
|
||||
import static org.springframework.web.servlet.function.RequestPredicates.POST;
|
||||
import static org.springframework.web.servlet.function.RequestPredicates.all;
|
||||
import static org.springframework.web.servlet.function.RequestPredicates.path;
|
||||
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions;
|
||||
import org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions;
|
||||
import org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.function.RequestPredicate;
|
||||
import org.springframework.web.servlet.function.RouterFunction;
|
||||
import org.springframework.web.servlet.function.ServerResponse;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(BridgeProperties.class)
|
||||
public class GatewayProxyConfiguration {
|
||||
|
||||
@Bean
|
||||
RouterFunction<ServerResponse> gatewayRoutes(final BridgeProperties bridgeProperties) {
|
||||
final RequestPredicate locallyHandledRoutes = GET("/api/version")
|
||||
.or(GET("/api/tags"))
|
||||
.or(POST("/api/show"))
|
||||
.or(path("/error"));
|
||||
|
||||
return GatewayRouterFunctions.route("llama-server-proxy")
|
||||
.before(BeforeFilterFunctions.uri(bridgeProperties.getUpstreamBaseUrl()))
|
||||
.route(all().and(locallyHandledRoutes.negate()), HandlerFunctions.http())
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
251
src/main/java/de/extio/ollama_oai_bridge/LlamaServerClient.java
Normal file
251
src/main/java/de/extio/ollama_oai_bridge/LlamaServerClient.java
Normal file
@@ -0,0 +1,251 @@
|
||||
package de.extio.ollama_oai_bridge;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import tools.jackson.databind.JsonNode;
|
||||
import tools.jackson.databind.node.MissingNode;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestClient;
|
||||
import org.springframework.web.client.RestClientException;
|
||||
import org.springframework.web.client.RestClientResponseException;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
@Service
|
||||
public class LlamaServerClient {
|
||||
|
||||
private final RestClient restClient;
|
||||
|
||||
public LlamaServerClient(RestClient.Builder restClientBuilder, BridgeProperties bridgeProperties) {
|
||||
this.restClient = restClientBuilder.baseUrl(bridgeProperties.getUpstreamBaseUrl().toString()).build();
|
||||
}
|
||||
|
||||
public LlamaServerSnapshot fetchSnapshot() {
|
||||
JsonNode props = getJson("/props");
|
||||
JsonNode slots = getJson("/slots");
|
||||
JsonNode modelsResponse = getJson("/v1/models");
|
||||
|
||||
return new LlamaServerSnapshot(props, slots, normalizeModels(modelsResponse));
|
||||
}
|
||||
|
||||
private JsonNode getJson(String path) {
|
||||
try {
|
||||
JsonNode response = this.restClient.get().uri(path).retrieve().body(JsonNode.class);
|
||||
if (response == null) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_GATEWAY,
|
||||
"llama-server returned an empty response for " + path);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
catch (RestClientResponseException exception) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_GATEWAY,
|
||||
"llama-server returned " + exception.getStatusCode().value() + " for " + path, exception);
|
||||
}
|
||||
catch (RestClientException exception) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_GATEWAY,
|
||||
"Failed to call llama-server endpoint " + path, exception);
|
||||
}
|
||||
}
|
||||
|
||||
private List<LlamaModelDescriptor> normalizeModels(JsonNode modelsResponse) {
|
||||
List<LlamaModelDescriptor> models = new ArrayList<>();
|
||||
|
||||
if (modelsResponse.path("models").isArray()) {
|
||||
for (JsonNode modelNode : modelsResponse.path("models")) {
|
||||
models.add(fromOllamaStyleModel(modelNode));
|
||||
}
|
||||
return List.copyOf(models);
|
||||
}
|
||||
|
||||
if (modelsResponse.path("data").isArray()) {
|
||||
for (JsonNode modelNode : modelsResponse.path("data")) {
|
||||
models.add(fromOpenAiStyleModel(modelNode));
|
||||
}
|
||||
}
|
||||
|
||||
return List.copyOf(models);
|
||||
}
|
||||
|
||||
private LlamaModelDescriptor fromOllamaStyleModel(JsonNode modelNode) {
|
||||
JsonNode detailsNode = modelNode.path("details");
|
||||
String name = text(modelNode, "name");
|
||||
return new LlamaModelDescriptor(
|
||||
name,
|
||||
defaultText(modelNode, "model", name),
|
||||
text(modelNode, "modified_at"),
|
||||
longValue(modelNode.path("size")),
|
||||
text(modelNode, "digest"),
|
||||
new LlamaModelDetails(
|
||||
text(detailsNode, "parent_model"),
|
||||
text(detailsNode, "format"),
|
||||
text(detailsNode, "family"),
|
||||
textList(detailsNode.path("families")),
|
||||
text(detailsNode, "parameter_size"),
|
||||
upperCase(text(detailsNode, "quantization_level"))),
|
||||
textList(modelNode.path("capabilities")),
|
||||
modelNode.path("meta"),
|
||||
text(modelNode, "owned_by"));
|
||||
}
|
||||
|
||||
private LlamaModelDescriptor fromOpenAiStyleModel(JsonNode modelNode) {
|
||||
JsonNode metaNode = modelNode.path("meta");
|
||||
String name = text(modelNode, "id");
|
||||
String family = inferFamily(name);
|
||||
return new LlamaModelDescriptor(
|
||||
name,
|
||||
name,
|
||||
"",
|
||||
longValue(metaNode.path("size")),
|
||||
"",
|
||||
new LlamaModelDetails(
|
||||
"",
|
||||
inferFormat(name),
|
||||
family,
|
||||
family.isBlank() ? List.of() : List.of(family),
|
||||
humanizeParameterCount(longValue(metaNode.path("n_params"))),
|
||||
inferQuantization(name)),
|
||||
List.of("completion"),
|
||||
metaNode,
|
||||
text(modelNode, "owned_by"));
|
||||
}
|
||||
|
||||
private String text(JsonNode node, String fieldName) {
|
||||
JsonNode field = node.path(fieldName);
|
||||
return field.isMissingNode() || field.isNull() ? "" : field.asText("");
|
||||
}
|
||||
|
||||
private String defaultText(JsonNode node, String fieldName, String defaultValue) {
|
||||
String value = text(node, fieldName);
|
||||
return value.isBlank() ? defaultValue : value;
|
||||
}
|
||||
|
||||
private long longValue(JsonNode node) {
|
||||
if (node == null || node.isMissingNode() || node.isNull()) {
|
||||
return 0L;
|
||||
}
|
||||
|
||||
if (node.isIntegralNumber()) {
|
||||
return node.longValue();
|
||||
}
|
||||
|
||||
if (node.isTextual()) {
|
||||
String value = node.asText().trim();
|
||||
if (value.isEmpty()) {
|
||||
return 0L;
|
||||
}
|
||||
try {
|
||||
return Long.parseLong(value);
|
||||
}
|
||||
catch (NumberFormatException ignored) {
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
return node.asLong(0L);
|
||||
}
|
||||
|
||||
private List<String> textList(JsonNode arrayNode) {
|
||||
if (!arrayNode.isArray()) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
List<String> values = new ArrayList<>();
|
||||
for (JsonNode node : arrayNode) {
|
||||
String value = node.asText("").trim();
|
||||
if (!value.isEmpty()) {
|
||||
values.add(value);
|
||||
}
|
||||
}
|
||||
return List.copyOf(values);
|
||||
}
|
||||
|
||||
private String humanizeParameterCount(long parameterCount) {
|
||||
if (parameterCount <= 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
double billions = parameterCount / 1_000_000_000d;
|
||||
if (billions >= 1d) {
|
||||
return String.format(Locale.ROOT, "%.1fB", billions);
|
||||
}
|
||||
|
||||
double millions = parameterCount / 1_000_000d;
|
||||
return String.format(Locale.ROOT, "%.1fM", millions);
|
||||
}
|
||||
|
||||
private String inferFormat(String modelName) {
|
||||
String lowerCaseName = modelName.toLowerCase(Locale.ROOT);
|
||||
return lowerCaseName.endsWith(".gguf") ? "gguf" : "";
|
||||
}
|
||||
|
||||
private String inferQuantization(String modelName) {
|
||||
String upperCaseName = modelName.toUpperCase(Locale.ROOT);
|
||||
int quantizationIndex = upperCaseName.indexOf("Q");
|
||||
if (quantizationIndex < 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
StringBuilder quantization = new StringBuilder();
|
||||
for (int index = quantizationIndex; index < upperCaseName.length(); index++) {
|
||||
char currentCharacter = upperCaseName.charAt(index);
|
||||
if (Character.isLetterOrDigit(currentCharacter) || currentCharacter == '_') {
|
||||
quantization.append(currentCharacter);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return quantization.toString();
|
||||
}
|
||||
|
||||
private String inferFamily(String modelName) {
|
||||
String normalizedName = modelName.toLowerCase(Locale.ROOT)
|
||||
.replace(":latest", "")
|
||||
.replace(".gguf", "");
|
||||
|
||||
if (normalizedName.startsWith("gemma-")) {
|
||||
return normalizedName.replaceFirst("^gemma-(\\d+).*$", "gemma$1");
|
||||
}
|
||||
|
||||
if (normalizedName.startsWith("llama")) {
|
||||
return "llama";
|
||||
}
|
||||
|
||||
if (normalizedName.startsWith("mistral")) {
|
||||
return "mistral";
|
||||
}
|
||||
|
||||
if (normalizedName.startsWith("qwen")) {
|
||||
return normalizedName.replaceFirst("^([a-z]+\\d*).*$", "$1");
|
||||
}
|
||||
|
||||
return normalizedName.replaceFirst("^([a-z]+[0-9]*).*$", "$1");
|
||||
}
|
||||
|
||||
private String upperCase(String value) {
|
||||
return value.isBlank() ? value : value.toUpperCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
record LlamaServerSnapshot(JsonNode props, JsonNode slots, List<LlamaModelDescriptor> models) {
|
||||
}
|
||||
|
||||
record LlamaModelDescriptor(String name, String model, String modifiedAt, long size, String digest,
|
||||
LlamaModelDetails details, List<String> capabilities, JsonNode meta, String ownedBy) {
|
||||
|
||||
LlamaModelDescriptor {
|
||||
meta = meta == null ? MissingNode.getInstance() : meta.deepCopy();
|
||||
}
|
||||
}
|
||||
|
||||
record LlamaModelDetails(String parentModel, String format, String family, List<String> families,
|
||||
String parameterSize, String quantizationLevel) {
|
||||
|
||||
LlamaModelDetails {
|
||||
families = families == null ? List.of() : List.copyOf(families);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package de.extio.ollama_oai_bridge;
|
||||
|
||||
import tools.jackson.databind.node.ObjectNode;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class OllamaDiscoveryController {
|
||||
|
||||
private final BridgeProperties bridgeProperties;
|
||||
|
||||
private final OllamaDiscoveryService ollamaDiscoveryService;
|
||||
|
||||
public OllamaDiscoveryController(BridgeProperties bridgeProperties, OllamaDiscoveryService ollamaDiscoveryService) {
|
||||
this.bridgeProperties = bridgeProperties;
|
||||
this.ollamaDiscoveryService = ollamaDiscoveryService;
|
||||
}
|
||||
|
||||
@GetMapping(path = "/api/version", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
ObjectNode version() {
|
||||
return this.ollamaDiscoveryService.version(this.bridgeProperties.getVersion());
|
||||
}
|
||||
|
||||
@GetMapping(path = "/api/tags", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
ObjectNode tags() {
|
||||
return this.ollamaDiscoveryService.tags();
|
||||
}
|
||||
|
||||
@PostMapping(path = "/api/show", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
ObjectNode show(@RequestBody ShowModelRequest showModelRequest) {
|
||||
return this.ollamaDiscoveryService.show(showModelRequest.model());
|
||||
}
|
||||
|
||||
record ShowModelRequest(String model, Boolean verbose) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,393 @@
|
||||
package de.extio.ollama_oai_bridge;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import tools.jackson.databind.JsonNode;
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
import tools.jackson.databind.node.ArrayNode;
|
||||
import tools.jackson.databind.node.ObjectNode;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import de.extio.ollama_oai_bridge.LlamaServerClient.LlamaModelDescriptor;
|
||||
import de.extio.ollama_oai_bridge.LlamaServerClient.LlamaModelDetails;
|
||||
import de.extio.ollama_oai_bridge.LlamaServerClient.LlamaServerSnapshot;
|
||||
|
||||
@Service
|
||||
public class OllamaDiscoveryService {
|
||||
|
||||
private final LlamaServerClient llamaServerClient;
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public OllamaDiscoveryService(LlamaServerClient llamaServerClient, ObjectMapper objectMapper) {
|
||||
this.llamaServerClient = llamaServerClient;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public ObjectNode version(String version) {
|
||||
ObjectNode response = this.objectMapper.createObjectNode();
|
||||
response.put("version", version);
|
||||
return response;
|
||||
}
|
||||
|
||||
public ObjectNode tags() {
|
||||
LlamaServerSnapshot snapshot = this.llamaServerClient.fetchSnapshot();
|
||||
ObjectNode response = this.objectMapper.createObjectNode();
|
||||
ArrayNode models = response.putArray("models");
|
||||
|
||||
for (LlamaModelDescriptor model : snapshot.models()) {
|
||||
models.add(buildTagModel(model, snapshot.props()));
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
public ObjectNode show(String modelName) {
|
||||
if (modelName == null || modelName.isBlank()) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "model is required");
|
||||
}
|
||||
|
||||
LlamaServerSnapshot snapshot = this.llamaServerClient.fetchSnapshot();
|
||||
LlamaModelDescriptor model = snapshot.models().stream()
|
||||
.filter(candidate -> matchesModel(modelName, candidate))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,
|
||||
"Model not found: " + modelName));
|
||||
|
||||
ObjectNode response = this.objectMapper.createObjectNode();
|
||||
response.put("license", "");
|
||||
response.put("modelfile", buildModelfile(model, snapshot.props(), snapshot.slots()));
|
||||
response.put("parameters", buildParameters(snapshot.props()));
|
||||
response.put("template", text(snapshot.props(), "chat_template"));
|
||||
response.put("system", "");
|
||||
response.set("details", detailsNode(resolveDetails(model, snapshot.props())));
|
||||
response.set("model_info", buildModelInfo(model, snapshot.props(), snapshot.slots()));
|
||||
response.set("capabilities", capabilitiesNode(resolveCapabilities(model, snapshot.props())));
|
||||
response.put("modified_at", defaultString(model.modifiedAt()));
|
||||
return response;
|
||||
}
|
||||
|
||||
private boolean matchesModel(String requestedModel, LlamaModelDescriptor candidate) {
|
||||
return requestedModel.equals(candidate.name()) || requestedModel.equals(candidate.model());
|
||||
}
|
||||
|
||||
private ObjectNode buildTagModel(LlamaModelDescriptor model, JsonNode props) {
|
||||
LlamaModelDetails details = resolveDetails(model, props);
|
||||
ObjectNode response = this.objectMapper.createObjectNode();
|
||||
response.put("name", model.name());
|
||||
response.put("model", model.model());
|
||||
response.put("modified_at", defaultString(model.modifiedAt()));
|
||||
response.put("size", model.size());
|
||||
response.put("digest", defaultString(model.digest()));
|
||||
response.set("details", detailsNode(details));
|
||||
return response;
|
||||
}
|
||||
|
||||
private ObjectNode detailsNode(LlamaModelDetails details) {
|
||||
ObjectNode detailsNode = this.objectMapper.createObjectNode();
|
||||
detailsNode.put("parent_model", defaultString(details.parentModel()));
|
||||
detailsNode.put("format", defaultString(details.format()));
|
||||
detailsNode.put("family", defaultString(details.family()));
|
||||
|
||||
ArrayNode familiesNode = detailsNode.putArray("families");
|
||||
for (String family : details.families()) {
|
||||
familiesNode.add(family);
|
||||
}
|
||||
|
||||
detailsNode.put("parameter_size", defaultString(details.parameterSize()));
|
||||
detailsNode.put("quantization_level", defaultString(details.quantizationLevel()));
|
||||
return detailsNode;
|
||||
}
|
||||
|
||||
private ArrayNode capabilitiesNode(List<String> capabilities) {
|
||||
ArrayNode capabilitiesNode = this.objectMapper.createArrayNode();
|
||||
for (String capability : capabilities) {
|
||||
capabilitiesNode.add(capability);
|
||||
}
|
||||
return capabilitiesNode;
|
||||
}
|
||||
|
||||
private LlamaModelDetails resolveDetails(LlamaModelDescriptor model, JsonNode props) {
|
||||
String family = firstNonBlank(model.details().family(), inferFamily(text(props, "model_alias")), inferFamily(model.name()));
|
||||
List<String> families = model.details().families().isEmpty() && !family.isBlank()
|
||||
? List.of(family)
|
||||
: model.details().families();
|
||||
|
||||
return new LlamaModelDetails(
|
||||
model.details().parentModel(),
|
||||
firstNonBlank(model.details().format(), inferFormat(text(props, "model_path")), inferFormat(model.name())),
|
||||
family,
|
||||
families,
|
||||
firstNonBlank(model.details().parameterSize(), humanizeParameterCount(longValue(model.meta(), "n_params"))),
|
||||
firstNonBlank(model.details().quantizationLevel(), inferQuantization(text(props, "model_alias")), inferQuantization(model.name())));
|
||||
}
|
||||
|
||||
private List<String> resolveCapabilities(LlamaModelDescriptor model, JsonNode props) {
|
||||
LinkedHashSet<String> capabilities = new LinkedHashSet<>(model.capabilities());
|
||||
if (capabilities.isEmpty()) {
|
||||
capabilities.add("completion");
|
||||
}
|
||||
|
||||
if (props.path("modalities").path("vision").asBoolean(false)) {
|
||||
capabilities.add("vision");
|
||||
}
|
||||
|
||||
String chatTemplate = text(props, "chat_template").toLowerCase(Locale.ROOT);
|
||||
if (chatTemplate.contains("tool") || chatTemplate.contains("function")) {
|
||||
capabilities.add("tools");
|
||||
}
|
||||
if (chatTemplate.contains("reasoning") || chatTemplate.contains("thinking")) {
|
||||
capabilities.add("thinking");
|
||||
}
|
||||
|
||||
return List.copyOf(capabilities);
|
||||
}
|
||||
|
||||
private String buildModelfile(LlamaModelDescriptor model, JsonNode props, JsonNode slots) {
|
||||
StringBuilder modelfile = new StringBuilder("# Modelfile generated by ollama-oai-bridge\n");
|
||||
modelfile.append("FROM ")
|
||||
.append(firstNonBlank(text(props, "model_path"), model.model(), model.name()))
|
||||
.append('\n');
|
||||
|
||||
String template = text(props, "chat_template");
|
||||
if (!template.isBlank()) {
|
||||
modelfile.append("TEMPLATE \"\"\"")
|
||||
.append(template)
|
||||
.append("\"\"\"\n");
|
||||
}
|
||||
|
||||
appendNumericModelfileParameter(modelfile, "num_ctx", contextLength(model, props, slots));
|
||||
appendTextModelfileParameter(modelfile, "temperature", text(props.path("default_generation_settings").path("params"), "temperature"));
|
||||
appendTextModelfileParameter(modelfile, "top_k", text(props.path("default_generation_settings").path("params"), "top_k"));
|
||||
appendTextModelfileParameter(modelfile, "top_p", text(props.path("default_generation_settings").path("params"), "top_p"));
|
||||
appendTextModelfileParameter(modelfile, "min_p", text(props.path("default_generation_settings").path("params"), "min_p"));
|
||||
appendTextModelfileParameter(modelfile, "repeat_last_n", text(props.path("default_generation_settings").path("params"), "repeat_last_n"));
|
||||
appendTextModelfileParameter(modelfile, "repeat_penalty", text(props.path("default_generation_settings").path("params"), "repeat_penalty"));
|
||||
appendTextModelfileParameter(modelfile, "presence_penalty", text(props.path("default_generation_settings").path("params"), "presence_penalty"));
|
||||
appendTextModelfileParameter(modelfile, "frequency_penalty", text(props.path("default_generation_settings").path("params"), "frequency_penalty"));
|
||||
appendQuotedModelfileParameter(modelfile, "stop", text(props, "eos_token"));
|
||||
return modelfile.toString().trim();
|
||||
}
|
||||
|
||||
private String buildParameters(JsonNode props) {
|
||||
StringBuilder parameters = new StringBuilder();
|
||||
appendParameterLine(parameters, "num_ctx", text(props.path("default_generation_settings"), "n_ctx"));
|
||||
appendParameterLine(parameters, "temperature", text(props.path("default_generation_settings").path("params"), "temperature"));
|
||||
appendParameterLine(parameters, "top_k", text(props.path("default_generation_settings").path("params"), "top_k"));
|
||||
appendParameterLine(parameters, "top_p", text(props.path("default_generation_settings").path("params"), "top_p"));
|
||||
appendParameterLine(parameters, "min_p", text(props.path("default_generation_settings").path("params"), "min_p"));
|
||||
appendParameterLine(parameters, "repeat_last_n", text(props.path("default_generation_settings").path("params"), "repeat_last_n"));
|
||||
appendParameterLine(parameters, "repeat_penalty", text(props.path("default_generation_settings").path("params"), "repeat_penalty"));
|
||||
appendParameterLine(parameters, "presence_penalty", text(props.path("default_generation_settings").path("params"), "presence_penalty"));
|
||||
appendParameterLine(parameters, "frequency_penalty", text(props.path("default_generation_settings").path("params"), "frequency_penalty"));
|
||||
appendParameterLine(parameters, "stop", quoted(text(props, "eos_token")));
|
||||
return parameters.toString().trim();
|
||||
}
|
||||
|
||||
private ObjectNode buildModelInfo(LlamaModelDescriptor model, JsonNode props, JsonNode slots) {
|
||||
LlamaModelDetails details = resolveDetails(model, props);
|
||||
ObjectNode modelInfo = this.objectMapper.createObjectNode();
|
||||
|
||||
String architecture = firstNonBlank(details.family(), "unknown");
|
||||
modelInfo.put("general.architecture", architecture);
|
||||
modelInfo.put("general.basename", stripExtension(stripTag(model.name())));
|
||||
putLongIfPositive(modelInfo, "general.parameter_count", longValue(model.meta(), "n_params"));
|
||||
putStringIfPresent(modelInfo, "general.size_label", details.parameterSize());
|
||||
putStringIfPresent(modelInfo, "general.quantization", details.quantizationLevel());
|
||||
putStringIfPresent(modelInfo, architecture + ".context_length", String.valueOf(contextLength(model, props, slots)));
|
||||
putLongIfPositive(modelInfo, architecture + ".embedding_length", longValue(model.meta(), "n_embd"));
|
||||
putLongIfPositive(modelInfo, architecture + ".vocab_size", longValue(model.meta(), "n_vocab"));
|
||||
putLongIfPositive(modelInfo, architecture + ".trained_context_length", longValue(model.meta(), "n_ctx_train"));
|
||||
putStringIfPresent(modelInfo, "tokenizer.ggml.bos_token", text(props, "bos_token"));
|
||||
putStringIfPresent(modelInfo, "tokenizer.ggml.eos_token", text(props, "eos_token"));
|
||||
putStringIfPresent(modelInfo, "bridge.build_info", text(props, "build_info"));
|
||||
modelInfo.put("bridge.total_slots", slots.isArray() ? slots.size() : 0);
|
||||
modelInfo.put("bridge.busy_slots", busySlots(slots));
|
||||
putStringIfPresent(modelInfo, "bridge.owned_by", model.ownedBy());
|
||||
return modelInfo;
|
||||
}
|
||||
|
||||
private int busySlots(JsonNode slots) {
|
||||
if (!slots.isArray()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int busySlots = 0;
|
||||
for (JsonNode slot : slots) {
|
||||
if (slot.path("is_processing").asBoolean(false)) {
|
||||
busySlots++;
|
||||
}
|
||||
}
|
||||
return busySlots;
|
||||
}
|
||||
|
||||
private long contextLength(LlamaModelDescriptor model, JsonNode props, JsonNode slots) {
|
||||
long metaContext = longValue(model.meta(), "n_ctx");
|
||||
if (metaContext > 0) {
|
||||
return metaContext;
|
||||
}
|
||||
|
||||
long configuredContext = longValue(props.path("default_generation_settings"), "n_ctx");
|
||||
if (configuredContext > 0) {
|
||||
return configuredContext;
|
||||
}
|
||||
|
||||
if (slots.isArray() && !slots.isEmpty()) {
|
||||
return longValue(slots.get(0), "n_ctx");
|
||||
}
|
||||
|
||||
return 0L;
|
||||
}
|
||||
|
||||
private void appendNumericModelfileParameter(StringBuilder modelfile, String key, long value) {
|
||||
if (value > 0) {
|
||||
modelfile.append("PARAMETER ").append(key).append(' ').append(value).append('\n');
|
||||
}
|
||||
}
|
||||
|
||||
private void appendTextModelfileParameter(StringBuilder modelfile, String key, String value) {
|
||||
if (!value.isBlank()) {
|
||||
modelfile.append("PARAMETER ").append(key).append(' ').append(value).append('\n');
|
||||
}
|
||||
}
|
||||
|
||||
private void appendQuotedModelfileParameter(StringBuilder modelfile, String key, String value) {
|
||||
if (!value.isBlank()) {
|
||||
modelfile.append("PARAMETER ").append(key).append(' ').append(quoted(value)).append('\n');
|
||||
}
|
||||
}
|
||||
|
||||
private void appendParameterLine(StringBuilder parameters, String key, String value) {
|
||||
if (!value.isBlank()) {
|
||||
parameters.append(key).append(' ').append(value).append('\n');
|
||||
}
|
||||
}
|
||||
|
||||
private void putLongIfPositive(ObjectNode node, String key, long value) {
|
||||
if (value > 0) {
|
||||
node.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
private void putStringIfPresent(ObjectNode node, String key, String value) {
|
||||
if (!value.isBlank()) {
|
||||
node.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
private String text(JsonNode node, String fieldName) {
|
||||
JsonNode field = node.path(fieldName);
|
||||
return field.isMissingNode() || field.isNull() ? "" : field.asText("");
|
||||
}
|
||||
|
||||
private long longValue(JsonNode node, String fieldName) {
|
||||
JsonNode field = node.path(fieldName);
|
||||
if (field.isMissingNode() || field.isNull()) {
|
||||
return 0L;
|
||||
}
|
||||
if (field.isIntegralNumber()) {
|
||||
return field.longValue();
|
||||
}
|
||||
if (field.isTextual()) {
|
||||
try {
|
||||
return Long.parseLong(field.asText().trim());
|
||||
}
|
||||
catch (NumberFormatException ignored) {
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
return field.asLong(0L);
|
||||
}
|
||||
|
||||
private String humanizeParameterCount(long parameterCount) {
|
||||
if (parameterCount <= 0) {
|
||||
return "";
|
||||
}
|
||||
double billions = parameterCount / 1_000_000_000d;
|
||||
if (billions >= 1d) {
|
||||
return String.format(Locale.ROOT, "%.1fB", billions);
|
||||
}
|
||||
double millions = parameterCount / 1_000_000d;
|
||||
return String.format(Locale.ROOT, "%.1fM", millions);
|
||||
}
|
||||
|
||||
private String inferFormat(String value) {
|
||||
String lowerCaseValue = value.toLowerCase(Locale.ROOT);
|
||||
return lowerCaseValue.endsWith(".gguf") ? "gguf" : "";
|
||||
}
|
||||
|
||||
private String inferQuantization(String value) {
|
||||
String upperCaseValue = value.toUpperCase(Locale.ROOT);
|
||||
int quantizationStart = upperCaseValue.indexOf('Q');
|
||||
if (quantizationStart < 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
StringBuilder quantization = new StringBuilder();
|
||||
for (int index = quantizationStart; index < upperCaseValue.length(); index++) {
|
||||
char character = upperCaseValue.charAt(index);
|
||||
if (Character.isLetterOrDigit(character) || character == '_') {
|
||||
quantization.append(character);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return quantization.toString();
|
||||
}
|
||||
|
||||
private String inferFamily(String value) {
|
||||
String normalizedValue = stripExtension(stripTag(value.toLowerCase(Locale.ROOT)));
|
||||
if (normalizedValue.startsWith("gemma-")) {
|
||||
return normalizedValue.replaceFirst("^gemma-(\\d+).*$", "gemma$1");
|
||||
}
|
||||
if (normalizedValue.startsWith("llama")) {
|
||||
return "llama";
|
||||
}
|
||||
if (normalizedValue.startsWith("mistral")) {
|
||||
return "mistral";
|
||||
}
|
||||
if (normalizedValue.startsWith("qwen")) {
|
||||
return normalizedValue.replaceFirst("^([a-z]+\\d*).*$", "$1");
|
||||
}
|
||||
return normalizedValue.replaceFirst("^([a-z]+[0-9]*).*$", "$1");
|
||||
}
|
||||
|
||||
private String stripTag(String value) {
|
||||
int tagSeparator = value.indexOf(':');
|
||||
return tagSeparator >= 0 ? value.substring(0, tagSeparator) : value;
|
||||
}
|
||||
|
||||
private String stripExtension(String value) {
|
||||
return value.endsWith(".gguf") ? value.substring(0, value.length() - 5) : value;
|
||||
}
|
||||
|
||||
private String quoted(String value) {
|
||||
return '"' + value.replace("\\", "\\\\").replace("\"", "\\\"") + '"';
|
||||
}
|
||||
|
||||
private String firstNonBlank(String... values) {
|
||||
if (values == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
for (String value : values) {
|
||||
if (value != null && !value.trim().isEmpty()) {
|
||||
return value.trim();
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
private String defaultString(String value) {
|
||||
return value == null ? "" : value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package de.extio.ollama_oai_bridge;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class OllamaOaiBridgeApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(OllamaOaiBridgeApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package de.extio.ollama_oai_bridge;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
import org.springframework.web.util.ContentCachingRequestWrapper;
|
||||
|
||||
@Component
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class RequestBodyCachingFilter extends OncePerRequestFilter {
|
||||
|
||||
private static final int CACHE_LIMIT = 1024 * 1024;
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
||||
throws ServletException, IOException {
|
||||
filterChain.doFilter(new ContentCachingRequestWrapper(request, CACHE_LIMIT), response);
|
||||
}
|
||||
|
||||
}
|
||||
5
src/main/resources/application.properties
Normal file
5
src/main/resources/application.properties
Normal file
@@ -0,0 +1,5 @@
|
||||
spring.application.name=ollama-oai-bridge
|
||||
server.port=11434
|
||||
|
||||
bridge.upstream-base-url=http://localhost:5001
|
||||
bridge.version=0.24.0
|
||||
@@ -0,0 +1,226 @@
|
||||
package de.extio.ollama_oai_bridge;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
|
||||
import tools.jackson.databind.JsonNode;
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
class OllamaOaiBridgeApplicationTests {
|
||||
|
||||
private static final StubLlamaServer LLAMA_SERVER = StubLlamaServer.start();
|
||||
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
private final HttpClient httpClient = HttpClient.newHttpClient();
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@DynamicPropertySource
|
||||
static void registerProperties(DynamicPropertyRegistry registry) {
|
||||
registry.add("bridge.upstream-base-url", () -> "http://localhost:" + LLAMA_SERVER.port());
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void resetStubServer() {
|
||||
LLAMA_SERVER.reset();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void stopStubServer() {
|
||||
LLAMA_SERVER.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
@Test
|
||||
void versionIsServedLocally() throws Exception {
|
||||
JsonNode response = getJson("/api/version");
|
||||
|
||||
assertThat(response.path("version").asText()).isEqualTo("0.24.0");
|
||||
assertThat(LLAMA_SERVER.requests()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagsMapsLlamaServerModels() throws Exception {
|
||||
stubCommonUpstreamResponses(DATA_MODELS_RESPONSE);
|
||||
|
||||
JsonNode response = getJson("/api/tags");
|
||||
|
||||
assertThat(response.path("models")).hasSize(1);
|
||||
JsonNode model = response.path("models").get(0);
|
||||
assertThat(model.path("name").asText()).isEqualTo("gemma-4-E4B-it-Q6_K.gguf");
|
||||
assertThat(model.path("size").asLong()).isEqualTo(7_059_101_864L);
|
||||
assertThat(model.path("details").path("format").asText()).isEqualTo("gguf");
|
||||
assertThat(model.path("details").path("family").asText()).isEqualTo("gemma4");
|
||||
assertThat(model.path("details").path("parameter_size").asText()).isEqualTo("7.5B");
|
||||
assertThat(model.path("details").path("quantization_level").asText()).isEqualTo("Q6_K");
|
||||
assertThat(LLAMA_SERVER.requests()).containsExactly("GET /props", "GET /slots", "GET /v1/models");
|
||||
}
|
||||
|
||||
@Test
|
||||
void showMapsToStrictOllamaLikeResponse() throws Exception {
|
||||
stubCommonUpstreamResponses(OLLAMA_STYLE_MODELS_RESPONSE);
|
||||
|
||||
JsonNode response = postJson("/api/show", """
|
||||
{"model":"gemma-4-26B-A4B-it-UD-Q4_K_M:latest"}
|
||||
""");
|
||||
|
||||
assertThat(response.path("modelfile").asText()).contains("FROM /mnt/data/data/lm/models/gguf/s/gemma-4-E4B-it-Q6_K.gguf");
|
||||
assertThat(response.path("template").asText()).contains("tool_call");
|
||||
assertThat(response.path("details").path("quantization_level").asText()).isEqualTo("Q4_K_M");
|
||||
assertThat(response.path("capabilities")).extracting(JsonNode::asText)
|
||||
.contains("completion", "tools", "thinking");
|
||||
assertThat(response.path("model_info").path("bridge.total_slots").asInt()).isEqualTo(4);
|
||||
assertThat(response.path("model_info").path("bridge.busy_slots").asInt()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonOverriddenRoutesAreProxied() throws Exception {
|
||||
LLAMA_SERVER.stubJson("GET", "/props", PROPS_RESPONSE);
|
||||
|
||||
HttpResponse<String> response = send(HttpRequest.newBuilder(uri("/props")).GET().build());
|
||||
|
||||
assertThat(response.statusCode()).isEqualTo(200);
|
||||
assertThat(response.body()).isEqualTo(PROPS_RESPONSE);
|
||||
assertThat(LLAMA_SERVER.requests()).containsExactly("GET /props");
|
||||
}
|
||||
|
||||
private void stubCommonUpstreamResponses(String modelsResponse) {
|
||||
LLAMA_SERVER.stubJson("GET", "/props", PROPS_RESPONSE);
|
||||
LLAMA_SERVER.stubJson("GET", "/slots", SLOTS_RESPONSE);
|
||||
LLAMA_SERVER.stubJson("GET", "/v1/models", modelsResponse);
|
||||
}
|
||||
|
||||
private JsonNode getJson(String path) throws Exception {
|
||||
HttpResponse<String> response = send(HttpRequest.newBuilder(uri(path)).GET().build());
|
||||
assertThat(response.statusCode()).isEqualTo(200);
|
||||
return OBJECT_MAPPER.readTree(response.body());
|
||||
}
|
||||
|
||||
private JsonNode postJson(String path, String body) throws Exception {
|
||||
HttpRequest request = HttpRequest.newBuilder(uri(path))
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(body))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = send(request);
|
||||
assertThat(response.statusCode()).isEqualTo(200);
|
||||
return OBJECT_MAPPER.readTree(response.body());
|
||||
}
|
||||
|
||||
private HttpResponse<String> send(HttpRequest request) throws IOException, InterruptedException {
|
||||
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private URI uri(String path) {
|
||||
return URI.create("http://localhost:" + this.port + path);
|
||||
}
|
||||
|
||||
private static final String PROPS_RESPONSE = """
|
||||
{"default_generation_settings":{"params":{"temperature":1.0,"top_k":64,"top_p":0.95,"min_p":0.05,"repeat_last_n":64,"repeat_penalty":1.0,"presence_penalty":0.0,"frequency_penalty":0.0},"n_ctx":16128},"total_slots":4,"model_alias":"gemma-4-E4B-it-Q6_K.gguf","model_path":"/mnt/data/data/lm/models/gguf/s/gemma-4-E4B-it-Q6_K.gguf","modalities":{"vision":false,"audio":false},"bos_token":"<bos>","eos_token":"<turn|>","build_info":"b9164-d5dc2e0a0","chat_template":"template with tool_call and reasoning support"}
|
||||
""";
|
||||
|
||||
private static final String SLOTS_RESPONSE = """
|
||||
[{"id":0,"n_ctx":16128,"speculative":false,"is_processing":false},{"id":1,"n_ctx":16128,"speculative":false,"is_processing":true},{"id":2,"n_ctx":16128,"speculative":false,"is_processing":false},{"id":3,"n_ctx":16128,"speculative":false,"is_processing":false}]
|
||||
""";
|
||||
|
||||
private static final String DATA_MODELS_RESPONSE = """
|
||||
{"object":"list","data":[{"id":"gemma-4-E4B-it-Q6_K.gguf","aliases":[],"tags":[],"object":"model","created":1778857538,"owned_by":"llamacpp","meta":{"vocab_type":2,"n_vocab":262144,"n_ctx":16128,"n_ctx_train":131072,"n_embd":2560,"n_params":7518069290,"size":7059101864}}]}
|
||||
""";
|
||||
|
||||
private static final String OLLAMA_STYLE_MODELS_RESPONSE = """
|
||||
{"models":[{"name":"gemma-4-26B-A4B-it-UD-Q4_K_M:latest","model":"gemma-4-26B-A4B-it-UD-Q4_K_M:latest","modified_at":"2026-05-15T12:43:43.896860597+02:00","size":16868241115,"digest":"b16ef1d08ff9df45d8d95d352be6b0578785d9ab2d7e2a78a14151292b6093a4","capabilities":["completion"],"details":{"parent_model":"","format":"gguf","family":"gemma4","families":["gemma4"],"parameter_size":"25.2B","quantization_level":"Q4_K_M"}}]}
|
||||
""";
|
||||
|
||||
private static final class StubLlamaServer {
|
||||
|
||||
private final HttpServer httpServer;
|
||||
|
||||
private final Map<String, StubResponse> responses = new ConcurrentHashMap<>();
|
||||
|
||||
private final List<String> requests = new CopyOnWriteArrayList<>();
|
||||
|
||||
private StubLlamaServer(HttpServer httpServer) {
|
||||
this.httpServer = httpServer;
|
||||
this.httpServer.createContext("/", this::handle);
|
||||
this.httpServer.start();
|
||||
}
|
||||
|
||||
static StubLlamaServer start() {
|
||||
try {
|
||||
return new StubLlamaServer(HttpServer.create(new InetSocketAddress(0), 0));
|
||||
}
|
||||
catch (IOException exception) {
|
||||
throw new IllegalStateException("Failed to start stub llama-server", exception);
|
||||
}
|
||||
}
|
||||
|
||||
int port() {
|
||||
return this.httpServer.getAddress().getPort();
|
||||
}
|
||||
|
||||
void reset() {
|
||||
this.responses.clear();
|
||||
this.requests.clear();
|
||||
}
|
||||
|
||||
void stop() {
|
||||
this.httpServer.stop(0);
|
||||
}
|
||||
|
||||
void stubJson(String method, String path, String body) {
|
||||
this.responses.put(key(method, path), new StubResponse(200, body));
|
||||
}
|
||||
|
||||
List<String> requests() {
|
||||
return List.copyOf(this.requests);
|
||||
}
|
||||
|
||||
private void handle(HttpExchange exchange) throws IOException {
|
||||
String key = key(exchange.getRequestMethod(), exchange.getRequestURI().getPath());
|
||||
this.requests.add(key);
|
||||
|
||||
StubResponse response = this.responses.getOrDefault(key, new StubResponse(404, "{}"));
|
||||
byte[] body = response.body().getBytes(StandardCharsets.UTF_8);
|
||||
exchange.getResponseHeaders().add("Content-Type", "application/json");
|
||||
exchange.sendResponseHeaders(response.status(), body.length);
|
||||
exchange.getResponseBody().write(body);
|
||||
exchange.close();
|
||||
}
|
||||
|
||||
private static String key(String method, String path) {
|
||||
return method + " " + path;
|
||||
}
|
||||
|
||||
record StubResponse(int status, String body) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user