Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
dae16a20
authored
Nov 28, 2023
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化一些交互逻辑
parent
559edb04
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
25 additions
and
45 deletions
+25
-45
controller/relay-openai.go
+23
-37
web/src/components/LoginForm.js
+2
-6
web/src/components/UsersTable.js
+0
-2
No files found.
controller/relay-openai.go
View file @
dae16a20
...
@@ -9,11 +9,10 @@ import (
...
@@ -9,11 +9,10 @@ import (
"net/http"
"net/http"
"one-api/common"
"one-api/common"
"strings"
"strings"
"sync"
)
)
func
openaiStreamHandler
(
c
*
gin
.
Context
,
resp
*
http
.
Response
,
relayMode
int
)
(
*
OpenAIErrorWithStatusCode
,
string
)
{
func
openaiStreamHandler
(
c
*
gin
.
Context
,
resp
*
http
.
Response
,
relayMode
int
)
(
*
OpenAIErrorWithStatusCode
,
string
)
{
var
responseTextBuilder
strings
.
Builder
responseText
:=
""
scanner
:=
bufio
.
NewScanner
(
resp
.
Body
)
scanner
:=
bufio
.
NewScanner
(
resp
.
Body
)
scanner
.
Split
(
func
(
data
[]
byte
,
atEOF
bool
)
(
advance
int
,
token
[]
byte
,
err
error
)
{
scanner
.
Split
(
func
(
data
[]
byte
,
atEOF
bool
)
(
advance
int
,
token
[]
byte
,
err
error
)
{
if
atEOF
&&
len
(
data
)
==
0
{
if
atEOF
&&
len
(
data
)
==
0
{
...
@@ -29,10 +28,7 @@ func openaiStreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*O
...
@@ -29,10 +28,7 @@ func openaiStreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*O
})
})
dataChan
:=
make
(
chan
string
)
dataChan
:=
make
(
chan
string
)
stopChan
:=
make
(
chan
bool
)
stopChan
:=
make
(
chan
bool
)
var
wg
sync
.
WaitGroup
go
func
()
{
go
func
()
{
wg
.
Add
(
1
)
var
streamItems
[]
string
for
scanner
.
Scan
()
{
for
scanner
.
Scan
()
{
data
:=
scanner
.
Text
()
data
:=
scanner
.
Text
()
if
len
(
data
)
<
6
{
// ignore blank line or wrong format
if
len
(
data
)
<
6
{
// ignore blank line or wrong format
...
@@ -44,39 +40,30 @@ func openaiStreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*O
...
@@ -44,39 +40,30 @@ func openaiStreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*O
dataChan
<-
data
dataChan
<-
data
data
=
data
[
6
:
]
data
=
data
[
6
:
]
if
!
strings
.
HasPrefix
(
data
,
"[DONE]"
)
{
if
!
strings
.
HasPrefix
(
data
,
"[DONE]"
)
{
streamItems
=
append
(
streamItems
,
data
)
switch
relayMode
{
}
case
RelayModeChatCompletions
:
}
var
streamResponse
ChatCompletionsStreamResponseSimple
streamResp
:=
"["
+
strings
.
Join
(
streamItems
,
","
)
+
"]"
err
:=
json
.
Unmarshal
(
common
.
StringToByteSlice
(
data
),
&
streamResponse
)
switch
relayMode
{
if
err
!=
nil
{
case
RelayModeChatCompletions
:
common
.
SysError
(
"error unmarshalling stream response: "
+
err
.
Error
())
var
streamResponses
[]
ChatCompletionsStreamResponseSimple
continue
// just ignore the error
err
:=
json
.
Unmarshal
(
common
.
StringToByteSlice
(
streamResp
),
&
streamResponses
)
}
if
err
!=
nil
{
for
_
,
choice
:=
range
streamResponse
.
Choices
{
common
.
SysError
(
"error unmarshalling stream response: "
+
err
.
Error
())
responseText
+=
choice
.
Delta
.
Content
wg
.
Done
()
}
return
// just ignore the error
case
RelayModeCompletions
:
}
var
streamResponse
CompletionsStreamResponse
for
_
,
streamResponse
:=
range
streamResponses
{
err
:=
json
.
Unmarshal
(
common
.
StringToByteSlice
(
data
),
&
streamResponse
)
for
_
,
choice
:=
range
streamResponse
.
Choices
{
if
err
!=
nil
{
responseTextBuilder
.
WriteString
(
choice
.
Delta
.
Content
)
common
.
SysError
(
"error unmarshalling stream response: "
+
err
.
Error
())
}
continue
}
}
case
RelayModeCompletions
:
for
_
,
choice
:=
range
streamResponse
.
Choices
{
var
streamResponses
[]
CompletionsStreamResponse
responseText
+=
choice
.
Text
err
:=
json
.
Unmarshal
(
common
.
StringToByteSlice
(
streamResp
),
&
streamResponses
)
}
if
err
!=
nil
{
common
.
SysError
(
"error unmarshalling stream response: "
+
err
.
Error
())
wg
.
Done
()
return
// just ignore the error
}
for
_
,
streamResponse
:=
range
streamResponses
{
for
_
,
choice
:=
range
streamResponse
.
Choices
{
responseTextBuilder
.
WriteString
(
choice
.
Text
)
}
}
}
}
}
}
wg
.
Done
()
stopChan
<-
true
stopChan
<-
true
}()
}()
setEventStreamHeaders
(
c
)
setEventStreamHeaders
(
c
)
...
@@ -98,8 +85,7 @@ func openaiStreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*O
...
@@ -98,8 +85,7 @@ func openaiStreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*O
if
err
!=
nil
{
if
err
!=
nil
{
return
errorWrapper
(
err
,
"close_response_body_failed"
,
http
.
StatusInternalServerError
),
""
return
errorWrapper
(
err
,
"close_response_body_failed"
,
http
.
StatusInternalServerError
),
""
}
}
wg
.
Wait
()
return
nil
,
responseText
return
nil
,
responseTextBuilder
.
String
()
}
}
func
openaiHandler
(
c
*
gin
.
Context
,
resp
*
http
.
Response
,
promptTokens
int
,
model
string
)
(
*
OpenAIErrorWithStatusCode
,
*
Usage
)
{
func
openaiHandler
(
c
*
gin
.
Context
,
resp
*
http
.
Response
,
promptTokens
int
,
model
string
)
(
*
OpenAIErrorWithStatusCode
,
*
Usage
)
{
...
...
web/src/components/LoginForm.js
View file @
dae16a20
import
React
,
{
useContext
,
useEffect
,
useState
}
from
'react'
;
import
React
,
{
useContext
,
useEffect
,
useState
}
from
'react'
;
import
{
Modal
,
}
from
'semantic-ui-react'
;
import
{
Link
,
useNavigate
,
useSearchParams
}
from
'react-router-dom'
;
import
{
Link
,
useNavigate
,
useSearchParams
}
from
'react-router-dom'
;
import
{
UserContext
}
from
'../context/User'
;
import
{
UserContext
}
from
'../context/User'
;
import
{
API
,
getLogo
,
isMobile
,
showError
,
showInfo
,
showSuccess
,
showWarning
}
from
'../helpers'
;
import
{
API
,
getLogo
,
isMobile
,
showError
,
showInfo
,
showSuccess
,
showWarning
}
from
'../helpers'
;
import
{
onGitHubOAuthClicked
}
from
'./utils'
;
import
{
onGitHubOAuthClicked
}
from
'./utils'
;
import
Turnstile
from
"react-turnstile"
;
import
Turnstile
from
"react-turnstile"
;
import
{
Layout
,
Card
,
Image
,
Form
,
Button
,
Divider
}
from
"@douyinfe/semi-ui"
;
import
{
Layout
,
Card
,
Image
,
Form
,
Button
,
Divider
,
Modal
}
from
"@douyinfe/semi-ui"
;
import
Title
from
"@douyinfe/semi-ui/lib/es/typography/title"
;
import
Title
from
"@douyinfe/semi-ui/lib/es/typography/title"
;
import
Text
from
"@douyinfe/semi-ui/lib/es/typography/text"
;
import
Text
from
"@douyinfe/semi-ui/lib/es/typography/text"
;
...
@@ -92,8 +89,7 @@ const LoginForm = () => {
...
@@ -92,8 +89,7 @@ const LoginForm = () => {
localStorage
.
setItem
(
'user'
,
JSON
.
stringify
(
data
));
localStorage
.
setItem
(
'user'
,
JSON
.
stringify
(
data
));
showSuccess
(
'登录成功!'
);
showSuccess
(
'登录成功!'
);
if
(
username
===
'root'
&&
password
===
'123456'
)
{
if
(
username
===
'root'
&&
password
===
'123456'
)
{
showWarning
(
'请立刻修改默认密码!'
);
Modal
.
error
({
title
:
'您正在使用默认密码!'
,
content
:
'请立刻修改默认密码!'
,
centered
:
true
});
Modal
.
error
({
title
:
'您正在使用默认密码!'
,
content
:
'请立刻修改默认密码!'
});
}
}
navigate
(
'/token'
);
navigate
(
'/token'
);
}
else
{
}
else
{
...
...
web/src/components/UsersTable.js
View file @
dae16a20
...
@@ -80,7 +80,6 @@ const UsersTable = () => {
...
@@ -80,7 +80,6 @@ const UsersTable = () => {
<
Popconfirm
<
Popconfirm
title
=
"确定?"
title
=
"确定?"
okType
=
{
'warning'
}
okType
=
{
'warning'
}
position
=
{
'left'
}
onConfirm
=
{()
=>
{
onConfirm
=
{()
=>
{
manageUser
(
record
.
username
,
'promote'
,
record
)
manageUser
(
record
.
username
,
'promote'
,
record
)
}}
}}
...
@@ -90,7 +89,6 @@ const UsersTable = () => {
...
@@ -90,7 +89,6 @@ const UsersTable = () => {
<
Popconfirm
<
Popconfirm
title
=
"确定?"
title
=
"确定?"
okType
=
{
'warning'
}
okType
=
{
'warning'
}
position
=
{
'left'
}
onConfirm
=
{()
=>
{
onConfirm
=
{()
=>
{
manageUser
(
record
.
username
,
'demote'
,
record
)
manageUser
(
record
.
username
,
'demote'
,
record
)
}}
}}
...
...
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