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
4d7a39fa
authored
Aug 03, 2024
by
CalciumIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: 优化relay代码
parent
da5e1c18
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
5 additions
and
9 deletions
+5
-9
controller/relay.go
+5
-9
No files found.
controller/relay.go
View file @
4d7a39fa
...
@@ -2,6 +2,7 @@ package controller
...
@@ -2,6 +2,7 @@ package controller
import
(
import
(
"bytes"
"bytes"
"errors"
"fmt"
"fmt"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"io"
"io"
...
@@ -47,14 +48,9 @@ func Relay(c *gin.Context) {
...
@@ -47,14 +48,9 @@ func Relay(c *gin.Context) {
for
i
:=
0
;
i
<=
common
.
RetryTimes
;
i
++
{
for
i
:=
0
;
i
<=
common
.
RetryTimes
;
i
++
{
channel
,
err
:=
getChannel
(
c
,
group
,
originalModel
,
i
)
channel
,
err
:=
getChannel
(
c
,
group
,
originalModel
,
i
)
if
err
!=
nil
{
if
err
!=
nil
{
errMsg
:=
fmt
.
Sprintf
(
"获取渠道出错: %s"
,
err
.
Error
())
common
.
LogError
(
c
,
err
.
Error
())
common
.
LogError
(
c
,
errMsg
)
openaiErr
=
service
.
OpenAIErrorWrapperLocal
(
err
,
"get_channel_failed"
,
http
.
StatusInternalServerError
)
openaiErr
=
service
.
OpenAIErrorWrapperLocal
(
err
,
"get_channel_failed"
,
http
.
StatusInternalServerError
)
openaiErr
.
Error
.
Message
=
common
.
MessageWithRequestId
(
errMsg
,
requestId
)
break
c
.
JSON
(
openaiErr
.
StatusCode
,
gin
.
H
{
"error"
:
openaiErr
.
Error
,
})
return
}
}
openaiErr
=
relayRequest
(
c
,
relayMode
,
channel
)
openaiErr
=
relayRequest
(
c
,
relayMode
,
channel
)
...
@@ -72,7 +68,7 @@ func Relay(c *gin.Context) {
...
@@ -72,7 +68,7 @@ func Relay(c *gin.Context) {
useChannel
:=
c
.
GetStringSlice
(
"use_channel"
)
useChannel
:=
c
.
GetStringSlice
(
"use_channel"
)
if
len
(
useChannel
)
>
1
{
if
len
(
useChannel
)
>
1
{
retryLogStr
:=
fmt
.
Sprintf
(
"重试:%s"
,
strings
.
Trim
(
strings
.
Join
(
strings
.
Fields
(
fmt
.
Sprint
(
useChannel
)),
"->"
),
"[]"
))
retryLogStr
:=
fmt
.
Sprintf
(
"重试:%s"
,
strings
.
Trim
(
strings
.
Join
(
strings
.
Fields
(
fmt
.
Sprint
(
useChannel
)),
"->"
),
"[]"
))
common
.
LogInfo
(
c
.
Request
.
Context
()
,
retryLogStr
)
common
.
LogInfo
(
c
,
retryLogStr
)
}
}
if
openaiErr
!=
nil
{
if
openaiErr
!=
nil
{
...
@@ -109,7 +105,7 @@ func getChannel(c *gin.Context, group, originalModel string, retryCount int) (*m
...
@@ -109,7 +105,7 @@ func getChannel(c *gin.Context, group, originalModel string, retryCount int) (*m
}
}
channel
,
err
:=
model
.
CacheGetRandomSatisfiedChannel
(
group
,
originalModel
,
retryCount
)
channel
,
err
:=
model
.
CacheGetRandomSatisfiedChannel
(
group
,
originalModel
,
retryCount
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
ors
.
New
(
fmt
.
Sprintf
(
"获取重试渠道失败: %s"
,
err
.
Error
()))
}
}
middleware
.
SetupContextForSelectedChannel
(
c
,
channel
,
originalModel
)
middleware
.
SetupContextForSelectedChannel
(
c
,
channel
,
originalModel
)
return
channel
,
nil
return
channel
,
nil
...
...
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