feat: get tokens from api usage (#4671)

This commit is contained in:
Archer
2025-04-27 01:13:38 +08:00
committed by GitHub
parent 0720bbe4da
commit 1465999c46
26 changed files with 270 additions and 223 deletions

View File

@@ -16,7 +16,7 @@ import { reRankRecall } from '@fastgpt/service/core/ai/rerank';
import { aiTranscriptions } from '@fastgpt/service/core/ai/audio/transcriptions';
import { isProduction } from '@fastgpt/global/common/system/constants';
import * as fs from 'fs';
import { llmCompletionsBodyFormat } from '@fastgpt/service/core/ai/utils';
import { llmCompletionsBodyFormat, llmResponseToAnswerText } from '@fastgpt/service/core/ai/utils';
export type testQuery = { model: string; channelId?: number };
@@ -88,23 +88,10 @@ const testLLMModel = async (model: LLMModelItemType, headers: Record<string, str
}
}
});
const { text: answer } = await llmResponseToAnswerText(response);
if (isStreamResponse) {
for await (const part of response) {
const content = part.choices?.[0]?.delta?.content || '';
// @ts-ignore
const reasoningContent = part.choices?.[0]?.delta?.reasoning_content || '';
if (content || reasoningContent) {
response?.controller?.abort();
return;
}
}
} else {
addLog.info(`Model not stream response`);
const answer = response.choices?.[0]?.message?.content || '';
if (answer) {
return answer;
}
if (answer) {
return answer;
}
return Promise.reject('Model response empty');