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
2fd011e4
authored
Jul 05, 2024
by
CalciumIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 记录渠道测试的消费日志 (close #334)
parent
c19767b9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
2 deletions
+23
-2
controller/channel-test.go
+23
-2
No files found.
controller/channel-test.go
View file @
2fd011e4
...
@@ -6,6 +6,7 @@ import (
...
@@ -6,6 +6,7 @@ import (
"errors"
"errors"
"fmt"
"fmt"
"io"
"io"
"math"
"net/http"
"net/http"
"net/http/httptest"
"net/http/httptest"
"net/url"
"net/url"
...
@@ -24,6 +25,7 @@ import (
...
@@ -24,6 +25,7 @@ import (
)
)
func
testChannel
(
channel
*
model
.
Channel
,
testModel
string
)
(
err
error
,
openaiErr
*
dto
.
OpenAIError
)
{
func
testChannel
(
channel
*
model
.
Channel
,
testModel
string
)
(
err
error
,
openaiErr
*
dto
.
OpenAIError
)
{
tik
:=
time
.
Now
()
if
channel
.
Type
==
common
.
ChannelTypeMidjourney
{
if
channel
.
Type
==
common
.
ChannelTypeMidjourney
{
return
errors
.
New
(
"midjourney channel test is not supported"
),
nil
return
errors
.
New
(
"midjourney channel test is not supported"
),
nil
}
}
...
@@ -120,6 +122,25 @@ func testChannel(channel *model.Channel, testModel string) (err error, openaiErr
...
@@ -120,6 +122,25 @@ func testChannel(channel *model.Channel, testModel string) (err error, openaiErr
if
err
!=
nil
{
if
err
!=
nil
{
return
err
,
nil
return
err
,
nil
}
}
modelPrice
,
usePrice
:=
common
.
GetModelPrice
(
testModel
,
false
)
modelRatio
:=
common
.
GetModelRatio
(
testModel
)
completionRatio
:=
common
.
GetCompletionRatio
(
testModel
)
ratio
:=
modelRatio
quota
:=
0
if
!
usePrice
{
quota
=
usage
.
PromptTokens
+
int
(
math
.
Round
(
float64
(
usage
.
CompletionTokens
)
*
completionRatio
))
quota
=
int
(
math
.
Round
(
float64
(
quota
)
*
ratio
))
if
ratio
!=
0
&&
quota
<=
0
{
quota
=
1
}
}
else
{
quota
=
int
(
modelPrice
*
common
.
QuotaPerUnit
)
}
tok
:=
time
.
Now
()
milliseconds
:=
tok
.
Sub
(
tik
)
.
Milliseconds
()
consumedTime
:=
float64
(
milliseconds
)
/
1000.0
other
:=
service
.
GenerateTextOtherInfo
(
c
,
meta
,
modelRatio
,
1
,
completionRatio
,
modelPrice
)
model
.
RecordConsumeLog
(
c
,
1
,
channel
.
Id
,
usage
.
PromptTokens
,
usage
.
CompletionTokens
,
testModel
,
"模型测试"
,
quota
,
"模型测试"
,
0
,
quota
,
int
(
consumedTime
),
false
,
other
)
common
.
SysLog
(
fmt
.
Sprintf
(
"testing channel #%d, response:
\n
%s"
,
channel
.
Id
,
string
(
respBody
)))
common
.
SysLog
(
fmt
.
Sprintf
(
"testing channel #%d, response:
\n
%s"
,
channel
.
Id
,
string
(
respBody
)))
return
nil
,
nil
return
nil
,
nil
}
}
...
@@ -140,7 +161,7 @@ func buildTestRequest() *dto.GeneralOpenAIRequest {
...
@@ -140,7 +161,7 @@ func buildTestRequest() *dto.GeneralOpenAIRequest {
}
}
func
TestChannel
(
c
*
gin
.
Context
)
{
func
TestChannel
(
c
*
gin
.
Context
)
{
i
d
,
err
:=
strconv
.
Atoi
(
c
.
Param
(
"id"
))
channelI
d
,
err
:=
strconv
.
Atoi
(
c
.
Param
(
"id"
))
if
err
!=
nil
{
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"success"
:
false
,
...
@@ -148,7 +169,7 @@ func TestChannel(c *gin.Context) {
...
@@ -148,7 +169,7 @@ func TestChannel(c *gin.Context) {
})
})
return
return
}
}
channel
,
err
:=
model
.
GetChannelById
(
i
d
,
true
)
channel
,
err
:=
model
.
GetChannelById
(
channelI
d
,
true
)
if
err
!=
nil
{
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"success"
:
false
,
...
...
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