Class: Crimson::Message::Assistant

Inherits:
Base
  • Object
show all
Defined in:
lib/crimson/message.rb

Overview

An assistant (model) response, optionally containing tool calls.

Instance Attribute Summary collapse

Attributes inherited from Base

#role

Instance Method Summary collapse

Constructor Details

#initialize(content: nil, tool_calls: []) ⇒ Assistant

Returns a new instance of Assistant.

Parameters:

  • content (String, nil) (defaults to: nil)
  • tool_calls (Array<ToolCall>) (defaults to: [])


72
73
74
75
76
# File 'lib/crimson/message.rb', line 72

def initialize(content: nil, tool_calls: [])
  super("assistant")
  @content = content
  @tool_calls = tool_calls
end

Instance Attribute Details

#contentString, ... (readonly)

Returns:

  • (String, nil)

    the text content

  • (Array<ToolCall>)

    any tool calls requested



68
69
70
# File 'lib/crimson/message.rb', line 68

def content
  @content
end

#tool_callsString, ... (readonly)

Returns:

  • (String, nil)

    the text content

  • (Array<ToolCall>)

    any tool calls requested



68
69
70
# File 'lib/crimson/message.rb', line 68

def tool_calls
  @tool_calls
end

Instance Method Details

#to_anthropic_hHash

Returns Anthropic-compatible representation.

Returns:

  • (Hash)

    Anthropic-compatible representation



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/crimson/message.rb', line 92

def to_anthropic_h
  content_blocks = []
  content_blocks << { type: "text", text: @content } if @content && !@content.empty?
  @tool_calls.each do |tc|
    content_blocks << {
      type: "tool_use",
      id: tc.id,
      name: tc.name,
      input: tc.arguments
    }
  end
  { role: "assistant", content: content_blocks }
end

#to_openai_hHash

Returns OpenAI-compatible representation.

Returns:

  • (Hash)

    OpenAI-compatible representation



84
85
86
87
88
89
# File 'lib/crimson/message.rb', line 84

def to_openai_h
  h = { role: "assistant" }
  h[:content] = @content if @content
  h[:tool_calls] = @tool_calls.map(&:to_openai_h) if tool_call?
  h
end

#tool_call?Boolean

Returns whether this message contains tool calls.

Returns:

  • (Boolean)

    whether this message contains tool calls



79
80
81
# File 'lib/crimson/message.rb', line 79

def tool_call?
  !@tool_calls.nil? && !@tool_calls.empty?
end