Ollama uses Modelfile to define and customize models. Modelfile is a text file, similar to Dockerfile, used to describe how to build and configure models.
Basic Modelfile Structure:
dockerfileFROM llama3.1 PARAMETER temperature 0.7 PARAMETER top_p 0.9 PARAMETER num_ctx 4096 SYSTEM You are a helpful assistant. TEMPLATE """ {{- range .Messages }} {{- if eq .Role "user" }}User: {{ .Content }} {{- else if eq .Role "assistant" }}Assistant: {{ .Content }} {{- end }} {{- end }} Assistant: """
Common Instructions:
- FROM - Specify base model
dockerfileFROM llama3.1:8b
- PARAMETER - Set model parameters
dockerfilePARAMETER temperature 0.7 PARAMETER top_p 0.9 PARAMETER num_ctx 4096 PARAMETER repeat_penalty 1.1
- SYSTEM - Set system prompt
dockerfileSYSTEM You are an expert Python developer.
- TEMPLATE - Customize prompt template
dockerfileTEMPLATE """ <|user|> {{ .Prompt }}<|end|> <|assistant|> """
- LICENSE - Add license information
dockerfileLICENSE MIT
- MESSAGE - Add example conversations
dockerfileMESSAGE user Hello MESSAGE assistant Hi there!
Creating Custom Models:
bash# Create Modelfile cat > Modelfile << EOF FROM llama3.1 SYSTEM You are a coding assistant specialized in Python. PARAMETER temperature 0.5 EOF # Build model ollama create my-coder -f Modelfile # Run custom model ollama run my-coder
View Model's Modelfile:
bashollama show --modelfile my-coder