Skip to content

Commit

Permalink
Fix hanging issue when sending empty content (ollama#2399)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmorganca authored Feb 8, 2024
1 parent ab0d37f commit a0a199b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
21 changes: 12 additions & 9 deletions server/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 12 additions & 12 deletions server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit a0a199b

Please sign in to comment.