Skip to content

Commit 21d9b04

Browse files
pweglikjakmrochmjkb
authored
feat: Support tokenizer config and add tool support.
## Description <!-- Provide a concise and descriptive summary of the changes implemented in this PR. --> ### Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Documentation update (improves or adds clarity to existing documentation) ### Tested on - [x] iOS - [x] Android ### Testing instructions <!-- Provide step-by-step instructions on how to test your changes. Include setup details if necessary. --> ### Screenshots <!-- Add screenshots here, if applicable --> ### Related issues <!-- Link related issues here using #issue-number --> ### Checklist - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have updated the documentation accordingly - [x] My changes generate no new warnings ### Additional notes <!-- Include any additional information, assumptions, or context that reviewers might need to understand this PR. --> --------- Co-authored-by: Jakub Mroz <115979017+jakmro@users.noreply.github.com> Co-authored-by: Jakub Chmura <92989966+chmjkb@users.noreply.github.com>
1 parent 61fd540 commit 21d9b04

31 files changed

Lines changed: 843 additions & 669 deletions

File tree

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,17 @@ Add this to your component file:
4343

4444
```tsx
4545
import {
46-
LLAMA3_2_3B_QLORA,
47-
LLAMA3_2_3B_TOKENIZER,
4846
useLLM,
47+
LLAMA3_2_1B,
48+
LLAMA3_2_TOKENIZER_CONFIG,
4949
} from 'react-native-executorch';
5050

5151
function MyComponent() {
5252
// Initialize the model 🚀
5353
const llama = useLLM({
54-
modelSource: LLAMA3_2_3B_QLORA,
55-
tokenizerSource: LLAMA3_2_3B_TOKENIZER,
54+
modelSource: LLAMA3_2_1B,
55+
tokenizerSource: LLAMA3_2_TOKENIZER,
56+
tokenizerConfigSource: LLAMA3_2_TOKENIZER_CONFIG,
5657
});
5758
// ... rest of your component
5859
}
@@ -67,8 +68,8 @@ const handleGenerate = async () => {
6768
const prompt = 'The meaning of life is';
6869

6970
// Generate text based on your desired prompt
70-
const response = await llama.generate(prompt);
71-
console.log('Llama says:', response);
71+
await llama.runInference(prompt);
72+
console.log('Llama says:', llama.response);
7273
};
7374
```
7475

android/src/main/java/com/swmansion/rnexecutorch/LLM.kt

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,13 @@ package com.swmansion.rnexecutorch
33
import android.util.Log
44
import com.facebook.react.bridge.Promise
55
import com.facebook.react.bridge.ReactApplicationContext
6-
import com.facebook.react.bridge.ReadableArray
7-
import com.swmansion.rnexecutorch.utils.ArrayUtils
8-
import com.swmansion.rnexecutorch.utils.llms.ChatRole
9-
import com.swmansion.rnexecutorch.utils.llms.ConversationManager
10-
import com.swmansion.rnexecutorch.utils.llms.END_OF_TEXT_TOKEN
116
import org.pytorch.executorch.extension.llm.LlmCallback
127
import org.pytorch.executorch.extension.llm.LlmModule
138

149
class LLM(
1510
reactContext: ReactApplicationContext,
16-
) : NativeLLMSpec(reactContext),
17-
LlmCallback {
18-
private var llamaModule: LlmModule? = null
19-
private var tempLlamaResponse = StringBuilder()
20-
private lateinit var conversationManager: ConversationManager
11+
) : NativeLLMSpec(reactContext), LlmCallback {
12+
private var llmModule: LlmModule? = null
2113

2214
override fun getName(): String = NAME
2315

@@ -27,7 +19,6 @@ class LLM(
2719

2820
override fun onResult(result: String) {
2921
emitOnToken(result)
30-
this.tempLlamaResponse.append(result)
3122
}
3223

3324
override fun onStats(tps: Float) {
@@ -37,20 +28,10 @@ class LLM(
3728
override fun loadLLM(
3829
modelSource: String,
3930
tokenizerSource: String,
40-
systemPrompt: String,
41-
messageHistory: ReadableArray,
42-
contextWindowLength: Double,
4331
promise: Promise,
4432
) {
4533
try {
46-
this.conversationManager =
47-
ConversationManager(
48-
contextWindowLength.toInt(),
49-
systemPrompt,
50-
ArrayUtils.createMapArray<String>(messageHistory),
51-
)
52-
llamaModule = LlmModule(modelSource, tokenizerSource, 0.7f)
53-
this.tempLlamaResponse.clear()
34+
llmModule = LlmModule(modelSource, tokenizerSource, 0.7f)
5435
promise.resolve("Model loaded successfully")
5536
} catch (e: Exception) {
5637
promise.reject("Model loading failed", e.message)
@@ -61,35 +42,19 @@ class LLM(
6142
input: String,
6243
promise: Promise,
6344
) {
64-
this.conversationManager.addResponse(input, ChatRole.USER)
65-
val conversation = this.conversationManager.getConversation()
66-
6745
Thread {
68-
llamaModule!!.generate(conversation, this)
69-
70-
// When we call .interrupt(), the LLM doesn't produce EOT token, that also could happen when the
71-
// generated sequence length is larger than specified in the JNI callback, hence we check if EOT
72-
// is there and if not, we append it to the output and emit the EOT token to the JS side.
73-
if (!this.tempLlamaResponse.endsWith(END_OF_TEXT_TOKEN)) {
74-
this.onResult(END_OF_TEXT_TOKEN)
75-
}
76-
77-
// We want to add the LLM response to the conversation once all the tokens are generated.
78-
// Each token is appended to the tempLlamaResponse StringBuilder in onResult callback.
79-
this.conversationManager.addResponse(this.tempLlamaResponse.toString(), ChatRole.ASSISTANT)
80-
this.tempLlamaResponse.clear()
81-
Log.d("ExecutorchLib", this.conversationManager.getConversation())
82-
}.start()
83-
84-
promise.resolve("Inference completed successfully")
46+
llmModule!!.generate(input, this)
47+
promise.resolve("Inference completed successfully")
48+
}
49+
.start()
8550
}
8651

8752
override fun interrupt() {
88-
llamaModule!!.stop()
53+
llmModule!!.stop()
8954
}
9055

9156
override fun deleteModule() {
92-
llamaModule = null
57+
llmModule = null
9358
}
9459

9560
companion object {

android/src/main/java/com/swmansion/rnexecutorch/utils/llms/ConversationManager.kt

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)