🐛 fix: Repair will cache errors or empty messages

This commit is contained in:
Martial BE 2024-04-24 17:40:40 +08:00
parent 763c8883da
commit 755dbf79be
No known key found for this signature in database
GPG Key ID: D06C32DF0EDB9084
3 changed files with 16 additions and 2 deletions

View File

@ -70,8 +70,11 @@ func (r *relayChat) send() (err *types.OpenAIErrorWithStatusCode, done bool) {
return return
} }
err = responseJsonClient(r.c, response) err = responseJsonClient(r.c, response)
if err == nil && response.GetContent() != "" {
r.cache.SetResponse(response) r.cache.SetResponse(response)
} }
}
if err != nil { if err != nil {
done = true done = true

View File

@ -146,6 +146,8 @@ func responseStreamClient(c *gin.Context, stream requester.StreamReaderInterface
if !errors.Is(err, io.EOF) { if !errors.Is(err, io.EOF) {
fmt.Fprint(w, "data: "+err.Error()+"\n\n") fmt.Fprint(w, "data: "+err.Error()+"\n\n")
errWithOP = common.ErrorWrapper(err, "stream_error", http.StatusInternalServerError) errWithOP = common.ErrorWrapper(err, "stream_error", http.StatusInternalServerError)
// 报错不应该缓存
cache.NoCache()
} }
streamData := "data: [DONE]\n" streamData := "data: [DONE]\n"

View File

@ -90,7 +90,16 @@ func (p *ChatCacheProps) SetResponse(response any) {
return return
} }
p.Response = common.Marshal(response) responseStr := common.Marshal(response)
if responseStr == "" {
return
}
p.Response = responseStr
}
func (p *ChatCacheProps) NoCache() {
p.Cache = false
} }
func (p *ChatCacheProps) StoreCache(channelId, promptTokens, completionTokens int, modelName string) error { func (p *ChatCacheProps) StoreCache(channelId, promptTokens, completionTokens int, modelName string) error {