internal/infra/inference/prompt.go
internal/infra/inference · 185 lines · 10 declarations · source
Declarations
var extractionPromptFile
var extractionPromptFile []byte
The extraction prompt is a file because it is prose, and prose is reviewed as prose. It is compiled in because it decides what this system extracts, and extraction feeds the write path: a prompt the filesystem can change is a path by which whoever can write next to the binary changes what ends up in a customer's memory. Embedding keeps the review benefit and closes that.
const relationsPlaceholder
const relationsPlaceholder = "{{relations}}"
relationsPlaceholder is where the vocabulary is rendered.
The predicate table is not written in the file: it is enforced by a foreign key, and a second copy of a closed set is a second place for it to drift. The file marks the position, the code renders the rows.
type promptFile
type promptFile struct {
ModelContract struct {
Temperature float32 `yaml:"temperature"`
ResponseFormat string `yaml:"response_format"`
} `yaml:"model_contract"`
Task string `yaml:"task"`
Relations string `yaml:"relations"`
QuoteRule string `yaml:"quote_rule"`
Polarity string `yaml:"polarity"`
Tense string `yaml:"tense"`
ReplyShape string `yaml:"reply_shape"`
DataBoundary string `yaml:"data_boundary"`
}
promptFile is the wording, and only the wording.
Every field is required. A section that may be absent is a prompt that can be silently shortened by an edit, and the section most worth removing — the one telling the model the message is data — is the one whose absence produces no error and no visible change until somebody plants a fact.
var extractionPrompt
var extractionPrompt = mustLoadPrompt(extractionPromptFile)
extractionPrompt is parsed once, at startup.
A panic is right here. The file is embedded, so a failure is not a runtime condition that might clear — it is a binary that must not start, and it is caught by any test run before it is ever caught by a deployment.
func mustLoadPrompt
func mustLoadPrompt(raw []byte) promptFile
func loadPrompt
func loadPrompt(raw []byte) (promptFile, error)
loadPrompt parses the prompt file and refuses one that could not compose a whole prompt.
func systemPrompt
func systemPrompt(vocabulary domain.Ontology) string
systemPrompt composes the prompt: the file's wording, in an order the file does not choose.
The order is fixed here, and data_boundary is last, because that section's job is to be the final word before somebody else's text arrives. A file that could reorder the sections could put the injection boundary in the middle, where an instruction after it appears to supersede it.
func renderVocabulary
func renderVocabulary(vocabulary domain.Ontology) string
renderVocabulary writes the relations out in full, one per line.
Each relation carries its description and the kind of thing its object should be. Naming the relations without saying what they mean produces confident misuse — `part_of` for employment, `has_status` for anything — and a wrong relation is worse than an unmapped one, because it is admitted and nothing downstream can tell it apart from a right one.
The order is the vocabulary's own, which is the order the database holds it in. Stable, so that two extractions of one message are given the same list: a prompt that reorders produces different output for the same input, and extraction quality stops being measurable.
type reportPromptFileShape
type reportPromptFileShape struct {
ModelContract struct {
Temperature float32 `yaml:"temperature"`
ResponseFormat string `yaml:"response_format"`
} `yaml:"model_contract"`
Task string `yaml:"task"`
Grounding string `yaml:"grounding"`
Shape string `yaml:"shape"`
ReplyShape string `yaml:"reply_shape"`
DataBoundary string `yaml:"data_boundary"`
}
reportPromptFileShape is the report prompt's wording, and only the wording.
Every field is required, for the reason the extraction prompt's are: a section that may be absent is a prompt that can be silently shortened by an edit, and the section most worth removing — the one telling the model the material is data — is the one whose absence produces no error and no visible change until somebody plants an instruction in a quote.
func loadReportPrompt
func loadReportPrompt(raw []byte) (reportPromptFileShape, error)
loadReportPrompt parses the report prompt and refuses one that could not compose a whole prompt.