GBNF Guide GBNF (GGML BNF) is a format for defining formal grammars to constrain model…
- User
GBNF Guide
GBNF (GGML BNF) is a format for defining formal grammars to constrain model outputs in
llama.cpp. For example, you can use it to force the model to generate valid JSON, or speak only in emojis. GBNF grammars are supported in various ways inexamples/mainandexamples/server.Background
Bakus-Naur Form (BNF) is a notation for describing the syntax of formal languages like programming languages, file formats, and protocols. GBNF is an extension of BNF that primarily adds a few modern regex-like features.
Basics
In GBNF, we define production rules that specify how a non-terminal (rule name) can be replaced with sequences of terminals (characters, specifically Unicode code points) and other non-terminals. The basic format of a production rule is
nonterminal ::= sequence....Example
Before going deeper, let's look at some of the features demonstrated in
grammars/chess.gbnf, a small chess notation grammar:# `root` specifies the pattern for the overall output root ::= ( # it must start with the characters "1. " followed by a sequence # of characters that match the `move` rule, followed by a space, followed # by another move, and then a newline "1. " move " " move "\n" # it's followed by one or more subsequent moves, numbered with one or two digits ([1-9] [0-9]? ". " move " " move "\n")+ ) # `move` is an abstract representation, which can be a pawn, nonpawn, or castle. # The `[+#]?` denotes the possibility of checking or mate signs after moves move ::= (pawn | nonpawn | castle) [+#]? pawn ::= ... nonpawn ::= ... castle ::= ...Non-Terminals and Terminals
Non-terminal symbols (rule names) stand for a pattern of terminals and other non-terminals. They are required to be a dashed lowercase word, like
move,castle, orcheck-mate.Terminals are actual characters (code points). They can be specified as a sequence like
"1"or"O-O"or as ranges like[1-9]or[NBKQR].Characters and character ranges
Terminals support the full range of Unicode. Unicode characters can be specified directly in the grammar, for example , or with escapes: 8-bit (), 16-bit () or 32-bit ().
- Assistant
- User
Now convert the request from this text into the format described in the file I sent you "At the beginning of the game, the AI receives a message with the names and description of each character. Players write the actions their characters should perform. A player can also write "Nothing", in which case their character does absolutely nothing and just stands still. The AI then generates a response. Before the answer, the AI uses the [thinking] template, in which it ponders. What follows are PERSONAL conclusions for players, which are text messages meant only for a specific player, describing what is happening to their character at the moment. Each PERSONAL conclusion has a [[statistics]] field, which describes in very brief form the state of the player's character - his health, his inventory."
- Assistant
Based on the text you've provided and the format detailed in the GBNF guide, I'll craft a GBNF grammar that captures the essence of this game interaction scenario. The designed grammar aims to formalize the structure of the game's storytelling and response generation process, explicitly capturing the sequence of messages, player actions (including "Nothing"), AI's contemplative response, and personalized player conclusions with statistics.
- User
Now just give me this format, without youtr comments
- Assistant
# Grammar for a game interaction scenario root ::= ( gameStartMessage playerActions aiResponsePrepend aiPersonalConclusions* ) gameStartMessage ::= "At the beginning of the game, the AI receives a message with the names and description of each character.\n" playerActions ::= ( playerAction "\n" )* playerAction ::= playerName ": " (action | "Nothing") action ::= [^\n]+ playerName ::= [A-Za-z]+ aiResponsePrepend ::= "[thinking] " aiPersonalConclusions ::= ( "PERSONAL conclusion for " playerName ": " personalMessage " [[" statistics "]]\n" ) personalMessage ::= [^\n]+ statistics ::= "Health: " [0-9]+ ", Inventory: " inventoryItems inventoryItems ::= (item ", ")* item | "empty" item ::= [^\n]+