Skip to content

Commit

Permalink
add proxy switch
Browse files Browse the repository at this point in the history
  • Loading branch information
SchneeHertz committed Sep 7, 2023
1 parent b99af5d commit 18a1d60
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 138 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
SpeechSynthesisVoiceName,
ADMIN_NAME, AI_NAME,
systemPrompt,
useProxy,
proxyObject,
} = config
const proxyString = `${proxyObject.protocol}://${proxyObject.host}:${proxyObject.port}`
Expand Down Expand Up @@ -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
})

Expand Down
15 changes: 10 additions & 5 deletions modules/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

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

Expand All @@ -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
})

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

Expand Down
127 changes: 0 additions & 127 deletions modules/edge-tts.js

This file was deleted.

6 changes: 3 additions & 3 deletions modules/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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',
Expand All @@ -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, {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "chat-xiuliu",
"private": true,
"version": "2.1.2",
"version": "2.1.3",
"scripts": {
"dev": "vite",
"build": "vite build",
Expand Down
5 changes: 4 additions & 1 deletion src/components/Setting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ defineExpose({
<n-form-item label="使用高级解释器" path="allowPowerfulInterpreter">
<n-switch v-model:value="config.allowPowerfulInterpreter" @update:value="alertJSPRisk"/>
</n-form-item>
<n-form-item label="代理服务器" :show-feedback="false">
<n-form-item label="使用代理服务器" path="useProxy">
<n-switch v-model:value="config.useProxy"/>
</n-form-item>
<n-form-item label="代理服务器" :show-feedback="false" v-if="config.useProxy">
<n-grid :cols="3" :x-gap="24">
<n-form-item-gi path="proxyObject.protocol">
<n-input v-model:value="config.proxyObject.protocol" placeholder="http"/>
Expand Down
1 change: 1 addition & 0 deletions utils/loadConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ try {
systemPrompt: '你是虚拟猫娘休留,以下是你在回复时应该遵循的规则:\n1.你能够灵活的运用猫娘的风格进行回复.\n2.调用函数来提高你的回复质量',
writeFolder: '',
allowPowerfulInterpreter: false,
useProxy: false,
proxyObject: {
protocol: 'http',
host: '127.0.0.1',
Expand Down

0 comments on commit 18a1d60

Please sign in to comment.