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, tool_name: 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



63
64
65
66
67
68
# File 'lib/ollama/message.rb', line 63

def initialize(role:, content:, thinking: nil, images: nil, tool_calls: nil, tool_name: nil, **)
  @role, @content, @thinking, @images, @tool_calls, @tool_name =
    role, content, thinking, (Array(images) if images),
    (Array(tool_calls) if tool_calls),
    tool_name
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

#tool_callsArray<Hash>? (readonly)

The tool_calls attribute reader returns the tool call hashes or objects associated with the message.

Returns:

  • (Array<Hash>, nil)

    an array of tool call definitions, or nil if none



48
49
50
# File 'lib/ollama/message.rb', line 48

def tool_calls
  @tool_calls
end

#tool_nameString? (readonly)

The tool_name attribute reader returns the name of the tool used in the message, if any.

Returns:

  • (String, nil)

    the name of the tool, or nil if not set



54
55
56
# File 'lib/ollama/message.rb', line 54

def tool_name
  @tool_name
end