Feedback

Chat Icon

Local AI Engineering with Ollama

Run, understand, customize, fine-tune, and build agentic apps on your own hardware

Building, Running, and Sharing Custom Models for Ollama (Modelfile)
59%

Step 4: Stop the Chatter with a Stop Sequence

In this step, we want to get the commit message and nothing after it.

Models often keep talking. They give you the message, then add one or many lines to explain what they did or why, but we want to get the message and nicely end the chat.

Let's examine a typical response:

# ollama run commitwriter "added a retry with backoff to the API client and a test for it"

## Beginning of response ## 
feat(api): add retry with backoff

Implement exponential backoff strategy for failed API requests. Added corresponding unit test to verify behavior under various failure scenarios.
## End of response ##

A git commit message starts with a one-line subject. If there is more to say, a blank line follows, then a body that explains the change. This tool only needs the subject, so you want the model to stop at the end of that first line. The stop sequence for that is a newline, the line break that comes right after the subject.

One catch carries over from before: the value has to be a real line break, not the text \n. A Modelfile does not turn \n into a newline, so you embed an actual line break with triple quotes.

Here's how we can translate this into the Modelfile:

cat < $HOME/commitwriter/Modelfile
# commitwriter - turns a code change into a commit message
FROM granite4.1:3b

SYSTEM """
You write git commit messages in Conventional Commits format.
Read the user's description or code diff. Reply with one commit message and nothing else.

Use this shape:
type(

Local AI Engineering with Ollama

Run, understand, customize, fine-tune, and build agentic apps on your own hardware

Enroll now to unlock all content and receive all future updates for free.