Skip to content

Commit

Permalink
feat: show api balance (Chanzhaoyu#582)
Browse files Browse the repository at this point in the history
* feat: show api balance

* Update index.ts

* 保留小数点后五位

* perf: 判断优化

---------

Co-authored-by: Redon <[email protected]>
  • Loading branch information
pzcn and Chanzhaoyu committed Mar 17, 2023
1 parent 116ed7b commit e46d368
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 4 deletions.
1 change: 1 addition & 0 deletions service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"common:cleanup": "rimraf node_modules && rimraf pnpm-lock.yaml"
},
"dependencies": {
"axios": "^1.3.4",
"chatgpt": "^5.0.10",
"dotenv": "^16.0.3",
"esno": "^0.16.3",
Expand Down
51 changes: 51 additions & 0 deletions service/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 30 additions & 4 deletions service/src/chatgpt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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'
Expand Down Expand Up @@ -99,14 +100,35 @@ async function chatReplyProcess(
}
}

async function fetchBalance() {
const OPENAI_API_KEY = process.env.OPENAI_API_KEY
if (!isNotEmptyString(OPENAI_API_KEY))
return Promise.resolve('-')

try {
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${OPENAI_API_KEY}`,
}
const response = await axios.get('https://api.openai.com/dashboard/billing/credit_grants', { headers })
const balance = response.data.total_available ?? 0
return Promise.resolve(balance.toFixed(3))
}
catch {
return Promise.resolve('-')
}
}

async function chatConfig() {
const balance = await fetchBalance()
const reverseProxy = process.env.API_REVERSE_PROXY ?? '-'
const socksProxy = (process.env.SOCKS_PROXY_HOST && process.env.SOCKS_PROXY_PORT) ? (`${process.env.SOCKS_PROXY_HOST}:${process.env.SOCKS_PROXY_PORT}`) : '-'
const httpsProxy = (process.env.HTTPS_PROXY || process.env.ALL_PROXY) ?? '-'

const socksProxy = (process.env.SOCKS_PROXY_HOST && process.env.SOCKS_PROXY_PORT)
? (`${process.env.SOCKS_PROXY_HOST}:${process.env.SOCKS_PROXY_PORT}`)
: '-'
return sendResponse<ModelConfig>({
type: 'Success',
data: { apiModel, reverseProxy, timeoutMs, socksProxy, httpsProxy },
data: { apiModel, reverseProxy, timeoutMs, socksProxy, httpsProxy, balance },
})
}

Expand All @@ -133,6 +155,10 @@ function setupProxy(options: ChatGPTAPIOptions | ChatGPTUnofficialProxyAPIOption
}
}

function currentModel(): ApiModel {
return apiModel
}

export type { ChatContext, ChatMessage }

export { chatReplyProcess, chatConfig }
export { chatReplyProcess, chatConfig, currentModel }
1 change: 1 addition & 0 deletions service/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface ModelConfig {
timeoutMs?: number
socksProxy?: string
httpsProxy?: string
balance?: string
}

export type ApiModel = 'ChatGPTAPI' | 'ChatGPTUnofficialProxyAPI' | undefined
2 changes: 2 additions & 0 deletions src/components/common/Setting/About.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface ConfigState {
apiModel?: string
socksProxy?: string
httpsProxy?: string
balance?: string
}
const loading = ref(false)
Expand Down Expand Up @@ -55,6 +56,7 @@ onMounted(() => {
</p>
</div>
<p>{{ $t("setting.api") }}:{{ config?.apiModel ?? '-' }}</p>
<p>{{ $t("setting.balance") }}:{{ config?.balance ?? '-' }}</p>
<p>{{ $t("setting.reverseProxy") }}:{{ config?.reverseProxy ?? '-' }}</p>
<p>{{ $t("setting.timeout") }}:{{ config?.timeoutMs ?? '-' }}</p>
<p>{{ $t("setting.socks") }}:{{ config?.socksProxy ?? '-' }}</p>
Expand Down
1 change: 1 addition & 0 deletions src/locales/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default {
timeout: 'Timeout',
socks: 'Socks',
httpsProxy: 'HTTPS Proxy',
balance: 'API Balance',
},
store: {
local: 'Local',
Expand Down
1 change: 1 addition & 0 deletions src/locales/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default {
timeout: '超时',
socks: 'Socks',
httpsProxy: 'HTTPS Proxy',
balance: 'API余额',
},
store: {
local: '本地',
Expand Down
1 change: 1 addition & 0 deletions src/locales/zh-TW.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default {
timeout: '逾時',
socks: 'Socks',
httpsProxy: 'HTTPS Proxy',
balance: 'API余額',
},
store: {
local: '本機',
Expand Down

0 comments on commit e46d368

Please sign in to comment.