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/response_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, ResponseParser Classes: ErrorHandler, Models
Constant Summary collapse
- HOST =
"localhost"
Instance Method Summary collapse
-
#assistant_role ⇒ String
Returns the role of the assistant in the conversation.
-
#complete(prompt, role = :user, model: default_model, schema: nil, **params) ⇒ LLM::Response::Completion
Provides an interface to the chat completions API.
-
#default_model ⇒ String
Returns the default model for chat completions.
-
#embed(input, model: "llama3.2", **params) ⇒ LLM::Response::Embedding
Provides an embedding.
-
#initialize(secret) ⇒ 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, #respond, #respond!, #responses, #schema
Constructor Details
Instance Method Details
#assistant_role ⇒ String
Returns the role of the assistant in the conversation. Usually “assistant” or “model”
86 87 88 |
# File 'lib/llm/providers/ollama.rb', line 86 def assistant_role "assistant" end |
#complete(prompt, role = :user, model: default_model, schema: nil, **params) ⇒ LLM::Response::Completion
Provides an interface to the chat completions API
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/llm/providers/ollama.rb', line 63 def complete(prompt, role = :user, model: default_model, schema: nil, **params) params = {model:, stream: false} .merge!((schema)) .merge!(params) .compact req = Net::HTTP::Post.new("/api/chat", headers) = [*(params.delete(:messages) || []), LLM::Message.new(role, prompt)] body = JSON.dump({messages: format()}.merge!(params)) set_body_stream(req, StringIO.new(body)) res = request(@http, req) Response::Completion.new(res).extend(response_parser) end |
#default_model ⇒ String
Returns the default model for chat completions
94 95 96 |
# File 'lib/llm/providers/ollama.rb', line 94 def default_model "llama3.2" end |
#embed(input, model: "llama3.2", **params) ⇒ LLM::Response::Embedding
Provides an embedding
43 44 45 46 47 48 49 |
# File 'lib/llm/providers/ollama.rb', line 43 def (input, model: "llama3.2", **params) params = {model:}.merge!(params) req = Net::HTTP::Post.new("/v1/embeddings", headers) req.body = JSON.dump({input:}.merge!(params)) res = request(@http, req) Response::Embedding.new(res).extend(response_parser) end |
#models ⇒ LLM::Ollama::Models
Provides an interface to Ollama’s models API
80 81 82 |
# File 'lib/llm/providers/ollama.rb', line 80 def models LLM::Ollama::Models.new(self) end |