restorePromptPrefix method
Keeps a matching prompt prefix or rewinds to a matching checkpoint for callers that feed the rest themselves. Leaves at least the last token to be fed. The embedding arguments follow prefill's key rules.
Implementation
Future<int> restorePromptPrefix(
List<int> ids, {
Map<int, Float32List> embeddingOverrides = const {},
Map<int, Object> embeddingKeys = const {},
}) async {
_checkOpen();
if (ids.isEmpty) throw ArgumentError('empty prompt');
if (embeddingKeys.keys.any((p) => !embeddingOverrides.containsKey(p))) {
throw ArgumentError('every embedding key needs an override');
}
bool same(int i) {
if (!embeddingOverrides.containsKey(i)) return _history[i] == ids[i];
final key = embeddingKeys[i];
return key != null && _history[i] == -1 && _embeddingKeys[i] == key;
}
var common = 0;
final limit = math.min(_history.length, ids.length - 1);
while (common < limit && same(common)) {
common++;
}
if (common < length) {
final start = _checkpoints.keys
.where((p) => p <= common)
.fold(0, math.max);
await _rewind(start);
common = start;
}
return common;
}