From 18a1d6002856ba0b9d56b9bbcfca36dd57d54d13 Mon Sep 17 00:00:00 2001 From: SchneeHertz <39257008+SchneeHertz@users.noreply.github.com> Date: Thu, 7 Sep 2023 10:18:22 +0800 Subject: [PATCH] add proxy switch --- index.js | 3 +- modules/common.js | 15 +++-- modules/edge-tts.js | 127 ------------------------------------- modules/functions.js | 6 +- package.json | 2 +- src/components/Setting.vue | 5 +- utils/loadConfig.js | 1 + 7 files changed, 21 insertions(+), 138 deletions(-) delete mode 100644 modules/edge-tts.js diff --git a/index.js b/index.js index cd5a63c..4107904 100644 --- a/index.js +++ b/index.js @@ -21,6 +21,7 @@ const { SpeechSynthesisVoiceName, ADMIN_NAME, AI_NAME, systemPrompt, + useProxy, proxyObject, } = config const proxyString = `${proxyObject.protocol}://${proxyObject.host}:${proxyObject.port}` @@ -63,7 +64,7 @@ let tts = new EdgeTTS({ voice: SpeechSynthesisVoiceName, lang: 'zh-CN', outputFormat: 'audio-24khz-96kbitrate-mono-mp3', - proxy: proxyString, + proxy: useProxy ? proxyString : undefined, saveSubtitles: true }) diff --git a/modules/common.js b/modules/common.js index 9828f04..ce7a683 100644 --- a/modules/common.js +++ b/modules/common.js @@ -5,14 +5,19 @@ const _ = require('lodash') const { config: { OPENAI_API_KEY, OPENAI_API_ENDPOINT, DEFAULT_MODEL, AZURE_OPENAI_KEY, AZURE_OPENAI_ENDPOINT, AZURE_API_VERSION, AZURE_CHAT_MODEL, AZURE_EMBEDDING_MODEL, - proxyObject + useProxy, proxyObject } } = require('../utils/loadConfig.js') const proxyString = `${proxyObject.protocol}://${proxyObject.host}:${proxyObject.port}` +let httpAgent +try { + httpAgent = useProxy ? new HttpsProxyAgent(proxyString) :undefined +} catch {} + const openai = new OpenAI({ apiKey: OPENAI_API_KEY, baseURL: OPENAI_API_ENDPOINT ? OPENAI_API_ENDPOINT : 'https://api.openai.com/v1', - httpAgent: new HttpsProxyAgent(proxyString), + httpAgent, timeout: 40000 }) @@ -73,7 +78,7 @@ const azureOpenaiChat = ({ model = AZURE_CHAT_MODEL, messages, functions, functi baseURL: `https://${AZURE_OPENAI_ENDPOINT}.openai.azure.com/openai/deployments/${model}`, defaultQuery: { 'api-version': AZURE_API_VERSION }, defaultHeaders: { 'api-key': AZURE_OPENAI_KEY }, - httpAgent: new HttpsProxyAgent(proxyString), + httpAgent, timeout: 40000 }) @@ -90,7 +95,7 @@ const azureOpenaiChatStream = async function* ({ model = AZURE_CHAT_MODEL, messa baseURL: `https://${AZURE_OPENAI_ENDPOINT}.openai.azure.com/openai/deployments/${model}`, defaultQuery: { 'api-version': AZURE_API_VERSION }, defaultHeaders: { 'api-key': AZURE_OPENAI_KEY }, - httpAgent: new HttpsProxyAgent(proxyString), + httpAgent, timeout: 40000 }) @@ -124,7 +129,7 @@ const azureOpenaiEmbedding = ({ input, model = AZURE_EMBEDDING_MODEL }) => { baseURL: `https://${AZURE_OPENAI_ENDPOINT}.openai.azure.com/openai/deployments/${model}`, defaultQuery: { 'api-version': AZURE_API_VERSION }, defaultHeaders: { 'api-key': AZURE_OPENAI_KEY }, - httpAgent: new HttpsProxyAgent(proxyString), + httpAgent, timeout: 40000 }) diff --git a/modules/edge-tts.js b/modules/edge-tts.js deleted file mode 100644 index dc483ee..0000000 --- a/modules/edge-tts.js +++ /dev/null @@ -1,127 +0,0 @@ -const { randomBytes } = require('node:crypto') -const { writeFileSync, createWriteStream } = require('node:fs') -const { WebSocket } = require('ws') -const { HttpsProxyAgent } = require('https-proxy-agent') - - -class EdgeTTS { - - voice - lang - outputFormat - proxy - saveSubtitles - - constructor ({ - voice = 'zh-CN-XiaoyiNeural', - lang = 'zh-CN', - outputFormat = 'audio-24khz-48kbitrate-mono-mp3', - saveSubtitles = false, - proxy - }) { - this.voice = voice - this.lang = lang - this.outputFormat = outputFormat - this.saveSubtitles = saveSubtitles - this.proxy = proxy - } - - async _connectWebSocket () { - const wsConnect = new WebSocket(`wss://speech.platform.bing.com/consumer/speech/synthesize/readaloud/edge/v1?TrustedClientToken=6A5AA1D4EAFF4E9FB37E23D68491D6F4`, { - host: 'speech.platform.bing.com', - origin: 'chrome-extension://jdiccldimpdaibmpdkjnbmckianbfold', - headers: { - 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.66 Safari/537.36 Edg/103.0.1264.44', - }, - agent: this.proxy ? new HttpsProxyAgent(this.proxy) : undefined - }) - await new Promise((resolve, reject) => { - wsConnect.on('open', () => { - wsConnect.send(`Content-Type:application/json; charset=utf-8\r\nPath:speech.config\r\n\r\n - { - "context": { - "synthesis": { - "audio": { - "metadataoptions": { - "sentenceBoundaryEnabled": "false", - "wordBoundaryEnabled": "true" - }, - "outputFormat": "${this.outputFormat}" - } - } - } - } - `) - resolve() - }) - }) - return wsConnect - } - - _saveSubFile (subFile, text, audioPath) { - let subPath = audioPath + '.json' - let subChars = text.split('') - let subCharIndex = 0 - subFile.forEach((cue, index) => { - let fullPart = '' - let stepIndex = 0 - for (let sci = subCharIndex; sci < subChars.length; sci++) { - if (subChars[sci] === cue.part[stepIndex]) { - fullPart = fullPart + subChars[sci] - stepIndex += 1 - } else if (subChars[sci] === subFile?.[index + 1]?.part?.[0]) { - subCharIndex = sci - break - } else { - fullPart = fullPart + subChars[sci] - } - } - cue.part = fullPart - }) - writeFileSync(subPath, JSON.stringify(subFile, null, ' '), { encoding: 'utf-8' }) - } - - async ttsPromise (text, audioPath) { - const _wsConnect = await this._connectWebSocket() - return await new Promise((resolve, reject) => { - let audioStream = createWriteStream(audioPath) - let subFile = [] - _wsConnect.on('message', async (data, isBinary) => { - if (isBinary) { - let separator = 'Path:audio\r\n' - let index = data.indexOf(separator) + separator.length - let audioData = data.slice(index) - audioStream.write(audioData) - } else { - let message = data.toString() - if (message.includes('Path:turn.end')) { - audioStream.end() - if (this.saveSubtitles) { - this._saveSubFile(subFile, text, audioPath) - } - resolve() - } else if (message.includes('Path:audio.metadata')) { - let splitTexts = message.split('\r\n') - try { - let metadata = JSON.parse(splitTexts[splitTexts.length - 1]) - metadata['Metadata'].forEach(element => { - subFile.push({ part: element['Data']['text']['Text'], start: Math.floor(element['Data']['Offset'] / 10000) }) - }) - } catch {} - } - } - }) - let requestId = randomBytes(16).toString('hex') - _wsConnect.send(`X-RequestId:${requestId}\r\nContent-Type:application/ssml+xml\r\nPath:ssml\r\n\r\n - ` + ` - - ${text} - - `) - }) - } -} - -module.exports = { - EdgeTTS -} \ No newline at end of file diff --git a/modules/functions.js b/modules/functions.js index 7582618..7060445 100644 --- a/modules/functions.js +++ b/modules/functions.js @@ -7,7 +7,7 @@ const { getQuickJS, shouldInterruptAfterDeadline } = require('quickjs-emscripte const { shell } = require('electron') const { js: beautify } = require('js-beautify/js') -let { config: { proxyObject, AI_NAME, writeFolder, allowPowerfulInterpreter } } = require('../utils/loadConfig.js') +let { config: { useProxy, proxyObject, AI_NAME, writeFolder, allowPowerfulInterpreter } } = require('../utils/loadConfig.js') const proxyString = `${proxyObject.protocol}://${proxyObject.host}:${proxyObject.port}` const { sliceStringbyTokenLength } = require('./tiktoken.js') @@ -193,7 +193,7 @@ const functionAction = { } const getInformationFromGoogle = async ({ queryString }) => { - let options = { proxy: proxyString } + let options = { proxy: useProxy ? proxyString : undefined } let additionalQueryParam = { // lr: 'lang_zh-CN', // hl: 'zh-CN', @@ -206,7 +206,7 @@ const getInformationFromGoogle = async ({ queryString }) => { } const getContentOfWebpage = async ({ url }) => { - return await axios.get(url, { proxy: proxyObject }) + return await axios.get(url, { proxy: useProxy ? proxyObject : undefined }) .then(async res=>{ let html = await res.data let content = convert(html, { diff --git a/package.json b/package.json index 31bbc14..3d2c634 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "chat-xiuliu", "private": true, - "version": "2.1.2", + "version": "2.1.3", "scripts": { "dev": "vite", "build": "vite build", diff --git a/src/components/Setting.vue b/src/components/Setting.vue index d767840..e63deae 100644 --- a/src/components/Setting.vue +++ b/src/components/Setting.vue @@ -135,7 +135,10 @@ defineExpose({ - + + + + diff --git a/utils/loadConfig.js b/utils/loadConfig.js index bf9ec9c..1494c8a 100644 --- a/utils/loadConfig.js +++ b/utils/loadConfig.js @@ -26,6 +26,7 @@ try { systemPrompt: '你是虚拟猫娘休留,以下是你在回复时应该遵循的规则:\n1.你能够灵活的运用猫娘的风格进行回复.\n2.调用函数来提高你的回复质量', writeFolder: '', allowPowerfulInterpreter: false, + useProxy: false, proxyObject: { protocol: 'http', host: '127.0.0.1',