Class: LLM::Ollama
- Includes:
- Format
- Defined in:
- lib/llm/providers/ollama.rb,
lib/llm/providers/ollama/format.rb,
lib/llm/providers/ollama/models.rb,
lib/llm/providers/ollama/error_handler.rb,
lib/llm/providers/ollama/stream_parser.rb
Overview
The Ollama class implements a provider for [Ollama](ollama.ai/).
This provider supports a wide range of models, it is relatively straight forward to run on your own hardware, and includes multi-modal models that can process images and text. See the example for a demonstration of a multi-modal model by the name ‘llava`
Defined Under Namespace
Modules: Format, Response Classes: ErrorHandler, Models, StreamParser
Constant Summary collapse
- HOST =
"localhost"
Instance Method Summary collapse
-
#assistant_role ⇒ String
Returns the role of the assistant in the conversation.
-
#complete(prompt, params = {}) ⇒ LLM::Response
Provides an interface to the chat completions API.
-
#default_model ⇒ String
Returns the default model for chat completions.
-
#embed(input, model: default_model, **params) ⇒ LLM::Response
Provides an embedding.
-
#initialize ⇒ Ollama
constructor
A new instance of Ollama.
-
#models ⇒ LLM::Ollama::Models
Provides an interface to Ollama’s models API.
Methods included from Format
Methods inherited from Provider
#audio, #chat, #chat!, #files, #images, #inspect, #moderations, #respond, #respond!, #responses, #schema, #vector_stores, #with
Constructor Details
Instance Method Details
#assistant_role ⇒ String
Returns the role of the assistant in the conversation. Usually “assistant” or “model”
87 88 89 |
# File 'lib/llm/providers/ollama.rb', line 87 def assistant_role "assistant" end |
#complete(prompt, params = {}) ⇒ LLM::Response
Provides an interface to the chat completions API
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/llm/providers/ollama.rb', line 64 def complete(prompt, params = {}) params = {role: :user, model: default_model, stream: true}.merge!(params) params = [params, {format: params[:schema]}, format_tools(params)].inject({}, &:merge!).compact role, stream = params.delete(:role), params.delete(:stream) params[:stream] = true if stream.respond_to?(:<<) || stream == true req = Net::HTTP::Post.new("/api/chat", headers) = [*(params.delete(:messages) || []), LLM::Message.new(role, prompt)] body = JSON.dump({messages: [format()].flatten}.merge!(params)) set_body_stream(req, StringIO.new(body)) res = execute(request: req, stream:) LLM::Response.new(res).extend(LLM::Ollama::Response::Completion) end |
#default_model ⇒ String
Returns the default model for chat completions
95 96 97 |
# File 'lib/llm/providers/ollama.rb', line 95 def default_model "qwen3:latest" end |
#embed(input, model: default_model, **params) ⇒ LLM::Response
Provides an embedding
46 47 48 49 50 51 52 |
# File 'lib/llm/providers/ollama.rb', line 46 def (input, model: default_model, **params) params = {model:}.merge!(params) req = Net::HTTP::Post.new("/v1/embeddings", headers) req.body = JSON.dump({input:}.merge!(params)) res = execute(request: req) LLM::Response.new(res).extend(LLM::Ollama::Response::Embedding) end |
#models ⇒ LLM::Ollama::Models
Provides an interface to Ollama’s models API
81 82 83 |
# File 'lib/llm/providers/ollama.rb', line 81 def models LLM::Ollama::Models.new(self) end |