-
Notifications
You must be signed in to change notification settings - Fork 55
Implement structured output generation for both LlamaLanguageModel / MLXLanguageModel #75
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
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
ded134c
Fix SystemLanguageModel to pass schema for structured generation
eastriverlee 0fa246d
Implement logit-constrained structured generation for LlamaLanguageModel
eastriverlee be79024
Implement logit-constrained structured generation for MLXLanguageModel
eastriverlee 887964a
Fix duplicate type crash in schema generation
eastriverlee ecae1bd
Enforce count + numeric range guides
eastriverlee 9fb930b
Respect temperature for structured generation
eastriverlee 77950e3
Refactor Llama and MLX structured generation to shared constrained ge…
eastriverlee c8fdb9d
swift format -i -r .
mattt 200886b
Restore SystemLanguageModel.swift from HEAD of main
mattt 51294c6
Align structured generation prompts and defaults, and enrich schema p…
mattt 2488201
Respect schema prompt flag and enhance structured prompts with JSONSc…
mattt 04650e1
Add documentation comments to helper methods
mattt a5ccc1e
Refactor tests and fixtures
mattt 996db5a
Incorporate feedback from review
mattt 3659992
Conform internal GenerationSchema.Node to Equatable
mattt e76f048
Refactor and reorganize GenerationSchema
mattt 87ef375
Rework StructuredGeneration adding docs and tests
mattt 2564d59
Incorporate feedback from review
mattt 5e5ed58
Update GenerableMacro to build guides using .minimum, .maximum, .rang…
mattt 42e5934
Lower APIs from package to internal access
mattt 4768a98
Change var to let
mattt dfb2f5d
Adjust constrained generation to never allow end tokens as valid term…
mattt c0cc2a3
Tighten free-string budget so we don’t exhaust the token budget on mu…
mattt 8e46927
Fix eos token logic
mattt 27f39b6
Incorporate feedback from review
mattt 5d10c2a
Fix parsing of guide constraints without a description string
mattt 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
Sources/AnyLanguageModel/Extensions/Character+Extensions.swift
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,25 @@ | ||
| extension Character { | ||
| static let jsonQuoteScalars: Set<UInt32> = [0x22, 0x201C, 0x201D, 0x2018, 0x2019] | ||
| static let jsonAllowedWhitespaceCharacters: Set<Character> = [" ", "\t", "\n"] | ||
|
|
||
| var containsEmojiScalar: Bool { | ||
| unicodeScalars.contains { scalar in | ||
| scalar.properties.isEmojiPresentation || scalar.properties.isEmoji | ||
| } | ||
| } | ||
|
|
||
| var isValidJSONStringCharacter: Bool { | ||
| guard self != "\\" else { return false } | ||
| guard let scalar = unicodeScalars.first, scalar.value >= 0x20 else { return false } | ||
| guard !Self.jsonQuoteScalars.contains(scalar.value) else { return false } | ||
|
|
||
| if let ascii = asciiValue { | ||
| let char = Character(UnicodeScalar(ascii)) | ||
| if Self.jsonAllowedWhitespaceCharacters.contains(char) { return true } | ||
| return isLetter || isNumber || (isASCII && (isPunctuation || isSymbol)) | ||
| } | ||
|
|
||
| // Allow non-ASCII letters/numbers and emoji, but disallow non-ASCII punctuation (e.g. "】") | ||
| return isLetter || isNumber || containsEmojiScalar | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
Sources/AnyLanguageModel/Extensions/JSONDecoder+Extensions.swift
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.
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.
Uh oh!
There was an error while loading. Please reload this page.