- 
                Notifications
    
You must be signed in to change notification settings  - Fork 2k
 
Ollama: Pull models automatically at startup #1554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Closed
      
      
            ThomasVitale
  wants to merge
  1
  commit into
  spring-projects:main
from
ThomasVitale:ollama-auto-pull-models-startup
  
      
      
   
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -22,7 +22,6 @@ | |
| import java.util.regex.Pattern; | ||
| 
     | 
||
| import io.micrometer.observation.ObservationRegistry; | ||
| import org.springframework.ai.chat.metadata.EmptyUsage; | ||
| import org.springframework.ai.document.Document; | ||
| import org.springframework.ai.embedding.*; | ||
| import org.springframework.ai.embedding.observation.DefaultEmbeddingModelObservationConvention; | ||
| 
        
          
        
         | 
    @@ -32,24 +31,20 @@ | |
| import org.springframework.ai.model.ModelOptionsUtils; | ||
| import org.springframework.ai.ollama.api.OllamaApi; | ||
| import org.springframework.ai.ollama.api.OllamaApi.EmbeddingsResponse; | ||
| import org.springframework.ai.ollama.api.OllamaModelPuller; | ||
| import org.springframework.ai.ollama.management.ModelManagementOptions; | ||
| import org.springframework.ai.ollama.management.OllamaModelManager; | ||
| import org.springframework.ai.ollama.api.OllamaOptions; | ||
| import org.springframework.ai.ollama.management.PullModelStrategy; | ||
| import org.springframework.ai.ollama.metadata.OllamaEmbeddingUsage; | ||
| import org.springframework.util.Assert; | ||
| import org.springframework.util.StringUtils; | ||
| 
     | 
||
| /** | ||
| * {@link EmbeddingModel} implementation for {@literal Ollama}. | ||
| * | ||
| * Ollama allows developers to run large language models and generate embeddings locally. | ||
| * It supports open-source models available on [Ollama AI | ||
| * Library](https://ollama.ai/library). | ||
| * | ||
| * Examples of models supported: - Llama 2 (7B parameters, 3.8GB size) - Mistral (7B | ||
| * parameters, 4.1GB size) | ||
| * | ||
| * Please refer to the <a href="https://ollama.ai/">official Ollama website</a> for the | ||
| * most up-to-date information on available models. | ||
| * {@link EmbeddingModel} implementation for {@literal Ollama}. Ollama allows developers | ||
| * to run large language models and generate embeddings locally. It supports open-source | ||
| * models available on [Ollama AI Library](<a href="https://ollama.ai/library">...</a>) | ||
| * and on Hugging Face. Please refer to the <a href="https://ollama.ai/">official Ollama | ||
| * website</a> for the most up-to-date information on available models. | ||
| * | ||
| * @author Christian Tzolov | ||
| * @author Thomas Vitale | ||
| 
        
          
        
         | 
    @@ -61,41 +56,31 @@ public class OllamaEmbeddingModel extends AbstractEmbeddingModel { | |
| 
     | 
||
| private final OllamaApi ollamaApi; | ||
| 
     | 
||
| /** | ||
| * Default options to be used for all chat requests. | ||
| */ | ||
| private final OllamaOptions defaultOptions; | ||
| 
     | 
||
| /** | ||
| * Observation registry used for instrumentation. | ||
| */ | ||
| private final ObservationRegistry observationRegistry; | ||
| 
     | 
||
| /** | ||
| * Conventions to use for generating observations. | ||
| */ | ||
| private EmbeddingModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION; | ||
| 
     | 
||
| private final OllamaModelPuller modelPuller; | ||
| private final OllamaModelManager modelManager; | ||
| 
     | 
||
| public OllamaEmbeddingModel(OllamaApi ollamaApi) { | ||
| this(ollamaApi, OllamaOptions.create().withModel(OllamaOptions.DEFAULT_MODEL)); | ||
| } | ||
| 
     | 
||
| public OllamaEmbeddingModel(OllamaApi ollamaApi, OllamaOptions defaultOptions) { | ||
| this(ollamaApi, defaultOptions, ObservationRegistry.NOOP); | ||
| } | ||
| private EmbeddingModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION; | ||
| 
     | 
||
| public OllamaEmbeddingModel(OllamaApi ollamaApi, OllamaOptions defaultOptions, | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here, I introduced a Builder  | 
||
| ObservationRegistry observationRegistry) { | ||
| Assert.notNull(ollamaApi, "openAiApi must not be null"); | ||
| ObservationRegistry observationRegistry, ModelManagementOptions modelManagementOptions) { | ||
| Assert.notNull(ollamaApi, "ollamaApi must not be null"); | ||
| Assert.notNull(defaultOptions, "options must not be null"); | ||
| Assert.notNull(observationRegistry, "observationRegistry must not be null"); | ||
| Assert.notNull(modelManagementOptions, "modelManagementOptions must not be null"); | ||
| 
     | 
||
| this.ollamaApi = ollamaApi; | ||
| this.defaultOptions = defaultOptions; | ||
| this.observationRegistry = observationRegistry; | ||
| this.modelPuller = new OllamaModelPuller(ollamaApi); | ||
| this.modelManager = new OllamaModelManager(ollamaApi, modelManagementOptions); | ||
| 
     | 
||
| initializeModelIfEnabled(defaultOptions.getModel(), modelManagementOptions.pullModelStrategy()); | ||
| } | ||
| 
     | 
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
| 
     | 
||
| @Override | ||
| 
          
            
          
           | 
    @@ -153,9 +138,9 @@ OllamaApi.EmbeddingsRequest ollamaEmbeddingRequest(List<String> inputContent, Em | |
| 
     | 
||
| OllamaOptions mergedOptions = ModelOptionsUtils.merge(runtimeOptions, this.defaultOptions, OllamaOptions.class); | ||
| 
     | 
||
| mergedOptions.setPullMissingModel(this.defaultOptions.isPullMissingModel()); | ||
| if (runtimeOptions != null && runtimeOptions.isPullMissingModel() != null) { | ||
| mergedOptions.setPullMissingModel(runtimeOptions.isPullMissingModel()); | ||
| mergedOptions.setPullModelStrategy(this.defaultOptions.getPullModelStrategy()); | ||
| if (runtimeOptions != null && runtimeOptions.getPullModelStrategy() != null) { | ||
| mergedOptions.setPullModelStrategy(runtimeOptions.getPullModelStrategy()); | ||
| } | ||
| 
     | 
||
| // Override the model. | ||
| 
        
          
        
         | 
    @@ -164,9 +149,7 @@ OllamaApi.EmbeddingsRequest ollamaEmbeddingRequest(List<String> inputContent, Em | |
| } | ||
| String model = mergedOptions.getModel(); | ||
| 
     | 
||
| if (mergedOptions.isPullMissingModel()) { | ||
| this.modelPuller.pullModel(model, true); | ||
| } | ||
| initializeModelIfEnabled(mergedOptions.getModel(), mergedOptions.getPullModelStrategy()); | ||
| 
     | 
||
| return new OllamaApi.EmbeddingsRequest(model, inputContent, DurationParser.parse(mergedOptions.getKeepAlive()), | ||
| OllamaOptions.filterNonSupportedFields(mergedOptions.toMap()), mergedOptions.getTruncate()); | ||
| 
        
          
        
         | 
    @@ -176,6 +159,15 @@ private EmbeddingOptions buildRequestOptions(OllamaApi.EmbeddingsRequest request | |
| return EmbeddingOptionsBuilder.builder().withModel(request.model()).build(); | ||
| } | ||
| 
     | 
||
| /** | ||
| * Pull the given model into Ollama based on the specified strategy. | ||
| */ | ||
| private void initializeModelIfEnabled(String model, PullModelStrategy pullModelStrategy) { | ||
| if (!PullModelStrategy.NEVER.equals(pullModelStrategy)) { | ||
| this.modelManager.pullModel(model, pullModelStrategy); | ||
| } | ||
| } | ||
| 
     | 
||
| /** | ||
| * Use the provided convention for reporting observation data | ||
| * @param observationConvention The provided convention | ||
| 
          
            
          
           | 
    @@ -216,4 +208,43 @@ public static Duration parse(String input) { | |
| 
     | 
||
| } | ||
| 
     | 
||
| public static class Builder { | ||
| 
     | 
||
| private OllamaApi ollamaApi; | ||
| 
     | 
||
| private OllamaOptions defaultOptions = OllamaOptions.create().withModel(OllamaOptions.DEFAULT_MODEL); | ||
| 
     | 
||
| private ObservationRegistry observationRegistry = ObservationRegistry.NOOP; | ||
| 
     | 
||
| private ModelManagementOptions modelManagementOptions = ModelManagementOptions.defaults(); | ||
| 
     | 
||
| private Builder() { | ||
| } | ||
| 
     | 
||
| public Builder withOllamaApi(OllamaApi ollamaApi) { | ||
| this.ollamaApi = ollamaApi; | ||
| return this; | ||
| } | ||
| 
     | 
||
| public Builder withDefaultOptions(OllamaOptions defaultOptions) { | ||
| this.defaultOptions = defaultOptions; | ||
| return this; | ||
| } | ||
| 
     | 
||
| public Builder withObservationRegistry(ObservationRegistry observationRegistry) { | ||
| this.observationRegistry = observationRegistry; | ||
| return this; | ||
| } | ||
| 
     | 
||
| public Builder withModelManagementOptions(ModelManagementOptions modelManagementOptions) { | ||
| this.modelManagementOptions = modelManagementOptions; | ||
| return this; | ||
| } | ||
| 
     | 
||
| public OllamaEmbeddingModel build() { | ||
| return new OllamaEmbeddingModel(ollamaApi, defaultOptions, observationRegistry, modelManagementOptions); | ||
| } | ||
| 
     | 
||
| } | ||
| 
     | 
||
| } | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The argument list grew so much that I didn't want to add even more overloaded constructors. Instead, I introduced a Builder to help making this whole initialisation code more readable.