diff --git a/relay/chat.go b/relay/chat.go index c7ccd9ca..0e28be26 100644 --- a/relay/chat.go +++ b/relay/chat.go @@ -70,7 +70,10 @@ func (r *relayChat) send() (err *types.OpenAIErrorWithStatusCode, done bool) { return } err = responseJsonClient(r.c, response) - r.cache.SetResponse(response) + + if err == nil && response.GetContent() != "" { + r.cache.SetResponse(response) + } } if err != nil { diff --git a/relay/common.go b/relay/common.go index bbaaa209..b6872b51 100644 --- a/relay/common.go +++ b/relay/common.go @@ -146,6 +146,8 @@ func responseStreamClient(c *gin.Context, stream requester.StreamReaderInterface if !errors.Is(err, io.EOF) { fmt.Fprint(w, "data: "+err.Error()+"\n\n") errWithOP = common.ErrorWrapper(err, "stream_error", http.StatusInternalServerError) + // 报错不应该缓存 + cache.NoCache() } streamData := "data: [DONE]\n" diff --git a/relay/util/cache.go b/relay/util/cache.go index f8f25c11..8cb3a6d2 100644 --- a/relay/util/cache.go +++ b/relay/util/cache.go @@ -90,7 +90,16 @@ func (p *ChatCacheProps) SetResponse(response any) { 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 {