-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathdesktop_inference_model.dart
More file actions
279 lines (243 loc) · 7.56 KB
/
desktop_inference_model.dart
File metadata and controls
279 lines (243 loc) · 7.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
part of 'flutter_gemma_desktop.dart';
/// Desktop implementation of InferenceModel using gRPC
class DesktopInferenceModel extends InferenceModel {
DesktopInferenceModel({
required this.grpcClient,
required this.maxTokens,
required this.modelType,
this.fileType = ModelFileType.task,
this.supportImage = false,
this.supportAudio = false,
required this.onClose,
});
final LiteRtLmClient grpcClient;
final ModelType modelType;
@override
final ModelFileType fileType;
@override
final int maxTokens;
final bool supportImage;
final bool supportAudio;
final VoidCallback onClose;
DesktopInferenceModelSession? _session;
Completer<InferenceModelSession>? _createCompleter;
bool _isClosed = false;
@override
InferenceModelSession? get session => _session;
@override
Future<InferenceModelSession> createSession({
double temperature = .8,
int randomSeed = 1,
int topK = 1,
double? topP,
String? loraPath,
bool? enableVisionModality,
bool? enableAudioModality,
}) async {
if (_isClosed) {
throw StateError('Model is closed. Create a new instance to use it again');
}
if (_createCompleter case Completer<InferenceModelSession> completer) {
return completer.future;
}
final completer = _createCompleter = Completer<InferenceModelSession>();
try {
// Create conversation on server with sampler config
await grpcClient.createConversation(
temperature: temperature,
topK: topK,
topP: topP,
);
final session = _session = DesktopInferenceModelSession(
grpcClient: grpcClient,
modelType: modelType,
fileType: fileType,
supportImage: enableVisionModality ?? supportImage,
supportAudio: enableAudioModality ?? supportAudio,
onClose: () {
_session = null;
_createCompleter = null;
},
);
completer.complete(session);
return session;
} catch (e, st) {
completer.completeError(e, st);
_createCompleter = null;
rethrow;
}
}
@override
Future<InferenceChat> createChat({
double temperature = .8,
int randomSeed = 1,
int topK = 1,
double? topP,
int tokenBuffer = 256,
String? loraPath,
bool? supportImage,
bool? supportAudio,
List<Tool> tools = const [],
bool? supportsFunctionCalls,
bool isThinking = false,
ModelType? modelType,
ToolChoice toolChoice = ToolChoice.auto,
}) async {
chat = InferenceChat(
sessionCreator: () => createSession(
temperature: temperature,
randomSeed: randomSeed,
topK: topK,
topP: topP,
loraPath: loraPath,
enableVisionModality: supportImage ?? this.supportImage,
enableAudioModality: supportAudio ?? this.supportAudio,
),
maxTokens: maxTokens,
tokenBuffer: tokenBuffer,
supportImage: supportImage ?? this.supportImage,
supportAudio: supportAudio ?? this.supportAudio,
supportsFunctionCalls: supportsFunctionCalls ?? false,
tools: tools,
modelType: modelType ?? this.modelType,
isThinking: isThinking,
fileType: fileType,
toolChoice: toolChoice,
);
await chat!.initSession();
return chat!;
}
@override
Future<void> close() async {
_isClosed = true;
try {
await _session?.close();
} finally {
try {
await grpcClient.shutdown();
} finally {
try {
await grpcClient.disconnect();
} finally {
onClose();
}
}
}
}
}
/// Desktop implementation of InferenceModelSession.
///
/// Uses gRPC to communicate with the LiteRT-LM server.
/// Buffers query chunks, images, and audio until [getResponse] is called.
///
/// **Thread Safety:** This session is NOT thread-safe. All method calls
/// must originate from the same isolate. Concurrent access from multiple
/// isolates may cause undefined behavior.
class DesktopInferenceModelSession extends InferenceModelSession {
DesktopInferenceModelSession({
required this.grpcClient,
required this.modelType,
required this.fileType,
required this.supportImage,
required this.supportAudio,
required this.onClose,
});
final LiteRtLmClient grpcClient;
final ModelType modelType;
final ModelFileType fileType;
final bool supportImage;
final bool supportAudio;
final VoidCallback onClose;
final StringBuffer _queryBuffer = StringBuffer();
Uint8List? _pendingImage;
Uint8List? _pendingAudio;
bool _isClosed = false;
void _assertNotClosed() {
if (_isClosed) {
throw StateError('Session is closed');
}
}
@override
Future<void> addQueryChunk(Message message) async {
_assertNotClosed();
debugPrint('[DesktopSession] addQueryChunk: hasAudio=${message.hasAudio}, audioBytes=${message.audioBytes?.length}, supportAudio=$supportAudio');
final prompt = message.transformToChatPrompt(type: modelType, fileType: fileType);
_queryBuffer.write(prompt);
if (message.hasImage && message.imageBytes != null && supportImage) {
_pendingImage = message.imageBytes;
}
if (message.hasAudio && message.audioBytes != null && supportAudio) {
_pendingAudio = message.audioBytes;
}
}
@override
Future<String> getResponse() async {
_assertNotClosed();
final text = _queryBuffer.toString();
_queryBuffer.clear();
// Capture and clear pending media BEFORE making the call
// This prevents stale media from being reused if the call fails
final audio = _pendingAudio;
final image = _pendingImage;
_pendingAudio = null;
_pendingImage = null;
final buffer = StringBuffer();
if (audio != null) {
await for (final token in grpcClient.chatWithAudio(text, audio)) {
buffer.write(token);
}
} else if (image != null) {
await for (final token in grpcClient.chatWithImage(text, image)) {
buffer.write(token);
}
} else {
await for (final token in grpcClient.chat(text)) {
buffer.write(token);
}
}
return buffer.toString();
}
@override
Stream<String> getResponseAsync() async* {
_assertNotClosed();
final text = _queryBuffer.toString();
_queryBuffer.clear();
// Capture and clear pending media BEFORE making the call
// This prevents stale media from being reused if the call fails
final audio = _pendingAudio;
final image = _pendingImage;
_pendingAudio = null;
_pendingImage = null;
debugPrint('[DesktopSession] getResponseAsync: audio=${audio?.length}, image=${image?.length}');
if (audio != null) {
debugPrint('[DesktopSession] Calling chatWithAudio: audio=${audio.length} bytes');
yield* grpcClient.chatWithAudio(text, audio);
} else if (image != null) {
debugPrint('[DesktopSession] Calling chatWithImage: image=${image.length} bytes');
yield* grpcClient.chatWithImage(text, image);
} else {
debugPrint('[DesktopSession] Calling chat (no image/audio)');
yield* grpcClient.chat(text);
}
}
@override
Future<int> sizeInTokens(String text) async {
// Approximate token count (LiteRT-LM doesn't expose tokenizer directly)
// Using ~4 chars per token as rough estimate
return (text.length / 4).ceil();
}
@override
Future<void> stopGeneration() async {
await grpcClient.cancelGeneration();
}
@override
Future<void> close() async {
_isClosed = true;
// Clear pending buffers to prevent memory leaks
_queryBuffer.clear();
_pendingImage = null;
_pendingAudio = null;
await grpcClient.closeConversation();
onClose();
}
}