narrative

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package narrative provides LLM-powered narrative generation for development episodes. It defines a provider-agnostic LLM interface with concrete implementations for OpenAI and deterministic mocks for testing. The generator consumes pre-assembled prompts and returns structured narrative objects.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLLMFailed     = errors.New("LLM request failed")
	ErrInvalidConfig = errors.New("invalid LLM configuration")
)
View Source
var (
	ErrGenerationFailed = errors.New("narrative generation failed")
)
View Source
var (
	ErrMissingTargetEpisode = errors.New("target episode required for episode-level narrative")
)

Functions

func AssemblePrompt

func AssemblePrompt(targetEpisode *cluster.Episode, contextChunks []rag.ContextChunk) (string, error)

Types

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator produces narratives from episodes using an LLM. It invokes an LLM on an already-assembled prompt.

func NewGenerator

func NewGenerator(llm LLM, config LLMConfig) *Generator

NewGenerator creates a narrative generator with the given LLM implementation.

func (*Generator) Generate

func (g *Generator) Generate(ctx context.Context, episodeID string, prompt string) (*Narrative, error)

Generate creates a narrative by invoking the LLM with an already-assembled prompt. It must not perform retrieval or prompt construction.

type LLM

type LLM interface {
	// Generate produces text from a prompt using the configured model.
	// Returns the generated text or an error if generation fails.
	Generate(ctx context.Context, prompt string) (string, error)
}

LLM defines the interface for interacting with language models. Implementations must be stateless and thread-safe.

type LLMConfig

type LLMConfig struct {
	// Model specifies the model identifier (e.g., "gpt-4", "gpt-3.5-turbo")
	Model string

	// Temperature controls randomness (0.0 = deterministic, 2.0 = very random)
	Temperature float32

	// MaxTokens limits the response length (0 = use provider default)
	MaxTokens int

	// APIKey is the authentication key for the provider
	APIKey string
}

LLMConfig holds common configuration options for LLM providers.

func DefaultLLMConfig

func DefaultLLMConfig() LLMConfig

DefaultLLMConfig returns sensible defaults for narrative generation.

type MockLLM

type MockLLM struct {
	// Response is the fixed text returned by Generate.
	// If empty, a default response is generated from the prompt.
	Response string

	// Error, if set, is returned by Generate instead of a response.
	Error error

	// LastPrompt stores the most recent prompt passed to Generate.
	LastPrompt string
}

MockLLM is a deterministic LLM implementation for testing. It returns predictable responses based on prompt content.

func NewMockLLM

func NewMockLLM(response string) *MockLLM

NewMockLLM creates a mock LLM with the given fixed response.

func NewMockLLMWithError

func NewMockLLMWithError(err error) *MockLLM

NewMockLLMWithError creates a mock LLM that always returns an error.

func (*MockLLM) Generate

func (m *MockLLM) Generate(ctx context.Context, prompt string) (string, error)

Generate returns the configured response or generates a deterministic one.

type Narrative

type Narrative struct {
	// EpisodeID identifies the episode this narrative describes
	EpisodeID string `json:"episode_id"`

	// Text is the generated narrative content
	Text string `json:"text"`

	// GeneratedAt is when this narrative was created
	GeneratedAt time.Time `json:"generated_at"`

	// Model is the LLM model used to generate this narrative
	Model string `json:"model"`
}

Narrative represents a generated human-readable explanation of an episode.

type OpenAILLM

type OpenAILLM struct {
	// contains filtered or unexported fields
}

OpenAILLM implements the LLM interface using OpenAI's API.

func NewOpenAILLM

func NewOpenAILLM(config LLMConfig) (*OpenAILLM, error)

NewOpenAILLM creates an OpenAI-backed LLM implementation. Returns an error if the API key is missing or invalid.

func (*OpenAILLM) Generate

func (o *OpenAILLM) Generate(ctx context.Context, prompt string) (string, error)

Generate sends the prompt to OpenAI and returns the generated text.

Jump to

Keyboard shortcuts

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