Repository navigation
Add OutputImage message part for generated images #76
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'] | ||
|
|
||
|
|
@@ -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): | ||
|
|
@@ -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" | ||
|
|
@@ -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 | ||
| @patch | ||
| def doc(self:OutputImage, showthink=False, mx=2000): return f'' if self.text else '' | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return the image as |
||
| @patch | ||
| def _repr_markdown_(self:OutputImage): return f'' 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." | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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')" | ||
| ] | ||
| }, | ||
| { | ||
|
|
@@ -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:" | ||
| ] | ||
| }, | ||
| { | ||
|
|
@@ -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", | ||
|
|
@@ -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'' if self.text else ''\n", | ||
| "@patch\n", | ||
| "def _repr_markdown_(self:OutputImage): return f'' 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(), '')\n", | ||
| "test_eq(out_img._repr_markdown_(), '')\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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
|
|
||
There was a problem hiding this comment.
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>