Skip to content

Commit 89757fd

Browse files
DevDesai444sanchitmonga22
authored andcommitted
Preserve LLM context length for Flutter validation
1 parent 7cfbf24 commit 89757fd

3 files changed

Lines changed: 23 additions & 4 deletions

File tree

sdk/runanywhere-flutter/packages/runanywhere/lib/native/dart_bridge_llm.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class DartBridgeLLM {
4848

4949
RacHandle? _handle;
5050
String? _loadedModelId;
51+
int? _loadedContextLength;
5152
final _logger = SDKLogger('DartBridge.LLM');
5253

5354
/// Active stream subscription for cancellation
@@ -153,6 +154,7 @@ class DartBridgeLLM {
153154
String modelPath,
154155
String modelId,
155156
String modelName,
157+
int? contextLength,
156158
) async {
157159
final handle = getHandle();
158160

@@ -181,6 +183,7 @@ class DartBridgeLLM {
181183
}
182184

183185
_loadedModelId = modelId;
186+
_loadedContextLength = contextLength;
184187
_logger.info('LLM model loaded: $modelId');
185188
} finally {
186189
calloc.free(pathPtr);
@@ -200,6 +203,7 @@ class DartBridgeLLM {
200203

201204
cleanupFn(_handle!);
202205
_loadedModelId = null;
206+
_loadedContextLength = null;
203207
_logger.info('LLM model unloaded');
204208
} catch (e) {
205209
_logger.error('Failed to unload LLM model: $e');
@@ -386,8 +390,10 @@ class DartBridgeLLM {
386390
String? systemPrompt,
387391
bool streamingEnabled = false,
388392
}) {
393+
final contextLength = _loadedContextLength;
394+
389395
LLMConfiguration(
390-
contextLength: 32768,
396+
contextLength: contextLength ?? 32768,
391397
maxTokens: maxTokens,
392398
temperature: temperature,
393399
systemPrompt: systemPrompt,
@@ -408,6 +414,7 @@ class DartBridgeLLM {
408414
destroyFn(_handle!);
409415
_handle = null;
410416
_loadedModelId = null;
417+
_loadedContextLength = null;
411418
_logger.debug('LLM component destroyed');
412419
} catch (e) {
413420
_logger.error('Failed to destroy LLM component: $e');

sdk/runanywhere-flutter/packages/runanywhere/lib/native/dart_bridge_model_registry.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ class DartBridgeModelRegistry {
151151
modelPtr.ref.localPath =
152152
pathDart != null ? strdupFn(pathDart) : nullptr;
153153
modelPtr.ref.downloadSize = model.sizeBytes;
154+
modelPtr.ref.contextLength = model.contextLength;
154155
modelPtr.ref.source = model.source;
155156

156157
final result = saveFn(_registryHandle!, modelPtr);
@@ -196,6 +197,7 @@ class DartBridgeModelRegistry {
196197
framework: _frameworkToFfi(model.framework),
197198
source: _sourceToFfi(model.source),
198199
sizeBytes: model.downloadSize ?? 0,
200+
contextLength: model.contextLength ?? 0,
199201
downloadURL: model.downloadURL?.toString(),
200202
localPath: model.localPath?.toFilePath(),
201203
version: null,
@@ -384,6 +386,7 @@ class DartBridgeModelRegistry {
384386
? Uri.file(ffiModel.localPath!)
385387
: null,
386388
downloadSize: ffiModel.sizeBytes > 0 ? ffiModel.sizeBytes : null,
389+
contextLength: ffiModel.contextLength > 0 ? ffiModel.contextLength : null,
387390
source: _sourceFromFfi(ffiModel.source),
388391
);
389392
}
@@ -804,6 +807,7 @@ class DartBridgeModelRegistry {
804807
framework: struct.ref.framework,
805808
source: struct.ref.source,
806809
sizeBytes: struct.ref.downloadSize,
810+
contextLength: struct.ref.contextLength,
807811
downloadURL: downloadURL,
808812
localPath: localPath,
809813
version: null,
@@ -1092,6 +1096,7 @@ class ModelInfo {
10921096
final int framework;
10931097
final int source;
10941098
final int sizeBytes;
1099+
final int contextLength;
10951100
final String? downloadURL;
10961101
final String? localPath;
10971102
final String? version;
@@ -1104,6 +1109,7 @@ class ModelInfo {
11041109
required this.framework,
11051110
required this.source,
11061111
required this.sizeBytes,
1112+
required this.contextLength,
11071113
this.downloadURL,
11081114
this.localPath,
11091115
this.version,
@@ -1119,6 +1125,7 @@ class ModelInfo {
11191125
'framework': framework,
11201126
'source': source,
11211127
'sizeBytes': sizeBytes,
1128+
'contextLength': contextLength,
11221129
if (downloadURL != null) 'downloadURL': downloadURL,
11231130
if (localPath != null) 'localPath': localPath,
11241131
if (version != null) 'version': version,

sdk/runanywhere-flutter/packages/runanywhere/lib/public/runanywhere.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,12 @@ class RunAnywhere {
451451
// Load model directly via DartBridgeLLM (mirrors Swift CppBridge.LLM pattern)
452452
// The C++ layer handles finding the right backend via the service registry
453453
logger.debug('Loading model via C++ bridge: $resolvedPath');
454-
await DartBridge.llm.loadModel(resolvedPath, modelId, model.name);
454+
await DartBridge.llm.loadModel(
455+
resolvedPath,
456+
modelId,
457+
model.name,
458+
model.contextLength,
459+
);
455460

456461
// Verify the model loaded successfully
457462
if (!DartBridge.llm.isLoaded) {
@@ -1943,7 +1948,7 @@ class RunAnywhere {
19431948
latencyMs: latencyMs.round(),
19441949
temperature: opts.temperature,
19451950
maxTokens: opts.maxTokens,
1946-
contextLength: 8192, // Default context length for LlamaCpp
1951+
contextLength: modelInfo?.contextLength,
19471952
tokensPerSecond: tokensPerSecond,
19481953
isStreaming: false,
19491954
);
@@ -2124,7 +2129,7 @@ class RunAnywhere {
21242129
latencyMs: latencyMs.round(),
21252130
temperature: opts.temperature,
21262131
maxTokens: opts.maxTokens,
2127-
contextLength: 8192, // Default context length for LlamaCpp
2132+
contextLength: modelInfo?.contextLength,
21282133
tokensPerSecond: tokensPerSecond,
21292134
timeToFirstTokenMs: timeToFirstTokenMs,
21302135
isStreaming: true,

0 commit comments

Comments
 (0)