Skip to content
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

initial checkin for vision support with tests #2240

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

montiblanc97
Copy link

@montiblanc97 montiblanc97 commented Dec 12, 2024

Please describe the purpose of this pull request.
Add vision support when using OpenAI models

  • both image URL and base64-encoded images
  • tests

Remaining work before merge:

  • Renaming Message.text -> Message.content which is used in a lot of places
  • Updating the ORM Message object which is typed str and requires migration.
    • Current behavior is just ignoring the images and storing the text
  • CLI support (ollama-like). Parse any local file paths in CLI text input and treat them as image uploads
  • Solution to saving base64 images. They probably should not go in the Postgres DB
    • We can store uploaded images to cloud blob storage (S3, GCP) then replace images in the messages with S3 urls
    • For local servers: min.io, open-source S3 alternative with same API
      • Deployed via docker-compose. Can wrap under letta_server like PG or separate service
      • API is same, so the only differences between cloud/local server will be some config values if using AWS
    • Sarah also suggested an alternative for local dev which is saving local file paths. Much simpler

Extensions:

  • Model switching to non-multimodal models, when the history contains images. Ideas:
    • Captioning model fallback
    • Insertion of "unavailable image" or something along those lines

How to test
How can we test your PR during review? What commands should we run? What outcomes should we expect?

Unit tests included

Have you tested this PR?
Have you tested the latest commit on the PR? If so please provide outputs from your tests.

Unit tests pass

Related issues or PRs
Please link any related GitHub issues or PRs.

Is your PR over 500 lines of code?
If so, please break up your PR into multiple smaller PRs so that we can review them quickly, or provide justification for its length.

Additional context
Add any other context or screenshots about the PR here.

@@ -152,10 +152,11 @@ def send_message(
stream: Optional[bool] = False,
stream_steps: bool = False,
stream_tokens: bool = False,
image: Optional[str] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the ideal way to do this is we actually make message (currently str) be type MultiMediaContent, which itself is Union[str, List[MultiMediaContentPart]]

Then we will be able to support multiple images and future modalities like audio.

Copy link
Author

Choose a reason for hiding this comment

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

sounds good, updated. see message in your other comment

@@ -421,8 +423,22 @@ def to_openai_dict(

elif self.role == "user":
assert all([v is not None for v in [self.text, self.role]]), vars(self)

if self.image is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

So inside of Message, .text becomes .content, which basically is a 1:1 mapping to OpenAI's format:
image

https://platform.openai.com/docs/api-reference/chat/create

Copy link
Author

Choose a reason for hiding this comment

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

agree with this and made the initial typing changes/enough to pass tests. However left off before two major changes:

  1. Renaming Message.text -> Message.content which is used in a lot of places
  2. Updating the ORM Message object which is typed str and requires migration. Tricky because images probably shouldn't be stored raw in the DB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants