Class: LLM::Ollama

Inherits:
Provider show all
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 – and the provider supports a wide range of models. It is straight forward to run on your own hardware, and there are a number of multi-modal models that can process both images and text.

Examples:

#!/usr/bin/env ruby
require "llm"

llm = LLM.ollama(key: nil)
bot = LLM::Bot.new(llm, model: "llava")
bot.chat ["Tell me about this image", File.open("/images/parrot.png", "rb")]
bot.messages.select(&:assistant?).each { print "[#{_1.role}]", _1.content, "\n" }

Defined Under Namespace

Modules: Response Classes: Models

Constant Summary collapse

HOST =
"localhost"

Instance Method Summary collapse

Methods inherited from Provider

#audio, #chat, #chat!, #files, #images, #inspect, #moderations, #respond, #respond!, #responses, #schema, #vector_stores, #with

Constructor Details

#initializeOllama

Returns a new instance of Ollama.

Parameters:

  • key (String, nil)

    The secret key for authentication



32
33
34
# File 'lib/llm/providers/ollama.rb', line 32

def initialize(**)
  super(host: HOST, port: 11434, ssl: false, **)
end

Instance Method Details

#embed(input, model: default_model, **params) ⇒ LLM::Response

Provides an embedding

Parameters:

  • input (String, Array<String>)

    The input to embed

  • model (String) (defaults to: default_model)

    The embedding model to use

  • params (Hash)

    Other embedding parameters

Returns:



43
44
45
46
47
48
49
# File 'lib/llm/providers/ollama.rb', line 43

def embed(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

#complete(prompt, params = {}) ⇒ LLM::Response

Provides an interface to the chat completions API

Examples:

llm = LLM.openai(ENV["KEY"])
messages = [{role: "system", content: "Your task is to answer all of my questions"}]
res = llm.complete("5 + 2 ?", messages:)
print "[#{res.choices[0].role}]", res.choices[0].content, "\n"

Parameters:

  • prompt (String)

    The input prompt to be completed

  • params (Hash) (defaults to: {})

    The parameters to maintain throughout the conversation. Any parameter the provider supports can be included and not only those listed here.

Returns:

Raises:

See Also:



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/llm/providers/ollama.rb', line 61

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)
  messages = [*(params.delete(:messages) || []), LLM::Message.new(role, prompt)]
  body = JSON.dump({messages: [format(messages)].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

#modelsLLM::Ollama::Models

Provides an interface to Ollama’s models API

Returns:

See Also:



78
79
80
# File 'lib/llm/providers/ollama.rb', line 78

def models
  LLM::Ollama::Models.new(self)
end

#assistant_roleString

Returns the role of the assistant in the conversation. Usually “assistant” or “model”

Returns:

  • (String)

    Returns the role of the assistant in the conversation. Usually “assistant” or “model”



84
85
86
# File 'lib/llm/providers/ollama.rb', line 84

def assistant_role
  "assistant"
end

#default_modelString

Returns the default model for chat completions

Returns:

  • (String)

See Also:



92
93
94
# File 'lib/llm/providers/ollama.rb', line 92

def default_model
  "qwen3:latest"
end