Class: Langchain::Assistant::Messages::OpenAIMessage

Inherits:
Base
  • Object
show all
Defined in:
lib/langchain/assistant/messages/openai_message.rb

Constant Summary collapse

ROLES =

OpenAI uses the following roles:

[
  "system",
  "assistant",
  "user",
  "tool"
].freeze
TOOL_ROLE =
"tool"

Instance Attribute Summary

Attributes inherited from Base

#content, #image_url, #role, #tool_call_id, #tool_calls

Instance Method Summary collapse

Methods inherited from Base

#standard_role, #user?

Constructor Details

#initialize(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil) ⇒ OpenAIMessage

Initialize a new OpenAI message

Parameters:

  • role (String)

    The role of the message

  • content (String) (defaults to: nil)

    The content of the message

  • image_url (String) (defaults to: nil)

    The URL of the image

  • tool_calls (Array<Hash>) (defaults to: [])

    The tool calls made in the message

  • tool_call_id (String) (defaults to: nil)

    The ID of the tool call

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/langchain/assistant/messages/openai_message.rb', line 24

def initialize(
  role:,
  content: nil,
  image_url: nil,
  tool_calls: [],
  tool_call_id: nil
)
  raise ArgumentError, "Role must be one of #{ROLES.join(", ")}" unless ROLES.include?(role)
  raise ArgumentError, "Tool calls must be an array of hashes" unless tool_calls.is_a?(Array) && tool_calls.all? { |tool_call| tool_call.is_a?(Hash) }

  @role = role
  # Some Tools return content as a JSON hence `.to_s`
  @content = content.to_s
  @image_url = image_url
  @tool_calls = tool_calls
  @tool_call_id = tool_call_id
end

Instance Method Details

#assistant?Boolean

Check if the message came from an LLM

Returns:

  • (Boolean)

    true/false whether this message was produced by an LLM



85
86
87
# File 'lib/langchain/assistant/messages/openai_message.rb', line 85

def assistant?
  role == "assistant"
end

#llm?Boolean

Check if the message came from an LLM

Returns:

  • (Boolean)

    true/false whether this message was produced by an LLM



45
46
47
# File 'lib/langchain/assistant/messages/openai_message.rb', line 45

def llm?
  assistant?
end

#system?Boolean

Check if the message are system instructions

Returns:

  • (Boolean)

    true/false whether this message are system instructions



92
93
94
# File 'lib/langchain/assistant/messages/openai_message.rb', line 92

def system?
  role == "system"
end

#to_hashHash

Convert the message to an OpenAI API-compatible hash

Returns:

  • (Hash)

    The message as an OpenAI API-compatible hash



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
# File 'lib/langchain/assistant/messages/openai_message.rb', line 52

def to_hash
  {}.tap do |h|
    h[:role] = role

    if tool_calls.any?
      h[:tool_calls] = tool_calls
    else
      h[:tool_call_id] = tool_call_id if tool_call_id

      h[:content] = []

      if content && !content.empty?
        h[:content] << {
          type: "text",
          text: content
        }
      end

      if image_url
        h[:content] << {
          type: "image_url",
          image_url: {
            url: image_url
          }
        }
      end
    end
  end
end

#tool?Boolean

Check if the message is a tool call

Returns:

  • (Boolean)

    true/false whether this message is a tool call



99
100
101
# File 'lib/langchain/assistant/messages/openai_message.rb', line 99

def tool?
  role == "tool"
end