Skip to content

Commit e46d368

Browse files
pzcnChanzhaoyu
andauthored
feat: show api balance (#582)
* feat: show api balance * Update index.ts * 保留小数点后五位 * perf: 判断优化 --------- Co-authored-by: Redon <790348264@qq.com>
1 parent 116ed7b commit e46d368

File tree

8 files changed

+88
-4
lines changed

8 files changed

+88
-4
lines changed

service/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"common:cleanup": "rimraf node_modules && rimraf pnpm-lock.yaml"
2525
},
2626
"dependencies": {
27+
"axios": "^1.3.4",
2728
"chatgpt": "^5.0.10",
2829
"dotenv": "^16.0.3",
2930
"esno": "^0.16.3",

service/pnpm-lock.yaml

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

service/src/chatgpt/index.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ChatGPTAPI, ChatGPTUnofficialProxyAPI } from 'chatgpt'
55
import { SocksProxyAgent } from 'socks-proxy-agent'
66
import { HttpsProxyAgent } from 'https-proxy-agent'
77
import fetch from 'node-fetch'
8+
import axios from 'axios'
89
import { sendResponse } from '../utils'
910
import { isNotEmptyString } from '../utils/is'
1011
import type { ApiModel, ChatContext, ChatGPTUnofficialProxyAPIOptions, ModelConfig } from '../types'
@@ -99,14 +100,35 @@ async function chatReplyProcess(
99100
}
100101
}
101102

103+
async function fetchBalance() {
104+
const OPENAI_API_KEY = process.env.OPENAI_API_KEY
105+
if (!isNotEmptyString(OPENAI_API_KEY))
106+
return Promise.resolve('-')
107+
108+
try {
109+
const headers = {
110+
'Content-Type': 'application/json',
111+
'Authorization': `Bearer ${OPENAI_API_KEY}`,
112+
}
113+
const response = await axios.get('https://api.openai.com/dashboard/billing/credit_grants', { headers })
114+
const balance = response.data.total_available ?? 0
115+
return Promise.resolve(balance.toFixed(3))
116+
}
117+
catch {
118+
return Promise.resolve('-')
119+
}
120+
}
121+
102122
async function chatConfig() {
123+
const balance = await fetchBalance()
103124
const reverseProxy = process.env.API_REVERSE_PROXY ?? '-'
104-
const socksProxy = (process.env.SOCKS_PROXY_HOST && process.env.SOCKS_PROXY_PORT) ? (`${process.env.SOCKS_PROXY_HOST}:${process.env.SOCKS_PROXY_PORT}`) : '-'
105125
const httpsProxy = (process.env.HTTPS_PROXY || process.env.ALL_PROXY) ?? '-'
106-
126+
const socksProxy = (process.env.SOCKS_PROXY_HOST && process.env.SOCKS_PROXY_PORT)
127+
? (`${process.env.SOCKS_PROXY_HOST}:${process.env.SOCKS_PROXY_PORT}`)
128+
: '-'
107129
return sendResponse<ModelConfig>({
108130
type: 'Success',
109-
data: { apiModel, reverseProxy, timeoutMs, socksProxy, httpsProxy },
131+
data: { apiModel, reverseProxy, timeoutMs, socksProxy, httpsProxy, balance },
110132
})
111133
}
112134

@@ -133,6 +155,10 @@ function setupProxy(options: ChatGPTAPIOptions | ChatGPTUnofficialProxyAPIOption
133155
}
134156
}
135157

158+
function currentModel(): ApiModel {
159+
return apiModel
160+
}
161+
136162
export type { ChatContext, ChatMessage }
137163

138-
export { chatReplyProcess, chatConfig }
164+
export { chatReplyProcess, chatConfig, currentModel }

service/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface ModelConfig {
2020
timeoutMs?: number
2121
socksProxy?: string
2222
httpsProxy?: string
23+
balance?: string
2324
}
2425

2526
export type ApiModel = 'ChatGPTAPI' | 'ChatGPTUnofficialProxyAPI' | undefined

src/components/common/Setting/About.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ interface ConfigState {
1010
apiModel?: string
1111
socksProxy?: string
1212
httpsProxy?: string
13+
balance?: string
1314
}
1415
1516
const loading = ref(false)
@@ -55,6 +56,7 @@ onMounted(() => {
5556
</p>
5657
</div>
5758
<p>{{ $t("setting.api") }}:{{ config?.apiModel ?? '-' }}</p>
59+
<p>{{ $t("setting.balance") }}:{{ config?.balance ?? '-' }}</p>
5860
<p>{{ $t("setting.reverseProxy") }}:{{ config?.reverseProxy ?? '-' }}</p>
5961
<p>{{ $t("setting.timeout") }}:{{ config?.timeoutMs ?? '-' }}</p>
6062
<p>{{ $t("setting.socks") }}:{{ config?.socksProxy ?? '-' }}</p>

src/locales/en-US.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export default {
6363
timeout: 'Timeout',
6464
socks: 'Socks',
6565
httpsProxy: 'HTTPS Proxy',
66+
balance: 'API Balance',
6667
},
6768
store: {
6869
local: 'Local',

src/locales/zh-CN.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export default {
6363
timeout: '超时',
6464
socks: 'Socks',
6565
httpsProxy: 'HTTPS Proxy',
66+
balance: 'API余额',
6667
},
6768
store: {
6869
local: '本地',

src/locales/zh-TW.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export default {
6363
timeout: '逾時',
6464
socks: 'Socks',
6565
httpsProxy: 'HTTPS Proxy',
66+
balance: 'API余額',
6667
},
6768
store: {
6869
local: '本機',

0 commit comments

Comments
 (0)