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
082fb544
authored
Mar 21, 2024
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: 代码结构优化
parent
bcad2762
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
11 deletions
+9
-11
relay/channel/openai/relay-openai.go
+9
-11
No files found.
relay/channel/openai/relay-openai.go
View file @
082fb544
...
...
@@ -157,7 +157,7 @@ func OpenaiHandler(c *gin.Context, resp *http.Response, promptTokens int, model
if
textResponse
.
Usage
.
TotalTokens
==
0
||
checkSensitive
{
completionTokens
:=
0
for
_
,
choice
:=
range
textResponse
.
Choices
{
for
i
,
choice
:=
range
textResponse
.
Choices
{
stringContent
:=
string
(
choice
.
Message
.
Content
)
ctkm
,
_
,
_
:=
service
.
CountTokenText
(
stringContent
,
model
,
false
)
completionTokens
+=
ctkm
...
...
@@ -167,7 +167,7 @@ func OpenaiHandler(c *gin.Context, resp *http.Response, promptTokens int, model
triggerSensitive
=
true
msg
:=
choice
.
Message
msg
.
Content
=
common
.
StringToByteSlice
(
stringContent
)
choice
.
Message
=
msg
textResponse
.
Choices
[
i
]
.
Message
=
msg
sensitiveWords
=
append
(
sensitiveWords
,
words
...
)
}
}
...
...
@@ -179,8 +179,13 @@ func OpenaiHandler(c *gin.Context, resp *http.Response, promptTokens int, model
}
}
if
checkSensitive
&&
constant
.
StopOnSensitiveEnabled
&&
triggerSensitive
{
if
checkSensitive
&&
triggerSensitive
&&
constant
.
StopOnSensitiveEnabled
{
sensitiveWords
=
common
.
RemoveDuplicate
(
sensitiveWords
)
return
service
.
OpenAIErrorWrapper
(
errors
.
New
(
fmt
.
Sprintf
(
"sensitive words detected on response: %s"
,
strings
.
Join
(
sensitiveWords
,
", "
))),
"sensitive_words_detected"
,
http
.
StatusBadRequest
),
&
textResponse
.
Usage
,
&
dto
.
SensitiveResponse
{
SensitiveWords
:
sensitiveWords
,
}
}
else
{
responseBody
,
err
=
json
.
Marshal
(
textResponse
)
// Reset response body
...
...
@@ -202,12 +207,5 @@ func OpenaiHandler(c *gin.Context, resp *http.Response, promptTokens int, model
return
service
.
OpenAIErrorWrapper
(
err
,
"close_response_body_failed"
,
http
.
StatusInternalServerError
),
nil
,
nil
}
}
if
checkSensitive
&&
triggerSensitive
{
sensitiveWords
=
common
.
RemoveDuplicate
(
sensitiveWords
)
return
service
.
OpenAIErrorWrapper
(
errors
.
New
(
fmt
.
Sprintf
(
"sensitive words detected on response: %s"
,
strings
.
Join
(
sensitiveWords
,
", "
))),
"sensitive_words_detected"
,
http
.
StatusBadRequest
),
&
textResponse
.
Usage
,
&
dto
.
SensitiveResponse
{
SensitiveWords
:
sensitiveWords
,
}
}
return
nil
,
&
textResponse
.
Usage
,
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