[PR #7513] [CLOSED] grammar: surgically wrenching gbnf from system messages #38319

Closed
opened 2026-04-22 22:59:22 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/7513
Author: @tucnak
Created: 11/5/2024
Status: Closed

Base: mainHead: patch/gbnf


📝 Commits (1)

  • f1e69be grammar: surgically wrenching gbnf from system messages

📊 Changes

9 files changed (+286 additions, -0 deletions)

View changed files

📝 llm/server.go (+2 -0)
server/grammar.go (+53 -0)
server/grammar_test.go (+63 -0)
📝 server/routes.go (+18 -0)
server/testdata/grammar/gbnf.txt (+21 -0)
server/testdata/grammar/gbnf_markdown.txt (+11 -0)
server/testdata/grammar/gbnf_multi.txt (+36 -0)
server/testdata/grammar/gbnf_too.txt (+31 -0)
server/testdata/grammar/jsonschema.txt (+51 -0)

📄 Description

Some people have reached out to me re: my comment from earlier https://github.com/ollama/ollama/issues/6237#issuecomment-2428338129 so I'd decided it might be worth a shot. To re-cap: this pull request implements wrenching GBNF's (only one at a time!) from the system prompt. I know a bunch of pull requests to similar effect already exist.

However, I also believe that the prior efforts are completely misguided. What they should be doing instead is parsing the system prompt for ```gbnf code blocks. This approach does not impact the API surface, and it would also allow for dynamically generating the grammar on the fly from any existing Ollama client.

More details in the linked comment.

set GRAMMAR '
```gbnf
root  ::= (expr "=" ws term "\n")+
expr  ::= term ([-+*/] term)*
term  ::= ident | num | "(" ws expr ")" ws
ident ::= [a-z] [a-z0-9_]* ws
num   ::= [0-9]+ ws
ws    ::= [ \t\n]*
```'
curl http://ollama.lan/chat -d '{
  "model": "llama3.2",
  "messages": [
    {
      "role": "system",
      "content": "You are helpful assistant.\n$GRAMMAR"
    },
    {
      "role": "user",
      "content": "why is the sky blue?"
    }
  ]
}'

I've so far only committed the bare-bones portion (i.e. vanilla GBNF only) and some tests I had lying around—due to obvious reasons. I don't believe a jsonschema and/or openapi function-calling implementation is necessary, as it may as well be done trivially on the client-side, but hey, that may be fun for all I care.

Otherwise, happy to bestow my genius upon this world.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ollama/ollama/pull/7513 **Author:** [@tucnak](https://github.com/tucnak) **Created:** 11/5/2024 **Status:** ❌ Closed **Base:** `main` ← **Head:** `patch/gbnf` --- ### 📝 Commits (1) - [`f1e69be`](https://github.com/ollama/ollama/commit/f1e69bebcf6ec559f80eec9f33b49a549a8cdae8) grammar: surgically wrenching gbnf from system messages ### 📊 Changes **9 files changed** (+286 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `llm/server.go` (+2 -0) ➕ `server/grammar.go` (+53 -0) ➕ `server/grammar_test.go` (+63 -0) 📝 `server/routes.go` (+18 -0) ➕ `server/testdata/grammar/gbnf.txt` (+21 -0) ➕ `server/testdata/grammar/gbnf_markdown.txt` (+11 -0) ➕ `server/testdata/grammar/gbnf_multi.txt` (+36 -0) ➕ `server/testdata/grammar/gbnf_too.txt` (+31 -0) ➕ `server/testdata/grammar/jsonschema.txt` (+51 -0) </details> ### 📄 Description Some people have reached out to me re: my comment from earlier https://github.com/ollama/ollama/issues/6237#issuecomment-2428338129 so I'd decided it might be worth a shot. To re-cap: this pull request implements wrenching GBNF's (only one at a time!) from the system prompt. I know a bunch of pull requests to similar effect already exist. However, I also believe that the prior efforts are completely misguided. What they should be doing instead is parsing the system prompt for **\`\`\`gbnf** code blocks. This approach does not impact the API surface, and it would also allow for dynamically generating the grammar on the fly from _any_ existing Ollama client. More details in the linked comment. ```bash set GRAMMAR ' ```gbnf root ::= (expr "=" ws term "\n")+ expr ::= term ([-+*/] term)* term ::= ident | num | "(" ws expr ")" ws ident ::= [a-z] [a-z0-9_]* ws num ::= [0-9]+ ws ws ::= [ \t\n]* ```' curl http://ollama.lan/chat -d '{ "model": "llama3.2", "messages": [ { "role": "system", "content": "You are helpful assistant.\n$GRAMMAR" }, { "role": "user", "content": "why is the sky blue?" } ] }' ``` I've so far only committed the bare-bones portion (i.e. vanilla GBNF only) and some tests I had lying around—due to obvious reasons. I don't believe a `jsonschema` and/or `openapi` function-calling implementation is necessary, as it may as well be done trivially on the client-side, but hey, that may be fun for all I care. Otherwise, happy to bestow my genius upon this world. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-22 22:59:22 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#38319