Repository navigation
Add image generation support #102
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
dannyjameswilliams
wants to merge
9
commits into
main
Choose a base branch
from
feat/image_generation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1458181
Add image generation support
dannyjameswilliams c148865
allow annotated at root of output_format
dannyjameswilliams a2721e4
Merge branch 'main' into feat/image_generation
dannyjameswilliams 0c1eed5
reduce timeout -> 60s
dannyjameswilliams fd74ffb
Merge branch 'feat/image_generation' of https://github.com/weaviate/w…
dannyjameswilliams 55abdb1
rename QAImage -> GeneratedImage
dannyjameswilliams 4c52421
rename ImageOptions -> GeneratedImageOptions
dannyjameswilliams 8d11571
remove timeout increase
dannyjameswilliams c01b8f5
remove refernece to root Annotated field
dannyjameswilliams File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from dataclasses import dataclass | ||
| from typing import Optional | ||
|
|
||
| from pydantic import BaseModel, ConfigDict, GetJsonSchemaHandler | ||
| from pydantic.json_schema import JsonSchemaValue, SkipJsonSchema | ||
| from pydantic_core import core_schema | ||
| from typing_extensions import Literal | ||
|
|
||
| IMAGE_KEYWORD = "X-query-agent-image" | ||
|
|
||
| ImageShape = Literal["square", "landscape", "portrait"] | ||
|
|
||
|
|
||
| class GeneratedImage(BaseModel): | ||
| model_config = ConfigDict(json_schema_extra={IMAGE_KEYWORD: True}) | ||
|
|
||
| image_prompt: str | ||
| base64: SkipJsonSchema[str] # hidden from the LLM's schema; server fills it | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class GeneratedImageOptions: | ||
| shape: Optional[ImageShape] = None # unset falls back to the backend default | ||
|
|
||
| def __get_pydantic_json_schema__( | ||
| self, core_schema_: core_schema.CoreSchema, handler: GetJsonSchemaHandler | ||
| ) -> JsonSchemaValue: | ||
| schema = handler(core_schema_) | ||
| if self.shape is not None: | ||
| schema["X-image-shape"] = self.shape | ||
| return schema |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This works, but you could avoid this special case handling and also the user having to import and use
Annotatedby using a class method on theGeneratedImagetype, something likeso that
works the same as using the default shape. Optional suggestion 🙂
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.
I like this idea, it is a lot neater, though it needs a
# type: ignore[valid-type,misc]to avoid mypy not liking the inheriting ofcls.Do you think it is overkill to have both options available for the user?
Also think it would be more user-friendly to have just
i.e. add the option arguments directly rather than using
GeneratedImageOptions, though would need updating in two places when new options become available, I think this is neaterThere 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.
Yeah, mypy doesn't know what to do with the dynamic base class unfortunately. You can avoid that by using
GeneratedImage/type[GeneratedImage]in place ofcls/type[Self]so that the base class is static and mypy understands it, but then if the user subclassesGeneratedImagethemself thewith_optionswon't respect that, which is probably worse than a littletype: ignore🙂Agreed, I think you're right that's nicer! If you did that, do you need to keep the
GeneratedImageOptionstoo? Does having theAnnotatedform too have any advantages over just this classmethod? If not, I'd be tempted to remove that, and go with your options-as-args only I thinkThere 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.
You're right, the only reason I want to keep
GeneratedImageOptionsis so I don't have to change the docs again 😭, but this is nicer UI, and TS client already has a different way of doing this. I'll make the switchUh oh!
There was an error while loading. Please reload this page.
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.
I just tested it and you get typing errors like this

even though it works at runtime. So it works but there will be friction, maybe I just stick to the
Annotatedversion?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.
That's annoying 🫤
That's probably for the best, sorry for the distraction haha!
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.
Haha no worries, I liked the suggestion if it worked