Module: LLM::GLMAIMethods

Included in:
GLM
Defined in:
lib/scout/llm/backends/glm.rb

Overview

GLM Chat Completions backend.

Implemented as a module exposing singleton methods (LLM::OpenAI.ask, etc). We compose the backend by:

- prepending GLMAIMethods into the singleton class (overrides)
- including Backend::ClassMethods into the singleton class (shared logic)

Instance Method Summary collapse

Instance Method Details

#format_other(message) ⇒ Object



13
14
15
16
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
# File 'lib/scout/llm/backends/glm.rb', line 13

def format_other(message)
  role = message[:role]

  case role.to_s
  when 'image'
    path = message[:content]
    path = Chat.find_file path
    if Open.remote?(path)
      { role: :user, content: { type: :image_url, image_url: {url: path} } }
    elsif Open.exists?(path)
      path = encode_image(path)
      { role: :user, content: [{ type: :image_url, image_url: {url: path} }] }
    else
      raise "Image does not exist in #{path}"
    end
  when 'pdf'
    path = original_path = message[:content]
    if Open.remote?(path)
      { role: :user, content: { type: :input_file, file_url: path } }
    elsif Open.exists?(path)
      data = encode_pdf(path)
      { role: :user, content: [{ type: :input_file, file_data: data, filename: File.basename(path) }] }
    else
      raise "PDF does not exist in #{path}"
    end
  when 'websearch'
    { role: :tool, content: { type: 'web_search_preview' } }
  when 'previous_response_id'
    nil
  else
    message
  end
end