-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathLLM.kt
More file actions
63 lines (52 loc) · 1.34 KB
/
LLM.kt
File metadata and controls
63 lines (52 loc) · 1.34 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
package com.swmansion.rnexecutorch
import android.util.Log
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import org.pytorch.executorch.extension.llm.LlmCallback
import org.pytorch.executorch.extension.llm.LlmModule
class LLM(
reactContext: ReactApplicationContext,
) : NativeLLMSpec(reactContext), LlmCallback {
private var llmModule: LlmModule? = null
override fun getName(): String = NAME
override fun initialize() {
super.initialize()
}
override fun onResult(result: String) {
emitOnToken(result)
}
override fun onStats(tps: Float) {
Log.d("rn_executorch", "TPS: $tps")
}
override fun loadLLM(
modelSource: String,
tokenizerSource: String,
promise: Promise,
) {
try {
llmModule = LlmModule(modelSource, tokenizerSource, 0.7f)
promise.resolve("Model loaded successfully")
} catch (e: Exception) {
promise.reject("Model loading failed", e.message)
}
}
override fun forward(
input: String,
promise: Promise,
) {
Thread {
llmModule!!.generate(input, this)
promise.resolve("Inference completed successfully")
}
.start()
}
override fun interrupt() {
llmModule!!.stop()
}
override fun releaseResources() {
llmModule = null
}
companion object {
const val NAME = "LLM"
}
}