Skip to content

Commit 0eee8b6

Browse files
feat: requested changes
1 parent 23f5c99 commit 0eee8b6

File tree

8 files changed

+30
-37
lines changed

8 files changed

+30
-37
lines changed

docs/docs/03-hooks/01-natural-language-processing/useLLM.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ The `imagePath` should be a local file path on the device.
531531

532532
### Functional generation with images
533533

534-
You can also use `generate` directly, passing `imagePaths` as the third argument:
534+
You can also use `generate` directly by setting `mediaPath` on user messages:
535535

536536
```tsx
537537
const llm = useLLM({ model: LFM2_VL_1_6B_QUANTIZED });
@@ -540,14 +540,12 @@ const handleGenerate = async () => {
540540
const chat: Message[] = [
541541
{
542542
role: 'user',
543-
content: [
544-
{ type: 'image' },
545-
{ type: 'text', text: 'Describe this image.' },
546-
],
543+
content: 'Describe this image.',
544+
mediaPath: '/path/to/image.jpg',
547545
},
548546
];
549547

550-
const response = await llm.generate(chat, undefined, ['/path/to/image.jpg']);
548+
const response = await llm.generate(chat);
551549
console.log(response);
552550
};
553551
```

docs/docs/04-typescript-api/01-natural-language-processing/LLMModule.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,18 @@ const response = await llm.sendMessage('What is in this image?', {
147147
});
148148
```
149149

150-
Or use [`generate`](../../06-api-reference/classes/LLMModule.md#generate) with `imagePaths` directly:
150+
Or use [`generate`](../../06-api-reference/classes/LLMModule.md#generate) with `mediaPath` on the message:
151151

152152
```typescript
153153
const chat: Message[] = [
154154
{
155155
role: 'user',
156-
content: [
157-
{ type: 'image' },
158-
{ type: 'text', text: 'Describe this image.' },
159-
],
156+
content: 'Describe this image.',
157+
mediaPath: '/path/to/image.jpg',
160158
},
161159
];
162160

163-
const response = await llm.generate(chat, undefined, ['/path/to/image.jpg']);
161+
const response = await llm.generate(chat);
164162
```
165163

166164
## Deleting the model from memory

packages/react-native-executorch/common/rnexecutorch/threads/GlobalThreadPool.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class GlobalThreadPool {
3535
}
3636

3737
numThreads = std::max(numThreads.value(), 2u);
38+
log(rnexecutorch::LOG_LEVEL::Info, "Initializing global thread pool with",
39+
numThreads, "threads");
3840
instance = std::make_unique<HighPerformanceThreadPool>(numThreads.value(),
3941
config);
4042
// Disable OpenCV's internal threading to prevent it from overriding our

packages/react-native-executorch/src/constants/modelUrls.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ export const LFM2_VL_1_6B_QUANTIZED = {
385385
modelSource: LFM2_VL_1_6B_QUANTIZED_MODEL,
386386
tokenizerSource: LFM2_VL_TOKENIZER,
387387
tokenizerConfigSource: LFM2_VL_TOKENIZER_CONFIG,
388-
};
388+
} as const;
389389

390390
// Classification
391391
const EFFICIENTNET_V2_S_MODEL =

packages/react-native-executorch/src/controllers/LLMController.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,7 @@ export class LLMController {
288288

289289
public async generate(
290290
messages: Message[],
291-
tools?: LLMTool[],
292-
imagePaths?: string[]
291+
tools?: LLMTool[]
293292
): Promise<string> {
294293
if (!this._isReady) {
295294
throw new RnExecutorchError(
@@ -309,6 +308,10 @@ export class LLMController {
309308
);
310309
}
311310

311+
const imagePaths = messages
312+
.filter((m) => m.mediaPath)
313+
.map((m) => m.mediaPath!);
314+
312315
const renderedChat: string = this.applyChatTemplate(
313316
messages,
314317
this.tokenizerConfig,
@@ -317,7 +320,10 @@ export class LLMController {
317320
{ tools_in_user_message: false, add_generation_prompt: true }
318321
);
319322

320-
return await this.forward(renderedChat, imagePaths);
323+
return await this.forward(
324+
renderedChat,
325+
imagePaths.length > 0 ? imagePaths : undefined
326+
);
321327
}
322328

323329
public async sendMessage(
@@ -367,14 +373,9 @@ export class LLMController {
367373
countTokensCallback
368374
);
369375

370-
const imagePaths = messageHistoryWithPrompt
371-
.filter((m) => m.mediaPath)
372-
.map((m) => m.mediaPath!);
373-
374376
const response = await this.generate(
375377
messageHistoryWithPrompt,
376-
this.toolsConfig?.tools,
377-
imagePaths.length > 0 ? imagePaths : undefined
378+
this.toolsConfig?.tools
378379
);
379380

380381
if (!this.toolsConfig || this.toolsConfig.displayToolCalls) {

packages/react-native-executorch/src/hooks/natural_language_processing/useLLM.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ export function useLLM({
9797
);
9898

9999
const generate = useCallback(
100-
(messages: Message[], tools?: LLMTool[], imagePaths?: string[]) => {
100+
(messages: Message[], tools?: LLMTool[]) => {
101101
setResponse('');
102-
return controllerInstance.generate(messages, tools, imagePaths);
102+
return controllerInstance.generate(messages, tools);
103103
},
104104
[controllerInstance]
105105
);

packages/react-native-executorch/src/modules/natural_language_processing/LLMModule.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,14 @@ export class LLMModule {
108108

109109
/**
110110
* Runs model to complete chat passed in `messages` argument. It doesn't manage conversation context.
111+
* For multimodal models, set `mediaPath` on user messages to include images.
111112
*
112-
* @param messages - Array of messages representing the chat history.
113+
* @param messages - Array of messages representing the chat history. User messages may include a `mediaPath` field with a local image path.
113114
* @param tools - Optional array of tools that can be used during generation.
114115
* @returns The generated response as a string.
115116
*/
116-
async generate(
117-
messages: Message[],
118-
tools?: LLMTool[],
119-
imagePaths?: string[]
120-
): Promise<string> {
121-
return await this.controller.generate(messages, tools, imagePaths);
117+
async generate(messages: Message[], tools?: LLMTool[]): Promise<string> {
118+
return await this.controller.generate(messages, tools);
122119
}
123120

124121
/**

packages/react-native-executorch/src/types/llm.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,13 @@ export interface LLMTypeBase {
103103
getGeneratedTokenCount: () => number;
104104
/**
105105
* Runs model to complete chat passed in `messages` argument. It doesn't manage conversation context.
106+
* For multimodal models, set `mediaPath` on user messages to include images.
106107
*
107-
* @param messages - Array of messages representing the chat history.
108+
* @param messages - Array of messages representing the chat history. User messages may include a `mediaPath` field with a local image path.
108109
* @param tools - Optional array of tools that can be used during generation.
109110
* @returns The generated tokens as `string`.
110111
*/
111-
generate: (
112-
messages: Message[],
113-
tools?: LLMTool[],
114-
imagePaths?: string[]
115-
) => Promise<string>;
112+
generate: (messages: Message[], tools?: LLMTool[]) => Promise<string>;
116113
/**
117114
* Returns the number of total tokens from the previous generation. This is a sum of prompt tokens and generated tokens.
118115
*

0 commit comments

Comments
 (0)