|
| 1 | +package org.testcontainers.containers; |
| 2 | + |
| 3 | +import com.github.dockerjava.api.command.InspectContainerResponse; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
| 5 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 6 | +import org.testcontainers.utility.DockerImageName; |
| 7 | + |
| 8 | +import java.io.BufferedReader; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStreamReader; |
| 11 | +import java.io.OutputStream; |
| 12 | +import java.net.HttpURLConnection; |
| 13 | +import java.net.URL; |
| 14 | +import java.nio.charset.StandardCharsets; |
| 15 | + |
| 16 | +/** |
| 17 | + * Testcontainers proxy container for the Docker Model Runner service |
| 18 | + * provided by Docker Desktop. |
| 19 | + * <p> |
| 20 | + * Supported images: {@code alpine/socat} |
| 21 | + * <p> |
| 22 | + * Exposed ports: 80 |
| 23 | + */ |
| 24 | +@Slf4j |
| 25 | +public class DockerModelRunnerContainer extends SocatContainer { |
| 26 | + |
| 27 | + private static final String MODEL_RUNNER_ENDPOINT = "model-runner.docker.internal"; |
| 28 | + |
| 29 | + private static final int PORT = 80; |
| 30 | + |
| 31 | + private String model; |
| 32 | + |
| 33 | + public DockerModelRunnerContainer(String image) { |
| 34 | + this(DockerImageName.parse(image)); |
| 35 | + } |
| 36 | + |
| 37 | + public DockerModelRunnerContainer(DockerImageName image) { |
| 38 | + super(image); |
| 39 | + withTarget(PORT, MODEL_RUNNER_ENDPOINT); |
| 40 | + waitingFor(Wait.forHttp("/").forResponsePredicate(res -> res.contains("The service is running"))); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected void containerIsStarted(InspectContainerResponse containerInfo) { |
| 45 | + if (this.model != null) { |
| 46 | + logger().info("Pulling model: {}. Please be patient.", this.model); |
| 47 | + |
| 48 | + String url = getBaseEndpoint() + "/models/create"; |
| 49 | + String payload = String.format("{\"from\": \"%s\"}", this.model); |
| 50 | + |
| 51 | + try { |
| 52 | + HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); |
| 53 | + connection.setRequestMethod("POST"); |
| 54 | + connection.setRequestProperty("Content-Type", "application/json"); |
| 55 | + connection.setDoOutput(true); |
| 56 | + |
| 57 | + try (OutputStream os = connection.getOutputStream()) { |
| 58 | + os.write(payload.getBytes()); |
| 59 | + os.flush(); |
| 60 | + } |
| 61 | + |
| 62 | + try ( |
| 63 | + BufferedReader br = new BufferedReader( |
| 64 | + new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8) |
| 65 | + ) |
| 66 | + ) { |
| 67 | + while (br.readLine() != null) {} |
| 68 | + } |
| 69 | + connection.disconnect(); |
| 70 | + } catch (IOException e) { |
| 71 | + logger().error("Failed to pull model {}: {}", this.model, e); |
| 72 | + } |
| 73 | + logger().info("Finished pulling model: {}", this.model); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public DockerModelRunnerContainer withModel(String model) { |
| 78 | + this.model = model; |
| 79 | + return this; |
| 80 | + } |
| 81 | + |
| 82 | + public String getBaseEndpoint() { |
| 83 | + return "http://" + getHost() + ":" + getMappedPort(PORT); |
| 84 | + } |
| 85 | + |
| 86 | + public String getOpenAIEndpoint() { |
| 87 | + return getBaseEndpoint() + "/engines"; |
| 88 | + } |
| 89 | +} |
0 commit comments