Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
chat-query
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
jaden
chat-query
Commits
a159bed1
Commit
a159bed1
authored
Jul 21, 2023
by
jaden
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: fix openai porxy use deno
parent
311f4e5e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
116 additions
and
26 deletions
+116
-26
components/AITool/index.tsx
components/AITool/index.tsx
+8
-7
deno-proxy.ts
deno-proxy.ts
+6
-0
middleware.ts
middleware.ts
+98
-15
next.config.js
next.config.js
+4
-4
No files found.
components/AITool/index.tsx
View file @
a159bed1
...
...
@@ -266,6 +266,7 @@ export function AIWrapper({
loading
,
messageList
,
currentAssistantMessage
,
setCurrentAssistantMessage
,
input
,
handleButtonClickSuccess
,
scrollContainer
,
...
...
@@ -759,14 +760,14 @@ export function AIWrapper({
}
export default function Ai(props: AIProps) {
const [
_, refresh] = useReducer(state => state++
, 0);
const [
initNun, refresh] = useReducer(state => ++state
, 0);
useEffect(() => {
(async () =>
{
i
f (window && !useSpeechToText)
{
useSpeechToText =
(await import('react-hook-speech-to-text')).default
;
if (window && !useSpeechToText)
{
i
mport('react-hook-speech-to-text').then(({ default: fx }) =>
{
useSpeechToText =
fx
;
refresh();
}
}
)();
}
);
}
}, []);
return useSpeechToText ? <AIWrapper {...props} /> : null;
return
initNun ||
useSpeechToText ? <AIWrapper {...props} /> : null;
}
deno-proxy.ts
0 → 100644
View file @
a159bed1
import
{
Application
,
proxy
}
from
'
https://deno.land/x/oak/mod.ts
'
;
const
app
=
new
Application
();
app
.
use
(
proxy
(
'
https://api.openai.com
'
));
app
.
listen
({
port
:
8000
});
middleware.ts
View file @
a159bed1
import
{
NextResponse
}
from
'
next/server
'
;
import
type
{
NextRequest
}
from
'
next/server
'
;
import
{
NextRequest
,
NextResponse
}
from
'
next/server
'
;
export
function
middleware
(
request
:
NextRequest
)
{
// Clone the request headers
// You can modify them with headers API: https://developer.mozilla.org/en-US/docs/Web/API/Headers
const
requestHeaders
=
new
Headers
(
request
.
headers
);
const
url
=
new
URL
(
`
${
process
.
env
.
OPENAI_PROXY_URL
||
'
https://api.openai.com
'
}
`
);
const
PROTOCOL
=
url
.
protocol
;
const
BASE_URL
=
url
.
host
;
const
DISABLE_GPT4
=
!!
process
.
env
.
DISABLE_GPT4
;
console
.
log
(
PROTOCOL
,
BASE_URL
);
// Add new request headers
requestHeaders
.
set
(
'
authorization
'
,
`Bearer
${
process
.
env
.
NEXT_PUBLIC_OPEN_AI_API_KEY
||
''
}
`
);
export
async
function
requestOpenai
(
req
:
NextRequest
)
{
const
controller
=
new
AbortController
();
const
authValue
=
`Bearer
${
process
.
env
.
NEXT_PUBLIC_OPEN_AI_API_KEY
||
''
}
`
;
const
openaiPath
=
`
${
req
.
nextUrl
.
pathname
}${
req
.
nextUrl
.
search
}
`
.
replaceAll
(
'
/openai/
'
,
''
);
//Todo 需要获取questionType,动态添加Prompt
let
baseUrl
=
BASE_URL
;
// You can also set request headers in NextResponse.rewrite
return
NextResponse
.
next
({
request
:
{
// New request headers
headers
:
requestHeaders
,
if
(
!
baseUrl
.
startsWith
(
'
http
'
))
{
baseUrl
=
`
${
PROTOCOL
}
//
${
baseUrl
}
`
;
}
console
.
log
(
'
[Proxy]
'
,
openaiPath
);
console
.
log
(
'
[Base Url]
'
,
baseUrl
);
if
(
process
.
env
.
OPENAI_ORG_ID
)
{
console
.
log
(
'
[Org ID]
'
,
process
.
env
.
OPENAI_ORG_ID
);
}
const
timeoutId
=
setTimeout
(()
=>
{
controller
.
abort
();
},
10
*
60
*
1000
);
const
fetchUrl
=
`
${
baseUrl
}
/
${
openaiPath
}
`
;
const
fetchOptions
:
RequestInit
=
{
headers
:
{
'
Content-Type
'
:
'
application/json
'
,
Authorization
:
authValue
,
...(
process
.
env
.
OPENAI_ORG_ID
&&
{
'
OpenAI-Organization
'
:
process
.
env
.
OPENAI_ORG_ID
,
}),
},
});
cache
:
'
no-store
'
,
method
:
req
.
method
,
body
:
req
.
body
,
// @ts-ignore
duplex
:
'
half
'
,
signal
:
controller
.
signal
,
};
// #1815 try to refuse gpt4 request
if
(
DISABLE_GPT4
&&
req
.
body
)
{
try
{
const
clonedBody
=
await
req
.
text
();
fetchOptions
.
body
=
clonedBody
;
const
jsonBody
=
JSON
.
parse
(
clonedBody
);
if
((
jsonBody
?.
model
??
''
).
includes
(
'
gpt-4
'
))
{
return
NextResponse
.
json
(
{
error
:
true
,
message
:
'
you are not allowed to use gpt-4 model
'
,
},
{
status
:
403
,
}
);
}
}
catch
(
e
)
{
console
.
error
(
'
[OpenAI] gpt4 filter
'
,
e
);
}
}
try
{
const
res
=
await
fetch
(
fetchUrl
,
fetchOptions
);
// to prevent browser prompt for credentials
const
newHeaders
=
new
Headers
(
res
.
headers
);
newHeaders
.
delete
(
'
www-authenticate
'
);
// to disbale ngnix buffering
newHeaders
.
set
(
'
X-Accel-Buffering
'
,
'
no
'
);
return
new
Response
(
res
.
body
,
{
status
:
res
.
status
,
statusText
:
res
.
statusText
,
headers
:
res
.
headers
,
});
}
finally
{
clearTimeout
(
timeoutId
);
}
}
export
async
function
middleware
(
request
:
NextRequest
)
{
// Only process requests that start with /openai/
console
.
log
(
new
URL
(
request
.
url
).
pathname
,
'
targetUrl
'
);
const
path
=
new
URL
(
request
.
url
).
pathname
;
if
(
!
path
.
startsWith
(
'
/openai/
'
))
{
return
NextResponse
.
next
();
}
try
{
return
await
requestOpenai
(
request
);
}
catch
(
e
)
{
console
.
log
(
e
);
}
}
next.config.js
View file @
a159bed1
...
...
@@ -10,10 +10,10 @@
const
rewrites
=
async
()
=>
{
const
ret
=
[
{
source
:
'
/openai/:path*
'
,
destination
:
`
${
process
.
env
.
OPENAI_PROXY_URL
||
'
https://api.openai.com
'
}
/:path*`
,
},
//
{
//
source: '/openai/:path*',
//
destination: `${process.env.OPENAI_PROXY_URL || 'https://api.openai.com'}/:path*`,
//
},
{
source
:
'
/google-fonts/:path*
'
,
destination
:
'
https://fonts.googleapis.com/:path*
'
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment