Class: Ollama::Message

Inherits:
Object
  • Object
show all
Includes:
DTO
Defined in:
lib/ollama/message.rb

Overview

Ollama::Message

Represents a message object used in communication with the Ollama API. This class encapsulates the essential components of a message, including the role of the sender, the content of the message, optional thinking content, associated images, and tool calls.

Examples:

Creating a basic message

message = Ollama::Message.new(
  role: 'user',
  content: 'Hello, world!'
)

Creating a message with additional attributes

image = Ollama::Image.for_filename('path/to/image.jpg')
message = Ollama::Message.new(
  role: 'user',
  content: 'Look at this image',
  images: [image]
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DTO

#==, #as_array, #as_array_of_hashes, #as_hash, #as_json, #empty?, #to_json

Constructor Details

#initialize(role:, content:, thinking: nil, images: nil, tool_calls: nil) ⇒ Message

The initialize method sets up a new Message instance with the specified attributes.

Parameters:

  • role (String)

    the role of the message sender, such as ‘user’ or ‘assistant’

  • content (String)

    the textual content of the message

  • thinking (String, nil) (defaults to: nil)

    optional thinking content for the message

  • images (Ollama::Image, Array<Ollama::Image>, nil) (defaults to: nil)

    optional image objects associated with the message

  • tool_calls (Hash, Array<Hash>, nil) (defaults to: nil)

    optional tool calls made in the message



51
52
53
54
55
# File 'lib/ollama/message.rb', line 51

def initialize(role:, content:, thinking: nil, images: nil, tool_calls: nil, **)
  @role, @content, @thinking, @images, @tool_calls =
    role, content, thinking, (Array(images) if images),
    (Array(tool_calls) if tool_calls)
end

Instance Attribute Details

#contentString (readonly)

The content attribute reader returns the textual content of the message.

Returns:

  • (String)

    the content of the message



32
33
34
# File 'lib/ollama/message.rb', line 32

def content
  @content
end

#imagesArray<Ollama::Image>? (readonly)

The images attribute reader returns the image objects associated with the message.

Returns:

  • (Array<Ollama::Image>, nil)

    an array of image objects, or nil if no images are associated with the message



42
43
44
# File 'lib/ollama/message.rb', line 42

def images
  @images
end

#roleString (readonly)

The role attribute reader returns the role associated with the message.

Returns:

  • (String)

    the role of the message sender, such as ‘user’ or ‘assistant’



27
28
29
# File 'lib/ollama/message.rb', line 27

def role
  @role
end

#thinkingString? (readonly)

The thinking attribute reader returns the thinking content associated with the message.

Returns:

  • (String, nil)

    the thinking content of the message, or nil if not set



37
38
39
# File 'lib/ollama/message.rb', line 37

def thinking
  @thinking
end