feat: 增加API key和设定的配置功能

This commit is contained in:
ikechan8370 2023-03-08 10:54:10 +08:00
parent d1a8f667d6
commit fa1b1877d0
5 changed files with 109 additions and 9 deletions

View file

@ -7,6 +7,7 @@ import crypto from 'crypto'
import HttpsProxyAgent from 'https-proxy-agent'
import { Config } from './config.js'
import {isCN} from "./common.js";
if (!globalThis.fetch) {
globalThis.fetch = fetch
@ -58,9 +59,9 @@ export default class SydneyAIClient {
...opts,
host: opts.host || Config.sydneyReverseProxy || 'https://www.bing.com'
}
if (opts.proxy && !Config.sydneyForceUseReverse) {
this.opts.host = 'https://www.bing.com'
}
// if (opts.proxy && !Config.sydneyForceUseReverse) {
// this.opts.host = 'https://www.bing.com'
// }
this.debug = opts.debug
}
@ -102,6 +103,11 @@ export default class SydneyAIClient {
if (this.opts.proxy) {
fetchOptions.agent = proxy(Config.proxy)
}
let accessible = !(await isCN()) || this.opts.proxy
if (accessible && !Config.sydneyForceUseReverse) {
// 本身能访问bing.com那就不用反代啦重置host
this.opts.host = 'https://www.bing.com'
}
const response = await fetch(`${this.opts.host}/turing/conversation/create`, fetchOptions)
let text = await response.text()
try {

View file

@ -351,3 +351,20 @@ export function formatDuration (duration) {
return result || '0秒钟'
}
/**
* 判断服务器所在地是否为中国
* @returns {Promise<boolean>}
*/
export async function isCN () {
if (await redis.get('CHATGPT:COUNTRY_CODE')) {
return await redis.get('CHATGPT:COUNTRY_CODE') === 'CN'
} else {
let response = await fetch('https://ipinfo.io/country')
let countryCode = await response.text()
await redis.set('CHATGPT:COUNTRY_CODE', countryCode, { EX: 3600 * 24 * 7 })
if (countryCode !== 'CN') {
return false
}
}
}

View file

@ -2,6 +2,10 @@ import fs from 'fs'
import lodash from 'lodash'
export const defaultChatGPTAPI = 'https://pimon.d201.cn/backend-api/conversation'
export const officialChatGPTAPI = 'https://apps.openai.com/api/conversation'
// Reverse proxy of https://api.openai.com
export const defaultOpenAIReverseProxy = 'https://mondstadt.d201.eu.org'
// blocked in China Mainland
export const defaultOpenAIAPI = 'https://api.openai.com'
const defaultConfig = {
blockWords: ['屏蔽词1', '屏蔽词b'],
promptBlockWords: ['屏蔽词1', '屏蔽词b'],
@ -20,7 +24,7 @@ const defaultConfig = {
cacheUrl: 'https://content.alcedogroup.com',
cacheEntry: false,
apiKey: '',
openAiBaseUrl: 'https://api.openai.com',
openAiBaseUrl: defaultOpenAIReverseProxy,
openAiForceUseReverse: false,
drawCD: 30,
model: '',
@ -51,7 +55,7 @@ const defaultConfig = {
noiseScaleW: 0.668,
lengthScale: 1.2,
initiativeChatGroups: [],
version: 'v2.1.0'
version: 'v2.1.1'
}
const _path = process.cwd()
let config = {}