Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/docs/03-hooks/01-natural-language-processing/useLLM.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ For more information on loading resources, take a look at [loading models](../..
| `deleteMessage` | `(index: number) => void` | Deletes all messages starting with message on `index` position. After deletion `messageHistory` will be updated. |
| `messageHistory` | `Message[]` | History containing all messages in conversation. This field is updated after model responds to `sendMessage`. |
| `getGeneratedTokenCount` | `() => number` | Returns the number of tokens generated in the last response. |
| `getPromptTokenCount` | `() => number` | Returns the number of tokens in the prompt (input tokens) for the current session. |
| `getTotalTokenCount` | `() => number` | Returns the total number of tokens processed (sum of prompt tokens and generated tokens). |

<details>
<summary>Type definitions</summary>
Expand Down Expand Up @@ -110,6 +112,8 @@ interface LLMType {
generationConfig?: GenerationConfig;
}) => void;
getGeneratedTokenCount: () => number;
getPromptTokenCount: () => number;
getTotalTokenCount: () => number;
generate: (messages: Message[], tools?: LLMTool[]) => Promise<string>;
sendMessage: (message: string) => Promise<string>;
deleteMessage: (index: number) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ llm.delete();
| `delete` | `() => void` | Method to delete the model from memory. Note you cannot delete model while it's generating. You need to interrupt it first and make sure model stopped generation. |
| `interrupt` | `() => void` | Interrupts model generation. It may return one more token after interrupt. |
| `getGeneratedTokenCount` | `() => number` | Returns the number of tokens generated in the last response. |
| `getPromptTokenCount` | `() => number` | Returns the number of tokens in the prompt (input tokens) for the current session. |
| `getTotalTokenCount` | `() => number` | Returns the total number of tokens processed (sum of prompt tokens and generated tokens). |

<details>
<summary>Type definitions</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
synchronousHostFunction<&Model::getGeneratedTokenCount>,
"getGeneratedTokenCount"));

addFunctions(JSI_EXPORT_FUNCTION(
ModelHostObject<Model>,
synchronousHostFunction<&Model::getPromptTokenCount>,
"getPromptTokenCount"));

addFunctions(
JSI_EXPORT_FUNCTION(ModelHostObject<Model>,
synchronousHostFunction<&Model::setCountInterval>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ size_t LLM::getGeneratedTokenCount() const noexcept {
return runner->stats_.num_generated_tokens;
}

size_t LLM::getPromptTokenCount() const noexcept {
if (!runner || !runner->is_loaded()) {
return 0;
}
return runner->stats_.num_prompt_tokens;
}

size_t LLM::getMemoryLowerBound() const noexcept {
return memorySizeLowerBound;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class LLM : public BaseModel {
void interrupt();
void unload() noexcept;
size_t getGeneratedTokenCount() const noexcept;
size_t getPromptTokenCount() const noexcept;
size_t getMemoryLowerBound() const noexcept;
void setCountInterval(size_t countInterval);
void setTemperature(float temperature);
Expand Down
14 changes: 14 additions & 0 deletions packages/react-native-executorch/src/controllers/LLMController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,20 @@ export class LLMController {
return this.nativeModule.getGeneratedTokenCount();
}

public getPromptTokenCount(): number {
if (!this.nativeModule) {
throw new RnExecutorchError(
RnExecutorchErrorCode.ModuleNotLoaded,
"Cannot get prompt token count for a model that's not loaded."
);
}
return this.nativeModule.getPromptTokenCount();
}

public getTotalTokenCount(): number {
return this.getGeneratedTokenCount() + this.getPromptTokenCount();
}

public async generate(
messages: Message[],
tools?: LLMTool[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ export const useLLM = ({
[controllerInstance]
);

const getPromptTokenCount = useCallback(
() => controllerInstance.getPromptTokenCount(),
[controllerInstance]
);

const getTotalTokenCount = useCallback(
() => controllerInstance.getTotalTokenCount(),
[controllerInstance]
);

return {
messageHistory,
response,
Expand All @@ -139,6 +149,8 @@ export const useLLM = ({
downloadProgress,
error,
getGeneratedTokenCount: getGeneratedTokenCount,
getPromptTokenCount: getPromptTokenCount,
getTotalTokenCount: getTotalTokenCount,
configure: configure,
generate: generate,
sendMessage: sendMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ export class LLMModule {
return this.controller.getGeneratedTokenCount();
}

getPromptTokensCount() {
return this.controller.getPromptTokenCount();
}

getTotalTokensCount() {
return this.controller.getTotalTokenCount();
}

delete() {
this.controller.delete();
}
Expand Down
2 changes: 2 additions & 0 deletions packages/react-native-executorch/src/types/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface LLMType {
generationConfig?: GenerationConfig;
}) => void;
getGeneratedTokenCount: () => number;
getTotalTokenCount: () => number;
getPromptTokenCount: () => number;
generate: (messages: Message[], tools?: LLMTool[]) => Promise<string>;
sendMessage: (message: string) => Promise<string>;
deleteMessage: (index: number) => void;
Expand Down
Loading