feat: show api balance (#582)

* feat: show api balance

* Update index.ts

* 保留小数点后五位

* perf: 判断优化

---------

Co-authored-by: Redon <790348264@qq.com>
This commit is contained in:
pzcn
2023-03-17 09:21:25 +08:00
committed by GitHub
parent 116ed7b0bf
commit e46d3685a0
8 changed files with 90 additions and 6 deletions

View File

@@ -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'
@@ -99,14 +100,35 @@ async function chatReplyProcess(
}
}
async function chatConfig() {
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) ?? '-'
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 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 },
})
}
@@ -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 }

View File

@@ -20,6 +20,7 @@ export interface ModelConfig {
timeoutMs?: number
socksProxy?: string
httpsProxy?: string
balance?: string
}
export type ApiModel = 'ChatGPTAPI' | 'ChatGPTUnofficialProxyAPI' | undefined