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

Code before:

func generate(c *gin.Context) {
	var req api.GenerateRequest
	req.ModelOptions = api.DefaultModelOptions
	req.PredictOptions = api.DefaultPredictOptions
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
		return
	}

	remoteModel, err := getRemote(req.Model)
	if err != nil {
		// couldn't check the directory, proceed in offline mode
		_, err := os.Stat(req.Model)
		if err != nil {
			if !os.IsNotExist(err) {
				c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
				return
			}
			// couldn't find the model file, try setting the model to the cache directory
			req.Model = path.Join(cacheDir(), "models", req.Model+".bin")
		}
	}
	if remoteModel != nil {
		req.Model = remoteModel.FullName()
	}

	modelOpts := getModelOpts(req)
	modelOpts.NGPULayers = 1 // hard-code this for now

	model, err := llama.New(req.Model, modelOpts)
	if err != nil {
		fmt.Println("Loading the model failed:", err.Error())
		return
	}
	defer model.Free()

	templateNames := make([]string, 0, len(templates.Templates()))
	for _, template := range templates.Templates() {
		templateNames = append(templateNames, template.Name())
	}

	match, _ := matchRankOne(path.Base(req.Model), templateNames)
	if template := templates.Lookup(match); template != nil {
		var sb strings.Builder
		if err := template.Execute(&sb, req); err != nil {
			fmt.Println("Prompt template failed:", err.Error())
			return
		}

		req.Prompt = sb.String()

Review comment (performance):
This can be simplified without explicitly checking the error since it doesnt' _really_ matter what the error is

```suggestion
	if remoteModel, _ := getRemote(req.Model); remoteModel != nil {
		req.Model = remoteModel.FullName()
	}

	if _, err := os.Stat(req.Model); err != nil {
		if !errors.Is(err, os.ErrNotExist) {
			c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
			return
		}

		req.Model = path.Join(cacheDir(), "models", req.Model+".bin")
	}
```

Code after:

func generate(c *gin.Context) {
	var req api.GenerateRequest
	req.ModelOptions = api.DefaultModelOptions
	req.PredictOptions = api.DefaultPredictOptions
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
		return
	}

	if remoteModel, _ := getRemote(req.Model); remoteModel != nil {
		req.Model = remoteModel.FullName()
	}
	if _, err := os.Stat(req.Model); err != nil {
		if !errors.Is(err, os.ErrNotExist) {
			c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
			return
		}
		req.Model = path.Join(cacheDir(), "models", req.Model+".bin")
	}

	modelOpts := getModelOpts(req)
	modelOpts.NGPULayers = 1 // hard-code this for now

	model, err := llama.New(req.Model, modelOpts)
	if err != nil {
		fmt.Println("Loading the model failed:", err.Error())
		return
	}
	defer model.Free()

	templateNames := make([]string, 0, len(templates.Templates()))
	for _, template := range templates.Templates() {
		templateNames = append(templateNames, template.Name())
	}

	match, _ := matchRankOne(path.Base(req.Model), templateNames)
	if template := templates.Lookup(match); template != nil {
		var sb strings.Builder
		if err := template.Execute(&sb, req); err != nil {
			fmt.Println("Prompt template failed:", err.Error())
			return
		}

		req.Prompt = sb.String()
	}

	ch := make(chan string)
	model.SetTokenCallback(func(token string) bool {
		ch <- token
		return true