fix empty response when receiving runner error

This commit is contained in:
Jeffrey Morgan 2023-12-10 10:53:38 -05:00
parent d9a250e9b5
commit 32064a0646
2 changed files with 40 additions and 20 deletions

View file

@ -252,7 +252,7 @@ curl http://localhost:11434/api/generate -d '{
"penalize_newline": true, "penalize_newline": true,
"stop": ["\n", "user:"], "stop": ["\n", "user:"],
"numa": false, "numa": false,
"num_ctx": 4, "num_ctx": 1024,
"num_batch": 2, "num_batch": 2,
"num_gqa": 1, "num_gqa": 1,
"num_gpu": 1, "num_gpu": 1,
@ -267,7 +267,7 @@ curl http://localhost:11434/api/generate -d '{
"rope_frequency_base": 1.1, "rope_frequency_base": 1.1,
"rope_frequency_scale": 0.8, "rope_frequency_scale": 0.8,
"num_thread": 8 "num_thread": 8
} }
}' }'
``` ```

View file

@ -300,19 +300,30 @@ func GenerateHandler(c *gin.Context) {
}() }()
if req.Stream != nil && !*req.Stream { if req.Stream != nil && !*req.Stream {
// Wait for the channel to close // Accumulate responses into the final response
var r api.GenerateResponse var final api.GenerateResponse
var sb strings.Builder var sb strings.Builder
for resp := range ch { for resp := range ch {
var ok bool switch r := resp.(type) {
if r, ok = resp.(api.GenerateResponse); !ok { case api.GenerateResponse:
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) sb.WriteString(r.Response)
final = r
case gin.H:
if errorMsg, ok := r["error"].(string); ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": errorMsg})
return
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": "unexpected error format in response"})
return
}
default:
c.JSON(http.StatusInternalServerError, gin.H{"error": "unexpected error"})
return return
} }
sb.WriteString(r.Response)
} }
r.Response = sb.String()
c.JSON(http.StatusOK, r) final.Response = sb.String()
c.JSON(http.StatusOK, final)
return return
} }
@ -1008,21 +1019,30 @@ func ChatHandler(c *gin.Context) {
}() }()
if req.Stream != nil && !*req.Stream { if req.Stream != nil && !*req.Stream {
// Wait for the channel to close // Accumulate responses into the final response
var r api.ChatResponse var final api.ChatResponse
var sb strings.Builder var sb strings.Builder
for resp := range ch { for resp := range ch {
var ok bool switch r := resp.(type) {
if r, ok = resp.(api.ChatResponse); !ok { case api.ChatResponse:
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) sb.WriteString(r.Message.Content)
final = r
case gin.H:
if errorMsg, ok := r["error"].(string); ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": errorMsg})
return
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": "unexpected error format in response"})
return
}
default:
c.JSON(http.StatusInternalServerError, gin.H{"error": "unexpected error"})
return return
} }
if r.Message != nil {
sb.WriteString(r.Message.Content)
}
} }
r.Message = &api.Message{Role: "assistant", Content: sb.String()}
c.JSON(http.StatusOK, r) final.Message = &api.Message{Role: "assistant", Content: sb.String()}
c.JSON(http.StatusOK, final)
return return
} }