This commit is contained in:
Zheng Kai
2023-06-09 11:09:14 +08:00
parent 210c23b633
commit 51e0b5807a
8 changed files with 65 additions and 6 deletions

11
misc/test/chat-lamia.sh Executable file
View File

@@ -0,0 +1,11 @@
#!/bin/bash -ex
API_HOST="http://10.0.84.49:22035"
curl "${API_HOST}/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello!"}]
}'

11
misc/test/chat.sh Executable file
View File

@@ -0,0 +1,11 @@
#!/bin/bash -ex
API_HOST="http://localhost:22035"
curl "${API_HOST}/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello!"}]
}'

View File

@@ -1,7 +1,7 @@
#!/bin/bash -ex #!/bin/bash -ex
API_HOST="http://10.0.84.49:22035" # API_HOST="http://10.0.84.49:22035"
# API_HOST="http://localhost:22035" API_HOST="http://localhost:22035"
curl "${API_HOST}/v1/engines/text-embedding-ada-002/embeddings" \ curl "${API_HOST}/v1/engines/text-embedding-ada-002/embeddings" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \

View File

@@ -3,7 +3,7 @@ option go_package = "/pb";
package pb; package pb;
message Rsp { message Rsp {
uint32 id = 1; string id = 1;
string object = 2; string object = 2;
fixed32 created = 3; fixed32 created = 3;
string model = 4; string model = 4;

View File

@@ -7,6 +7,7 @@ import (
"project/pb" "project/pb"
"project/util" "project/util"
"project/zj" "project/zj"
"strings"
) )
func doMetrics(ab []byte, cached bool, r *http.Request) { func doMetrics(ab []byte, cached bool, r *http.Request) {
@@ -17,6 +18,7 @@ func doMetrics(ab []byte, cached bool, r *http.Request) {
err := json.Unmarshal(ab, o) err := json.Unmarshal(ab, o)
if err != nil { if err != nil {
zj.J(`unmarshal fail`, err) zj.J(`unmarshal fail`, err)
util.WriteFile(`metrics-json-fail`, ab)
return return
} }
@@ -31,6 +33,10 @@ func doMetrics(ab []byte, cached bool, r *http.Request) {
zj.J(`token`, u.PromptTokens, u.TotalTokens) zj.J(`token`, u.PromptTokens, u.TotalTokens)
} }
metrics.RspTokenByModel(o.Model, u.TotalTokens)
metrics.RspTokenByKey(strings.TrimPrefix(r.Header.Get(`Authorization`), `Bearer `), u.TotalTokens)
ip, err := util.GetIP(r) ip, err := util.GetIP(r)
sip := ip.String() sip := ip.String()
if err != nil { if err != nil {

View File

@@ -12,7 +12,7 @@ func (c *Core) getAB(p *pb.Req, r *http.Request) (ab []byte, cached bool, err er
canCache := p.Method != http.MethodGet && p.Method != http.MethodDelete canCache := p.Method != http.MethodGet && p.Method != http.MethodDelete
canCache = false // canCache = false
if canCache { if canCache {
var ok bool var ok bool

View File

@@ -16,4 +16,6 @@ func init() {
prometheus.MustRegister(rspTokenCachedCount) prometheus.MustRegister(rspTokenCachedCount)
prometheus.MustRegister(rspJSONFailCount) prometheus.MustRegister(rspJSONFailCount)
prometheus.MustRegister(rspTokenByIP) prometheus.MustRegister(rspTokenByIP)
prometheus.MustRegister(rspTokenByModel)
prometheus.MustRegister(rspTokenByKey)
} }

View File

@@ -10,10 +10,24 @@ var (
rspTokenCount = newCounter(`orca_rsp_token`, `token`) rspTokenCount = newCounter(`orca_rsp_token`, `token`)
rspTokenCachedCount = newCounter(`orca_rsp_token_cached`, `token cached`) rspTokenCachedCount = newCounter(`orca_rsp_token_cached`, `token cached`)
rspJSONFailCount = newCounter(`orca_rsp_json_fail`, `json fail`) rspJSONFailCount = newCounter(`orca_rsp_json_fail`, `json fail`)
rspTokenByIP = prometheus.NewCounterVec( rspTokenByModel = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: `orca_token_by_model`,
Help: `token by model`,
},
[]string{`model`},
)
rspTokenByKey = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: `orca_token_by_key`,
Help: `openai key`,
},
[]string{`key`},
)
rspTokenByIP = prometheus.NewCounterVec(
prometheus.CounterOpts{ prometheus.CounterOpts{
Name: `orca_token_by_ip`, Name: `orca_token_by_ip`,
Help: `API 返回报错`, Help: `token by ip`,
}, },
[]string{`ip`}, []string{`ip`},
) )
@@ -43,3 +57,18 @@ func RspJSONFail() {
func RspTokenByIP(ip string, token uint32) { func RspTokenByIP(ip string, token uint32) {
rspTokenByIP.WithLabelValues(ip).Add(float64(token)) rspTokenByIP.WithLabelValues(ip).Add(float64(token))
} }
// RspTokenByKey ...
func RspTokenByKey(key string, token uint32) {
if len(key) > 30 {
key = key[:30]
} else if key == `` {
key = `<empty>`
}
rspTokenByKey.WithLabelValues(key).Add(float64(token))
}
// RspTokenByModel ...
func RspTokenByModel(model string, token uint32) {
rspTokenByModel.WithLabelValues(model).Add(float64(token))
}