Class: Boxcars::Ollama

Inherits:
Engine
  • Object
show all
Includes:
UnifiedObservability
Defined in:
lib/boxcars/engine/ollama.rb

Overview

A engine that uses a local Ollama API (OpenAI-compatible).

Constant Summary collapse

DEFAULT_PARAMS =
{
  model: "llama3", # Default model for Ollama
  temperature: 0.1,
  max_tokens: 4096 # Check if Ollama respects this or has its own limits
}.freeze
DEFAULT_NAME =

Check if Ollama respects this or has its own limits

"Ollama engine"
DEFAULT_DESCRIPTION =
"useful for when you need to use local AI to answer questions. " \
"You should ask targeted questions"

Instance Attribute Summary collapse

Attributes inherited from Engine

#user_id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Engine

#extract_answer, #generate, #generation_info, #get_num_tokens, #validate_response!

Constructor Details

#initialize(name: DEFAULT_NAME, description: DEFAULT_DESCRIPTION, prompts: [], batch_size: 2, **kwargs) ⇒ Ollama

Returns a new instance of Ollama.



22
23
24
25
26
27
28
# File 'lib/boxcars/engine/ollama.rb', line 22

def initialize(name: DEFAULT_NAME, description: DEFAULT_DESCRIPTION, prompts: [], batch_size: 2, **kwargs)
  user_id = kwargs.delete(:user_id)
  @ollama_params = DEFAULT_PARAMS.merge(kwargs)
  @prompts = prompts
  @batch_size = batch_size # Retain if used by other methods
  super(description:, name:, user_id:)
end

Instance Attribute Details

#batch_sizeObject (readonly)

Returns the value of attribute batch_size.



11
12
13
# File 'lib/boxcars/engine/ollama.rb', line 11

def batch_size
  @batch_size
end

#model_kwargsObject (readonly)

Returns the value of attribute model_kwargs.



11
12
13
# File 'lib/boxcars/engine/ollama.rb', line 11

def model_kwargs
  @model_kwargs
end

#ollama_paramsObject (readonly)

Returns the value of attribute ollama_params.



11
12
13
# File 'lib/boxcars/engine/ollama.rb', line 11

def ollama_params
  @ollama_params
end

#promptsObject (readonly)

Returns the value of attribute prompts.



11
12
13
# File 'lib/boxcars/engine/ollama.rb', line 11

def prompts
  @prompts
end

Class Method Details

.ollama_clientObject

Renamed from open_ai_client to ollama_client for clarity Ollama doesn’t use an API key by default.



32
33
34
35
36
37
38
# File 'lib/boxcars/engine/ollama.rb', line 32

def self.ollama_client
  # The OpenAI gem requires an access_token, even if the local service doesn't.
  # Provide a dummy one if not needed, or allow configuration if Ollama setup requires one.
  ::OpenAI::Client.new(access_token: "ollama-dummy-key", uri_base: "http://localhost:11434/v1")
  # Added /v1 to uri_base, as OpenAI-compatible endpoints often version this way.
  # Verify Ollama's actual OpenAI-compatible endpoint path.
end

Instance Method Details

#client(prompt:, inputs: {}, **kwargs) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/boxcars/engine/ollama.rb', line 45

def client(prompt:, inputs: {}, **kwargs)
  start_time = Time.now
  response_data = { response_obj: nil, parsed_json: nil, success: false, error: nil, status_code: nil }
  current_params = @ollama_params.merge(kwargs)
  current_prompt_object = prompt.is_a?(Array) ? prompt.first : prompt
  api_request_params = nil # Initialize

  begin
    clnt = Ollama.ollama_client
    api_request_params = _prepare_ollama_request_params(current_prompt_object, inputs, current_params)

    log_messages_debug(api_request_params[:messages]) if Boxcars.configuration.log_prompts && api_request_params[:messages]

    _execute_and_process_ollama_call(clnt, api_request_params, response_data)
  rescue ::OpenAI::Error => e
    _handle_openai_error_for_ollama(e, response_data)
  rescue StandardError => e
    _handle_standard_error_for_ollama(e, response_data)
  ensure
    duration_ms = ((Time.now - start_time) * 1000).round
    request_context = {
      prompt: current_prompt_object,
      inputs:,
      conversation_for_api: api_request_params&.dig(:messages),
      user_id:
    }
    track_ai_generation(
      duration_ms:,
      current_params:,
      request_context:,
      response_data:,
      provider: :ollama
    )
  end

  _ollama_handle_call_outcome(response_data:)
end

#conversation_model?(_model_name) ⇒ Boolean

Ollama models are typically conversational.

Returns:

  • (Boolean)


41
42
43
# File 'lib/boxcars/engine/ollama.rb', line 41

def conversation_model?(_model_name)
  true
end

#default_paramsObject



90
91
92
# File 'lib/boxcars/engine/ollama.rb', line 90

def default_params
  @ollama_params
end

#run(question) ⇒ Object



83
84
85
86
87
88
# File 'lib/boxcars/engine/ollama.rb', line 83

def run(question, **)
  prompt = Prompt.new(template: question)
  answer = client(prompt:, inputs: {}, **) # Pass empty inputs hash
  Boxcars.debug("Answer: #{answer}", :cyan)
  answer
end