Module: LLM::OLlama

Defined in:
lib/scout/llm/backends/ollama.rb

Class Method Summary collapse

Class Method Details

.ask(question, options = {}, &block) ⇒ Object



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/scout/llm/backends/ollama.rb', line 33

def self.ask(question, options = {}, &block)
  original_options = options.dup

  messages = LLM.chat(question)
  options = options.merge LLM.options messages

  client, url, key, model, return_messages, format, stream, previous_response_id, tools = IndiferentHash.process_options options,
    :client, :url, :key, :model, :return_messages, :format, :stream, :previous_response_id, :tools,
    stream: false

  if client.nil?
    url ||= Scout::Config.get(:url, :ollama_ask, :ask, :ollama, env: 'OLLAMA_URL', default: "http://localhost:11434")
    key ||= LLM.get_url_config(:key, url, :ollama_ask, :ask, :ollama, env: 'OLLAMA_KEY')
    client = self.client url, key
  end

  if model.nil?
    url ||= Scout::Config.get(:url, :ollama_ask, :ask, :ollama, env: 'OLLAMA_URL', default: "http://localhost:11434")
    model ||= LLM.get_url_config(:model, url, :ollama_ask, :ask, :ollama, env: 'OLLAMA_MODEL', default: "mistral")
  end


  case format.to_sym
  when :json, :json_object
    options[:response_format] = {type: 'json_object'}
  else
    options[:response_format] = {type: format}
  end if format

  parameters = options.merge(model: model)

  # Process tools

  case tools
  when Array
    tools = tools.inject({}) do |acc,definition|
      IndiferentHash.setup definition
      name = definition.dig('name') || definition.dig('function', 'name')
      acc.merge(name => definition)
    end
  when nil
    tools = {}
  end

  tools.merge!(LLM.tools messages)
  tools.merge!(LLM.associations messages)

  if tools.any?
    parameters[:tools] = LLM.tool_definitions_to_ollama tools
  end

  Log.low "Calling ollama #{url}: #{Log.fingerprint(parameters.except(:tools))}}"
  Log.medium "Tools: #{Log.fingerprint tools.keys}}" if tools

  parameters[:messages] = LLM.tools_to_ollama messages

  parameters[:stream] = stream

  response = self.process_response client.chat(parameters), tools, &block

  res = if response.last[:role] == 'function_call_output' 
          #response + self.ask(messages + response, original_options.except(:tool_choice).merge(return_messages: true, tools: tools), &block)
          # This version seems to keep the original message from getting forgotten
          response + self.ask(response + messages, original_options.except(:tool_choice).merge(return_messages: true, tools: tools), &block)
        else
          response
        end

  if return_messages
    res
  else
    res.last['content']
  end
end

.client(url, key = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/scout/llm/backends/ollama.rb', line 6

def self.client(url, key = nil)
  Ollama.new(
    credentials: {
      address: url,
      bearer_token: key
    },
    options: { stream: false, debug: true }
  )
end

.embed(text, options = {}) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/scout/llm/backends/ollama.rb', line 108

def self.embed(text, options = {})

  client, url, key, model = IndiferentHash.process_options options, :client, :url, :key, :model

  if client.nil?
    url ||= Scout::Config.get(:url, :ollama_embed, :embed, :ollama, env: 'OLLAMA_URL', default: "http://localhost:11434")
    key ||= LLM.get_url_config(:key, url, :ollama_embed, :embed, :ollama, env: 'OLLAMA_KEY')
    client = self.client url, key
  end

  if model.nil?
    url ||= Scout::Config.get(:url, :ollama_embed, :embed, :ollama, env: 'OLLAMA_URL', default: "http://localhost:11434")
    model ||= LLM.get_url_config(:model, url, :ollama_embed, :embed, :ollama, env: 'OLLAMA_MODEL', default: "mistral")
  end

  parameters = { input: text, model: model }
  Log.debug "Calling client with parameters: #{Log.fingerprint parameters}"
  embeddings = client.request('api/embed', parameters)

  Array === text ? embeddings.first['embeddings'] : embeddings.first['embeddings'].first
end

.process_response(responses, tools, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/scout/llm/backends/ollama.rb', line 17

def self.process_response(responses, tools, &block)
  responses.collect do |response|
    Log.debug "Respose: #{Log.fingerprint response}"

    message = response['message']
    tool_calls = response.dig("tool_calls") ||
      response.dig("message", "tool_calls")

    if tool_calls && tool_calls.any?
      LLM.process_calls tools, tool_calls, &block
    else
      [message]
    end
  end.flatten
end