Skip to content

Commit 50bf7f9

Browse files
V4.8.17 feature (#3493)
* split tokens into input and output (#3477) * split tokens into input and output * query extension & tool call & question guide * fix * perf: input and output tokens * perf: tool call if else * perf: remove code * fix: extract usage count * fix: qa usage count --------- Co-authored-by: heheer <[email protected]>
1 parent da2831b commit 50bf7f9

File tree

46 files changed

+466
-229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+466
-229
lines changed

docSite/content/zh-cn/docs/development/upgrading/4817.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ curl --location --request POST 'https://{{host}}/api/admin/initv4817' \
2828

2929
会将用户绑定的 OpenAI 账号移动到团队中。
3030

31+
32+
## 调整 completions 接口返回值
33+
34+
/api/v1/chat/completions 接口返回值调整,对话节点、工具节点等使用到模型的节点,将不再返回 `tokens` 字段,改为返回 `inputTokens``outputTokens` 字段,分别表示输入和输出的 Token 数量。
35+
3136
## 完整更新内容
3237

3338
1. 新增 - 简易模式工具调用支持数组类型插件。
@@ -36,10 +41,12 @@ curl --location --request POST 'https://{{host}}/api/admin/initv4817' \
3641
4. 新增 - 商业版支持后台配置模板市场。
3742
5. 新增 - 商业版支持后台配置自定义工作流变量,用于与业务系统鉴权打通。
3843
6. 新增 - 搜索测试接口支持问题优化。
39-
7. 优化 - Markdown 大小测试,超出 20 万字符不使用 Markdown 组件,避免崩溃。
40-
8. 优化 - 知识库搜索参数,滑动条支持输入模式,可以更精准的控制。
41-
9. 优化 - 可用模型展示
42-
10. 优化 - Mongo 查询语句,增加 virtual 字段。
43-
11. 修复 - 文件返回接口缺少 Content-Length 头,导致通过非同源文件上传时,阿里 vision 模型无法识别图片。
44-
12. 修复 - 去除判断器两端字符串隐藏换行符,避免判断器失效。
45-
13. 修复 - 变量更新节点,手动输入更新内容时候,非字符串类型数据类型无法自动转化。
44+
7. 新增 - 工作流中 Input Token 和 Output Token 分开记录展示。并修复部分请求未记录输出 Token 计费问题。
45+
8. 优化 - Markdown 大小测试,超出 20 万字符不使用 Markdown 组件,避免崩溃。
46+
9. 优化 - 知识库搜索参数,滑动条支持输入模式,可以更精准的控制。
47+
10. 优化 - 可用模型展示UI。
48+
11. 优化 - Mongo 查询语句,增加 virtual 字段。
49+
12. 修复 - 文件返回接口缺少 Content-Length 头,导致通过非同源文件上传时,阿里 vision 模型无法识别图片。
50+
13. 修复 - 去除判断器两端字符串隐藏换行符,避免判断器失效。
51+
14. 修复 - 变量更新节点,手动输入更新内容时候,非字符串类型数据类型无法自动转化。
52+
15. 修复 - 豆包模型无法工具调用。

packages/global/core/ai/model.d.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import type { ModelProviderIdType } from './provider';
22

3-
export type LLMModelItemType = {
3+
type PriceType = {
4+
charsPointsPrice?: number; // 1k chars=n points; 60s=n points;
5+
6+
// If inputPrice is set, the input-output charging scheme is adopted
7+
inputPrice?: number; // 1k tokens=n points
8+
outputPrice?: number; // 1k tokens=n points
9+
};
10+
export type LLMModelItemType = PriceType & {
411
provider: ModelProviderIdType;
512
model: string;
613
name: string;
@@ -10,8 +17,6 @@ export type LLMModelItemType = {
1017
quoteMaxToken: number;
1118
maxTemperature: number;
1219

13-
charsPointsPrice: number; // 1k chars=n points
14-
1520
censor?: boolean;
1621
vision?: boolean;
1722

@@ -33,13 +38,12 @@ export type LLMModelItemType = {
3338
fieldMap?: Record<string, string>;
3439
};
3540

36-
export type VectorModelItemType = {
41+
export type VectorModelItemType = PriceType & {
3742
provider: ModelProviderIdType;
3843
model: string; // model name
3944
name: string; // show name
4045
avatar?: string;
4146
defaultToken: number; // split text default token
42-
charsPointsPrice: number; // 1k tokens=n points
4347
maxToken: number; // model max token
4448
weight: number; // training weight
4549
hidden?: boolean; // Disallow creation
@@ -48,25 +52,22 @@ export type VectorModelItemType = {
4852
queryConfig?: Record<string, any>; // Custom parameters for query
4953
};
5054

51-
export type ReRankModelItemType = {
55+
export type ReRankModelItemType = PriceType & {
5256
model: string;
5357
name: string;
54-
charsPointsPrice: number;
5558
requestUrl: string;
5659
requestAuth: string;
5760
};
5861

59-
export type AudioSpeechModelType = {
62+
export type AudioSpeechModelType = PriceType & {
6063
provider: ModelProviderIdType;
6164
model: string;
6265
name: string;
63-
charsPointsPrice: number;
6466
voices: { label: string; value: string; bufferId: string }[];
6567
};
6668

67-
export type STTModelType = {
69+
export type STTModelType = PriceType & {
6870
provider: ModelProviderIdType;
6971
model: string;
7072
name: string;
71-
charsPointsPrice: number; // 60s = n points
7273
};

packages/global/core/workflow/runtime/type.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ export type DispatchNodeResponseType = {
107107
mergeSignId?: string;
108108

109109
// bill
110-
tokens?: number;
110+
tokens?: number; // deprecated
111+
inputTokens?: number;
112+
outputTokens?: number;
111113
model?: string;
112114
contextTotalLen?: number;
113115
totalPoints?: number;
@@ -157,6 +159,8 @@ export type DispatchNodeResponseType = {
157159

158160
// tool
159161
toolCallTokens?: number;
162+
toolCallInputTokens?: number;
163+
toolCallOutputTokens?: number;
160164
toolDetail?: ChatHistoryItemResType[];
161165
toolStop?: boolean;
162166

packages/global/support/wallet/bill/type.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export type BillSchemaType = {
2323
};
2424

2525
export type ChatNodeUsageType = {
26-
tokens?: number;
26+
inputTokens?: number;
27+
outputTokens?: number;
2728
totalPoints: number;
2829
moduleName: string;
2930
model?: string;

packages/global/support/wallet/usage/type.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import { CreateUsageProps } from './api';
22
import { UsageSourceEnum } from './constants';
33

44
export type UsageListItemCountType = {
5-
tokens?: number;
5+
inputTokens?: number;
6+
outputTokens?: number;
67
charsLength?: number;
78
duration?: number;
9+
10+
// deprecated
11+
tokens?: number;
812
};
913
export type UsageListItemType = UsageListItemCountType & {
1014
moduleName: string;

packages/plugins/src/feishu/template.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "",
33
"version": "488",
44
"name": "飞书 webhook",
5-
"avatar": "/appMarketTemplates/plugin-feishu/avatar.svg",
5+
"avatar": "core/app/templates/plugin-feishu",
66
"intro": "向飞书机器人发起 webhook 请求。",
77
"courseUrl": "https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot#f62e72d5",
88
"showStatus": false,

packages/service/common/system/tools.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export const initFastGPTConfig = (config?: FastGPTConfigFileType) => {
1515
global.subPlans = config.subPlans;
1616

1717
global.llmModels = config.llmModels;
18+
global.llmModelPriceType = global.llmModels.some((item) => typeof item.inputPrice === 'number')
19+
? 'IO'
20+
: 'Tokens';
1821
global.vectorModels = config.vectorModels;
1922
global.audioSpeechModels = config.audioSpeechModels;
2023
global.whisperModel = config.whisperModel;

packages/service/core/ai/functions/createQuestionGuide.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ChatCompletionMessageParam } from '@fastgpt/global/core/ai/type.d';
22
import { createChatCompletion } from '../config';
3-
import { countGptMessagesTokens } from '../../../common/string/tiktoken/index';
3+
import { countGptMessagesTokens, countPromptTokens } from '../../../common/string/tiktoken/index';
44
import { loadRequestMessages } from '../../chat/utils';
55
import { llmCompletionsBodyFormat } from '../utils';
66
import {
@@ -20,7 +20,8 @@ export async function createQuestionGuide({
2020
customPrompt?: string;
2121
}): Promise<{
2222
result: string[];
23-
tokens: number;
23+
inputTokens: number;
24+
outputTokens: number;
2425
}> {
2526
const concatMessages: ChatCompletionMessageParam[] = [
2627
...messages,
@@ -29,17 +30,18 @@ export async function createQuestionGuide({
2930
content: `${customPrompt || PROMPT_QUESTION_GUIDE}\n${PROMPT_QUESTION_GUIDE_FOOTER}`
3031
}
3132
];
33+
const requestMessages = await loadRequestMessages({
34+
messages: concatMessages,
35+
useVision: false
36+
});
3237

3338
const { response: data } = await createChatCompletion({
3439
body: llmCompletionsBodyFormat(
3540
{
3641
model,
3742
temperature: 0.1,
3843
max_tokens: 200,
39-
messages: await loadRequestMessages({
40-
messages: concatMessages,
41-
useVision: false
42-
}),
44+
messages: requestMessages,
4345
stream: false
4446
},
4547
model
@@ -51,13 +53,15 @@ export async function createQuestionGuide({
5153
const start = answer.indexOf('[');
5254
const end = answer.lastIndexOf(']');
5355

54-
const tokens = await countGptMessagesTokens(concatMessages);
56+
const inputTokens = await countGptMessagesTokens(requestMessages);
57+
const outputTokens = await countPromptTokens(answer);
5558

5659
if (start === -1 || end === -1) {
5760
addLog.warn('Create question guide error', { answer });
5861
return {
5962
result: [],
60-
tokens: 0
63+
inputTokens: 0,
64+
outputTokens: 0
6165
};
6266
}
6367

@@ -69,14 +73,16 @@ export async function createQuestionGuide({
6973
try {
7074
return {
7175
result: json5.parse(jsonStr),
72-
tokens
76+
inputTokens,
77+
outputTokens
7378
};
7479
} catch (error) {
7580
console.log(error);
7681

7782
return {
7883
result: [],
79-
tokens: 0
84+
inputTokens: 0,
85+
outputTokens: 0
8086
};
8187
}
8288
}

packages/service/core/ai/functions/queryExtension.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { replaceVariable } from '@fastgpt/global/common/string/tools';
22
import { createChatCompletion } from '../config';
33
import { ChatItemType } from '@fastgpt/global/core/chat/type';
4-
import { countGptMessagesTokens } from '../../../common/string/tiktoken/index';
4+
import { countGptMessagesTokens, countPromptTokens } from '../../../common/string/tiktoken/index';
55
import { chatValue2RuntimePrompt } from '@fastgpt/global/core/chat/adapt';
66
import { getLLMModel } from '../model';
77
import { llmCompletionsBodyFormat } from '../utils';
@@ -121,7 +121,8 @@ export const queryExtension = async ({
121121
rawQuery: string;
122122
extensionQueries: string[];
123123
model: string;
124-
tokens: number;
124+
inputTokens: number;
125+
outputTokens: number;
125126
}> => {
126127
const systemFewShot = chatBg
127128
? `Q: 对话背景。
@@ -166,7 +167,8 @@ A: ${chatBg}
166167
rawQuery: query,
167168
extensionQueries: [],
168169
model,
169-
tokens: 0
170+
inputTokens: 0,
171+
outputTokens: 0
170172
};
171173
}
172174

@@ -181,15 +183,17 @@ A: ${chatBg}
181183
rawQuery: query,
182184
extensionQueries: Array.isArray(queries) ? queries : [],
183185
model,
184-
tokens: await countGptMessagesTokens(messages)
186+
inputTokens: await countGptMessagesTokens(messages),
187+
outputTokens: await countPromptTokens(answer)
185188
};
186189
} catch (error) {
187190
addLog.error(`Query extension error`, error);
188191
return {
189192
rawQuery: query,
190193
extensionQueries: [],
191194
model,
192-
tokens: 0
195+
inputTokens: 0,
196+
outputTokens: 0
193197
};
194198
}
195199
};

packages/service/core/ai/model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export const getLLMModel = (model?: string) => {
44
global.llmModels[0]
55
);
66
};
7+
78
export const getDatasetModel = (model?: string) => {
89
return (
910
global.llmModels

0 commit comments

Comments
 (0)