@inferrlm/react-native-mlx 0.4.2-alpha.1 → 0.4.2-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ios/Sources/HybridLLM.swift +84 -18
- package/package.json +1 -1
|
@@ -230,10 +230,6 @@ class HybridLLM: HybridLLMSpec {
|
|
|
230
230
|
}
|
|
231
231
|
|
|
232
232
|
return Promise.async { [self] in
|
|
233
|
-
if self.manageHistory {
|
|
234
|
-
self.messageHistory.append(LLMMessage(role: "user", content: prompt))
|
|
235
|
-
}
|
|
236
|
-
|
|
237
233
|
let task = Task<String, Error> {
|
|
238
234
|
log("Generating response for: \(prompt.prefix(50))...")
|
|
239
235
|
let result = try await session.respond(to: prompt)
|
|
@@ -247,6 +243,7 @@ class HybridLLM: HybridLLMSpec {
|
|
|
247
243
|
let result = try await task.value
|
|
248
244
|
|
|
249
245
|
if self.manageHistory {
|
|
246
|
+
self.messageHistory.append(LLMMessage(role: "user", content: prompt))
|
|
250
247
|
self.messageHistory.append(LLMMessage(role: "assistant", content: result))
|
|
251
248
|
}
|
|
252
249
|
|
|
@@ -266,10 +263,6 @@ class HybridLLM: HybridLLMSpec {
|
|
|
266
263
|
}
|
|
267
264
|
|
|
268
265
|
return Promise.async { [self] in
|
|
269
|
-
if self.manageHistory {
|
|
270
|
-
self.messageHistory.append(LLMMessage(role: "user", content: prompt))
|
|
271
|
-
}
|
|
272
|
-
|
|
273
266
|
let task = Task<String, Error> {
|
|
274
267
|
let startTime = Date()
|
|
275
268
|
var firstTokenTime: Date?
|
|
@@ -313,6 +306,7 @@ class HybridLLM: HybridLLMSpec {
|
|
|
313
306
|
let result = try await task.value
|
|
314
307
|
|
|
315
308
|
if self.manageHistory {
|
|
309
|
+
self.messageHistory.append(LLMMessage(role: "user", content: prompt))
|
|
316
310
|
self.messageHistory.append(LLMMessage(role: "assistant", content: result))
|
|
317
311
|
}
|
|
318
312
|
|
|
@@ -329,10 +323,6 @@ class HybridLLM: HybridLLMSpec {
|
|
|
329
323
|
}
|
|
330
324
|
|
|
331
325
|
return Promise.async { [self] in
|
|
332
|
-
if self.manageHistory {
|
|
333
|
-
self.messageHistory.append(LLMMessage(role: "user", content: prompt))
|
|
334
|
-
}
|
|
335
|
-
|
|
336
326
|
let task = Task<String, Error> {
|
|
337
327
|
let startTime = Date()
|
|
338
328
|
var firstTokenTime: Date?
|
|
@@ -389,6 +379,7 @@ class HybridLLM: HybridLLMSpec {
|
|
|
389
379
|
let result = try await task.value
|
|
390
380
|
|
|
391
381
|
if self.manageHistory {
|
|
382
|
+
self.messageHistory.append(LLMMessage(role: "user", content: prompt))
|
|
392
383
|
self.messageHistory.append(LLMMessage(role: "assistant", content: result))
|
|
393
384
|
}
|
|
394
385
|
|
|
@@ -461,6 +452,11 @@ class HybridLLM: HybridLLMSpec {
|
|
|
461
452
|
var thinkingMachine = ThinkingStateMachine()
|
|
462
453
|
var pendingToolCalls: [(id: String, tool: ToolDefinition, args: [String: Any], argsJson: String)] = []
|
|
463
454
|
|
|
455
|
+
let specialTokenPattern = try? NSRegularExpression(
|
|
456
|
+
pattern: "<\\|(?:im_end|im_start|endoftext|end|pad)\\|>",
|
|
457
|
+
options: []
|
|
458
|
+
)
|
|
459
|
+
|
|
464
460
|
let chat = buildChatMessages(prompt: prompt, toolResults: toolResults, depth: depth)
|
|
465
461
|
let userInput = UserInput(
|
|
466
462
|
chat: chat,
|
|
@@ -488,9 +484,19 @@ class HybridLLM: HybridLLMSpec {
|
|
|
488
484
|
for machineOutput in outputs {
|
|
489
485
|
switch machineOutput {
|
|
490
486
|
case .token(let token):
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
487
|
+
var cleaned = token
|
|
488
|
+
if let regex = specialTokenPattern {
|
|
489
|
+
cleaned = regex.stringByReplacingMatches(
|
|
490
|
+
in: cleaned,
|
|
491
|
+
range: NSRange(cleaned.startIndex..., in: cleaned),
|
|
492
|
+
withTemplate: ""
|
|
493
|
+
)
|
|
494
|
+
}
|
|
495
|
+
if !cleaned.isEmpty {
|
|
496
|
+
output += cleaned
|
|
497
|
+
emitter.emitToken(cleaned)
|
|
498
|
+
onTokenProcessed()
|
|
499
|
+
}
|
|
494
500
|
|
|
495
501
|
case .thinkingStart:
|
|
496
502
|
emitter.emitThinkingStart()
|
|
@@ -614,8 +620,14 @@ class HybridLLM: HybridLLMSpec {
|
|
|
614
620
|
}
|
|
615
621
|
|
|
616
622
|
var output = ""
|
|
623
|
+
var thinkingMachine = ThinkingStateMachine()
|
|
617
624
|
var pendingToolCalls: [(tool: ToolDefinition, args: [String: Any], argsJson: String)] = []
|
|
618
625
|
|
|
626
|
+
let specialTokenPattern = try? NSRegularExpression(
|
|
627
|
+
pattern: "<\\|(?:im_end|im_start|endoftext|end|pad)\\|>",
|
|
628
|
+
options: []
|
|
629
|
+
)
|
|
630
|
+
|
|
619
631
|
let chat = buildChatMessages(prompt: prompt, toolResults: toolResults, depth: depth)
|
|
620
632
|
let userInput = UserInput(
|
|
621
633
|
chat: chat,
|
|
@@ -638,8 +650,34 @@ class HybridLLM: HybridLLMSpec {
|
|
|
638
650
|
|
|
639
651
|
switch generation {
|
|
640
652
|
case .chunk(let text):
|
|
641
|
-
|
|
642
|
-
|
|
653
|
+
let outputs = thinkingMachine.process(token: text)
|
|
654
|
+
|
|
655
|
+
for machineOutput in outputs {
|
|
656
|
+
switch machineOutput {
|
|
657
|
+
case .token(let token):
|
|
658
|
+
var cleaned = token
|
|
659
|
+
if let regex = specialTokenPattern {
|
|
660
|
+
cleaned = regex.stringByReplacingMatches(
|
|
661
|
+
in: cleaned,
|
|
662
|
+
range: NSRange(cleaned.startIndex..., in: cleaned),
|
|
663
|
+
withTemplate: ""
|
|
664
|
+
)
|
|
665
|
+
}
|
|
666
|
+
if !cleaned.isEmpty {
|
|
667
|
+
output += cleaned
|
|
668
|
+
onToken(cleaned)
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
case .thinkingStart:
|
|
672
|
+
onToken("<think>")
|
|
673
|
+
|
|
674
|
+
case .thinkingChunk(let chunk):
|
|
675
|
+
onToken(chunk)
|
|
676
|
+
|
|
677
|
+
case .thinkingEnd:
|
|
678
|
+
onToken("</think>")
|
|
679
|
+
}
|
|
680
|
+
}
|
|
643
681
|
|
|
644
682
|
case .toolCall(let toolCall):
|
|
645
683
|
log("Tool call detected: \(toolCall.function.name)")
|
|
@@ -660,6 +698,31 @@ class HybridLLM: HybridLLMSpec {
|
|
|
660
698
|
}
|
|
661
699
|
}
|
|
662
700
|
|
|
701
|
+
let flushOutputs = thinkingMachine.flush()
|
|
702
|
+
for machineOutput in flushOutputs {
|
|
703
|
+
switch machineOutput {
|
|
704
|
+
case .token(let token):
|
|
705
|
+
var cleaned = token
|
|
706
|
+
if let regex = specialTokenPattern {
|
|
707
|
+
cleaned = regex.stringByReplacingMatches(
|
|
708
|
+
in: cleaned,
|
|
709
|
+
range: NSRange(cleaned.startIndex..., in: cleaned),
|
|
710
|
+
withTemplate: ""
|
|
711
|
+
)
|
|
712
|
+
}
|
|
713
|
+
if !cleaned.isEmpty {
|
|
714
|
+
output += cleaned
|
|
715
|
+
onToken(cleaned)
|
|
716
|
+
}
|
|
717
|
+
case .thinkingStart:
|
|
718
|
+
onToken("<think>")
|
|
719
|
+
case .thinkingChunk(let chunk):
|
|
720
|
+
onToken(chunk)
|
|
721
|
+
case .thinkingEnd:
|
|
722
|
+
onToken("</think>")
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
663
726
|
if !pendingToolCalls.isEmpty {
|
|
664
727
|
log("Executing \(pendingToolCalls.count) tool call(s)")
|
|
665
728
|
|
|
@@ -794,6 +857,9 @@ class HybridLLM: HybridLLMSpec {
|
|
|
794
857
|
|
|
795
858
|
func clearHistory() throws {
|
|
796
859
|
messageHistory = []
|
|
797
|
-
|
|
860
|
+
if let container = self.container {
|
|
861
|
+
self.session = ChatSession(container, instructions: self.systemPrompt)
|
|
862
|
+
}
|
|
863
|
+
log("History and session cleared")
|
|
798
864
|
}
|
|
799
865
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inferrlm/react-native-mlx",
|
|
3
3
|
"description": "MLX Swift integration for React Native - InferrLM fork with enhanced features",
|
|
4
|
-
"version": "0.4.2-alpha.
|
|
4
|
+
"version": "0.4.2-alpha.3",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"module": "./lib/module/index.js",
|
|
7
7
|
"types": "./lib/typescript/src/index.d.ts",
|