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

Code before:
	request := map[string]any{
		"prompt":            req.Prompt,
		"stream":            true,
		"n_predict":         req.Options.NumPredict,
		"n_keep":            req.Options.NumKeep,
		"main_gpu":          req.Options.MainGPU,
		"temperature":       req.Options.Temperature,
		"top_k":             req.Options.TopK,
		"top_p":             req.Options.TopP,
		"min_p":             req.Options.MinP,
		"typical_p":         req.Options.TypicalP,
		"repeat_last_n":     req.Options.RepeatLastN,
		"repeat_penalty":    req.Options.RepeatPenalty,
		"presence_penalty":  req.Options.PresencePenalty,
		"frequency_penalty": req.Options.FrequencyPenalty,
		"mirostat":          req.Options.Mirostat,
		"mirostat_tau":      req.Options.MirostatTau,
		"mirostat_eta":      req.Options.MirostatEta,
		"seed":              req.Options.Seed,
		"stop":              req.Options.Stop,
		"image_data":        req.Images,
		"cache_prompt":      true,
	}

	if len(req.Format) > 0 {
		strFormat := string(req.Format)
		if strFormat != `null` && strFormat != `""` {
			if s.textProcessor != nil {
				// New engine handles this on the backend
				request["format"] = req.Format
			} else {
				// old engine
				switch strFormat {
				case `"json"`:
					request["grammar"] = grammarJSON
				default:
					if req.Format[0] != '{' {
						return fmt.Errorf("invalid format: %q; expected \"json\" or a valid JSON Schema object", req.Format)
					}

					// User provided a JSON schema
					g := llama.SchemaToGrammar(req.Format)
					if g == nil {
						return fmt.Errorf("invalid JSON schema in format")
					}
					request["grammar"] = string(g)
				}
			}
		}
	}


Review comment (suggestion):
```suggestion
		format := string(req.Format)
```

Code after:
	request := map[string]any{
		"prompt":            req.Prompt,
		"stream":            true,
		"n_predict":         req.Options.NumPredict,
		"n_keep":            req.Options.NumKeep,
		"main_gpu":          req.Options.MainGPU,
		"temperature":       req.Options.Temperature,
		"top_k":             req.Options.TopK,
		"top_p":             req.Options.TopP,
		"min_p":             req.Options.MinP,
		"typical_p":         req.Options.TypicalP,
		"repeat_last_n":     req.Options.RepeatLastN,
		"repeat_penalty":    req.Options.RepeatPenalty,
		"presence_penalty":  req.Options.PresencePenalty,
		"frequency_penalty": req.Options.FrequencyPenalty,
		"mirostat":          req.Options.Mirostat,
		"mirostat_tau":      req.Options.MirostatTau,
		"mirostat_eta":      req.Options.MirostatEta,
		"seed":              req.Options.Seed,
		"stop":              req.Options.Stop,
		"image_data":        req.Images,
		"cache_prompt":      true,
	}

	if len(req.Format) > 0 {
		format := string(req.Format)
		if format != `null` && format != `""` {
			if s.textProcessor != nil {
				// New engine handles this on the backend
				request["format"] = req.Format
			} else {
				// old engine
				switch format {
				case `"json"`:
					request["grammar"] = grammarJSON
				default:
					if req.Format[0] != '{' {
						return fmt.Errorf("invalid format: %q; expected \"json\" or a valid JSON Schema object", req.Format)
					}

					// User provided a JSON schema
					g := llama.SchemaToGrammar(req.Format)
					if g == nil {
						return fmt.Errorf("invalid JSON schema in format")
					}
					request["grammar"] = string(g)
				}
			}
		}
	}