Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: max tokens #820

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions packages/core/src/ChatHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ export class SimpleChatHistory extends ChatHistory {
}
}

export type SummaryChatHistoryOptions = {
/**
* some LLM instance (like Ollama) might doesn't have a maxTokens metadata, so you should set it manually
*/
maxTokens: number;
};

export class SummaryChatHistory extends ChatHistory {
/**
* Tokenizer function that converts text to tokens,
Expand All @@ -77,20 +84,23 @@ export class SummaryChatHistory extends ChatHistory {
summaryPrompt: SummaryPrompt;
llm: LLM;
private messagesBefore: number;
readonly #maxTokens: number;

constructor(init?: Partial<SummaryChatHistory>) {
constructor(
init?: Partial<SummaryChatHistory> & Partial<SummaryChatHistoryOptions>,
) {
super();
this.messages = init?.messages ?? [];
this.messagesBefore = this.messages.length;
this.summaryPrompt = init?.summaryPrompt ?? defaultSummaryPrompt;
this.llm = init?.llm ?? new OpenAI();
if (!this.llm.metadata.maxTokens) {
if (!this.llm.metadata.maxTokens || !init?.maxTokens) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use Infinity if it's undefined?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code below won't work if maxTokens is not specified, so I think it's ok, to throw this error, it's basically a configuration error.

this class also needs a tokenizer to work correctly (and for ollama it could be any LLM with any tokenizer).

So I think we should set tokenizer to globalsHelper.defaultTokenizer.encode only if the LLM is OpenAI and throw an error if tokenizer is not explicitly specified otherwise

throw new Error(
"LLM maxTokens is not set. Needed so the summarizer ensures the context window size of the LLM.",
);
}
this.tokensToSummarize =
this.llm.metadata.contextWindow - this.llm.metadata.maxTokens;
this.#maxTokens = this.llm.metadata.maxTokens ?? init?.maxTokens;
this.tokensToSummarize = this.llm.metadata.contextWindow - this.#maxTokens;
if (this.tokensToSummarize < this.llm.metadata.contextWindow * 0.25) {
throw new Error(
"The number of tokens that trigger the summarize process are less than 25% of the context window. Try lowering maxTokens or use a model with a larger context window.",
Expand Down