Repository navigation
Accept images in decisions #338
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
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| """Ask decision questions about images with OpenAI ``gpt-6-luna``. | ||
|
|
||
| Set ``OPENAI_API_KEY`` and run: | ||
|
|
||
| uv run python examples/models/openai/decisions_images.py | ||
|
|
||
| OpenAI decisions take images in two places: | ||
|
|
||
| * in the state, as a list of text and file parts (one user message), and | ||
| * in a question's instructions, to give one question its own image. | ||
|
|
||
| OpenAI does not accept images nested inside JSON state or in criteria. | ||
| """ | ||
|
|
||
| import asyncio | ||
| import pathlib | ||
|
|
||
| import pydantic | ||
|
|
||
| import ai | ||
|
|
||
|
|
||
| class DeliveryQuestions(pydantic.BaseModel): | ||
| damaged: ai.ops.experimental.PredicateQuestion | ||
| condition: ai.ops.experimental.ScoreQuestion | ||
| matches_reference: ai.ops.experimental.PredicateQuestion | ||
|
|
||
|
|
||
| class DeliveryAnswers(pydantic.BaseModel): | ||
| damaged: ai.ops.experimental.PredicateAnswer | ||
| condition: ai.ops.experimental.ScoreAnswer | ||
| matches_reference: ai.ops.experimental.PredicateAnswer | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| model = ai.get_model("openai:gpt-6-luna") | ||
| if not model.provider.is_configured(): | ||
| print("Set OPENAI_API_KEY to run this example.") | ||
| return | ||
|
|
||
| image_path = ( | ||
| pathlib.Path(__file__).parents[2] / "media" / "sample_image.jpg" | ||
| ) | ||
|
|
||
| # Shared evidence for all questions: text and an image, like the content | ||
| # of a user message. | ||
| state: ai.ops.experimental.DecisionInput = [ | ||
| "A customer uploaded this photo of a delivered item. This is photo A.", | ||
| ai.file_part(image_path.read_bytes()), | ||
| ] | ||
|
|
||
| result = await ai.ops.experimental.decide( | ||
| model, | ||
| state, | ||
| DeliveryQuestions( | ||
| damaged=ai.ops.experimental.PredicateQuestion( | ||
| instructions=( | ||
| "Does the item in photo A have visible damage, such as a " | ||
| "crack, tear, or dent? Ignore shadows." | ||
| ), | ||
| ), | ||
| condition=ai.ops.experimental.ScoreQuestion( | ||
| instructions="What is the condition of the item in photo A?", | ||
| criteria=["Broken", "Worn", "Like new"], | ||
| ), | ||
| # This question has its own image. Here it is the same file, so | ||
| # the answer should be close to 100%. | ||
| matches_reference=ai.ops.experimental.PredicateQuestion( | ||
| instructions=[ | ||
| "Is candidate photo B a copy or variant of photo A, " | ||
| "including recompression or resizing?", | ||
| ai.file_part(image_path.read_bytes()), | ||
| ], | ||
| ), | ||
| ), | ||
| output_type=DeliveryAnswers, | ||
| ) | ||
|
|
||
| answers = result.value | ||
| print("damaged:", f"{answers.damaged.probability:.0%}") | ||
| print("condition:", answers.condition.score) | ||
| print("matches reference:", f"{answers.matches_reference.probability:.0%}") | ||
| print("usage:", result.usage) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
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,97 @@ | ||
| """Ask decision questions about images with TypeSafe. | ||
|
|
||
| Set ``TYPESAFE_API_KEY`` and run: | ||
|
|
||
| uv run python examples/models/typesafe/decisions_images.py | ||
|
|
||
| Images are file parts. TypeSafe accepts them anywhere text can go: inside | ||
| JSON state, in a question's instructions, and in its criteria. Questions | ||
| refer to state fields by name in backticks. | ||
| """ | ||
|
|
||
| import asyncio | ||
| import pathlib | ||
|
|
||
| import pydantic | ||
|
|
||
| import ai | ||
| from ai.providers.typesafe import TypeSafeProvider | ||
|
|
||
|
|
||
| class ListingQuestions(pydantic.BaseModel): | ||
| matches_listing: ai.ops.experimental.PredicateQuestion | ||
| lighting: ai.ops.experimental.ChoiceQuestion | ||
| photo_quality: ai.ops.experimental.ScoreQuestion | ||
|
|
||
|
|
||
| class ListingAnswers(pydantic.BaseModel): | ||
| matches_listing: ai.ops.experimental.PredicateAnswer | ||
| lighting: ai.ops.experimental.ChoiceAnswer | ||
| photo_quality: ai.ops.experimental.ScoreAnswer | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| model = ai.Model(id="jev-latest", provider=TypeSafeProvider()) | ||
| if not model.provider.is_configured(): | ||
| print("Set TYPESAFE_API_KEY to run this example.") | ||
| return | ||
|
|
||
| image_path = ( | ||
| pathlib.Path(__file__).parents[2] / "media" / "sample_image.jpg" | ||
| ) | ||
| photo = ai.file_part(image_path.read_bytes(), media_type="image/jpeg") | ||
|
|
||
| # The image sits inside structured state, next to the data it belongs to. | ||
| listing: ai.ops.experimental.DecisionInput = { | ||
| "listing": { | ||
| "title": "Scenic print, framed", | ||
| "price_usd": 120, | ||
| "photo": photo, | ||
| }, | ||
| "buyer_report": { | ||
| "message": "This is what arrived.", | ||
| "photo": photo, | ||
| }, | ||
| } | ||
|
|
||
| result = await ai.ops.experimental.decide( | ||
| model, | ||
| listing, | ||
| ListingQuestions( | ||
| matches_listing=ai.ops.experimental.PredicateQuestion( | ||
| instructions=( | ||
| "Does `buyer_report.photo` show the same item as " | ||
| "`listing.photo`?" | ||
| ), | ||
| ), | ||
| lighting=ai.ops.experimental.ChoiceQuestion( | ||
| instructions="What is the lighting in `listing.photo` like?", | ||
| criteria={ | ||
| "daylight": "Natural daylight", | ||
| "artificial": "Lamps or other artificial light", | ||
| "dark": "Little or no light", | ||
| }, | ||
| ), | ||
| # A criterion can be an image: here, the listing photo is the | ||
| # example of the top quality level. | ||
| photo_quality=ai.ops.experimental.ScoreQuestion( | ||
| instructions="How good is `buyer_report.photo` as evidence?", | ||
| criteria=[ | ||
| "Unusable: blurry, dark, or the item is not visible", | ||
| "Usable: the item is visible but details are unclear", | ||
| ["As clear as this reference photo:", photo], | ||
| ], | ||
| ), | ||
| ), | ||
| output_type=ListingAnswers, | ||
| ) | ||
|
|
||
| answers = result.value | ||
| print("matches listing:", f"{answers.matches_listing.probability:.0%}") | ||
| print("lighting:", answers.lighting.choice) | ||
| print("photo quality:", answers.photo_quality.score) | ||
| print("usage:", result.usage) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
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
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
Oops, something went wrong.
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.
Hmmmm: will serializing something with a FilePart and then deserializing it back have any way to preserve the file part? Probably not, right?
But I guess that's kind of inevitable without more of a schema for it...