LlamaCppEx.Schema (LlamaCppEx v0.7.0)

Copy Markdown View Source

Converts Ecto schema modules to JSON Schema maps for structured output.

Requires {:ecto, "~> 3.0"} as an optional dependency. If Ecto is not available, calling these functions will raise at runtime.

Type Mapping

Ecto typeJSON Schema
:string, :binary{"type": "string"}
:integer, :id{"type": "integer"}
:float, :decimal{"type": "number"}
:boolean{"type": "boolean"}
:map{"type": "object"}
{:array, inner}{"type": "array", "items": ...}
:date{"type": "string", "format": "date"}
:utc_datetime, etc.{"type": "string", "format": "date-time"}
embeds_onenested object
embeds_manyarray of nested objects

Excluded Fields

The following fields are automatically excluded: :id, :inserted_at, :updated_at, and virtual fields.

Examples

defmodule MyApp.Person do
  use Ecto.Schema

  schema "people" do
    field :name, :string
    field :age, :integer
    field :email, :string
    timestamps()
  end
end

schema = LlamaCppEx.Schema.to_json_schema(MyApp.Person)
# => %{"type" => "object", "properties" => %{"name" => ..., "age" => ..., "email" => ...}, ...}

# Use directly with generate/chat
{:ok, json} = LlamaCppEx.chat(model, messages, json_schema: schema, temp: 0.0)

Summary

Functions

Converts an Ecto schema module to a JSON Schema map.

Functions

to_json_schema(module)

@spec to_json_schema(module()) :: map()

Converts an Ecto schema module to a JSON Schema map.

Extracts fields from the schema, maps Ecto types to JSON Schema types, and marks all non-virtual fields as required. Excludes :id, timestamp fields (:inserted_at, :updated_at), and virtual fields.