Class: Ollama::Client::Chat::RequestPreparer

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama/client/chat/request_preparer.rb

Overview

Prepares Chat Request objects from client configuration and inputs

Instance Method Summary collapse

Constructor Details

#initialize(config:, provider:) ⇒ RequestPreparer

Returns a new instance of RequestPreparer.



11
12
13
14
# File 'lib/ollama/client/chat/request_preparer.rb', line 11

def initialize(config:, provider:)
  @config = config
  @provider = provider
end

Instance Method Details

#build_request(params, client_instance) ⇒ Object

rubocop:disable Metrics/MethodLength



17
18
19
20
21
22
23
24
25
26
27
28
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
56
57
58
# File 'lib/ollama/client/chat/request_preparer.rb', line 17

def build_request(params, client_instance)
  target_model = params.model || @config.model
  active_profile = client_instance.send(:resolve_profile, target_model, params.profile)
  adapter = PromptAdapters.for(active_profile) if active_profile

  messages = if params.inputs
               apply_inputs(params.messages, params.inputs, active_profile)
             else
               params.messages
             end

  adapted_messages = if adapter
                       adapter.adapt_messages(messages, think: !params.think.nil?, tools: params.tools)
                     else
                       messages
                     end

  effective_think = resolve_think_flag(params.think, adapter)
  stream_enabled = params.stream.nil? ? hooks_present?(params.hooks) : params.stream

  Request.new(
    endpoint: :chat,
    model: target_model,
    messages: adapted_messages,
    tools: params.tools,
    format: params.format,
    think: effective_think,
    keep_alive: params.keep_alive,
    options: client_instance.send(:build_options_with_profile, params.options, active_profile),
    stream: stream_enabled,
    metadata: {
      base_url: @config.base_url,
      timeout: @config.timeout,
      hooks: params.hooks,
      uri: @provider.chat_endpoint
    },
    raw_options: {
      logprobs: params.logprobs,
      top_logprobs: params.top_logprobs
    }
  )
end