Skip to content

Commit

Permalink
🐛 fix: Fix max token calc
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Nov 23, 2023
1 parent aebceb9 commit 4318d15
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 9 deletions.
6 changes: 1 addition & 5 deletions packages/lobe-i18n/src/core/TranslateLocale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { ChatPromptTemplate } from 'langchain/prompts';
import { promptJsonTranslate, promptStringTranslate } from '@/prompts/translate';
import { LocaleObj } from '@/types';
import { I18nConfig } from '@/types/config';
import { LanguageModel, ModelTokens } from '@/types/models';

export class TranslateLocale {
private model: ChatOpenAI;
Expand All @@ -19,6 +18,7 @@ export class TranslateLocale {
configuration: {
baseURL: openAIProxyUrl,
},
maxConcurrency: config.concurrency,
maxRetries: 4,
maxTokens: config.splitToken,
modelName: config.modelName,
Expand Down Expand Up @@ -72,10 +72,6 @@ export class TranslateLocale {
to,
});

this.model.maxTokens =
ModelTokens[this.config.modelName || LanguageModel.GPT3_5] -
JSON.stringify(formattedChatPrompt).length;

const res = await this.model.call(
formattedChatPrompt,
this.isJsonMode
Expand Down
4 changes: 2 additions & 2 deletions packages/lobe-i18n/src/utils/calcToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export const PRIMITIVE_EXTRA_TOKENS = 3;
export const KEY_EXTRA_TOKENS = 2; // For `"key":`
export const OBJECT_EXTRA_TOKENS = 2; // For `{}`

export const calcToken = (str: any) => {
return encode(String(str)).length;
export const calcToken = (str: string) => {
return encode(str).length;
};

// Function to calculate the size of encoded key
Expand Down
3 changes: 1 addition & 2 deletions packages/lobe-i18n/src/utils/splitJsonToChunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ const splitJSONtoSmallChunks = (object: LocaleObj, splitToken: number) =>
).map(([chunk]) => chunk);

export const getSplitToken = (config: I18nConfig, prompt: string) => {
let splitToken =
(ModelTokens[config.modelName || LanguageModel.GPT3_5] - calcToken(prompt)) / 2.5;
let splitToken = (ModelTokens[config.modelName || LanguageModel.GPT3_5] - calcToken(prompt)) / 3;
if (config.splitToken && config.splitToken < splitToken) {
splitToken = config.splitToken;
}
Expand Down

0 comments on commit 4318d15

Please sign in to comment.