Documentation
¶
Overview ¶
Package promptr is the runtime that generated .promptr code calls into: a minimal, provider-agnostic Provider interface plus the parse-with-repair loop that turns a model's loose reply into a typed Go value via the coerce kernel.
The compiler (cmd/promptr) turns each `function` in a .promptr file into a Go function whose body is a single call to Extract — so the generated code stays thin and readable, and all the retry/coercion logic lives here, tested once.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Extract ¶
Extract runs prompt against the provider and coerces the reply into T. If the reply will not coerce, it re-asks — appending the unparseable reply and the parse error so the model can correct itself — up to Options.Attempts times.
This is the function every generated .promptr function calls.
func Render ¶ added in v0.2.0
Render expands a prompt template against a context map and returns the finished prompt. It is the small, dependency-free template engine the compiler targets: generated functions call Render with the call's parameters (plus a "ctx" entry carrying the baked output-schema) so prompts can use control flow over runtime values.
Supported syntax (all inside {{ }}):
{{ name }} value lookup, with dotted paths: {{ user.name }}
{{ ctx.output_schema }} the compiler-injected schema description
{{ if cond }}…{{ end }} conditional, with optional {{ else }}
{{ for x in items }}…{{ end }} iterate a slice, binding x in the body
A condition is a path (truthy test), `not <path>`, or `<path> == "lit"` / `<path> != "lit"`. Lookups miss softly: an unknown name renders as empty rather than erroring, so a template can never panic on real model context. Render only returns an error for a structurally broken template (an unterminated {{ if }}/{{ for }}).
Types ¶
type Message ¶
Message is one turn in a chat-style exchange. Roles follow the usual "system" / "user" / "assistant" convention; a Provider maps them to whatever its backend expects.
type Options ¶
type Options struct {
// Attempts is the maximum number of model calls (default 2): the first
// try plus repair re-asks when the reply will not coerce into T.
Attempts int
// System, when non-empty, is prepended as a system message.
System string
}
Options tunes an Extract call.
type Provider ¶
Provider is the single seam between promptr and a language model. Implement it with net/http against any chat API — the core imports no vendor SDK. A deterministic fake lives in providers/fake for tests and the playground.
func Fallback ¶ added in v0.2.0
Fallback wraps providers so Complete tries each in order, returning the first success; if all fail, the last error is returned. Use it to fail over from a primary model to a backup.
func Retry ¶ added in v0.2.0
Retry wraps p so that Complete is retried up to attempts times on error, sleeping backoff between tries (respecting context cancellation). attempts < 1 is treated as 1. This is reliability against transient failures, distinct from Extract's parse-repair loop (which re-asks on unparseable but successful replies).
func RoundRobin ¶ added in v0.2.0
RoundRobin wraps providers so each Complete call goes to the next provider in rotation — load-spreading across keys or endpoints. It is safe for concurrent use.
type Registry ¶ added in v0.2.0
Registry maps the client names declared in a .promptr file to live Providers the caller has wired up. Generated client constructors resolve their provider references through a Registry, so the DSL stays free of credentials.
type TemplateError ¶ added in v0.2.0
type TemplateError struct{ Msg string }
TemplateError reports a structurally invalid template.
func (*TemplateError) Error ¶ added in v0.2.0
func (e *TemplateError) Error() string
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
promptr
command
Command promptr compiles .promptr schema files into Go.
|
Command promptr compiles .promptr schema files into Go. |
|
Package codegen turns a parsed .promptr File into idiomatic Go source.
|
Package codegen turns a parsed .promptr File into idiomatic Go source. |
|
Package coerce is a schema-aligned parser: it turns the loose, near-JSON text that language models actually emit into typed Go values.
|
Package coerce is a schema-aligned parser: it turns the loose, near-JSON text that language models actually emit into typed Go values. |
|
Package dsl lexes and parses the .promptr schema language into an AST.
|
Package dsl lexes and parses the .promptr schema language into an AST. |
|
examples
|
|
|
ticket
Package ticket is an end-to-end promptr example: a .promptr schema compiled to Go (ticket.promptr.go) and exercised against the fake provider in ticket_test.go.
|
Package ticket is an end-to-end promptr example: a .promptr schema compiled to Go (ticket.promptr.go) and exercised against the fake provider in ticket_test.go. |
|
providers
|
|
|
anthropic
Package anthropic is a promptr.Provider backed by the Anthropic Messages API, built on net/http alone — no vendor SDK, in keeping with promptr's zero-SDK core.
|
Package anthropic is a promptr.Provider backed by the Anthropic Messages API, built on net/http alone — no vendor SDK, in keeping with promptr's zero-SDK core. |
|
fake
Package fake is a deterministic promptr.Provider for tests, examples and the playground — no network, no API key.
|
Package fake is a deterministic promptr.Provider for tests, examples and the playground — no network, no API key. |
|
gemini
Package gemini is a promptr.Provider for Google's Generative Language API (Gemini), built on net/http alone — no vendor SDK.
|
Package gemini is a promptr.Provider for Google's Generative Language API (Gemini), built on net/http alone — no vendor SDK. |
|
ollama
Package ollama is a promptr.Provider for a local Ollama server, built on net/http alone — no vendor SDK.
|
Package ollama is a promptr.Provider for a local Ollama server, built on net/http alone — no vendor SDK. |
|
openai
Package openai is a promptr.Provider for any OpenAI Chat Completions-compatible API, built on net/http alone — no vendor SDK.
|
Package openai is a promptr.Provider for any OpenAI Chat Completions-compatible API, built on net/http alone — no vendor SDK. |