Repository: ollama/ollama
File: server/images.go (Go)

Code before:
				}
				// Check for NaN and Inf in the embedding, which can't be stored
				for _, value := range embed {
					if math.IsNaN(value) || math.IsInf(value, 0) {
						log.Printf("reloading model, embedding contains NaN or Inf")
						// reload the model to get a new embedding
						llm, err = llama.New(model.ModelPath, e.opts)
						if err != nil {
							return nil, fmt.Errorf("load model to generate embeddings: %v", err)
						}
						retry++
						goto generate
					}
				}
				embeddings = append(embeddings, vector.Embedding{Data: d, Vector: embed})
			}

			b, err := json.Marshal(embeddings)
			if err != nil {
				return nil, fmt.Errorf("failed to encode embeddings: %w", err)
			}
			r := bytes.NewReader(b)

			digest, size := GetSHA256Digest(r)
			// Reset the position of the reader after calculating the digest
			if _, err := r.Seek(0, 0); err != nil {
				return nil, fmt.Errorf("could not reset embed reader: %w", err)
			}

			layer := &LayerReader{
				Layer: Layer{
					MediaType: "application/vnd.ollama.image.embed",
					Digest:    digest,
					Size:      size,
				},
				Reader: r,
			}

			layers = append(layers, layer)
		}
	}
	return layers, nil
}

func removeLayerFromLayers(layers []*LayerReader, mediaType string) []*LayerReader {
	j := 0
	for _, l := range layers {
		if l.MediaType != mediaType {
			layers[j] = l
			j++
		}

Review comment (suggestion):
nit
```suggestion
			if _, err := r.Seek(0, io.SeekStart); err != nil {
```

Code after:
				}
				f.Close()

				// the digest of the file is set here so that the client knows a new operation is in progress
				fileDigest, _ := GetSHA256Digest(bytes.NewReader([]byte(filePath)))

				embeddings := []vector.Embedding{}
				for i, d := range data {
					if strings.TrimSpace(d) == "" {
						continue
					}
					e.fn(api.ProgressResponse{
						Status:    fmt.Sprintf("creating embeddings for file %s", filePath),
						Digest:    fileDigest,
						Total:     len(data) - 1,
						Completed: i,
					})
					retry := 0
				generate:
					if retry > 3 {
						log.Printf("failed to generate embedding for '%s' line %d: %v", filePath, i+1, err)
						continue
					}
					embed, err := llm.Embedding(d)
					if err != nil {
						log.Printf("retrying embedding generation for '%s' line %d: %v", filePath, i+1, err)
						retry++
						goto generate
					}
					// Check for NaN and Inf in the embedding, which can't be stored
					for _, value := range embed {
						if math.IsNaN(value) || math.IsInf(value, 0) {
							log.Printf("reloading model, embedding contains NaN or Inf")
							// reload the model to get a new embedding, the seed can effect these outputs and reloading changes it
							llm.Close()
							llm, err = llama.New(e.model, e.opts)
							if err != nil {
								return nil, fmt.Errorf("load model to generate embeddings: %v", err)
							}
							retry++
							goto generate
						}
					}
					embeddings = append(embeddings, vector.Embedding{Data: d, Vector: embed})
				}

				b, err := json.Marshal(embeddings)
				if err != nil {
					return nil, fmt.Errorf("failed to encode embeddings: %w", err)
				}
				r := bytes.NewReader(b)