Skip to content

Fix Assistant OpenAI adapter to handle message content structure returned by to_hash method #952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/langchain/assistant/llm/adapters/openai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,19 @@ def build_chat_params(
# Build a OpenAI message
#
# @param role [String] The role of the message
# @param content [String] The content of the message
# @param content [String | Array<Hash>] The content of the message
# @param image_url [String] The image URL
# @param tool_calls [Array] The tool calls
# @param tool_call_id [String] The tool call ID
# @return [Messages::OpenAIMessage] The OpenAI message
def build_message(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil)
if content.is_a?(Array)
content.each do |c|
content = c[:text] if c[:type] == "text"
image_url = c[:image_url][:url] if c[:type] == "image_url"
end
end

Messages::OpenAIMessage.new(role: role, content: content, image_url: image_url, tool_calls: tool_calls, tool_call_id: tool_call_id)
end

Expand Down
29 changes: 29 additions & 0 deletions spec/langchain/assistant/llm/adapters/openai_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,33 @@
expect(subject.tool_role).to eq("tool")
end
end

describe "#build_message" do
context "when content is a string" do
it "returns the OpenAI message" do
expect(
subject.build_message(
role: "user",
content: "Hello",
image_url: "https://example.com/image.png",
tool_calls: [{"id" => "tool_call_id", "function" => {"name" => "langchain_tool_calculator__execute", "arguments" => "{\"a\": 1, \"b\": 2}"}}],
tool_call_id: "tool_call_id"
)
).to have_attributes(role: "user", content: "Hello", image_url: "https://example.com/image.png", tool_calls: [{"id" => "tool_call_id", "function" => {"name" => "langchain_tool_calculator__execute", "arguments" => "{\"a\": 1, \"b\": 2}"}}], tool_call_id: "tool_call_id")
end
end

context "when content is an array" do
it "returns the OpenAI message" do
expect(
subject.build_message(
role: "user",
content: [{type: "text", text: "Hello"}, {type: "image_url", image_url: {url: "https://example.com/image.png"}}],
tool_calls: [{"id" => "tool_call_id", "function" => {"name" => "langchain_tool_calculator__execute", "arguments" => "{\"a\": 1, \"b\": 2}"}}],
tool_call_id: "tool_call_id"
)
).to have_attributes(role: "user", content: "Hello", image_url: "https://example.com/image.png", tool_calls: [{"id" => "tool_call_id", "function" => {"name" => "langchain_tool_calculator__execute", "arguments" => "{\"a\": 1, \"b\": 2}"}}], tool_call_id: "tool_call_id")
end
end
end
end