Documentation
¶
Index ¶
- Variables
- func CloseQuietly(closer io.Closer)
- func CloseWithError(closer io.Closer, errp *error)
- func CloseWithLog(closer io.Closer, logFn func(error))
- type CloserFunc
- type Example
- type MultiCloser
- type PromptBuilder
- func (b *PromptBuilder) Build() string
- func (b *PromptBuilder) BuildWithTemplate(template string, vars map[string]string) string
- func (b *PromptBuilder) Reset() *PromptBuilder
- func (b *PromptBuilder) WithConstraint(constraint string) *PromptBuilder
- func (b *PromptBuilder) WithConstraints(constraints []string) *PromptBuilder
- func (b *PromptBuilder) WithContext(ctx string) *PromptBuilder
- func (b *PromptBuilder) WithContexts(contexts []string) *PromptBuilder
- func (b *PromptBuilder) WithExample(input, output string) *PromptBuilder
- func (b *PromptBuilder) WithOutputFormat(format string) *PromptBuilder
- func (b *PromptBuilder) WithSystemPrompt(prompt string) *PromptBuilder
- func (b *PromptBuilder) WithTask(task string) *PromptBuilder
- type ResponseParser
- func (p *ResponseParser) ExtractAllCodeBlocks() map[string]string
- func (p *ResponseParser) ExtractCodeBlock(language string) (string, error)
- func (p *ResponseParser) ExtractJSON() (string, error)
- func (p *ResponseParser) ExtractKeyValue(key string) (string, error)
- func (p *ResponseParser) ExtractList() []string
- func (p *ResponseParser) ExtractSection(title string) (string, error)
- func (p *ResponseParser) GetPlainText() string
- func (p *ResponseParser) IsEmpty() bool
- func (p *ResponseParser) Length() int
- func (p *ResponseParser) ParseToMap() (map[string]interface{}, error)
- func (p *ResponseParser) ParseToStruct(v interface{}) error
- func (p *ResponseParser) RemoveMarkdown() string
Constants ¶
This section is empty.
Variables ¶
var CommonPrompts = struct { // RootCauseAnalysis 根因分析模板 RootCauseAnalysis string // ProblemSummary 问题摘要模板 ProblemSummary string // RecommendationGeneration 建议生成模板 RecommendationGeneration string }{ RootCauseAnalysis: `You are an expert system administrator analyzing system failures. ## Context {{context}} ## Task Analyze the above information and identify the root cause of the failure. ## Output Format Provide your analysis in the following JSON format: { "root_cause": "Brief description of the root cause", "confidence": 0.0-1.0, "reasoning": "Detailed explanation of your analysis", "contributing_factors": ["factor1", "factor2", ...] }`, ProblemSummary: `Summarize the following problem in a clear and concise manner. ## Problem Details {{problem}} ## Requirements 1. Keep it under 100 words 2. Focus on the key issues 3. Use technical but accessible language`, RecommendationGeneration: `Based on the following root cause analysis, generate actionable recommendations. ## Root Cause {{root_cause}} ## Context {{context}} ## Requirements 1. Provide at least 3 recommendations 2. Prioritize by impact and feasibility 3. Include specific steps where possible`, }
CommonPrompts 提供常用的 Prompt 模板
Functions ¶
func CloseQuietly ¶ added in v0.7.0
CloseQuietly 静默关闭资源,忽略错误
适用于测试代码或确定不需要处理关闭错误的场景
用法:
defer utils.CloseQuietly(file)
func CloseWithError ¶ added in v0.7.0
CloseWithError 关闭资源并将错误合并到指定的错误指针
适用于需要将关闭错误与函数返回错误合并的场景 如果原错误为 nil,则设置为关闭错误 如果原错误非 nil,则使用 errors.Join 合并两个错误
用法:
func processFile(path string) (err error) {
file, err := os.Open(path)
if err != nil {
return err
}
defer utils.CloseWithError(file, &err)
// ... 处理文件
return nil
}
func CloseWithLog ¶ added in v0.7.0
CloseWithLog 关闭资源并在出错时调用日志函数
适用于生产代码中需要记录关闭错误但不影响主流程的场景
用法:
defer utils.CloseWithLog(file, func(err error) {
log.Error("failed to close file", "error", err)
})
Types ¶
type CloserFunc ¶ added in v0.7.0
type CloserFunc func() error
CloserFunc 将函数转换为 io.Closer 接口
适用于需要在 defer 中调用非标准关闭函数的场景
用法:
cleanup := utils.CloserFunc(func() error {
return someResource.Shutdown()
})
defer utils.CloseQuietly(cleanup)
type MultiCloser ¶ added in v0.7.0
type MultiCloser struct {
// contains filtered or unexported fields
}
MultiCloser 组合多个 Closer,按顺序关闭
适用于需要同时关闭多个资源的场景 所有错误会被收集并合并返回
用法:
mc := utils.NewMultiCloser(file1, file2, conn) defer utils.CloseQuietly(mc)
func NewMultiCloser ¶ added in v0.7.0
func NewMultiCloser(closers ...io.Closer) *MultiCloser
NewMultiCloser 创建 MultiCloser
func (*MultiCloser) Add ¶ added in v0.7.0
func (mc *MultiCloser) Add(closer io.Closer)
Add 添加 Closer
func (*MultiCloser) Close ¶ added in v0.7.0
func (mc *MultiCloser) Close() error
Close 关闭所有资源,合并所有错误
type PromptBuilder ¶
type PromptBuilder struct {
// contains filtered or unexported fields
}
PromptBuilder 提供 Prompt 构建工具
func (*PromptBuilder) BuildWithTemplate ¶
func (b *PromptBuilder) BuildWithTemplate(template string, vars map[string]string) string
BuildWithTemplate 使用模板构建 Prompt
func (*PromptBuilder) WithConstraint ¶
func (b *PromptBuilder) WithConstraint(constraint string) *PromptBuilder
WithConstraint 添加约束条件
func (*PromptBuilder) WithConstraints ¶
func (b *PromptBuilder) WithConstraints(constraints []string) *PromptBuilder
WithConstraints 添加多个约束条件
func (*PromptBuilder) WithContext ¶
func (b *PromptBuilder) WithContext(ctx string) *PromptBuilder
WithContext 添加上下文信息
func (*PromptBuilder) WithContexts ¶
func (b *PromptBuilder) WithContexts(contexts []string) *PromptBuilder
WithContexts 添加多个上下文信息
func (*PromptBuilder) WithExample ¶
func (b *PromptBuilder) WithExample(input, output string) *PromptBuilder
WithExample 添加示例
func (*PromptBuilder) WithOutputFormat ¶
func (b *PromptBuilder) WithOutputFormat(format string) *PromptBuilder
WithOutputFormat 设置输出格式要求
func (*PromptBuilder) WithSystemPrompt ¶
func (b *PromptBuilder) WithSystemPrompt(prompt string) *PromptBuilder
WithSystemPrompt 设置系统提示
func (*PromptBuilder) WithTask ¶
func (b *PromptBuilder) WithTask(task string) *PromptBuilder
WithTask 设置任务描述
type ResponseParser ¶
type ResponseParser struct {
// contains filtered or unexported fields
}
ResponseParser 提供响应解析工具
func NewResponseParser ¶
func NewResponseParser(content string) *ResponseParser
NewResponseParser 创建响应解析器
func (*ResponseParser) ExtractAllCodeBlocks ¶
func (p *ResponseParser) ExtractAllCodeBlocks() map[string]string
ExtractAllCodeBlocks 提取所有代码块
func (*ResponseParser) ExtractCodeBlock ¶
func (p *ResponseParser) ExtractCodeBlock(language string) (string, error)
ExtractCodeBlock 提取指定语言的代码块
func (*ResponseParser) ExtractJSON ¶
func (p *ResponseParser) ExtractJSON() (string, error)
ExtractJSON 提取 JSON 内容
func (*ResponseParser) ExtractKeyValue ¶
func (p *ResponseParser) ExtractKeyValue(key string) (string, error)
ExtractKeyValue 提取键值对
func (*ResponseParser) ExtractList ¶
func (p *ResponseParser) ExtractList() []string
ExtractList 提取列表项
func (*ResponseParser) ExtractSection ¶
func (p *ResponseParser) ExtractSection(title string) (string, error)
ExtractSection 提取指定章节
func (*ResponseParser) GetPlainText ¶
func (p *ResponseParser) GetPlainText() string
GetPlainText 获取纯文本内容
func (*ResponseParser) ParseToMap ¶
func (p *ResponseParser) ParseToMap() (map[string]interface{}, error)
ParseToMap 解析为 map
func (*ResponseParser) ParseToStruct ¶
func (p *ResponseParser) ParseToStruct(v interface{}) error
ParseToStruct 解析为结构体
func (*ResponseParser) RemoveMarkdown ¶
func (p *ResponseParser) RemoveMarkdown() string
RemoveMarkdown 移除 Markdown 格式 使用预编译的正则表达式提升性能,避免每次调用都重新编译