Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

fix: 查询使用量支持代理&修正使用量文案 #1296

Merged
merged 3 commits into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions service/src/chatgpt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import fetch from 'node-fetch'
import { sendResponse } from '../utils'
import { isNotEmptyString } from '../utils/is'
import type { ApiModel, ChatContext, ChatGPTUnofficialProxyAPIOptions, ModelConfig } from '../types'
import type { BalanceResponse, RequestOptions } from './types'
import type { RequestOptions, SetProxyOptions, UsageResponse } from './types'

const { HttpsProxyAgent } = httpsProxyAgent

Expand Down Expand Up @@ -124,9 +124,7 @@ async function chatReplyProcess(options: RequestOptions) {
}
}

async function fetchBalance() {
// 计算起始日期和结束日期

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

Expand All @@ -147,14 +145,21 @@ async function fetchBalance() {
'Content-Type': 'application/json',
}

const options = {} as SetProxyOptions

setupProxy(options)

try {
// 获取已使用量
const useResponse = await fetch(urlUsage, { headers })
const usageData = await useResponse.json() as BalanceResponse
const useResponse = await options.fetch(urlUsage, { headers })
if (!useResponse.ok)
throw new Error('获取使用量失败')
const usageData = await useResponse.json() as UsageResponse
const usage = Math.round(usageData.total_usage) / 100
return Promise.resolve(usage ? `$${usage}` : '-')
}
catch {
catch (error) {
global.console.log(error)
return Promise.resolve('-')
}
}
Expand All @@ -170,19 +175,19 @@ function formatDate(): string[] {
}

async function chatConfig() {
const balance = await fetchBalance()
const usage = await fetchUsage()
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, balance },
data: { apiModel, reverseProxy, timeoutMs, socksProxy, httpsProxy, usage },
})
}

function setupProxy(options: ChatGPTAPIOptions | ChatGPTUnofficialProxyAPIOptions) {
function setupProxy(options: SetProxyOptions) {
if (isNotEmptyString(process.env.SOCKS_PROXY_HOST) && isNotEmptyString(process.env.SOCKS_PROXY_PORT)) {
const agent = new SocksProxyAgent({
hostname: process.env.SOCKS_PROXY_HOST,
Expand All @@ -194,17 +199,20 @@ function setupProxy(options: ChatGPTAPIOptions | ChatGPTUnofficialProxyAPIOption
return fetch(url, { agent, ...options })
}
}
else {
if (isNotEmptyString(process.env.HTTPS_PROXY) || isNotEmptyString(process.env.ALL_PROXY)) {
const httpsProxy = process.env.HTTPS_PROXY || process.env.ALL_PROXY
if (httpsProxy) {
const agent = new HttpsProxyAgent(httpsProxy)
options.fetch = (url, options) => {
return fetch(url, { agent, ...options })
}
else if (isNotEmptyString(process.env.HTTPS_PROXY) || isNotEmptyString(process.env.ALL_PROXY)) {
const httpsProxy = process.env.HTTPS_PROXY || process.env.ALL_PROXY
if (httpsProxy) {
const agent = new HttpsProxyAgent(httpsProxy)
options.fetch = (url, options) => {
return fetch(url, { agent, ...options })
}
}
}
else {
options.fetch = (url, options) => {
return fetch(url, { ...options })
}
}
}

function currentModel(): ApiModel {
Expand Down
7 changes: 6 additions & 1 deletion service/src/chatgpt/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ChatMessage } from 'chatgpt'
import type fetch from 'node-fetch'

export interface RequestOptions {
message: string
Expand All @@ -7,6 +8,10 @@ export interface RequestOptions {
systemMessage?: string
}

export interface BalanceResponse {
export interface SetProxyOptions {
fetch?: typeof fetch
}

export interface UsageResponse {
total_usage: number
}
2 changes: 1 addition & 1 deletion service/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface ModelConfig {
timeoutMs?: number
socksProxy?: string
httpsProxy?: string
balance?: string
usage?: string
}

export type ApiModel = 'ChatGPTAPI' | 'ChatGPTUnofficialProxyAPI' | undefined
5 changes: 2 additions & 3 deletions src/components/common/Setting/About.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface ConfigState {
apiModel?: string
socksProxy?: string
httpsProxy?: string
balance?: string
usage?: string
}

const authStore = useAuthStore()
Expand Down Expand Up @@ -62,8 +62,7 @@ onMounted(() => {
</div>
<p>{{ $t("setting.api") }}:{{ config?.apiModel ?? '-' }}</p>
<p v-if="isChatGPTAPI">
{{ $t("setting.balance") }}:{{ config?.balance ?? '-' }}
<span class="text-xs text-neutral-400">({{ $t('setting.monthlyUsage') }})</span>
{{ $t("setting.monthlyUsage") }}:{{ config?.usage ?? '-' }}
</p>
<p v-if="!isChatGPTAPI">
{{ $t("setting.reverseProxy") }}:{{ config?.reverseProxy ?? '-' }}
Expand Down