formatter

package
v1.1.13 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Reset   = "\033[0m"
	Red     = "\033[31m"
	Green   = "\033[32m"
	Yellow  = "\033[33m"
	Blue    = "\033[34m"
	Magenta = "\033[35m"
	Cyan    = "\033[36m"
	White   = "\033[37m"
	Bold    = "\033[1m"
	Dim     = "\033[2m"
)

ANSI 颜色码

Variables

This section is empty.

Functions

func WithColors

func WithColors(score, content, meta, title string) func(*TerminalConfig)

WithColors 设置颜色方案

func WithContentMax

func WithContentMax(max int) func(*TerminalConfig)

WithContentMax 设置内容最大长度

func WithContentMaxPrompt

func WithContentMaxPrompt(max int) func(*PromptConfig)

WithContentMaxPrompt 设置内容最大长度

func WithContextTemplate

func WithContextTemplate(tmpl string) func(*PromptConfig)

WithContextTemplate 设置上下文模板

func WithDocumentTemplate

func WithDocumentTemplate(tmpl string) func(*PromptConfig)

WithDocumentTemplate 设置文档模板

func WithIncludeScore

func WithIncludeScore(include bool) func(*PromptConfig)

WithIncludeScore 设置是否包含分数

func WithIncludeSource

func WithIncludeSource(include bool) func(*PromptConfig)

WithIncludeSource 设置是否包含来源

func WithMaxDocuments

func WithMaxDocuments(max int) func(*PromptConfig)

WithMaxDocuments 设置最大文档数

func WithShowDocID

func WithShowDocID(show bool) func(*TerminalConfig)

WithShowDocID 设置是否显示文档ID

func WithShowIndex

func WithShowIndex(show bool) func(*TerminalConfig)

WithShowIndex 设置是否显示序号

func WithShowScore

func WithShowScore(show bool) func(*TerminalConfig)

WithShowScore 设置是否显示分数

func WithSystemPrompt

func WithSystemPrompt(prompt string) func(*PromptConfig)

WithSystemPrompt 设置系统提示词

Types

type JSONFormatter

type JSONFormatter struct {
	core.BaseFormatter
}

JSONFormatter JSON 格式化器

func NewJSONFormatter

func NewJSONFormatter() *JSONFormatter

NewJSONFormatter 创建 JSON 格式化器

func (*JSONFormatter) FormatAll

func (f *JSONFormatter) FormatAll(hits []core.Hit) string

FormatAll 格式化为 JSON

type Message

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

Message 对话消息

type PromptConfig

type PromptConfig struct {
	// 系统提示词模板
	SystemPrompt string
	// 上下文模板
	ContextTemplate string
	// 单个文档模板
	DocumentTemplate string
	// 是否包含分数
	IncludeScore bool
	// 是否包含来源
	IncludeSource bool
	// 内容最大长度
	ContentMax int
	// 最大文档数
	MaxDocuments int
	// 分隔符
	Separator string
}

PromptConfig Prompt 格式化配置

func DefaultPromptConfig

func DefaultPromptConfig() *PromptConfig

DefaultPromptConfig 默认 Prompt 配置

type PromptFormatter

type PromptFormatter struct {
	core.BaseFormatter
	// contains filtered or unexported fields
}

PromptFormatter LLM Prompt 格式化器 用于生成抑制幻觉的提示词

func NewPromptFormatter

func NewPromptFormatter(opts ...func(*PromptConfig)) *PromptFormatter

NewPromptFormatter 创建 Prompt 格式化器

func (*PromptFormatter) Format

func (f *PromptFormatter) Format(hit *core.Hit) string

Format 格式化单个 Hit

func (*PromptFormatter) FormatAll

func (f *PromptFormatter) FormatAll(hits []core.Hit) string

FormatAll 格式化多个 Hit(不包含查询)

func (*PromptFormatter) FormatForRAG

func (f *PromptFormatter) FormatForRAG(hits []core.Hit, query string) string

FormatForRAG 生成标准的 RAG Prompt 包含系统提示、文档上下文和用户查询

func (*PromptFormatter) FormatMessages

func (f *PromptFormatter) FormatMessages(hits []core.Hit, query string) []Message

FormatMessages 生成对话格式的消息列表 返回 [system, user] 两条消息

func (*PromptFormatter) FormatWithContext

func (f *PromptFormatter) FormatWithContext(hits []core.Hit, query string) string

FormatWithContext 格式化为完整 Prompt

func (*PromptFormatter) FormatWithTemplate

func (f *PromptFormatter) FormatWithTemplate(hits []core.Hit, query string) (string, error)

FormatWithTemplate 使用自定义模板格式化

func (*PromptFormatter) Write

func (f *PromptFormatter) Write(w io.Writer, hits []core.Hit) error

Write 格式化并写入输出流

func (*PromptFormatter) WriteWithContext

func (f *PromptFormatter) WriteWithContext(w io.Writer, hits []core.Hit, query string) error

WriteWithContext 格式化完整 Prompt 并写入输出流

type TerminalConfig

type TerminalConfig struct {
	ShowScore    bool   // 是否显示分数
	ShowDocID    bool   // 是否显示文档ID
	ShowIndex    bool   // 是否显示序号
	ContentMax   int    // 内容最大长度,0 表示不限制
	ScoreColor   string // 分数颜色
	ContentColor string // 内容颜色
	MetaColor    string // 元数据颜色
	TitleColor   string // 标题颜色
}

TerminalConfig 终端格式化配置

func DefaultTerminalConfig

func DefaultTerminalConfig() *TerminalConfig

DefaultTerminalConfig 默认终端配置

type TerminalFormatter

type TerminalFormatter struct {
	core.BaseFormatter
	// contains filtered or unexported fields
}

TerminalFormatter 终端彩色输出格式化器

func NewTerminalFormatter

func NewTerminalFormatter(opts ...func(*TerminalConfig)) *TerminalFormatter

NewTerminalFormatter 创建终端格式化器

func (*TerminalFormatter) Format

func (f *TerminalFormatter) Format(hit *core.Hit) string

Format 格式化单个 Hit

func (*TerminalFormatter) FormatAll

func (f *TerminalFormatter) FormatAll(hits []core.Hit) string

FormatAll 格式化多个 Hit

func (*TerminalFormatter) Print

func (f *TerminalFormatter) Print(hits []core.Hit)

Print 便捷方法:直接打印到标准输出

func (*TerminalFormatter) Println

func (f *TerminalFormatter) Println(hits []core.Hit)

Println 便捷方法:打印并换行

func (*TerminalFormatter) Write

func (f *TerminalFormatter) Write(w io.Writer, hits []core.Hit) error

Write 格式化并写入输出流

Jump to

Keyboard shortcuts

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