diff --git a/service/src/chatgpt/index.ts b/service/src/chatgpt/index.ts index 7e28ab1..1986540 100644 --- a/service/src/chatgpt/index.ts +++ b/service/src/chatgpt/index.ts @@ -5,11 +5,10 @@ import { ChatGPTAPI, ChatGPTUnofficialProxyAPI } from 'chatgpt' import { SocksProxyAgent } from 'socks-proxy-agent' import httpsProxyAgent from 'https-proxy-agent' import fetch from 'node-fetch' -import axios from 'axios' import { sendResponse } from '../utils' import { isNotEmptyString } from '../utils/is' import type { ApiModel, ChatContext, ChatGPTUnofficialProxyAPIOptions, ModelConfig } from '../types' -import type { RequestOptions } from './types' +import type { BalanceResponse, RequestOptions } from './types' const { HttpsProxyAgent } = httpsProxyAgent @@ -126,6 +125,8 @@ async function chatReplyProcess(options: RequestOptions) { } async function fetchBalance() { + // 计算起始日期和结束日期 + const OPENAI_API_KEY = process.env.OPENAI_API_KEY const OPENAI_API_BASE_URL = process.env.OPENAI_API_BASE_URL @@ -136,17 +137,38 @@ async function fetchBalance() { ? OPENAI_API_BASE_URL : 'https://api.openai.com' + const [startDate, endDate] = formatDate() + + // 每月使用量 + const urlUsage = `${API_BASE_URL}/v1/dashboard/billing/usage?start_date=${startDate}&end_date=${endDate}` + + const headers = { + 'Authorization': `Bearer ${OPENAI_API_KEY}`, + 'Content-Type': 'application/json', + } + try { - const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${OPENAI_API_KEY}` } - const response = await axios.get(`${API_BASE_URL}/dashboard/billing/credit_grants`, { headers }) - const balance = response.data.total_available ?? 0 - return Promise.resolve(balance.toFixed(3)) + // 获取已使用量 + const useResponse = await fetch(urlUsage, { headers }) + const usageData = await useResponse.json() as BalanceResponse + const usage = Math.round(usageData.total_usage) / 100 + return Promise.resolve(usage ? `$${usage}` : '-') } catch { return Promise.resolve('-') } } +function formatDate(): string[] { + const today = new Date() + const year = today.getFullYear() + const month = today.getMonth() + 1 + const lastDay = new Date(year, month, 0) + const formattedFirstDay = `${year}-${month.toString().padStart(2, '0')}-01` + const formattedLastDay = `${year}-${month.toString().padStart(2, '0')}-${lastDay.getDate().toString().padStart(2, '0')}` + return [formattedFirstDay, formattedLastDay] +} + async function chatConfig() { const balance = await fetchBalance() const reverseProxy = process.env.API_REVERSE_PROXY ?? '-' diff --git a/service/src/chatgpt/types.ts b/service/src/chatgpt/types.ts index 1e65f4c..420f465 100644 --- a/service/src/chatgpt/types.ts +++ b/service/src/chatgpt/types.ts @@ -6,3 +6,7 @@ export interface RequestOptions { process?: (chat: ChatMessage) => void systemMessage?: string } + +export interface BalanceResponse { + total_usage: number +} diff --git a/src/components/common/Setting/About.vue b/src/components/common/Setting/About.vue index 3edb317..d3bd423 100644 --- a/src/components/common/Setting/About.vue +++ b/src/components/common/Setting/About.vue @@ -63,6 +63,7 @@ onMounted(() => {
{{ $t("setting.api") }}:{{ config?.apiModel ?? '-' }}
{{ $t("setting.balance") }}:{{ config?.balance ?? '-' }} + ({{ $t('setting.monthlyUsage') }})
{{ $t("setting.reverseProxy") }}:{{ config?.reverseProxy ?? '-' }} diff --git a/src/locales/en-US.ts b/src/locales/en-US.ts index f6fe515..e524f37 100644 --- a/src/locales/en-US.ts +++ b/src/locales/en-US.ts @@ -69,6 +69,7 @@ export default { socks: 'Socks', httpsProxy: 'HTTPS Proxy', balance: 'API Balance', + monthlyUsage: 'Monthly Usage', }, store: { siderButton: 'Prompt Store', diff --git a/src/locales/zh-CN.ts b/src/locales/zh-CN.ts index cffcac3..80cfb23 100644 --- a/src/locales/zh-CN.ts +++ b/src/locales/zh-CN.ts @@ -69,6 +69,7 @@ export default { socks: 'Socks', httpsProxy: 'HTTPS Proxy', balance: 'API余额', + monthlyUsage: '本月使用量', }, store: { siderButton: '提示词商店', diff --git a/src/locales/zh-TW.ts b/src/locales/zh-TW.ts index 9a5abd6..9c0549b 100644 --- a/src/locales/zh-TW.ts +++ b/src/locales/zh-TW.ts @@ -69,6 +69,7 @@ export default { socks: 'Socks', httpsProxy: 'HTTPS Proxy', balance: 'API余額', + monthlyUsage: '本月使用量', }, store: { siderButton: '提示詞商店',