72 lines
3.0 KiB
Go
72 lines
3.0 KiB
Go
package service
|
||
|
||
import (
|
||
_ "embed"
|
||
"encoding/base64"
|
||
"regexp"
|
||
"strings"
|
||
|
||
"github.com/QuantumNous/new-api/common"
|
||
"github.com/QuantumNous/new-api/model"
|
||
fingerprint "github.com/QuantumNous/new-api/pkg/model_fingerprint"
|
||
)
|
||
|
||
const VisionOutputTokens = 4096
|
||
const CialloPrompt = "ciallo"
|
||
|
||
func CialloQuality(answer string) string {
|
||
if strings.Contains(answer, "Ciallo") {
|
||
return "normal"
|
||
}
|
||
return "degraded"
|
||
}
|
||
|
||
const VisionPrompt = "不搜索,告诉我这是什么"
|
||
|
||
//go:embed vision.png
|
||
var visionImage []byte
|
||
var VisionImageURL = "data:image/png;base64," + base64.StdEncoding.EncodeToString(visionImage)
|
||
|
||
var visionUnavailableAnswer = regexp.MustCompile(`(?i)` +
|
||
`(?:我(?:这边|這邊|这里|這裡)?(?:目前|现在|現在|暂时|暫時)?(?:还|還|也|并|並)?|^(?:抱歉[,, ]*)?(?:目前|当前|當前|现在|現在|暂时|暫時)?)` +
|
||
`(?:没(?:有)?收到|沒有收到|未收到|(?:无法|無法|不能)(?:直接)?(?:查看|看到|读取|讀取|处理|處理|识别|識別)|看不到|收不到|没有看到|沒有看到)` +
|
||
`[^。!?!?\n,,;;]{0,24}(?:图片|圖片|图像|圖像|附件)(?:内容|內容)?(?:$|[。!?!?\n,,;;或和及])` +
|
||
`|(?:^|你(?:还|還)?|(?:消息|訊息|请求|請求)(?:中|里|裡)?)(?:没有|沒有|未)(?:附带|附帶|附上|上传|上傳|提供)[^。!?!?\n,,;;]{0,12}(?:图片|圖片|图像|圖像|附件)` +
|
||
`|\b(?:i|we)\s+(?:(?:currently|still)\s+)?(?:(?:cannot|can't|can’t|am unable to|are unable to|am not able to)\s+(?:see|view|access|read|process|receive)|(?:did not|didn't|didn’t|have not|haven't|haven’t)\s+(?:receive|received)|(?:do not|don't|don’t)\s+(?:see|have))[^.!?\n]{0,60}\b(?:images?|pictures?|photos?|attachments?)\b` +
|
||
`|\b(?:i['’]m|we['’]re)\s+unable to\s+(?:see|view|access|read|process)[^.!?\n]{0,60}\b(?:images?|pictures?|photos?|attachments?)\b` +
|
||
`|\bno\s+(?:images?|pictures?|photos?|attachments?)\s+(?:(?:was|is|has been|were|are)\s+)?(?:attached|provided|received|uploaded)\b`)
|
||
|
||
func VisionQuality(answer string) string {
|
||
answer = strings.Join(strings.Fields(answer), " ")
|
||
if visionUnavailableAnswer.MatchString(answer) {
|
||
return "incomplete"
|
||
}
|
||
if strings.Contains(answer, "音响") || strings.Contains(answer, "音箱") {
|
||
return "normal"
|
||
}
|
||
return "degraded"
|
||
}
|
||
|
||
// Before custom.79 keyword misses and transport failures both used failed.
|
||
// Interpret that history without rewriting persisted detection records.
|
||
func NormalizeVisionRun(run model.DetectionRun) model.DetectionRun {
|
||
if run.Kind != "vision" {
|
||
return run
|
||
}
|
||
var result fingerprint.Result
|
||
hasAnswer := common.UnmarshalJsonStr(run.Result, &result) == nil && strings.TrimSpace(result.Closest) != ""
|
||
if run.Status == "failed" {
|
||
run.Quality = "unknown"
|
||
if run.Error == "vision_keyword_missing" && hasAnswer {
|
||
run.Status = "succeeded"
|
||
run.Error = ""
|
||
}
|
||
}
|
||
if run.Status == "succeeded" && hasAnswer {
|
||
run.Quality = VisionQuality(result.Closest)
|
||
} else if run.Status == "succeeded" {
|
||
run.Status, run.Quality = "failed", "unknown"
|
||
}
|
||
return run
|
||
}
|