@@ -6,6 +6,7 @@ import { SocksProxyAgent } from 'socks-proxy-agent'
6
6
import { HttpsProxyAgent } from 'https-proxy-agent'
7
7
import fetch from 'node-fetch'
8
8
import { sendResponse } from '../utils'
9
+ import { isNotEmptyString } from '../utils/is'
9
10
import type { ApiModel , ChatContext , ChatGPTUnofficialProxyAPIOptions , ModelConfig } from '../types'
10
11
11
12
const ErrorCodeMessage : Record < string , string > = {
@@ -27,42 +28,21 @@ if (!process.env.OPENAI_API_KEY && !process.env.OPENAI_ACCESS_TOKEN)
27
28
throw new Error ( 'Missing OPENAI_API_KEY or OPENAI_ACCESS_TOKEN environment variable' )
28
29
29
30
let api : ChatGPTAPI | ChatGPTUnofficialProxyAPI
30
- function setupProxy ( options ) {
31
- if ( process . env . SOCKS_PROXY_HOST && process . env . SOCKS_PROXY_PORT ) {
32
- const agent = new SocksProxyAgent ( {
33
- hostname : process . env . SOCKS_PROXY_HOST ,
34
- port : process . env . SOCKS_PROXY_PORT ,
35
- } )
36
- options . fetch = ( url , options ) => {
37
- return fetch ( url , { agent, ...options } )
38
- }
39
- }
40
-
41
- const httpsProxy = process . env . HTTPS_PROXY || process . env . https_proxy || process . env . ALL_PROXY || process . env . all_proxy
42
- if ( httpsProxy ) {
43
- const agent = new HttpsProxyAgent ( httpsProxy )
44
- options . fetch = ( url , options ) => {
45
- return fetch ( url , { agent, ...options } )
46
- }
47
- }
48
- }
49
31
50
32
( async ( ) => {
51
33
// More Info: https://github.com/transitive-bullshit/chatgpt-api
52
34
53
35
if ( process . env . OPENAI_API_KEY ) {
54
36
const OPENAI_API_MODEL = process . env . OPENAI_API_MODEL
55
- const model = ( typeof OPENAI_API_MODEL === 'string' && OPENAI_API_MODEL . length > 0 )
56
- ? OPENAI_API_MODEL
57
- : 'gpt-3.5-turbo'
37
+ const model = isNotEmptyString ( OPENAI_API_MODEL ) ? OPENAI_API_MODEL : 'gpt-3.5-turbo'
58
38
59
39
const options : ChatGPTAPIOptions = {
60
40
apiKey : process . env . OPENAI_API_KEY ,
61
41
completionParams : { model } ,
62
- debug : false ,
42
+ debug : true ,
63
43
}
64
44
65
- if ( process . env . OPENAI_API_BASE_URL && process . env . OPENAI_API_BASE_URL . trim ( ) . length > 0 )
45
+ if ( isNotEmptyString ( process . env . OPENAI_API_BASE_URL ) )
66
46
options . apiBaseUrl = process . env . OPENAI_API_BASE_URL
67
47
68
48
setupProxy ( options )
@@ -73,14 +53,14 @@ function setupProxy(options) {
73
53
else {
74
54
const options : ChatGPTUnofficialProxyAPIOptions = {
75
55
accessToken : process . env . OPENAI_ACCESS_TOKEN ,
76
- debug : false ,
56
+ debug : true ,
77
57
}
78
58
79
- setupProxy ( options )
80
-
81
- if ( process . env . API_REVERSE_PROXY )
59
+ if ( isNotEmptyString ( process . env . API_REVERSE_PROXY ) )
82
60
options . apiReverseProxyUrl = process . env . API_REVERSE_PROXY
83
61
62
+ setupProxy ( options )
63
+
84
64
api = new ChatGPTUnofficialProxyAPI ( { ...options } )
85
65
apiModel = 'ChatGPTUnofficialProxyAPI'
86
66
}
@@ -91,9 +71,6 @@ async function chatReplyProcess(
91
71
lastContext ?: { conversationId ?: string ; parentMessageId ?: string } ,
92
72
process ?: ( chat : ChatMessage ) => void ,
93
73
) {
94
- // if (!message)
95
- // return sendResponse({ type: 'Fail', message: 'Message is empty' })
96
-
97
74
try {
98
75
let options : SendMessageOptions = { timeoutMs }
99
76
@@ -123,20 +100,39 @@ async function chatReplyProcess(
123
100
}
124
101
125
102
async function chatConfig ( ) {
126
- const httpsProxy = process . env . HTTPS_PROXY || process . env . https_proxy || process . env . ALL_PROXY || process . env . all_proxy
103
+ const reverseProxy = process . env . API_REVERSE_PROXY ?? '-'
104
+ const socksProxy = ( process . env . SOCKS_PROXY_HOST && process . env . SOCKS_PROXY_PORT ) ? ( `${ process . env . SOCKS_PROXY_HOST } :${ process . env . SOCKS_PROXY_PORT } ` ) : '-'
105
+ const httpsProxy = ( process . env . HTTPS_PROXY || process . env . ALL_PROXY ) ?? '-'
127
106
128
- return sendResponse ( {
107
+ return sendResponse < ModelConfig > ( {
129
108
type : 'Success' ,
130
- data : {
131
- apiModel,
132
- reverseProxy : process . env . API_REVERSE_PROXY ,
133
- timeoutMs,
134
- socksProxy : ( process . env . SOCKS_PROXY_HOST && process . env . SOCKS_PROXY_PORT ) ? ( `${ process . env . SOCKS_PROXY_HOST } :${ process . env . SOCKS_PROXY_PORT } ` ) : '-' ,
135
- httpsProxy,
136
- } as ModelConfig ,
109
+ data : { apiModel, reverseProxy, timeoutMs, socksProxy, httpsProxy } ,
137
110
} )
138
111
}
139
112
113
+ function setupProxy ( options : ChatGPTAPIOptions | ChatGPTUnofficialProxyAPIOptions ) {
114
+ if ( process . env . SOCKS_PROXY_HOST && process . env . SOCKS_PROXY_PORT ) {
115
+ const agent = new SocksProxyAgent ( {
116
+ hostname : process . env . SOCKS_PROXY_HOST ,
117
+ port : process . env . SOCKS_PROXY_PORT ,
118
+ } )
119
+ options . fetch = ( url , options ) => {
120
+ return fetch ( url , { agent, ...options } )
121
+ }
122
+ }
123
+ else {
124
+ if ( process . env . HTTPS_PROXY || process . env . ALL_PROXY ) {
125
+ const httpsProxy = process . env . HTTPS_PROXY || process . env . ALL_PROXY
126
+ if ( httpsProxy ) {
127
+ const agent = new HttpsProxyAgent ( httpsProxy )
128
+ options . fetch = ( url , options ) => {
129
+ return fetch ( url , { agent, ...options } )
130
+ }
131
+ }
132
+ }
133
+ }
134
+ }
135
+
140
136
export type { ChatContext , ChatMessage }
141
137
142
138
export { chatReplyProcess , chatConfig }
0 commit comments