calculate streaming response cost
This commit is contained in:
@@ -49,6 +49,12 @@ interface LogprobsContent {
|
|||||||
logprob: number;
|
logprob: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface StreamingUsage {
|
||||||
|
prompt_tokens: number;
|
||||||
|
completion_tokens: number;
|
||||||
|
total_tokens: number;
|
||||||
|
}
|
||||||
|
|
||||||
export interface StreamingResponseChunk {
|
export interface StreamingResponseChunk {
|
||||||
id: string;
|
id: string;
|
||||||
object: string;
|
object: string;
|
||||||
@@ -56,6 +62,7 @@ export interface StreamingResponseChunk {
|
|||||||
model: string;
|
model: string;
|
||||||
system_fingerprint: string;
|
system_fingerprint: string;
|
||||||
choices: Choices[];
|
choices: Choices[];
|
||||||
|
usage: null | StreamingUsage;
|
||||||
}
|
}
|
||||||
export const getMessageText = (message: Message): string => {
|
export const getMessageText = (message: Message): string => {
|
||||||
if (typeof message.content === "string") {
|
if (typeof message.content === "string") {
|
||||||
@@ -224,6 +231,11 @@ class Chat {
|
|||||||
presence_penalty: this.presence_penalty,
|
presence_penalty: this.presence_penalty,
|
||||||
frequency_penalty: this.frequency_penalty,
|
frequency_penalty: this.frequency_penalty,
|
||||||
};
|
};
|
||||||
|
if (stream) {
|
||||||
|
body["stream_options"] = {
|
||||||
|
include_usage: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
if (this.enable_temperature) {
|
if (this.enable_temperature) {
|
||||||
body["temperature"] = this.temperature;
|
body["temperature"] = this.temperature;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import ChatGPT, {
|
|||||||
MessageDetail,
|
MessageDetail,
|
||||||
ToolCall,
|
ToolCall,
|
||||||
Logprobs,
|
Logprobs,
|
||||||
|
StreamingUsage,
|
||||||
} from "@/chatgpt";
|
} from "@/chatgpt";
|
||||||
import {
|
import {
|
||||||
ChatStore,
|
ChatStore,
|
||||||
@@ -88,9 +89,13 @@ export default function ChatBOX(props: {
|
|||||||
content: [],
|
content: [],
|
||||||
};
|
};
|
||||||
let response_model_name: string | null = null;
|
let response_model_name: string | null = null;
|
||||||
|
let usage: StreamingUsage | null = null;
|
||||||
for await (const i of client.processStreamResponse(response)) {
|
for await (const i of client.processStreamResponse(response)) {
|
||||||
response_model_name = i.model;
|
response_model_name = i.model;
|
||||||
responseTokenCount += 1;
|
responseTokenCount += 1;
|
||||||
|
if (i.usage) {
|
||||||
|
usage = i.usage;
|
||||||
|
}
|
||||||
|
|
||||||
const c = i.choices[0];
|
const c = i.choices[0];
|
||||||
|
|
||||||
@@ -160,6 +165,17 @@ export default function ChatBOX(props: {
|
|||||||
sum += msg.token;
|
sum += msg.token;
|
||||||
}
|
}
|
||||||
cost += sum * (models[response_model_name]?.price?.prompt ?? 0);
|
cost += sum * (models[response_model_name]?.price?.prompt ?? 0);
|
||||||
|
if (usage) {
|
||||||
|
// use the response usage if exists
|
||||||
|
cost = 0;
|
||||||
|
cost +=
|
||||||
|
(usage.prompt_tokens ?? 0) *
|
||||||
|
(models[response_model_name]?.price?.prompt ?? 0);
|
||||||
|
cost +=
|
||||||
|
(usage.completion_tokens ?? 0) *
|
||||||
|
models[response_model_name]?.price?.completion;
|
||||||
|
console.log("usage", usage, "cost", cost);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("cost", cost);
|
console.log("cost", cost);
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ export const newChatStore = (options: NewChatStoreOptions): ChatStore => {
|
|||||||
chatgpt_api_web_version: CHATGPT_API_WEB_VERSION,
|
chatgpt_api_web_version: CHATGPT_API_WEB_VERSION,
|
||||||
systemMessageContent: getDefaultParams(
|
systemMessageContent: getDefaultParams(
|
||||||
"sys",
|
"sys",
|
||||||
options.systemMessageContent ?? ""
|
options.systemMessageContent ?? "",
|
||||||
),
|
),
|
||||||
toolsString: options.toolsString ?? "",
|
toolsString: options.toolsString ?? "",
|
||||||
history: [],
|
history: [],
|
||||||
@@ -50,14 +50,14 @@ export const newChatStore = (options: NewChatStoreOptions): ChatStore => {
|
|||||||
maxTokens: getDefaultParams(
|
maxTokens: getDefaultParams(
|
||||||
"max",
|
"max",
|
||||||
models[getDefaultParams("model", options.model ?? DefaultModel)]
|
models[getDefaultParams("model", options.model ?? DefaultModel)]
|
||||||
?.maxToken ?? 2048
|
?.maxToken ?? 2048,
|
||||||
),
|
),
|
||||||
maxGenTokens: 2048,
|
maxGenTokens: 2048,
|
||||||
maxGenTokens_enabled: false,
|
maxGenTokens_enabled: false,
|
||||||
apiKey: getDefaultParams("key", options.apiKey ?? ""),
|
apiKey: getDefaultParams("key", options.apiKey ?? ""),
|
||||||
apiEndpoint: getDefaultParams(
|
apiEndpoint: getDefaultParams(
|
||||||
"api",
|
"api",
|
||||||
options.apiEndpoint ?? DefaultAPIEndpoint
|
options.apiEndpoint ?? DefaultAPIEndpoint,
|
||||||
),
|
),
|
||||||
streamMode: getDefaultParams("mode", options.streamMode ?? true),
|
streamMode: getDefaultParams("mode", options.streamMode ?? true),
|
||||||
model: getDefaultParams("model", options.model ?? DefaultModel),
|
model: getDefaultParams("model", options.model ?? DefaultModel),
|
||||||
@@ -71,12 +71,12 @@ export const newChatStore = (options: NewChatStoreOptions): ChatStore => {
|
|||||||
develop_mode: getDefaultParams("dev", options.dev ?? false),
|
develop_mode: getDefaultParams("dev", options.dev ?? false),
|
||||||
whisper_api: getDefaultParams(
|
whisper_api: getDefaultParams(
|
||||||
"whisper-api",
|
"whisper-api",
|
||||||
options.whisper_api ?? "https://api.openai.com/v1/audio/transcriptions"
|
options.whisper_api ?? "https://api.openai.com/v1/audio/transcriptions",
|
||||||
),
|
),
|
||||||
whisper_key: getDefaultParams("whisper-key", options.whisper_key ?? ""),
|
whisper_key: getDefaultParams("whisper-key", options.whisper_key ?? ""),
|
||||||
tts_api: getDefaultParams(
|
tts_api: getDefaultParams(
|
||||||
"tts-api",
|
"tts-api",
|
||||||
options.tts_api ?? "https://api.openai.com/v1/audio/speech"
|
options.tts_api ?? "https://api.openai.com/v1/audio/speech",
|
||||||
),
|
),
|
||||||
tts_key: getDefaultParams("tts-key", options.tts_key ?? ""),
|
tts_key: getDefaultParams("tts-key", options.tts_key ?? ""),
|
||||||
tts_voice: options.tts_voice ?? "alloy",
|
tts_voice: options.tts_voice ?? "alloy",
|
||||||
|
|||||||
Reference in New Issue
Block a user