Help us shape the future of Go! Take the 2026 Go Developer Survey

qa

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package qa generates multiple-choice questionnaires from parsed book sources chapter by chapter, using a caller-supplied model, and persists them as a JSON question bank.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Labels

func Labels(n int) []string

Labels returns A, B, C, ... for n options.

func SaveBank

func SaveBank(path string, bank *QuestionBank) error

SaveBank writes a question bank to path as indented JSON.

Types

type Config

type Config struct {
	// Sources limits generation to these source names. Empty means all.
	Sources []string `json:"sources,omitempty"`
	// PerChapter is the number of questions to request from each chapter.
	PerChapter int `json:"per_chapter,omitempty"`
	// Total, when PerChapter is zero, is distributed across the selected
	// chapters.
	Total int `json:"total,omitempty"`
	// MaxChapters caps how many chapters are sampled (spread evenly across the
	// whole book). Zero means all.
	MaxChapters int `json:"max_chapters,omitempty"`
	// Options is the number of answer choices per question. Default 4.
	Options int `json:"options,omitempty"`
	// Difficulty is the requested difficulty: easy, medium, hard, or mixed.
	// Default "hard".
	Difficulty string `json:"difficulty,omitempty"`
	// Style customizes the flavor of questions. Defaults to deep, subjective,
	// reasoning-heavy questions that avoid trivia.
	Style string `json:"style,omitempty"`
	// IncludeExplanation asks the model for a rationale for the correct answer.
	IncludeExplanation bool `json:"include_explanation,omitempty"`
	// MaxChapterChars bounds how much chapter text is sent per generation call;
	// longer chapters are summarized into key points first. Default 24000.
	MaxChapterChars int `json:"max_chapter_chars,omitempty"`
	// Language is the language of the generated questions. Default "English".
	Language string `json:"language,omitempty"`
	// Seed is an optional value included in prompts to nudge variety.
	Seed int64 `json:"seed,omitempty"`
}

Config controls questionnaire generation.

func (Config) Resolve

func (c Config) Resolve() Config

Resolve fills zero fields with their defaults.

type Generator

type Generator struct {
	// Model authors the questions.
	Model model.BaseModel[*schema.Message]
	// Logger receives progress output. Optional.
	Logger *slog.Logger
	// MaxRetries is the number of corrective re-prompts on unparseable output.
	// Default 2.
	MaxRetries int
	// System overrides the built-in system prompt.
	System string
}

Generator produces question banks from parsed sources using a model.

func NewGenerator

func NewGenerator(m model.BaseModel[*schema.Message], logger *slog.Logger) *Generator

NewGenerator returns a Generator for the given model.

func (*Generator) Generate

func (g *Generator) Generate(ctx context.Context, sources []*corpus.Source, cfg Config) (*QuestionBank, error)

Generate produces a question bank from the given sources. When cfg.Sources is set, only those sources are used.

type Question

type Question struct {
	ID           string   `json:"id"`
	Source       string   `json:"source"`
	Chapter      int      `json:"chapter"`
	ChapterTitle string   `json:"chapter_title"`
	Question     string   `json:"question"`
	Options      []string `json:"options"`
	Answer       int      `json:"answer"` // 0-based index of the correct option
	Explanation  string   `json:"explanation,omitempty"`
	Difficulty   string   `json:"difficulty,omitempty"`
	Tags         []string `json:"tags,omitempty"`
}

Question is a single generated multiple-choice question with its ground truth.

func (Question) AnswerLabel

func (q Question) AnswerLabel() string

AnswerLabel returns the label of the correct option.

func (Question) Labels

func (q Question) Labels() []string

Labels returns the option labels (A, B, C, ...) for a question.

type QuestionBank

type QuestionBank struct {
	CreatedAt time.Time  `json:"created_at"`
	Sources   []string   `json:"sources"`
	Config    Config     `json:"config"`
	Questions []Question `json:"questions"`
}

QuestionBank is a persisted set of generated questions together with the configuration and sources used to produce them.

func LoadBank

func LoadBank(path string) (*QuestionBank, error)

LoadBank reads a question bank written by SaveBank.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL