Class: Langchain::Assistant::Messages::OpenAIMessage
- 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
-
#assistant? ⇒ Boolean
Check if the message came from an LLM.
-
#assistant_hash ⇒ Hash
Convert the message to an OpenAI API-compatible hash.
-
#build_content_array ⇒ Array<Hash>
Builds the content value for the message hash.
-
#initialize(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil) ⇒ OpenAIMessage
constructor
Initialize a new OpenAI message.
-
#llm? ⇒ Boolean
Check if the message came from an LLM.
-
#system? ⇒ Boolean
Check if the message are system instructions.
-
#system_hash ⇒ Hash
Convert the message to an OpenAI API-compatible hash.
-
#to_hash ⇒ Hash
Convert the message to an OpenAI API-compatible hash.
-
#tool? ⇒ Boolean
Check if the message is a tool call.
-
#tool_hash ⇒ Hash
Convert the message to an OpenAI API-compatible hash.
- #user? ⇒ Boolean
-
#user_hash ⇒ Hash
Convert the message to an OpenAI API-compatible hash.
Methods inherited from Base
Constructor Details
#initialize(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil) ⇒ OpenAIMessage
Initialize a new OpenAI message
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
67 68 69 |
# File 'lib/langchain/assistant/messages/openai_message.rb', line 67 def assistant? role == "assistant" end |
#assistant_hash ⇒ Hash
Convert the message to an OpenAI API-compatible hash
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/langchain/assistant/messages/openai_message.rb', line 91 def assistant_hash if tool_calls.any? { role: "assistant", tool_calls: tool_calls } else { role: "assistant", content: build_content_array } end end |
#build_content_array ⇒ Array<Hash>
Builds the content value for the message hash
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/langchain/assistant/messages/openai_message.rb', line 135 def build_content_array content_details = [] if content && !content.empty? content_details << { type: "text", text: content } end if image_url content_details << { type: "image_url", image_url: { url: image_url } } end content_details end |
#llm? ⇒ Boolean
Check if the message came from 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
74 75 76 |
# File 'lib/langchain/assistant/messages/openai_message.rb', line 74 def system? role == "system" end |
#system_hash ⇒ Hash
Convert the message to an OpenAI API-compatible hash
107 108 109 110 111 112 |
# File 'lib/langchain/assistant/messages/openai_message.rb', line 107 def system_hash { role: "system", content: build_content_array } end |
#to_hash ⇒ Hash
Convert the message to an OpenAI API-compatible hash
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/langchain/assistant/messages/openai_message.rb', line 52 def to_hash if assistant? assistant_hash elsif system? system_hash elsif tool? tool_hash elsif user? user_hash end end |
#tool? ⇒ Boolean
Check if the message is a tool call
81 82 83 |
# File 'lib/langchain/assistant/messages/openai_message.rb', line 81 def tool? role == "tool" end |
#tool_hash ⇒ Hash
Convert the message to an OpenAI API-compatible hash
116 117 118 119 120 121 122 |
# File 'lib/langchain/assistant/messages/openai_message.rb', line 116 def tool_hash { role: "tool", tool_call_id: tool_call_id, content: build_content_array } end |
#user? ⇒ Boolean
85 86 87 |
# File 'lib/langchain/assistant/messages/openai_message.rb', line 85 def user? role == "user" end |
#user_hash ⇒ Hash
Convert the message to an OpenAI API-compatible hash
126 127 128 129 130 131 |
# File 'lib/langchain/assistant/messages/openai_message.rb', line 126 def user_hash { role: "user", content: build_content_array } end |