-
Notifications
You must be signed in to change notification settings - Fork 36
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
feat: openai vision toolkit #269
Draft
AaronGoldsmith
wants to merge
5
commits into
block:main
Choose a base branch
from
AaronGoldsmith:aarong/feat-vision-toolkit
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.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
62fbd06
initial commit with vision
AaronGoldsmith fc9fdde
update integration tests
AaronGoldsmith b1c0690
branch clean up
AaronGoldsmith 07f74de
fix pathing
AaronGoldsmith 3966947
feat: enhance image handling in message processing and tests
AaronGoldsmith 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 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 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 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 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,61 @@ | ||
# import pytest | ||
import base64 | ||
from exchange.exchange import Exchange | ||
from exchange.message import Message | ||
from exchange.content import ImageUrl | ||
from exchange.providers.openai import OpenAiProvider | ||
import httpx | ||
|
||
|
||
def test_describe_image_url(): | ||
image_url = ImageUrl(url="https://picsum.photos/id/1/200/200") | ||
user_message = Message(role="user", content=["Describe the image: ", image_url]) | ||
ex = Exchange( | ||
provider=OpenAiProvider.from_env(), | ||
model="gpt-4o", | ||
system="You are a helpful assistant.", | ||
tools=[], | ||
) | ||
ex.add(user_message) | ||
res = ex.reply() | ||
assert "laptop" in res.content[0].text | ||
|
||
|
||
def get_lorem_picsum_base64(url): | ||
image_response = httpx.get(url, follow_redirects=True) | ||
if image_response.is_redirect: | ||
redirect_url = image_response.headers["Location"] | ||
if "fastly.picsum.photos" in redirect_url: | ||
# Follow redirects to fastly | ||
image_response = httpx.get(redirect_url) | ||
|
||
image_content = image_response.content | ||
base_64_encoded_image = ( | ||
f'data:image/jpeg;base64,{base64.standard_b64encode(image_content).decode("utf-8")}' | ||
) | ||
return base_64_encoded_image | ||
|
||
|
||
def test_image_comparison_url_and_data(): | ||
|
||
base_64_image_data = get_lorem_picsum_base64("https://picsum.photos/id/1/200/200") | ||
base_64_image_url = ImageUrl(url=base_64_image_data) | ||
|
||
user_message = Message( | ||
role="user", | ||
content=[ | ||
"Are these images the same? ", | ||
ImageUrl(url=base_64_image_url), | ||
ImageUrl(url="https://picsum.photos/id/1/200/200"), | ||
], | ||
) | ||
ex = Exchange( | ||
provider=OpenAiProvider.from_env(), | ||
model="gpt-4o", | ||
system="Reply with yes or no.", | ||
tools=[], | ||
) | ||
ex.add(user_message) | ||
res = ex.reply() | ||
# The assertion below is based on comparing two identical URLs. | ||
assert "yes" in res.content[0].text.lower() |
This file contains 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 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 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,40 @@ | ||
import os | ||
from exchange.content import ImageUrl | ||
from exchange.providers.utils import encode_image | ||
from exchange.exchange import Exchange | ||
from exchange.message import Message | ||
from exchange.providers.openai import OpenAiProvider | ||
from goose.toolkit.base import Toolkit, tool | ||
|
||
class VisionToolkit(Toolkit): | ||
"""A toolkit for image analysis using AI capabilities.""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
@tool | ||
def describe_image(self, image: str, instructions: str="Describe the image") -> str: | ||
"""Analyze an image and return a description or other analysis based on the instructions. | ||
|
||
Args: | ||
image (ImageUrl): The URL or base64 encoded image to analyze. | ||
instructions (str): Instructions for the AI on what kind of analysis to perform. | ||
""" | ||
if os.path.isfile(image): | ||
encoded_image = encode_image(image) | ||
image = f"data:image/jpeg;base64,{encoded_image}" | ||
|
||
image = ImageUrl(url=image) | ||
user_message = Message(role="user", content=[f"{instructions}: ", image]) | ||
exchange = Exchange( | ||
provider=OpenAiProvider.from_env(), | ||
model="gpt-4o-mini", | ||
system="You are a helpful assistant.", | ||
messages=[user_message], | ||
tools=[], | ||
) | ||
assistant_response = exchange.reply() | ||
return assistant_response.content[0].text | ||
|
||
def system(self) -> str: | ||
return """This toolkit allows you to visually analyze images using AI capabilities.""" |
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.
Before updating
content_converter
I would get the following: