Skip to content
Merged
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
6 changes: 6 additions & 0 deletions aidialog/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@
'aidialog.msg_parts.InputImage': ('msg_parts.html#inputimage', 'aidialog/msg_parts.py'),
'aidialog.msg_parts.InputAudio': ('msg_parts.html#inputaudio', 'aidialog/msg_parts.py'),
'aidialog.msg_parts.InputFile': ('msg_parts.html#inputfile', 'aidialog/msg_parts.py'),
'aidialog.msg_parts.OutputImage': ('msg_parts.html#outputimage', 'aidialog/msg_parts.py'),
'aidialog.msg_parts.InputVideo': ('msg_parts.html#inputvideo', 'aidialog/msg_parts.py'),
'aidialog.msg_parts.InputVideo.__init__': ( 'msg_parts.html#inputvideo.__init__',
'aidialog/msg_parts.py'),
Expand Down Expand Up @@ -275,6 +276,11 @@
'aidialog.msg_parts.ToolResult.doc': ('msg_parts.html#toolresult.doc', 'aidialog/msg_parts.py'),
'aidialog.msg_parts.Part.ctext': ('msg_parts.html#part.ctext', 'aidialog/msg_parts.py'),
'aidialog.msg_parts.Media.ctext': ('msg_parts.html#media.ctext', 'aidialog/msg_parts.py'),
'aidialog.msg_parts.OutputImage.formatted': ( 'msg_parts.html#outputimage.formatted',
'aidialog/msg_parts.py'),
'aidialog.msg_parts.OutputImage.doc': ('msg_parts.html#outputimage.doc', 'aidialog/msg_parts.py'),
'aidialog.msg_parts.OutputImage._repr_markdown_': ( 'msg_parts.html#outputimage._repr_markdown_',
'aidialog/msg_parts.py'),
'aidialog.msg_parts.hist2fmt': ('msg_parts.html#hist2fmt', 'aidialog/msg_parts.py'),
'aidialog.msg_parts.mk_msg': ('msg_parts.html#mk_msg', 'aidialog/msg_parts.py'),
'aidialog.msg_parts.mk_msgs': ('msg_parts.html#mk_msgs', 'aidialog/msg_parts.py')}},
Expand Down
17 changes: 13 additions & 4 deletions aidialog/msg_parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

# %% auto #0
__all__ = ['PartType', 'tool_info', 'think_start', 'think_end', 'fence_call_re', 'TOOL_RESPONSE_MIME', 'Part', 'Text', 'Thinking',
'Refusal', 'Media', 'InputImage', 'InputAudio', 'InputFile', 'InputVideo', 'mk_part', 'Msg', 'msg2dict',
'dict2msg', 'ToolUse', 'ToolResult', 'display_list', 'Completion', 'mk_tool_res_msg', 'sys_text', 'part_txt',
'data_url', 'url_mime', 'MediaUrl', 'mk_content', 'extract_fence_call', 'mk_result_fence',
'Refusal', 'Media', 'InputImage', 'InputAudio', 'InputFile', 'OutputImage', 'InputVideo', 'mk_part', 'Msg',
'msg2dict', 'dict2msg', 'ToolUse', 'ToolResult', 'display_list', 'Completion', 'mk_tool_res_msg', 'sys_text',
'part_txt', 'data_url', 'url_mime', 'MediaUrl', 'mk_content', 'extract_fence_call', 'mk_result_fence',
'split_fence_msgs', 'tool_text', 'resp_part', 'ToolResponse', 'StopResponse', 'FullResponse', 'MdStr',
'trunc_str', 'mk_tr_details', 'hist2fmt', 'mk_msg', 'mk_msgs']

Expand Down Expand Up @@ -41,7 +41,7 @@ def replace(self, **kw):

# %% ../nbs/00_msg_parts.ipynb #5342db55
PartType = str_enum('PartType', 'text', 'thinking', 'refusal', 'tool_use', 'tool_result',
'input_image', 'input_audio', 'input_video', 'input_file')
'input_image', 'input_audio', 'input_video', 'input_file', 'output_image')

# %% ../nbs/00_msg_parts.ipynb #48496c08
class Text(Part, tag=PartType.text):
Expand Down Expand Up @@ -74,6 +74,7 @@ def __init__(self, text=None, mime=None, **kw):
class InputImage(Media, tag=PartType.input_image): "An image input."
class InputAudio(Media, tag=PartType.input_audio): "An audio input."
class InputFile (Media, tag=PartType.input_file ): "A file input."
class OutputImage(Media, tag=PartType.output_image): "A generated image output."

