domain

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package domain defines core types and interfaces for structured LLM outputs.

This package provides the foundational elements for parsing, validating, and working with structured outputs from language models. It includes parsers for various output formats and integration with schema validation.

Core Concepts:

Parser Interface: The Parser interface defines the contract for converting LLM text outputs into structured Go values. Implementations handle different output formats such as JSON, YAML, XML, and custom formats.

Parser Types:

  • JSONParser: Extracts and parses JSON from LLM responses
  • YAMLParser: Handles YAML-formatted outputs
  • XMLParser: Processes XML responses
  • RegexParser: Uses patterns to extract structured data
  • CustomParser: Allows domain-specific parsing logic

Schema Integration: Parsers work with the schema package to validate extracted data:

schema := &schema.Schema{
    Type: "object",
    Properties: map[string]*schema.Schema{
        "name": {Type: "string"},
        "age":  {Type: "number"},
    },
}

parser := NewJSONParser(schema)
result, err := parser.Parse(llmOutput)

Error Handling: The package provides detailed error types for different parsing failures:

  • ParseError: General parsing failures
  • ValidationError: Schema validation failures
  • FormatError: Unexpected format issues

Best Practices:

  • Always provide schemas for predictable parsing
  • Use appropriate parser types for expected formats
  • Handle partial extraction gracefully
  • Implement custom parsers for domain-specific needs

Example Usage:

// Create a parser with schema
parser := domain.NewJSONParser(mySchema)

// Parse LLM output
output := "The result is: {\"status\": \"success\", \"data\": [1,2,3]}"
result, err := parser.Parse(output)
if err != nil {
    log.Printf("Parse error: %v", err)
}

// Use the structured result
data := result.(map[string]interface{})

Package domain defines core domain models and interfaces for structured LLM outputs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Processor

type Processor interface {
	// Process processes a raw output string against a schema.
	// It extracts structured data from the LLM output and validates it
	// against the provided schema, returning the parsed result.
	Process(schema *schemaDomain.Schema, output string) (interface{}, error)

	// ProcessTyped processes a raw output string against a schema and maps it to a specific type.
	// The target parameter should be a pointer to the desired type. The extracted data
	// will be unmarshaled into this target after schema validation.
	ProcessTyped(schema *schemaDomain.Schema, output string, target interface{}) error

	// ToJSON converts an object to a JSON string.
	// This is useful for serializing structured data back to JSON format.
	ToJSON(obj interface{}) (string, error)
}

Processor defines the contract for structured output processing. Implementations extract and validate structured data from LLM text responses according to provided JSON schemas.

type PromptEnhancer

type PromptEnhancer interface {
	// Enhance adds schema information to a prompt.
	// It augments the original prompt with instructions about the expected
	// output format based on the provided JSON schema.
	Enhance(prompt string, schema *schemaDomain.Schema) (string, error)

	// EnhanceWithOptions adds schema information to a prompt with additional options.
	// Options may include formatting preferences, example outputs, or provider-specific
	// instructions for structured output generation.
	EnhanceWithOptions(prompt string, schema *schemaDomain.Schema, options map[string]interface{}) (string, error)
}

PromptEnhancer defines the contract for enhancing prompts with schema information. Implementations add schema constraints and formatting instructions to prompts to improve the likelihood of receiving properly structured responses from LLMs.

Jump to

Keyboard shortcuts

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