From 36cdd701f934236198d438cf23b11a2cba18f5f9 Mon Sep 17 00:00:00 2001 From: ikechan8370 Date: Sat, 11 Mar 2023 20:25:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=94=BB=E5=9B=BE=E6=8A=A5=E9=94=99unde?= =?UTF-8?q?fined=E7=9A=84=E8=A7=A3=E5=86=B3=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/config.js | 2 +- utils/dalle.js | 29 ++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/utils/config.js b/utils/config.js index 868046d..fc8be37 100644 --- a/utils/config.js +++ b/utils/config.js @@ -1,7 +1,7 @@ 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' +export const officialChatGPTAPI = 'https://pimon.d201.cn/backend-api/conversation' // Reverse proxy of https://api.openai.com export const defaultOpenAIReverseProxy = 'https://mondstadt.d201.eu.org' // blocked in China Mainland diff --git a/utils/dalle.js b/utils/dalle.js index 2e29d71..02d1312 100644 --- a/utils/dalle.js +++ b/utils/dalle.js @@ -1,5 +1,5 @@ import { Configuration, OpenAIApi } from 'openai' -import { Config } from './config.js' +import {Config, defaultOpenAIReverseProxy, officialChatGPTAPI} from './config.js' import fs from 'fs' import { mkdirs } from './common.js' let proxy @@ -11,15 +11,21 @@ if (Config.proxy) { } } function getProxy () { - if (proxy) { + if (!Config.proxy || proxy) { return proxy } else { throw new Error('未安装https-proxy-agent,请在插件目录下执行pnpm add https-proxy-agent') } } export async function createImage (prompt, n = 1, size = '512x512') { + let basePath = Config.openAiBaseUrl + if (Config.openAiBaseUrl && Config.proxy && !Config.openAiForceUseReverse) { + // 如果配了proxy,而且有反代,但是没开启强制反代 + basePath = defaultOpenAIReverseProxy + } const configuration = new Configuration({ - apiKey: Config.apiKey + apiKey: Config.apiKey, + basePath: basePath + '/v1' }) const openai = new OpenAIApi(configuration) if (Config.debug) { @@ -38,8 +44,15 @@ export async function createImage (prompt, n = 1, size = '512x512') { } export async function imageVariation (imageUrl, n = 1, size = '512x512') { + + let basePath = Config.openAiBaseUrl + if (Config.openAiBaseUrl && Config.proxy && !Config.openAiForceUseReverse) { + // 如果配了proxy,而且有反代,但是没开启强制反代 + basePath = defaultOpenAIReverseProxy + } const configuration = new Configuration({ - apiKey: Config.apiKey + apiKey: Config.apiKey, + basePath: basePath + '/v1' }) const openai = new OpenAIApi(configuration) if (Config.debug) { @@ -106,8 +119,14 @@ async function resizeAndCropImage (inputFilePath, outputFilePath, size = 512) { } export async function editImage (originalImage, mask = [], prompt, num = 1, size = '512x512') { + let basePath = Config.openAiBaseUrl + if (Config.openAiBaseUrl && Config.proxy && !Config.openAiForceUseReverse) { + // 如果配了proxy,而且有反代,但是没开启强制反代 + basePath = defaultOpenAIReverseProxy + } const configuration = new Configuration({ - apiKey: Config.apiKey + apiKey: Config.apiKey, + basePath: basePath + '/v1' }) const openai = new OpenAIApi(configuration) if (Config.debug) {