class InputVideo(Media, tag=PartType.input_video):
"A video input; `start`/`end` clip it to a range and `fps` sets the sampled frame rate, for providers that support them"
Expand Down Expand Up @@ -447,6 +448,14 @@ def ctext(self:Part): return self.formatted
@patch(as_prop=True)
def ctext(self:Media): return _media_tag(self)

# %% ../nbs/00_msg_parts.ipynb #e75741cc
@patch(as_prop=True)
def formatted(self:OutputImage): return self.ctext

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid returning the huge data:image/png;base64,... string and returns ctext, which in this case will be <media output_image image/png>

@patch
def doc(self:OutputImage, showthink=False, mx=2000): return f'![]({self.text})' if self.text else ''

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return the image as ![](data:...) so it renders properly in solveit after hist2fmt

@patch
def _repr_markdown_(self:OutputImage): return f'![]({self.text})' if self.text else self.ctext

# %% ../nbs/00_msg_parts.ipynb #e5acd16a
def hist2fmt(msgs:list[Msg], mx=2000, showthink=False)->str:
"Render assistant and tool messages as one editable Markdown reply."
Expand Down
48 changes: 46 additions & 2 deletions nbs/00_msg_parts.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
"outputs": [],
"source": [
"PartType = str_enum('PartType', 'text', 'thinking', 'refusal', 'tool_use', 'tool_result',\n",
" 'input_image', 'input_audio', 'input_video', 'input_file')"
" 'input_image', 'input_audio', 'input_video', 'input_file', 'output_image')"
]
},
{
Expand Down Expand Up @@ -235,7 +235,7 @@
"id": "07b19a34",
"metadata": {},
"source": [
"Choose the subclass for the media you want to send:"
"Choose the subclass for the media you send or receive:"
]
},
{
Expand All @@ -253,6 +253,7 @@
"class InputImage(Media, tag=PartType.input_image): \"An image input.\"\n",
"class InputAudio(Media, tag=PartType.input_audio): \"An audio input.\"\n",
"class InputFile (Media, tag=PartType.input_file ): \"A file input.\"\n",
"class OutputImage(Media, tag=PartType.output_image): \"A generated image output.\"\n",
"\n",
"class InputVideo(Media, tag=PartType.input_video):\n",
" \"A video input; `start`/`end` clip it to a range and `fps` sets the sampled frame rate, for providers that support them\"\n",
Expand Down Expand Up @@ -1961,6 +1962,49 @@
"test_eq(Text('hi').formatted, 'hi')"
]
},
{
"cell_type": "markdown",
"id": "e84a58a0",
"metadata": {},
"source": [
"`OutputImage.formatted` returns its `<media>` tag (`ctext`) instead of streaming raw image data. `doc` and `_repr_markdown_` render a Markdown image when `text` is set."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e75741cc",
"metadata": {
"nbdev": {
"export": "true"
}
},
"outputs": [],
"source": [
"@patch(as_prop=True)\n",
"def formatted(self:OutputImage): return self.ctext\n",
"@patch\n",
"def doc(self:OutputImage, showthink=False, mx=2000): return f'![]({self.text})' if self.text else ''\n",
"@patch\n",
"def _repr_markdown_(self:OutputImage): return f'![]({self.text})' if self.text else self.ctext"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2cff6465",
"metadata": {},
"outputs": [],
"source": [
"out_img = OutputImage('data:image/png;base64,xxx', mime='image/png')\n",
"test_eq(out_img.formatted, '<media output_image image/png>')\n",
"test_eq(out_img.doc(), '![](data:image/png;base64,xxx)')\n",
"test_eq(out_img._repr_markdown_(), '![](data:image/png;base64,xxx)')\n",
"test_eq(OutputImage(mime='image/png').doc(), '')\n",
"test_eq(OutputImage(mime='image/png')._repr_markdown_(), '<media output_image image/png>')\n",
"test_eq(mk_part(**_part2dict(out_img)), out_img)"
]
},
Comment on lines +1998 to +2007

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test checks plain-text formatting, Markdown rendering, empty-data fallbacks, and round-trip serialization.

{
"cell_type": "markdown",
"id": "bdf50d83",
Expand Down