Packages
llama_cpp_ex
0.7.5
0.8.36
0.8.35
0.8.34
0.8.33
0.8.32
0.8.31
0.8.28
0.8.27
0.8.26
0.8.25
0.8.24
0.8.23
0.8.22
0.8.21
0.8.20
0.8.19
0.8.18
0.8.17
0.8.16
0.8.15
0.8.14
0.8.13
0.8.12
0.8.11
0.8.10
0.8.9
0.8.8
0.8.7
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.9
0.7.8
0.7.7
0.7.6
0.7.5
0.7.4
0.7.3
0.7.2
0.7.0
0.6.14
0.6.13
0.6.12
0.6.11
0.6.10
0.6.9
0.6.8
0.6.7
0.6.6
0.6.5
0.6.4
0.6.3
0.6.1
0.6.0
0.5.0
0.4.4
0.4.3
0.4.2
0.4.1
0.3.0
0.2.0
Elixir bindings for llama.cpp — run LLMs locally with Metal, CUDA, Vulkan, or CPU acceleration.
Current section
Files
Jump to
Current section
Files
lib/llama_cpp_ex/grammar.ex
defmodule LlamaCppEx.Grammar do
@moduledoc """
Converts JSON Schema to GBNF grammar for constrained generation.
Uses llama.cpp's built-in `json_schema_to_grammar()` to convert a JSON Schema
into a GBNF grammar string that can be used with the `:grammar` option.
In most cases you don't need to call this module directly — pass `:json_schema`
to `LlamaCppEx.generate/3`, `LlamaCppEx.chat/3`, or any other generate function
and the conversion happens automatically.
## Examples
schema = %{
"type" => "object",
"properties" => %{
"name" => %{"type" => "string"},
"age" => %{"type" => "integer"}
},
"required" => ["name", "age"],
"additionalProperties" => false
}
{:ok, gbnf} = LlamaCppEx.Grammar.from_json_schema(schema)
# Use with the :grammar option
{:ok, sampler} = LlamaCppEx.Sampler.create(model, grammar: gbnf, temp: 0.0)
Supports all JSON Schema types: `object`, `array`, `string`, `number`,
`integer`, `boolean`, `null`, `enum`, `oneOf`, `anyOf`, `allOf`, `$ref`, etc.
"""
@doc """
Converts a JSON Schema map to a GBNF grammar string.
Returns `{:ok, gbnf_string}` on success or `{:error, reason}` on failure.
"""
@spec from_json_schema(map()) :: {:ok, String.t()} | {:error, String.t()}
def from_json_schema(schema) when is_map(schema) do
json_str = JSON.encode!(schema)
LlamaCppEx.NIF.json_schema_to_grammar_nif(json_str)
end
@doc """
Converts a JSON Schema map to a GBNF grammar string.
Returns the GBNF string on success or raises on failure.
"""
@spec from_json_schema!(map()) :: String.t()
def from_json_schema!(schema) when is_map(schema) do
case from_json_schema(schema) do
{:ok, gbnf} ->
gbnf
{:error, reason} ->
raise ArgumentError, "failed to convert JSON schema to grammar: #{reason}"
end
end
end