Skip to content

Commit caf03b0

Browse files
committed
Get generated images from tool in non-streaming call
1 parent 52bfa72 commit caf03b0

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

lib/ruby_llm/providers/openai/response.rb

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def parse_respond_response(response)
9393

9494
Message.new(
9595
role: :assistant,
96-
content: all_output_text(outputs),
96+
content: all_output_content(outputs),
9797
tool_calls: parse_response_tool_calls(outputs),
9898
input_tokens: data['usage']['input_tokens'],
9999
output_tokens: data['usage']['output_tokens'],
@@ -103,6 +103,47 @@ def parse_respond_response(response)
103103
)
104104
end
105105

106+
def all_output_content(outputs)
107+
text_content = extract_text_content(outputs)
108+
image_outputs = outputs.select { |o| o['type'] == 'image_generation_call' }
109+
110+
return text_content unless image_outputs.any?
111+
112+
build_content_with_images(text_content, image_outputs)
113+
end
114+
115+
private
116+
117+
def extract_text_content(outputs)
118+
outputs.select { |o| o['type'] == 'message' }.flat_map do |o|
119+
o['content'].filter_map do |c|
120+
c['type'] == 'output_text' && c['text']
121+
end
122+
end.join("\n")
123+
end
124+
125+
def build_content_with_images(text_content, image_outputs)
126+
content = RubyLLM::Content.new(text_content)
127+
image_outputs.each do |output|
128+
attach_image_to_content(content, output)
129+
end
130+
content
131+
end
132+
133+
def attach_image_to_content(content, output)
134+
image_data = output['result']
135+
output_format = output['output_format'] || 'png'
136+
mime_type = "image/#{output_format}"
137+
138+
content.attach(
139+
RubyLLM::ImageAttachment.new(
140+
data: image_data,
141+
mime_type: mime_type,
142+
model_id: nil
143+
)
144+
)
145+
end
146+
106147
def all_output_text(outputs)
107148
outputs.select { |o| o['type'] == 'message' }.flat_map do |o|
108149
o['content'].filter_map do |c|

lib/ruby_llm/providers/openai/tools.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def parse_tool_calls(tool_calls, parse_arguments: true)
8383

8484
def parse_response_tool_calls(outputs)
8585
# TODO: implement the other & built-in tools
86-
# 'web_search_call', 'file_search_call', 'image_generation_call',
86+
# 'web_search_call', 'file_search_call',
8787
# 'code_interpreter_call', 'local_shell_call', 'mcp_call',
8888
# 'mcp_list_tools', 'mcp_approval_request'
8989
outputs.select { |o| o['type'] == 'function_call' }.to_h do |o|

0 commit comments

Comments
 (0)