Class: RCrewAI::LLMClients::Ollama

Inherits:
Base
  • Object
show all
Defined in:
lib/rcrewai/llm_clients/ollama.rb

Constant Summary collapse

DEFAULT_URL =
'http://localhost:11434'
NATIVE_TOOL_MODELS =
%w[
  llama3.1 llama3.1:8b llama3.1:70b llama3.1:405b
  llama3.2 llama3.2:1b llama3.2:3b
  qwen2.5 qwen2.5:7b qwen2.5:14b qwen2.5:32b qwen2.5:72b
  mistral-nemo mistral-large
  command-r command-r-plus
  firefunction-v2
].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #logger

Instance Method Summary collapse

Methods inherited from Base

#complete

Constructor Details

#initialize(config = RCrewAI.configuration) ⇒ Ollama

Returns a new instance of Ollama.



24
25
26
27
# File 'lib/rcrewai/llm_clients/ollama.rb', line 24

def initialize(config = RCrewAI.configuration)
  super
  @base_url = config.base_url || ollama_url || DEFAULT_URL
end

Instance Method Details

#chat(messages:, tools: nil, tool_choice: :auto, stream: nil, **options) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rcrewai/llm_clients/ollama.rb', line 29

def chat(messages:, tools: nil, tool_choice: :auto, stream: nil, **options) # rubocop:disable Lint/UnusedMethodArgument
  payload = {
    model: config.model,
    messages: format_messages(messages),
    options: {
      temperature: options[:temperature] || config.temperature,
      num_predict: options[:max_tokens] || config.max_tokens,
      top_p: options[:top_p],
      top_k: options[:top_k],
      repeat_penalty: options[:repeat_penalty]
    }.compact
  }
  payload[:options][:stop] = options[:stop] if options[:stop]

  if tools && !tools.empty?
    payload[:tools] = ProviderSchema.for_many(:ollama, tools)
  end

  url = "#{@base_url}/api/chat"
  if stream
    payload[:stream] = true
    stream_chat(url, payload, stream)
  else
    payload[:stream] = false
    plain_chat(url, payload)
  end
end

#modelsObject



65
66
67
68
69
70
71
72
73
# File 'lib/rcrewai/llm_clients/ollama.rb', line 65

def models
  url = "#{@base_url}/api/tags"
  response = http_client.get(url, {}, build_headers)
  result = handle_response(response)
  Array(result['models']).map { |m| m['name'] }
rescue StandardError => e
  logger.warn "Failed to fetch Ollama models: #{e.message}"
  []
end

#pull_model(model_name) ⇒ Object



75
76
77
78
79
# File 'lib/rcrewai/llm_clients/ollama.rb', line 75

def pull_model(model_name)
  url = "#{@base_url}/api/pull"
  response = http_client.post(url, { name: model_name }, build_headers)
  handle_response(response)
end

#supports_native_tools?(model: config.model) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
# File 'lib/rcrewai/llm_clients/ollama.rb', line 57

def supports_native_tools?(model: config.model)
  override = RCrewAI.configuration.respond_to?(:ollama_native_tools) ? RCrewAI.configuration.ollama_native_tools : nil
  return override unless override.nil?

  base = model.to_s.split(':').first
  NATIVE_TOOL_MODELS.any? { |m| m == model || m.split(':').first == base }
end