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

Add support for GPT-4 Vision #17

Merged
merged 7 commits into from
Apr 13, 2024
Merged

Conversation

ronaldmannak
Copy link
Contributor

This PR adds support for GPT-4 Vision. Images can be added to a message as a URL or as Data.

The token counter for small images adds the correct amount of tokens. However, large images have a complex way of counting tokens and require the library to download the image. To keep things simple, this PR just adds the maximum number of tokens possible. Maybe a feature could be added in the future that optionally downloads the images and counts the tokens the right way.

let media = [
  MessageContent.text("Count the number of apples in this image"),   
  MessageContent.imageUrl("https://apples.com/apples.jpg"),
 ]
thread.addUserMessage(_ media: media) 

or

let imageData = UIImage(named: "apples.jpg").jpegData(compressionQuality: 1.0) 
let media = [
  MessageContent.text("Count the number of apples in this image"), 
  MessageContent.imageUrl(URLDetails(imageData: imageData))]
thread.addUserMessage(_ media: media) 

There is one breaking change. The content property in ChatMessage is now an enum and invoking description might be needed to fetch the string, as was the case in one of the unit tests:

  XCTAssertEqual(userMessageContent, chatThread.getNonSystemMessages().first?.content) // old
  XCTAssertEqual(userMessageContent, chatThread.getNonSystemMessages().first?.content?.description) // new

@btfranklin btfranklin merged commit cf2f607 into btfranklin:main Apr 13, 2024
1 check passed
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.

2 participants