Class: Legion::Extensions::Llm::Ollama::Provider
- Inherits:
-
Provider
- Object
- Provider
- Legion::Extensions::Llm::Ollama::Provider
- Includes:
- Logging::Helper
- Defined in:
- lib/legion/extensions/llm/ollama/provider.rb
Overview
Ollama provider implementation for the Legion::Extensions::Llm base provider contract.
Defined Under Namespace
Modules: Capabilities
Class Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
- #api_base ⇒ Object
- #completion_url ⇒ Object
- #config_base_url ⇒ Object
- #discover_offerings(live: false) ⇒ Object
- #embedding_url ⇒ Object
- #list_models ⇒ Object
- #list_running_models ⇒ Object
- #models_url ⇒ Object
- #pull_model(model, stream: false) ⇒ Object
- #pull_url ⇒ Object
- #readiness(live: false) ⇒ Object
- #running_models_url ⇒ Object
- #settings ⇒ Object
- #show_model(model) ⇒ Object
- #show_model_url ⇒ Object
- #stream_url ⇒ Object
- #version_url ⇒ Object
Class Attribute Details
.registry_publisher ⇒ Object
22 23 24 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 22 def registry_publisher @registry_publisher ||= Ollama.registry_publisher end |
Class Method Details
.capabilities ⇒ Object
20 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 20 def capabilities = Capabilities |
.configuration_requirements ⇒ Object
19 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 19 def configuration_requirements = [] |
.local? ⇒ Boolean
18 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 18 def local? = true |
.slug ⇒ Object
17 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 17 def slug = 'ollama' |
Instance Method Details
#api_base ⇒ Object
42 43 44 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 42 def api_base resolve_base_url || normalize_url(settings[:base_url] || '127.0.0.1:11434') end |
#completion_url ⇒ Object
50 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 50 def completion_url = '/api/chat' |
#config_base_url ⇒ Object
46 47 48 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 46 def config_base_url config.respond_to?(:base_url) ? config.base_url : settings[:base_url] end |
#discover_offerings(live: false) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 99 def discover_offerings(live: false, **) log.debug do "ollama provider discovering offerings live=#{live} cached_model_count=#{Array(@cached_models).size}" end models = if live @cached_models = list_models else Array(@cached_models) end models.map { |model_info| offering_from_model(model_info) }.tap do |offerings| log.debug { "ollama provider built offering_count=#{offerings.size} live=#{live}" } end rescue StandardError => e handle_exception(e, level: :warn, handled: true, operation: 'ollama.discover_offerings') [] end |
#embedding_url ⇒ Object
55 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 55 def (**) = '/api/embed' |
#list_models ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 74 def list_models log.debug { "ollama provider discovering models endpoint=#{api_base}#{models_url}" } super.tap do |models| log.debug { "ollama provider discovered model_count=#{models.size}" } self.class.registry_publisher.publish_models_async(models, readiness: readiness(live: false)) end end |
#list_running_models ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 59 def list_running_models log.debug { "ollama provider listing running models endpoint=#{api_base}#{running_models_url}" } connection.get(running_models_url).body.fetch('models', []) rescue StandardError => e handle_exception(e, level: :error, handled: true, operation: 'ollama.list_running_models') [] end |
#models_url ⇒ Object
52 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 52 def models_url = '/api/tags' |
#pull_model(model, stream: false) ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 90 def pull_model(model, stream: false) log.debug { "ollama provider pulling model=#{model} stream=#{stream}" } log.info { "pulling model #{model} stream=#{stream}" } connection.post(pull_url, { model: model, stream: stream }).body rescue StandardError => e handle_exception(e, level: :error, handled: true, operation: 'ollama.pull_model') raise end |
#pull_url ⇒ Object
56 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 56 def pull_url = '/api/pull' |
#readiness(live: false) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 67 def readiness(live: false) log.debug { "ollama provider checking readiness live=#{live} endpoint=#{api_base}" } super.tap do || self.class.registry_publisher.publish_readiness_async() if live end end |
#running_models_url ⇒ Object
53 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 53 def running_models_url = '/api/ps' |
#settings ⇒ Object
38 39 40 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 38 def settings Ollama.default_settings end |
#show_model(model) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 82 def show_model(model) log.debug { "ollama provider fetching model details model=#{model}" } connection.post(show_model_url, { model: model }).body rescue StandardError => e handle_exception(e, level: :error, handled: true, operation: 'ollama.show_model') raise end |
#show_model_url ⇒ Object
54 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 54 def show_model_url = '/api/show' |
#stream_url ⇒ Object
51 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 51 def stream_url = '/api/chat' |
#version_url ⇒ Object
57 |
# File 'lib/legion/extensions/llm/ollama/provider.rb', line 57 def version_url = '/api/version' |