Class OllamaContainerProvider

java.lang.Object
io.flowinquiry.testcontainers.SpringAwareContainerProvider<io.flowinquiry.testcontainers.ai.EnableOllamaContainer,org.testcontainers.ollama.OllamaContainer>
io.flowinquiry.testcontainers.ai.OllamaContainerProvider
All Implemented Interfaces:
io.flowinquiry.testcontainers.LifecycleAware

public class OllamaContainerProvider extends io.flowinquiry.testcontainers.SpringAwareContainerProvider<io.flowinquiry.testcontainers.ai.EnableOllamaContainer,org.testcontainers.ollama.OllamaContainer>
A Spring-aware container provider for Ollama AI containers.

This class manages the lifecycle of Ollama containers in a Spring environment, including container creation, configuration, and integration with Spring's property system. It handles pulling the specified AI model during container startup and configuring Spring AI properties to connect to the containerized Ollama instance.

The provider uses the EnableOllamaContainer annotation to configure the container with specific parameters such as the Docker image, version, model name, and model options (temperature and top-p values).

  • Field Summary

    Fields inherited from class io.flowinquiry.testcontainers.SpringAwareContainerProvider

    container, dockerImage, enableContainerAnnotation, version
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    applyTo(org.springframework.core.env.ConfigurableEnvironment environment)
    Applies Ollama-specific configuration to the Spring environment.
    protected org.testcontainers.ollama.OllamaContainer
    Creates and configures an Ollama container instance.
    io.flowinquiry.testcontainers.ContainerType
    Returns the type of container managed by this provider.
    void
    Starts the Ollama container and pulls the specified AI model.

    Methods inherited from class io.flowinquiry.testcontainers.SpringAwareContainerProvider

    initContainerInstance, stop

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • OllamaContainerProvider

      public OllamaContainerProvider()
  • Method Details

    • getContainerType

      public io.flowinquiry.testcontainers.ContainerType getContainerType()
      Returns the type of container managed by this provider.
      Specified by:
      getContainerType in class io.flowinquiry.testcontainers.SpringAwareContainerProvider<io.flowinquiry.testcontainers.ai.EnableOllamaContainer,org.testcontainers.ollama.OllamaContainer>
      Returns:
      the OLLAMA container type
    • createContainer

      protected org.testcontainers.ollama.OllamaContainer createContainer()
      Creates and configures an Ollama container instance.
      Specified by:
      createContainer in class io.flowinquiry.testcontainers.SpringAwareContainerProvider<io.flowinquiry.testcontainers.ai.EnableOllamaContainer,org.testcontainers.ollama.OllamaContainer>
      Returns:
      a new OllamaContainer instance configured with the specified Docker image and version
    • start

      public void start()
      Starts the Ollama container and pulls the specified AI model.

      This method first calls the parent class's start method to start the container, then executes the 'ollama pull' command inside the container to download the specified AI model.

      Specified by:
      start in interface io.flowinquiry.testcontainers.LifecycleAware
      Overrides:
      start in class io.flowinquiry.testcontainers.SpringAwareContainerProvider<io.flowinquiry.testcontainers.ai.EnableOllamaContainer,org.testcontainers.ollama.OllamaContainer>
      Throws:
      RuntimeException - if there is an error pulling the model
    • applyTo

      public void applyTo(org.springframework.core.env.ConfigurableEnvironment environment)
      Applies Ollama-specific configuration to the Spring environment.

      These properties are added to the Spring environment with high precedence to ensure they override any existing configuration.

      Specified by:
      applyTo in class io.flowinquiry.testcontainers.SpringAwareContainerProvider<io.flowinquiry.testcontainers.ai.EnableOllamaContainer,org.testcontainers.ollama.OllamaContainer>
      Parameters:
      environment - the Spring environment to configure