File tree Expand file tree Collapse file tree 1 file changed +9
-5
lines changed Expand file tree Collapse file tree 1 file changed +9
-5
lines changed Original file line number Diff line number Diff line change @@ -723,12 +723,13 @@ public func generate(
723
723
var promptTime : TimeInterval = 0
724
724
725
725
let additionalEOSTokenIds = Set (
726
- ( context. configuration. extraEOSTokens ?? [ ] )
726
+ context. configuration. extraEOSTokens
727
727
. compactMap {
728
728
context. tokenizer. convertTokenToId ( $0)
729
729
} )
730
730
731
731
var tokenCount = 0
732
+ var detokenizer = NaiveStreamingDetokenizer ( tokenizer: context. tokenizer)
732
733
733
734
for token in iterator {
734
735
@@ -748,8 +749,11 @@ public func generate(
748
749
break
749
750
}
750
751
751
- tokenCount += 1
752
- continuation. yield ( . token( token) )
752
+ detokenizer. append ( token: token)
753
+ if let chunk = detokenizer. next ( ) {
754
+ tokenCount += 1
755
+ continuation. yield ( . chunk( chunk) )
756
+ }
753
757
}
754
758
755
759
let now = Date . timeIntervalSinceReferenceDate
@@ -819,11 +823,11 @@ public struct GenerateCompletionInfo: Sendable {
819
823
/// Represents the different stages or outputs of the token generation process.
820
824
///
821
825
/// This enum distinguishes between the following:
822
- /// - `.token `: An individual token generated by the language model.
826
+ /// - `.chunk `: A decoded string from one or more tokens generated by the language model.
823
827
/// - `.info`: Metadata and performance statistics about the generation process.
824
828
public enum Generation {
825
829
/// A generated token represented as an integer.
826
- case token ( Int )
830
+ case chunk ( String )
827
831
/// Completion information summarizing token counts and performance metrics.
828
832
case info( GenerateCompletionInfo )
829
833
}
You can’t perform that action at this time.
0 commit comments