|
|
@ -1,22 +1,18 @@
|
|
|
|
import * as dotenv from 'dotenv'
|
|
|
|
import * as dotenv from 'dotenv'
|
|
|
|
import 'isomorphic-fetch'
|
|
|
|
import 'isomorphic-fetch'
|
|
|
|
import type { ChatGPTAPI, ChatMessage, SendMessageOptions } from 'chatgpt'
|
|
|
|
import type { ChatMessage, SendMessageOptions } from 'chatgpt'
|
|
|
|
import { ChatGPTUnofficialProxyAPI } from 'chatgpt'
|
|
|
|
import { ChatGPTAPI, ChatGPTUnofficialProxyAPI } from 'chatgpt'
|
|
|
|
import { SocksProxyAgent } from 'socks-proxy-agent'
|
|
|
|
import { SocksProxyAgent } from 'socks-proxy-agent'
|
|
|
|
import fetch from 'node-fetch'
|
|
|
|
import fetch from 'node-fetch'
|
|
|
|
import { sendResponse } from './utils'
|
|
|
|
import { sendResponse } from './utils'
|
|
|
|
|
|
|
|
import type { ApiModel, ChatContext, ChatGPTAPIOptions, ChatGPTUnofficialProxyAPIOptions, ModelConfig } from './types'
|
|
|
|
|
|
|
|
|
|
|
|
dotenv.config()
|
|
|
|
dotenv.config()
|
|
|
|
|
|
|
|
|
|
|
|
let apiModel: 'ChatGPTAPI' | 'ChatGPTUnofficialProxyAPI' | undefined
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface ChatContext {
|
|
|
|
|
|
|
|
conversationId?: string
|
|
|
|
|
|
|
|
parentMessageId?: string
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const timeoutMs: number = !isNaN(+process.env.TIMEOUT_MS) ? +process.env.TIMEOUT_MS : 30 * 1000
|
|
|
|
const timeoutMs: number = !isNaN(+process.env.TIMEOUT_MS) ? +process.env.TIMEOUT_MS : 30 * 1000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let apiModel: ApiModel
|
|
|
|
|
|
|
|
|
|
|
|
if (!process.env.OPENAI_API_KEY && !process.env.OPENAI_ACCESS_TOKEN)
|
|
|
|
if (!process.env.OPENAI_API_KEY && !process.env.OPENAI_ACCESS_TOKEN)
|
|
|
|
throw new Error('Missing OPENAI_API_KEY or OPENAI_ACCESS_TOKEN environment variable')
|
|
|
|
throw new Error('Missing OPENAI_API_KEY or OPENAI_ACCESS_TOKEN environment variable')
|
|
|
|
|
|
|
|
|
|
|
@ -25,15 +21,20 @@ let api: ChatGPTAPI | ChatGPTUnofficialProxyAPI
|
|
|
|
// To use ESM in CommonJS, you can use a dynamic import
|
|
|
|
// To use ESM in CommonJS, you can use a dynamic import
|
|
|
|
(async () => {
|
|
|
|
(async () => {
|
|
|
|
// More Info: https://github.com/transitive-bullshit/chatgpt-api
|
|
|
|
// More Info: https://github.com/transitive-bullshit/chatgpt-api
|
|
|
|
const { ChatGPTAPI } = await import('chatgpt')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (process.env.OPENAI_API_KEY) {
|
|
|
|
if (process.env.OPENAI_API_KEY) {
|
|
|
|
api = new ChatGPTAPI({ apiKey: process.env.OPENAI_API_KEY })
|
|
|
|
const options: ChatGPTAPIOptions = {
|
|
|
|
|
|
|
|
apiKey: process.env.OPENAI_API_KEY,
|
|
|
|
|
|
|
|
debug: false,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api = new ChatGPTAPI({ ...options })
|
|
|
|
apiModel = 'ChatGPTAPI'
|
|
|
|
apiModel = 'ChatGPTAPI'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
const options = {
|
|
|
|
const options: ChatGPTUnofficialProxyAPIOptions = {
|
|
|
|
debug: true,
|
|
|
|
accessToken: process.env.OPENAI_ACCESS_TOKEN,
|
|
|
|
|
|
|
|
debug: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (process.env.SOCKS_PROXY_HOST && process.env.SOCKS_PROXY_PORT) {
|
|
|
|
if (process.env.SOCKS_PROXY_HOST && process.env.SOCKS_PROXY_PORT) {
|
|
|
@ -41,16 +42,13 @@ let api: ChatGPTAPI | ChatGPTUnofficialProxyAPI
|
|
|
|
hostname: process.env.SOCKS_PROXY_HOST,
|
|
|
|
hostname: process.env.SOCKS_PROXY_HOST,
|
|
|
|
port: process.env.SOCKS_PROXY_PORT,
|
|
|
|
port: process.env.SOCKS_PROXY_PORT,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
globalThis.console.log(`Using socks proxy: ${process.env.SOCKS_PROXY_HOST}:${process.env.SOCKS_PROXY_PORT}`)
|
|
|
|
|
|
|
|
options.fetch = (url, options) => {
|
|
|
|
options.fetch = (url, options) => {
|
|
|
|
return fetch(url, { agent, ...options })
|
|
|
|
return fetch(url, { agent, ...options })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (process.env.API_REVERSE_PROXY) {
|
|
|
|
if (process.env.API_REVERSE_PROXY)
|
|
|
|
options.apiReverseProxyUrl = process.env.API_REVERSE_PROXY
|
|
|
|
options.apiReverseProxyUrl = process.env.API_REVERSE_PROXY
|
|
|
|
globalThis.console.log(`Using api reverse proxy: ${process.env.API_REVERSE_PROXY}`)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api = new ChatGPTUnofficialProxyAPI({
|
|
|
|
api = new ChatGPTUnofficialProxyAPI({
|
|
|
|
accessToken: process.env.OPENAI_ACCESS_TOKEN,
|
|
|
|
accessToken: process.env.OPENAI_ACCESS_TOKEN,
|
|
|
@ -82,7 +80,6 @@ async function chatReply(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 实验性质的函数,用于处理聊天过程中的中间结果 */
|
|
|
|
|
|
|
|
async function chatReplyProcess(
|
|
|
|
async function chatReplyProcess(
|
|
|
|
message: string,
|
|
|
|
message: string,
|
|
|
|
lastContext?: { conversationId?: string; parentMessageId?: string },
|
|
|
|
lastContext?: { conversationId?: string; parentMessageId?: string },
|
|
|
@ -119,7 +116,7 @@ async function chatConfig() {
|
|
|
|
reverseProxy: process.env.API_REVERSE_PROXY,
|
|
|
|
reverseProxy: process.env.API_REVERSE_PROXY,
|
|
|
|
timeoutMs,
|
|
|
|
timeoutMs,
|
|
|
|
socksProxy: (process.env.SOCKS_PROXY_HOST && process.env.SOCKS_PROXY_PORT) ? (`${process.env.SOCKS_PROXY_HOST}:${process.env.SOCKS_PROXY_PORT}`) : '-',
|
|
|
|
socksProxy: (process.env.SOCKS_PROXY_HOST && process.env.SOCKS_PROXY_PORT) ? (`${process.env.SOCKS_PROXY_HOST}:${process.env.SOCKS_PROXY_PORT}`) : '-',
|
|
|
|
},
|
|
|
|
} as ModelConfig,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|