mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 05:12:39 +00:00
perf: tiktoken count (#1507)
* perf: tiktoken count * fix: rerank histories * fix: rerank histories * update npmrc
This commit is contained in:
@@ -47,41 +47,45 @@ export const countGptMessagesTokens = (
|
||||
tools?: ChatCompletionTool[],
|
||||
functionCall?: ChatCompletionCreateParams.Function[]
|
||||
) => {
|
||||
return new Promise<number>((resolve) => {
|
||||
const start = Date.now();
|
||||
return new Promise<number>(async (resolve) => {
|
||||
try {
|
||||
const start = Date.now();
|
||||
|
||||
const { worker, callbackMap } = getTiktokenWorker();
|
||||
const { worker, callbackMap } = getTiktokenWorker();
|
||||
|
||||
const id = getNanoid();
|
||||
const id = getNanoid();
|
||||
|
||||
const timer = setTimeout(() => {
|
||||
console.log('Count token Time out');
|
||||
resolve(
|
||||
messages.reduce((sum, item) => {
|
||||
if (item.content) {
|
||||
return sum + item.content.length * 0.5;
|
||||
}
|
||||
return sum;
|
||||
}, 0)
|
||||
);
|
||||
delete callbackMap[id];
|
||||
}, 60000);
|
||||
const timer = setTimeout(() => {
|
||||
console.log('Count token Time out');
|
||||
resolve(
|
||||
messages.reduce((sum, item) => {
|
||||
if (item.content) {
|
||||
return sum + item.content.length * 0.5;
|
||||
}
|
||||
return sum;
|
||||
}, 0)
|
||||
);
|
||||
delete callbackMap[id];
|
||||
}, 60000);
|
||||
|
||||
callbackMap[id] = (data) => {
|
||||
// 检测是否有内存泄漏
|
||||
addLog.info(`Count token time: ${Date.now() - start}, token: ${data}`);
|
||||
// console.log(process.memoryUsage());
|
||||
callbackMap[id] = (data) => {
|
||||
// 检测是否有内存泄漏
|
||||
addLog.info(`Count token time: ${Date.now() - start}, token: ${data}`);
|
||||
// console.log(process.memoryUsage());
|
||||
|
||||
resolve(data);
|
||||
clearTimeout(timer);
|
||||
};
|
||||
resolve(data);
|
||||
clearTimeout(timer);
|
||||
};
|
||||
|
||||
worker.postMessage({
|
||||
id,
|
||||
messages,
|
||||
tools,
|
||||
functionCall
|
||||
});
|
||||
worker.postMessage({
|
||||
id,
|
||||
messages,
|
||||
tools,
|
||||
functionCall
|
||||
});
|
||||
} catch (error) {
|
||||
resolve(0);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
@@ -2,6 +2,7 @@ import { LLMModelItemType } from '@fastgpt/global/core/ai/model.d';
|
||||
import { queryExtension } from '../../ai/functions/queryExtension';
|
||||
import { ChatItemType } from '@fastgpt/global/core/chat/type';
|
||||
import { hashStr } from '@fastgpt/global/common/string/tools';
|
||||
import { chatValue2RuntimePrompt } from '@fastgpt/global/core/chat/adapt';
|
||||
|
||||
export const datasetSearchQueryExtension = async ({
|
||||
query,
|
||||
@@ -33,11 +34,11 @@ export const datasetSearchQueryExtension = async ({
|
||||
histories.length > 0
|
||||
? `${histories
|
||||
.map((item) => {
|
||||
return `${item.obj}: ${item.value}`;
|
||||
return `${item.obj}: ${chatValue2RuntimePrompt(item.value).text}`;
|
||||
})
|
||||
.join('\n')}
|
||||
Human: ${query}
|
||||
`
|
||||
Human: ${query}
|
||||
`
|
||||
: query;
|
||||
|
||||
/* if query already extension, direct parse */
|
||||
|
@@ -13,7 +13,6 @@
|
||||
"decompress": "^4.2.1",
|
||||
"domino-ext": "^2.1.4",
|
||||
"encoding": "^0.1.13",
|
||||
"fastgpt-js-tiktoken": "^1.0.12",
|
||||
"file-type": "^19.0.0",
|
||||
"iconv-lite": "^0.6.3",
|
||||
"joplin-turndown-plugin-gfm": "^1.0.12",
|
||||
@@ -29,6 +28,7 @@
|
||||
"papaparse": "5.4.1",
|
||||
"pdfjs-dist": "4.0.269",
|
||||
"pg": "^8.10.0",
|
||||
"tiktoken": "^1.0.15",
|
||||
"tunnel": "^0.0.6",
|
||||
"turndown": "^7.1.2"
|
||||
},
|
||||
|
@@ -1,5 +1,5 @@
|
||||
/* Only the token of gpt-3.5-turbo is used */
|
||||
import { Tiktoken } from 'fastgpt-js-tiktoken/lite';
|
||||
import { Tiktoken } from 'tiktoken/lite';
|
||||
import cl100k_base from './cl100k_base.json';
|
||||
import {
|
||||
ChatCompletionMessageParam,
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
import { ChatCompletionRequestMessageRoleEnum } from '@fastgpt/global/core/ai/constants';
|
||||
import { parentPort } from 'worker_threads';
|
||||
|
||||
const enc = new Tiktoken(cl100k_base);
|
||||
const enc = new Tiktoken(cl100k_base.bpe_ranks, cl100k_base.special_tokens, cl100k_base.pat_str);
|
||||
|
||||
/* count messages tokens */
|
||||
parentPort?.on(
|
||||
|
Reference in New Issue
Block a user