From c473995ac7d020ae1bbd809ab77c6875953bd84c Mon Sep 17 00:00:00 2001 From: Jhen-Jie Hong Date: Sat, 16 Nov 2024 19:19:53 +0800 Subject: [PATCH] feat(ts): expose template for getFormattedChat closes #84 --- docs/API/README.md | 22 +++++++-------- docs/API/classes/LlamaContext.md | 33 +++++++++++----------- docs/API/classes/SchemaGrammarConverter.md | 32 ++++++++++----------- example/src/App.tsx | 1 + src/index.ts | 12 ++++---- 5 files changed, 51 insertions(+), 49 deletions(-) diff --git a/docs/API/README.md b/docs/API/README.md index 6419ef5..bea6088 100644 --- a/docs/API/README.md +++ b/docs/API/README.md @@ -45,17 +45,17 @@ llama.rn #### Defined in -[index.ts:57](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L57) +[index.ts:58](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L58) ___ ### CompletionParams -Ƭ **CompletionParams**: `Omit`<`NativeCompletionParams`, ``"emit_partial_completion"`` \| ``"prompt"``\> & { `messages?`: `RNLlamaOAICompatibleMessage`[] ; `prompt?`: `string` } +Ƭ **CompletionParams**: `Omit`<`NativeCompletionParams`, ``"emit_partial_completion"`` \| ``"prompt"``\> & { `chatTemplate?`: `string` ; `messages?`: `RNLlamaOAICompatibleMessage`[] ; `prompt?`: `string` } #### Defined in -[index.ts:49](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L49) +[index.ts:49](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L49) ___ @@ -65,7 +65,7 @@ ___ #### Defined in -[index.ts:43](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L43) +[index.ts:43](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L43) ___ @@ -75,7 +75,7 @@ ___ #### Defined in -[index.ts:47](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L47) +[index.ts:47](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L47) ___ @@ -92,7 +92,7 @@ ___ #### Defined in -[index.ts:33](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L33) +[index.ts:33](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L33) ## Functions @@ -116,7 +116,7 @@ ___ #### Defined in -[grammar.ts:824](https://github.com/mybigday/llama.rn/blob/20a1819/src/grammar.ts#L824) +[grammar.ts:824](https://github.com/mybigday/llama.rn/blob/38fa660/src/grammar.ts#L824) ___ @@ -137,7 +137,7 @@ ___ #### Defined in -[index.ts:225](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L225) +[index.ts:225](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L225) ___ @@ -157,7 +157,7 @@ ___ #### Defined in -[index.ts:210](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L210) +[index.ts:210](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L210) ___ @@ -171,7 +171,7 @@ ___ #### Defined in -[index.ts:269](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L269) +[index.ts:275](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L275) ___ @@ -191,4 +191,4 @@ ___ #### Defined in -[index.ts:196](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L196) +[index.ts:196](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L196) diff --git a/docs/API/classes/LlamaContext.md b/docs/API/classes/LlamaContext.md index 0240db7..22a185f 100644 --- a/docs/API/classes/LlamaContext.md +++ b/docs/API/classes/LlamaContext.md @@ -42,7 +42,7 @@ #### Defined in -[index.ts:78](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L78) +[index.ts:79](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L79) ## Properties @@ -52,7 +52,7 @@ #### Defined in -[index.ts:70](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L70) +[index.ts:71](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L71) ___ @@ -62,7 +62,7 @@ ___ #### Defined in -[index.ts:68](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L68) +[index.ts:69](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L69) ___ @@ -78,7 +78,7 @@ ___ #### Defined in -[index.ts:74](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L74) +[index.ts:75](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L75) ___ @@ -88,7 +88,7 @@ ___ #### Defined in -[index.ts:72](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L72) +[index.ts:73](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L73) ## Methods @@ -111,7 +111,7 @@ ___ #### Defined in -[index.ts:171](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L171) +[index.ts:171](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L171) ___ @@ -132,7 +132,7 @@ ___ #### Defined in -[index.ts:115](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L115) +[index.ts:115](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L115) ___ @@ -152,7 +152,7 @@ ___ #### Defined in -[index.ts:160](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L160) +[index.ts:160](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L160) ___ @@ -173,19 +173,20 @@ ___ #### Defined in -[index.ts:164](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L164) +[index.ts:164](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L164) ___ ### getFormattedChat -▸ **getFormattedChat**(`messages`): `Promise`<`string`\> +▸ **getFormattedChat**(`messages`, `template?`): `Promise`<`string`\> #### Parameters | Name | Type | | :------ | :------ | | `messages` | `RNLlamaOAICompatibleMessage`[] | +| `template?` | `string` | #### Returns @@ -193,7 +194,7 @@ ___ #### Defined in -[index.ts:104](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L104) +[index.ts:105](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L105) ___ @@ -215,7 +216,7 @@ Load cached prompt & completion state from a file. #### Defined in -[index.ts:88](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L88) +[index.ts:89](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L89) ___ @@ -229,7 +230,7 @@ ___ #### Defined in -[index.ts:191](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L191) +[index.ts:191](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L191) ___ @@ -253,7 +254,7 @@ Save current cached prompt & completion state to a file. #### Defined in -[index.ts:97](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L97) +[index.ts:98](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L98) ___ @@ -267,7 +268,7 @@ ___ #### Defined in -[index.ts:152](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L152) +[index.ts:152](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L152) ___ @@ -287,4 +288,4 @@ ___ #### Defined in -[index.ts:156](https://github.com/mybigday/llama.rn/blob/20a1819/src/index.ts#L156) +[index.ts:156](https://github.com/mybigday/llama.rn/blob/38fa660/src/index.ts#L156) diff --git a/docs/API/classes/SchemaGrammarConverter.md b/docs/API/classes/SchemaGrammarConverter.md index a29b886..b88ba0d 100644 --- a/docs/API/classes/SchemaGrammarConverter.md +++ b/docs/API/classes/SchemaGrammarConverter.md @@ -46,7 +46,7 @@ #### Defined in -[grammar.ts:211](https://github.com/mybigday/llama.rn/blob/20a1819/src/grammar.ts#L211) +[grammar.ts:211](https://github.com/mybigday/llama.rn/blob/38fa660/src/grammar.ts#L211) ## Properties @@ -56,7 +56,7 @@ #### Defined in -[grammar.ts:201](https://github.com/mybigday/llama.rn/blob/20a1819/src/grammar.ts#L201) +[grammar.ts:201](https://github.com/mybigday/llama.rn/blob/38fa660/src/grammar.ts#L201) ___ @@ -66,7 +66,7 @@ ___ #### Defined in -[grammar.ts:203](https://github.com/mybigday/llama.rn/blob/20a1819/src/grammar.ts#L203) +[grammar.ts:203](https://github.com/mybigday/llama.rn/blob/38fa660/src/grammar.ts#L203) ___ @@ -76,7 +76,7 @@ ___ #### Defined in -[grammar.ts:199](https://github.com/mybigday/llama.rn/blob/20a1819/src/grammar.ts#L199) +[grammar.ts:199](https://github.com/mybigday/llama.rn/blob/38fa660/src/grammar.ts#L199) ___ @@ -90,7 +90,7 @@ ___ #### Defined in -[grammar.ts:207](https://github.com/mybigday/llama.rn/blob/20a1819/src/grammar.ts#L207) +[grammar.ts:207](https://github.com/mybigday/llama.rn/blob/38fa660/src/grammar.ts#L207) ___ @@ -100,7 +100,7 @@ ___ #### Defined in -[grammar.ts:209](https://github.com/mybigday/llama.rn/blob/20a1819/src/grammar.ts#L209) +[grammar.ts:209](https://github.com/mybigday/llama.rn/blob/38fa660/src/grammar.ts#L209) ___ @@ -114,7 +114,7 @@ ___ #### Defined in -[grammar.ts:205](https://github.com/mybigday/llama.rn/blob/20a1819/src/grammar.ts#L205) +[grammar.ts:205](https://github.com/mybigday/llama.rn/blob/38fa660/src/grammar.ts#L205) ## Methods @@ -135,7 +135,7 @@ ___ #### Defined in -[grammar.ts:693](https://github.com/mybigday/llama.rn/blob/20a1819/src/grammar.ts#L693) +[grammar.ts:693](https://github.com/mybigday/llama.rn/blob/38fa660/src/grammar.ts#L693) ___ @@ -156,7 +156,7 @@ ___ #### Defined in -[grammar.ts:224](https://github.com/mybigday/llama.rn/blob/20a1819/src/grammar.ts#L224) +[grammar.ts:224](https://github.com/mybigday/llama.rn/blob/38fa660/src/grammar.ts#L224) ___ @@ -179,7 +179,7 @@ ___ #### Defined in -[grammar.ts:710](https://github.com/mybigday/llama.rn/blob/20a1819/src/grammar.ts#L710) +[grammar.ts:710](https://github.com/mybigday/llama.rn/blob/38fa660/src/grammar.ts#L710) ___ @@ -200,7 +200,7 @@ ___ #### Defined in -[grammar.ts:312](https://github.com/mybigday/llama.rn/blob/20a1819/src/grammar.ts#L312) +[grammar.ts:312](https://github.com/mybigday/llama.rn/blob/38fa660/src/grammar.ts#L312) ___ @@ -220,7 +220,7 @@ ___ #### Defined in -[grammar.ts:518](https://github.com/mybigday/llama.rn/blob/20a1819/src/grammar.ts#L518) +[grammar.ts:518](https://github.com/mybigday/llama.rn/blob/38fa660/src/grammar.ts#L518) ___ @@ -241,7 +241,7 @@ ___ #### Defined in -[grammar.ts:323](https://github.com/mybigday/llama.rn/blob/20a1819/src/grammar.ts#L323) +[grammar.ts:323](https://github.com/mybigday/llama.rn/blob/38fa660/src/grammar.ts#L323) ___ @@ -255,7 +255,7 @@ ___ #### Defined in -[grammar.ts:813](https://github.com/mybigday/llama.rn/blob/20a1819/src/grammar.ts#L813) +[grammar.ts:813](https://github.com/mybigday/llama.rn/blob/38fa660/src/grammar.ts#L813) ___ @@ -276,7 +276,7 @@ ___ #### Defined in -[grammar.ts:247](https://github.com/mybigday/llama.rn/blob/20a1819/src/grammar.ts#L247) +[grammar.ts:247](https://github.com/mybigday/llama.rn/blob/38fa660/src/grammar.ts#L247) ___ @@ -297,4 +297,4 @@ ___ #### Defined in -[grammar.ts:529](https://github.com/mybigday/llama.rn/blob/20a1819/src/grammar.ts#L529) +[grammar.ts:529](https://github.com/mybigday/llama.rn/blob/38fa660/src/grammar.ts#L529) diff --git a/example/src/App.tsx b/example/src/App.tsx index dfe84e6..3c2a8ea 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -103,6 +103,7 @@ export default function App() { model: file.uri, use_mlock: true, n_gpu_layers: Platform.OS === 'ios' ? 0 : 0, // > 0: enable GPU + lora: '/Users/jhen/Library/Containers/6A136561-8ED9-4BEF-A7BD-E39F19C67FA1/Data/tmp/org.reactjs.native.example.RNLlamaExample-Inbox/lora-Llama-3-Instruct-abliteration-LoRA-8B-f16.gguf', // embedding: true, }, (progress) => { diff --git a/src/index.ts b/src/index.ts index 22ba726..bb12026 100644 --- a/src/index.ts +++ b/src/index.ts @@ -52,6 +52,7 @@ export type CompletionParams = Omit< > & { prompt?: string messages?: RNLlamaOAICompatibleMessage[] + chatTemplate?: string } export type BenchResult = { @@ -103,13 +104,12 @@ export class LlamaContext { async getFormattedChat( messages: RNLlamaOAICompatibleMessage[], + template?: string, ): Promise { const chat = formatChat(messages) - return RNLlama.getFormattedChat( - this.id, - chat, - this.model?.isChatTemplateSupported ? undefined : 'chatml', - ) + let tmpl = this.model?.isChatTemplateSupported ? undefined : 'chatml' + if (template) tmpl = template // Force replace if provided + return RNLlama.getFormattedChat(this.id, chat, tmpl) } async completion( @@ -119,7 +119,7 @@ export class LlamaContext { let finalPrompt = params.prompt if (params.messages) { // messages always win - finalPrompt = await this.getFormattedChat(params.messages) + finalPrompt = await this.getFormattedChat(params.messages, params.chatTemplate) } let tokenListener: any =