From a0a199b108e1ae241df357c9b989fd27e0bd19d9 Mon Sep 17 00:00:00 2001 From: Jeffrey Morgan Date: Wed, 7 Feb 2024 19:30:33 -0500 Subject: [PATCH] Fix hanging issue when sending empty content (#2399) --- server/images.go | 21 ++++++++++++--------- server/routes.go | 24 ++++++++++++------------ 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/server/images.go b/server/images.go index 6f59d72d..fb1c48e1 100644 --- a/server/images.go +++ b/server/images.go @@ -181,16 +181,19 @@ func (m *Model) ChatPrompts(msgs []api.Message) (*ChatHistory, error) { } currentVars.Prompt = msg.Content - for i := range msg.Images { - id := len(images) + i - currentVars.Prompt += fmt.Sprintf(" [img-%d]", id) - currentVars.Images = append(currentVars.Images, llm.ImageData{ - ID: id, - Data: msg.Images[i], - }) - } - images = append(images, currentVars.Images...) + if len(m.ProjectorPaths) > 0 { + for i := range msg.Images { + id := len(images) + i + currentVars.Prompt += fmt.Sprintf(" [img-%d]", id) + currentVars.Images = append(currentVars.Images, llm.ImageData{ + ID: id, + Data: msg.Images[i], + }) + } + + images = append(images, currentVars.Images...) + } case "assistant": currentVars.Response = msg.Content prompts = append(prompts, currentVars) diff --git a/server/routes.go b/server/routes.go index 7be4c126..3da62a07 100644 --- a/server/routes.go +++ b/server/routes.go @@ -1136,18 +1136,6 @@ func ChatHandler(c *gin.Context) { return } - // an empty request loads the model - if len(req.Messages) == 0 { - resp := api.ChatResponse{ - CreatedAt: time.Now().UTC(), - Model: req.Model, - Done: true, - Message: api.Message{Role: "assistant"}, - } - c.JSON(http.StatusOK, resp) - return - } - checkpointLoaded := time.Now() chat, err := model.ChatPrompts(req.Messages) @@ -1162,6 +1150,18 @@ func ChatHandler(c *gin.Context) { return } + // an empty request loads the model + if len(prompt) == 0 { + resp := api.ChatResponse{ + CreatedAt: time.Now().UTC(), + Model: req.Model, + Done: true, + Message: api.Message{Role: "assistant"}, + } + c.JSON(http.StatusOK, resp) + return + } + slog.Debug("chat handler", "prompt", prompt) ch := make(chan any)