Skip to content

Commit

Permalink
V4.8.17 feature (#3493)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
c121914yu and newfish-cmyk authored Dec 30, 2024
1 parent da2831b commit 50bf7f9
Show file tree
Hide file tree
Showing 46 changed files with 466 additions and 229 deletions.
21 changes: 14 additions & 7 deletions docSite/content/zh-cn/docs/development/upgrading/4817.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ curl --location --request POST 'https://{{host}}/api/admin/initv4817' \

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


## 调整 completions 接口返回值

/api/v1/chat/completions 接口返回值调整,对话节点、工具节点等使用到模型的节点,将不再返回 `tokens` 字段,改为返回 `inputTokens``outputTokens` 字段,分别表示输入和输出的 Token 数量。

## 完整更新内容

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

export type LLMModelItemType = {
type PriceType = {
charsPointsPrice?: number; // 1k chars=n points; 60s=n points;

// If inputPrice is set, the input-output charging scheme is adopted
inputPrice?: number; // 1k tokens=n points
outputPrice?: number; // 1k tokens=n points
};
export type LLMModelItemType = PriceType & {
provider: ModelProviderIdType;
model: string;
name: string;
Expand All @@ -10,8 +17,6 @@ export type LLMModelItemType = {
quoteMaxToken: number;
maxTemperature: number;

charsPointsPrice: number; // 1k chars=n points

censor?: boolean;
vision?: boolean;

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

export type VectorModelItemType = {
export type VectorModelItemType = PriceType & {
provider: ModelProviderIdType;
model: string; // model name
name: string; // show name
avatar?: string;
defaultToken: number; // split text default token
charsPointsPrice: number; // 1k tokens=n points
maxToken: number; // model max token
weight: number; // training weight
hidden?: boolean; // Disallow creation
Expand All @@ -48,25 +52,22 @@ export type VectorModelItemType = {
queryConfig?: Record<string, any>; // Custom parameters for query
};

export type ReRankModelItemType = {
export type ReRankModelItemType = PriceType & {
model: string;
name: string;
charsPointsPrice: number;
requestUrl: string;
requestAuth: string;
};

export type AudioSpeechModelType = {
export type AudioSpeechModelType = PriceType & {
provider: ModelProviderIdType;
model: string;
name: string;
charsPointsPrice: number;
voices: { label: string; value: string; bufferId: string }[];
};

export type STTModelType = {
export type STTModelType = PriceType & {
provider: ModelProviderIdType;
model: string;
name: string;
charsPointsPrice: number; // 60s = n points
};
6 changes: 5 additions & 1 deletion packages/global/core/workflow/runtime/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ export type DispatchNodeResponseType = {
mergeSignId?: string;

// bill
tokens?: number;
tokens?: number; // deprecated
inputTokens?: number;
outputTokens?: number;
model?: string;
contextTotalLen?: number;
totalPoints?: number;
Expand Down Expand Up @@ -157,6 +159,8 @@ export type DispatchNodeResponseType = {

// tool
toolCallTokens?: number;
toolCallInputTokens?: number;
toolCallOutputTokens?: number;
toolDetail?: ChatHistoryItemResType[];
toolStop?: boolean;

Expand Down
3 changes: 2 additions & 1 deletion packages/global/support/wallet/bill/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export type BillSchemaType = {
};

export type ChatNodeUsageType = {
tokens?: number;
inputTokens?: number;
outputTokens?: number;
totalPoints: number;
moduleName: string;
model?: string;
Expand Down
6 changes: 5 additions & 1 deletion packages/global/support/wallet/usage/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { CreateUsageProps } from './api';
import { UsageSourceEnum } from './constants';

export type UsageListItemCountType = {
tokens?: number;
inputTokens?: number;
outputTokens?: number;
charsLength?: number;
duration?: number;

// deprecated
tokens?: number;
};
export type UsageListItemType = UsageListItemCountType & {
moduleName: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/src/feishu/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "",
"version": "488",
"name": "飞书 webhook",
"avatar": "/appMarketTemplates/plugin-feishu/avatar.svg",
"avatar": "core/app/templates/plugin-feishu",
"intro": "向飞书机器人发起 webhook 请求。",
"courseUrl": "https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot#f62e72d5",
"showStatus": false,
Expand Down
3 changes: 3 additions & 0 deletions packages/service/common/system/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export const initFastGPTConfig = (config?: FastGPTConfigFileType) => {
global.subPlans = config.subPlans;

global.llmModels = config.llmModels;
global.llmModelPriceType = global.llmModels.some((item) => typeof item.inputPrice === 'number')
? 'IO'
: 'Tokens';
global.vectorModels = config.vectorModels;
global.audioSpeechModels = config.audioSpeechModels;
global.whisperModel = config.whisperModel;
Expand Down
26 changes: 16 additions & 10 deletions packages/service/core/ai/functions/createQuestionGuide.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ChatCompletionMessageParam } from '@fastgpt/global/core/ai/type.d';
import { createChatCompletion } from '../config';
import { countGptMessagesTokens } from '../../../common/string/tiktoken/index';
import { countGptMessagesTokens, countPromptTokens } from '../../../common/string/tiktoken/index';
import { loadRequestMessages } from '../../chat/utils';
import { llmCompletionsBodyFormat } from '../utils';
import {
Expand All @@ -20,7 +20,8 @@ export async function createQuestionGuide({
customPrompt?: string;
}): Promise<{
result: string[];
tokens: number;
inputTokens: number;
outputTokens: number;
}> {
const concatMessages: ChatCompletionMessageParam[] = [
...messages,
Expand All @@ -29,17 +30,18 @@ export async function createQuestionGuide({
content: `${customPrompt || PROMPT_QUESTION_GUIDE}\n${PROMPT_QUESTION_GUIDE_FOOTER}`
}
];
const requestMessages = await loadRequestMessages({
messages: concatMessages,
useVision: false
});

const { response: data } = await createChatCompletion({
body: llmCompletionsBodyFormat(
{
model,
temperature: 0.1,
max_tokens: 200,
messages: await loadRequestMessages({
messages: concatMessages,
useVision: false
}),
messages: requestMessages,
stream: false
},
model
Expand All @@ -51,13 +53,15 @@ export async function createQuestionGuide({
const start = answer.indexOf('[');
const end = answer.lastIndexOf(']');

const tokens = await countGptMessagesTokens(concatMessages);
const inputTokens = await countGptMessagesTokens(requestMessages);
const outputTokens = await countPromptTokens(answer);

if (start === -1 || end === -1) {
addLog.warn('Create question guide error', { answer });
return {
result: [],
tokens: 0
inputTokens: 0,
outputTokens: 0
};
}

Expand All @@ -69,14 +73,16 @@ export async function createQuestionGuide({
try {
return {
result: json5.parse(jsonStr),
tokens
inputTokens,
outputTokens
};
} catch (error) {
console.log(error);

return {
result: [],
tokens: 0
inputTokens: 0,
outputTokens: 0
};
}
}
14 changes: 9 additions & 5 deletions packages/service/core/ai/functions/queryExtension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { replaceVariable } from '@fastgpt/global/common/string/tools';
import { createChatCompletion } from '../config';
import { ChatItemType } from '@fastgpt/global/core/chat/type';
import { countGptMessagesTokens } from '../../../common/string/tiktoken/index';
import { countGptMessagesTokens, countPromptTokens } from '../../../common/string/tiktoken/index';
import { chatValue2RuntimePrompt } from '@fastgpt/global/core/chat/adapt';
import { getLLMModel } from '../model';
import { llmCompletionsBodyFormat } from '../utils';
Expand Down Expand Up @@ -121,7 +121,8 @@ export const queryExtension = async ({
rawQuery: string;
extensionQueries: string[];
model: string;
tokens: number;
inputTokens: number;
outputTokens: number;
}> => {
const systemFewShot = chatBg
? `Q: 对话背景。
Expand Down Expand Up @@ -166,7 +167,8 @@ A: ${chatBg}
rawQuery: query,
extensionQueries: [],
model,
tokens: 0
inputTokens: 0,
outputTokens: 0
};
}

Expand All @@ -181,15 +183,17 @@ A: ${chatBg}
rawQuery: query,
extensionQueries: Array.isArray(queries) ? queries : [],
model,
tokens: await countGptMessagesTokens(messages)
inputTokens: await countGptMessagesTokens(messages),
outputTokens: await countPromptTokens(answer)
};
} catch (error) {
addLog.error(`Query extension error`, error);
return {
rawQuery: query,
extensionQueries: [],
model,
tokens: 0
inputTokens: 0,
outputTokens: 0
};
}
};
1 change: 1 addition & 0 deletions packages/service/core/ai/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const getLLMModel = (model?: string) => {
global.llmModels[0]
);
};

export const getDatasetModel = (model?: string) => {
return (
global.llmModels
Expand Down
19 changes: 13 additions & 6 deletions packages/service/core/workflow/dispatch/agent/classifyQuestion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { chats2GPTMessages } from '@fastgpt/global/core/chat/adapt';
import { countMessagesTokens } from '../../../../common/string/tiktoken/index';
import {
countGptMessagesTokens,
countPromptTokens
} from '../../../../common/string/tiktoken/index';
import type { ChatItemType } from '@fastgpt/global/core/chat/type.d';
import { ChatItemValueTypeEnum, ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
import { createChatCompletion } from '../../../ai/config';
Expand Down Expand Up @@ -49,7 +52,7 @@ export const dispatchClassifyQuestion = async (props: Props): Promise<CQResponse

const chatHistories = getHistories(history, histories);

const { arg, tokens } = await completions({
const { arg, inputTokens, outputTokens } = await completions({
...props,
histories: chatHistories,
cqModel
Expand All @@ -59,7 +62,8 @@ export const dispatchClassifyQuestion = async (props: Props): Promise<CQResponse

const { totalPoints, modelName } = formatModelChars2Points({
model: cqModel.model,
tokens,
inputTokens: inputTokens,
outputTokens: outputTokens,
modelType: ModelTypeEnum.llm
});

Expand All @@ -72,7 +76,8 @@ export const dispatchClassifyQuestion = async (props: Props): Promise<CQResponse
totalPoints: externalProvider.openaiAccount?.key ? 0 : totalPoints,
model: modelName,
query: userChatInput,
tokens,
inputTokens: inputTokens,
outputTokens: outputTokens,
cqList: agents,
cqResult: result.value,
contextTotalLen: chatHistories.length + 2
Expand All @@ -82,7 +87,8 @@ export const dispatchClassifyQuestion = async (props: Props): Promise<CQResponse
moduleName: name,
totalPoints: externalProvider.openaiAccount?.key ? 0 : totalPoints,
model: modelName,
tokens
inputTokens: inputTokens,
outputTokens: outputTokens
}
]
};
Expand Down Expand Up @@ -148,7 +154,8 @@ const completions = async ({
}

return {
tokens: await countMessagesTokens(messages),
inputTokens: await countGptMessagesTokens(requestMessages),
outputTokens: await countPromptTokens(answer),
arg: { type: id }
};
};
Loading

0 comments on commit 50bf7f9

Please sign in to comment.