Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Sources/AnyLanguageModel/Shared/StructuredGeneration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ private final class StringTokenCache: @unchecked Sendable {
}

private let tokensByKey = Locked<[Key: Set<Int>]>([:])
private let integerTokensByKey = Locked<[Key: Set<Int>]>([:])
private let decimalTokensByKey = Locked<[Key: Set<Int>]>([:])

func tokens(for key: Key) -> Set<Int>? {
tokensByKey.withLock { $0[key] }
Expand All @@ -54,6 +56,22 @@ private final class StringTokenCache: @unchecked Sendable {
func store(_ tokens: Set<Int>, for key: Key) {
tokensByKey.withLock { $0[key] = tokens }
}

func integerTokens(for key: Key) -> Set<Int>? {
integerTokensByKey.withLock { $0[key] }
}

func storeIntegerTokens(_ tokens: Set<Int>, for key: Key) {
integerTokensByKey.withLock { $0[key] = tokens }
}

func decimalTokens(for key: Key) -> Set<Int>? {
decimalTokensByKey.withLock { $0[key] }
}

func storeDecimalTokens(_ tokens: Set<Int>, for key: Key) {
decimalTokensByKey.withLock { $0[key] = tokens }
}
}

// MARK: - JSON Generator
Expand Down Expand Up @@ -194,6 +212,11 @@ struct ConstrainedJSONGenerator<Backend: TokenBackend> {
}

private static func buildValidIntegerTokens(backend: Backend) -> Set<Int> {
let cacheKey = stringTokenCacheKey(for: backend)
if let cached = StringTokenCache.shared.integerTokens(for: cacheKey) {
return cached
}

var allowed = Set<Int>()
for token in 0 ..< backend.vocabSize {
if backend.isSpecialToken(token) { continue }
Expand All @@ -204,10 +227,17 @@ struct ConstrainedJSONGenerator<Backend: TokenBackend> {
allowed.insert(token)
}
}

StringTokenCache.shared.storeIntegerTokens(allowed, for: cacheKey)
return allowed
}

private static func buildValidDecimalTokens(backend: Backend) -> Set<Int> {
let cacheKey = stringTokenCacheKey(for: backend)
if let cached = StringTokenCache.shared.decimalTokens(for: cacheKey) {
return cached
}

var allowed = Set<Int>()
for token in 0 ..< backend.vocabSize {
if backend.isSpecialToken(token) { continue }
Expand All @@ -218,6 +248,8 @@ struct ConstrainedJSONGenerator<Backend: TokenBackend> {
allowed.insert(token)
}
}

StringTokenCache.shared.storeDecimalTokens(allowed, for: cacheKey)
return allowed
}

Expand Down
Loading