Documentation
¶
Overview ¶
Package parser extracts structured content from LLM responses.
Core types:
- Response: Contains structured data extracted from an LLM response
- CodeBlock: A fenced code block with language and content
- Parser: Extracts code blocks, JSON, YAML, sections, and lists
Example usage:
p := parser.NewParser()
resp := p.Parse(llmOutput)
// Access extracted code blocks
for _, block := range resp.CodeBlocks {
fmt.Printf("Language: %s\nCode:\n%s\n", block.Language, block.Content)
}
// Access parsed JSON
for _, data := range resp.JSONBlocks {
fmt.Printf("JSON: %v\n", data)
}
Convenience functions:
json := parser.ExtractJSON(response) code := parser.ExtractCode(response, "go") parsed := parser.Parse(response)
Index ¶
- func ExtractCode(response, language string) string
- func ExtractJSON(response string) map[string]any
- type CodeBlock
- type Parser
- func (p *Parser) ExtractAllCode(response string) []CodeBlock
- func (p *Parser) ExtractCode(response, language string) string
- func (p *Parser) ExtractJSON(response string) map[string]any
- func (p *Parser) ExtractJSONArray(response string) []map[string]any
- func (p *Parser) ExtractList(response string) []string
- func (p *Parser) ExtractNumberedList(response string) []string
- func (p *Parser) ExtractSection(response, title string) string
- func (p *Parser) ExtractYAML(response string) []map[string]any
- func (p *Parser) HasCodeBlock(response string) bool
- func (p *Parser) HasJSON(response string) bool
- func (p *Parser) Parse(response string) *Response
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractCode ¶
ExtractCode is a convenience function for code extraction.
func ExtractJSON ¶
ExtractJSON is a convenience function for JSON extraction.
Types ¶
type CodeBlock ¶
type CodeBlock struct {
// Language is the language specifier after the opening fence (e.g., "go", "python").
Language string
// Content is the code inside the block, excluding fences.
Content string
// Raw is the complete block including the fences.
Raw string
}
CodeBlock represents a fenced code block.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser extracts structured content from LLM responses.
func NewParser ¶
func NewParser() *Parser
NewParser creates a new response parser with compiled regexes.
func (*Parser) ExtractAllCode ¶
ExtractAllCode extracts all code blocks from the response.
func (*Parser) ExtractCode ¶
ExtractCode extracts the first code block with the given language. If language is empty, returns the first code block found.
func (*Parser) ExtractJSON ¶
ExtractJSON extracts and parses the first JSON block found. Returns nil if no valid JSON block is found.
func (*Parser) ExtractJSONArray ¶
ExtractJSONArray extracts and parses JSON arrays from code blocks. Returns all successfully parsed arrays.
func (*Parser) ExtractList ¶
ExtractList extracts list items from the response. Supports both - and * bullet points.
func (*Parser) ExtractNumberedList ¶
ExtractNumberedList extracts numbered list items.
func (*Parser) ExtractSection ¶
ExtractSection extracts the content of a specific section by title.
func (*Parser) ExtractYAML ¶
ExtractYAML extracts and parses YAML blocks.
func (*Parser) HasCodeBlock ¶
HasCodeBlock checks if the response contains any code block.
type Response ¶
type Response struct {
// Raw is the original response text.
Raw string
// Text is the response with code blocks removed.
Text string
// CodeBlocks contains all extracted code blocks.
CodeBlocks []CodeBlock
// JSONBlocks contains parsed JSON blocks.
JSONBlocks []map[string]any
// Sections contains extracted markdown sections by title.
Sections map[string]string
}
Response contains structured data extracted from an LLM response.