Skip to content

Commit 587f773

Browse files
chore: update default context strategy
1 parent 8b7ba1d commit 587f773

3 files changed

Lines changed: 8 additions & 24 deletions

File tree

apps/llm/app/llm/index.tsx

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ import {
1111
View,
1212
} from 'react-native';
1313
import SendIcon from '../../assets/icons/send_icon.svg';
14-
import {
15-
useLLM,
16-
LLAMA3_2_1B_SPINQUANT,
17-
SlidingWindowContextStrategy,
18-
} from 'react-native-executorch';
14+
import { useLLM, LLAMA3_2_1B_SPINQUANT } from 'react-native-executorch';
1915
import PauseIcon from '../../assets/icons/pause_icon.svg';
2016
import ColorPalette from '../../colors';
2117
import Messages from '../../components/Messages';
@@ -36,15 +32,6 @@ function LLMScreen() {
3632

3733
const llm = useLLM({ model: LLAMA3_2_1B_SPINQUANT });
3834

39-
useEffect(() => {
40-
llm.configure({
41-
chatConfig: {
42-
contextStrategy: new SlidingWindowContextStrategy(512),
43-
},
44-
});
45-
// eslint-disable-next-line react-hooks/exhaustive-deps
46-
}, []);
47-
4835
useEffect(() => {
4936
if (llm.error) {
5037
console.error('LLM error:', llm.error);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ChatConfig, Message } from '../types/llm';
2-
import { MessageCountContextStrategy } from '../utils/llms/context_strategy/MessageCountContextStrategy';
2+
import { SlidingWindowContextStrategy } from '../utils/llms/context_strategy';
33

44
/**
55
* Default system prompt used to guide the behavior of Large Language Models (LLMs).
@@ -35,11 +35,11 @@ ${structuredOutputSchema}
3535
export const DEFAULT_MESSAGE_HISTORY: Message[] = [];
3636

3737
/**
38-
* Default context window length for Large Language Models (LLMs).
38+
* Default context buffer tokens (number of tokens to keep for the model response) for Large Language Models (LLMs).
3939
*
4040
* @category Utilities - LLM
4141
*/
42-
export const DEFAULT_CONTEXT_WINDOW_LENGTH = 5;
42+
export const DEFAULT_CONTEXT_BUFFER_TOKENS = 5;
4343

4444
/**
4545
* Default chat configuration for Large Language Models (LLMs).
@@ -49,7 +49,7 @@ export const DEFAULT_CONTEXT_WINDOW_LENGTH = 5;
4949
export const DEFAULT_CHAT_CONFIG: ChatConfig = {
5050
systemPrompt: DEFAULT_SYSTEM_PROMPT,
5151
initialMessageHistory: DEFAULT_MESSAGE_HISTORY,
52-
contextStrategy: new MessageCountContextStrategy(
53-
DEFAULT_CONTEXT_WINDOW_LENGTH
52+
contextStrategy: new SlidingWindowContextStrategy(
53+
DEFAULT_CONTEXT_BUFFER_TOKENS
5454
),
5555
};

packages/react-native-executorch/src/utils/llms/context_strategy/MessageCountContextStrategy.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { DEFAULT_CONTEXT_WINDOW_LENGTH } from '../../../constants/llmDefaults';
21
import { ContextStrategy, Message } from '../../../types/llm';
32

43
/**
@@ -10,11 +9,9 @@ import { ContextStrategy, Message } from '../../../types/llm';
109
export class MessageCountContextStrategy implements ContextStrategy {
1110
/**
1211
* Initializes the MessageCountContextStrategy.
13-
* * @param {number} windowLength - The maximum number of recent messages to retain in the context. Defaults to {@link DEFAULT_CONTEXT_WINDOW_LENGTH}.
12+
* * @param {number} windowLength - The maximum number of recent messages to retain in the context. Defaults to 5.
1413
*/
15-
constructor(
16-
private readonly windowLength: number = DEFAULT_CONTEXT_WINDOW_LENGTH
17-
) {}
14+
constructor(private readonly windowLength: number = 5) {}
1815

1916
/**
2017
* Builds the context by slicing the history to retain only the most recent `windowLength` messages.

0 commit comments

Comments
 (0)