Documentation
¶
Overview ¶
Package reasoning provides primitives for working with reasoning content.
This package implements utilities for extracting and processing reasoning (step-by-step thinking) content from language model responses. It operates in two modes:
Streaming mode: Uses a stateful ChunkContentSplitter to process content chunks as they arrive, separating text from reasoning blocks marked with <think> or <thinking> tags. The splitter maintains state between calls to handle cases where reasoning blocks span multiple chunks.
Complete content mode: Uses SplitContent function to separate reasoning content from a complete response, handling cases where the LLM backend hasn't explicitly divided reasoning from regular content.
The package recognizes reasoning content enclosed in <think>...</think> or <thinking>...</thinking> tags, which various LLM providers use to indicate step-by-step thinking processes.
Example usage for streaming mode:
splitter := reasoning.NewChunkContentSplitter()
for chunk := range responseChunks {
text, reasoning := splitter.Split(chunk)
if reasoning != "" {
// Process reasoning content (e.g., display as step-by-step thinking)
fmt.Println("Reasoning:", reasoning)
}
if text != "" {
// Process regular text content
fmt.Println("Content:", text)
}
}
Example usage for complete content mode:
response := "Here's what I found: <thinking>First, I need to analyze the data.
The pattern shows increasing values.</thinking> The trend is clearly upward."
reasoning, content := reasoning.SplitContent(response)
fmt.Println("Reasoning:", reasoning) // "First, I need to analyze the data. The pattern shows increasing values."
fmt.Println("Content:", content) // "Here's what I found: The trend is clearly upward."
See also:
- IsReasoningModel: Checks if a model supports reasoning
- DefaultIsReasoningModel: Provides the default reasoning model detection logic
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultIsReasoningModel ¶
DefaultIsReasoningModel provides the default reasoning model detection logic. This can be used by LLM implementations that want to extend rather than replace the default detection logic.
func IsReasoningModel ¶
IsReasoningModel returns true if the model is a reasoning/thinking model. This includes OpenAI o1/o3/GPT-5 series, Anthropic Claude 3.7+, DeepSeek reasoner, etc. For runtime checking of LLM instances, use SupportsReasoningModel instead.
func SplitContent ¶
Types ¶
type ChunkContentSplitter ¶
type ChunkContentSplitter interface {
Split(chunk string) (string, string)
GetState() ChunkContentSplitterState
}
func NewChunkContentSplitter ¶
func NewChunkContentSplitter() ChunkContentSplitter
type ChunkContentSplitterState ¶
type ChunkContentSplitterState int
const ( ChunkContentSplitterStateText ChunkContentSplitterState = iota ChunkContentSplitterStateReasoning )
type ContentReasoning ¶
type ContentReasoning struct {
// Content is the reasoning content of the assistant message before the final answer.
Content string `json:"content,omitempty"`
// Signature is the signature of the reasoning contents.
Signature []byte `json:"signature,omitempty"`
// RedactedContent is the redacted reasoning contents for Anthropic provider.
RedactedContent []byte `json:"redacted_content,omitempty"`
}
func SplitContentWithReasoning ¶
func SplitContentWithReasoning(content string) (*ContentReasoning, string)
func (*ContentReasoning) IsEmpty ¶
func (r *ContentReasoning) IsEmpty() bool
func (*ContentReasoning) MarshalJSON ¶
func (r *ContentReasoning) MarshalJSON() ([]byte, error)
func (*ContentReasoning) String ¶
func (r *ContentReasoning) String() string
func (*ContentReasoning) UnmarshalJSON ¶
func (r *ContentReasoning) UnmarshalJSON(data []byte) error