Skip to content

Commit

Permalink
feat: 查询余额使用 API_BASE_URL 参数
Browse files Browse the repository at this point in the history
  • Loading branch information
Chanzhaoyu committed Mar 17, 2023
1 parent 8fad91a commit 08b5772
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions service/src/chatgpt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,18 @@ async function chatReplyProcess(

async function fetchBalance() {
const OPENAI_API_KEY = process.env.OPENAI_API_KEY
const OPENAI_API_BASE_URL = process.env.OPENAI_API_BASE_URL

if (!isNotEmptyString(OPENAI_API_KEY))
return Promise.resolve('-')

const API_BASE_URL = isNotEmptyString(OPENAI_API_BASE_URL)
? OPENAI_API_BASE_URL
: 'https://api.openai.com'

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 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))
}
Expand Down

0 comments on commit 08b5772

Please sign in to comment.