-
Notifications
You must be signed in to change notification settings - Fork 230
Implement Structured Chat Messages #257
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
Merged
davidkoski
merged 16 commits into
ml-explore:main
from
ibrahimcetin:structured-messages
Apr 18, 2025
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
016af9f
Initial implementation for structured chat messages
ibrahimcetin 52c568e
Move Qwen2VLMessageGenerator
ibrahimcetin 0b0f267
Do not require images and videos in init(messages:)
ibrahimcetin 47ea79e
Refactor message generation to use unified generate(from:) method
ibrahimcetin c723f56
Use Qwen2VLMessageGenerator temporarily
ibrahimcetin f8357cf
swift-format
davidkoski 6826c78
add stub tests
davidkoski f08fbee
add documentation, images/video are live
davidkoski d3518e4
convert this to the new structured format
davidkoski e450998
fix #270
davidkoski 3b49a8d
update qwen2.5 with structured message change
davidkoski 5f439e9
implement tests
davidkoski a7e98dc
add scheme
davidkoski f55502c
correct scheme name
davidkoski a2dd872
adjust deployment targets to run on CI
davidkoski 0015016
address PR feedback
davidkoski 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
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,114 @@ | ||
// Copyright © 2025 Apple Inc. | ||
|
||
public enum Chat { | ||
public struct Message { | ||
/// The role of the message sender. | ||
public var role: Role | ||
|
||
/// The content of the message. | ||
public var content: String | ||
|
||
/// Array of image data associated with the message. | ||
public var images: [UserInput.Image] | ||
|
||
/// Array of video data associated with the message. | ||
public var videos: [UserInput.Video] | ||
|
||
public init( | ||
role: Role, content: String, images: [UserInput.Image] = [], | ||
videos: [UserInput.Video] = [] | ||
) { | ||
self.role = role | ||
self.content = content | ||
self.images = images | ||
self.videos = videos | ||
} | ||
|
||
public static func system( | ||
_ content: String, images: [UserInput.Image] = [], videos: [UserInput.Video] = [] | ||
) -> Self { | ||
Self(role: .system, content: content, images: images, videos: videos) | ||
} | ||
|
||
public static func assistant( | ||
_ content: String, images: [UserInput.Image] = [], videos: [UserInput.Video] = [] | ||
) -> Self { | ||
Self(role: .assistant, content: content, images: images, videos: videos) | ||
} | ||
|
||
public static func user( | ||
_ content: String, images: [UserInput.Image] = [], videos: [UserInput.Video] = [] | ||
) -> Self { | ||
Self(role: .user, content: content, images: images, videos: videos) | ||
} | ||
|
||
public enum Role: String { | ||
case user | ||
case assistant | ||
case system | ||
} | ||
} | ||
} | ||
|
||
/// Protocol for something that can convert structured | ||
/// ``Chat.Message`` into model specific ``Message`` | ||
/// (raw dictionary) format. | ||
/// | ||
/// Typically this is owned and used by a ``UserInputProcessor``: | ||
/// | ||
/// ```swift | ||
/// public func prepare(input: UserInput) async throws -> LMInput { | ||
/// let messages = Qwen2VLMessageGenerator().generate(from: input) | ||
/// ... | ||
/// ``` | ||
public protocol MessageGenerator { | ||
|
||
/// Returns `[String: Any]` aka ``Message``. | ||
func generate(message: Chat.Message) -> Message | ||
} | ||
|
||
extension MessageGenerator { | ||
/// Returns array of `[String: Any]` aka ``Message`` | ||
public func generate(messages: [Chat.Message]) -> [Message] { | ||
var rawMessages: [Message] = [] | ||
|
||
for message in messages { | ||
let raw = generate(message: message) | ||
rawMessages.append(raw) | ||
} | ||
|
||
return rawMessages | ||
} | ||
|
||
/// Generates messages from the input. | ||
public func generate(from input: UserInput) -> [Message] { | ||
switch input.prompt { | ||
case .text(let text): | ||
generate(messages: [.user(text)]) | ||
case .messages(let messages): | ||
messages | ||
case .chat(let messages): | ||
generate(messages: messages) | ||
} | ||
} | ||
} | ||
|
||
/// Default implementation of ``MessageGenerator`` that produces a | ||
/// `role` and `content`. | ||
/// | ||
/// ```swift | ||
/// [ | ||
/// "role": message.role.rawValue, | ||
/// "content": message.content, | ||
/// ] | ||
/// ``` | ||
public struct DefaultMessageGenerator: MessageGenerator { | ||
public init() {} | ||
|
||
public func generate(message: Chat.Message) -> Message { | ||
[ | ||
"role": message.role.rawValue, | ||
"content": message.content, | ||
] | ||
} | ||
} |
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.
I added some documentation