utils

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func CloseQuietly(closer io.Closer)

CloseQuietly 静默关闭资源,忽略错误

适用于测试代码或确定不需要处理关闭错误的场景

用法:

defer utils.CloseQuietly(file)

func CloseWithError added in v0.7.0

func CloseWithError(closer io.Closer, errp *error)

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

func CloseWithLog(closer io.Closer, logFn func(error))

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)

func (CloserFunc) Close added in v0.7.0

func (f CloserFunc) Close() error

Close 实现 io.Closer 接口

type Example

type Example struct {
	Input  string
	Output string
}

Example 示例定义

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 NewPromptBuilder

func NewPromptBuilder() *PromptBuilder

NewPromptBuilder 创建 Prompt 构建器

func (*PromptBuilder) Build

func (b *PromptBuilder) Build() string

Build 构建最终的 Prompt

func (*PromptBuilder) BuildWithTemplate

func (b *PromptBuilder) BuildWithTemplate(template string, vars map[string]string) string

BuildWithTemplate 使用模板构建 Prompt

func (*PromptBuilder) Reset

func (b *PromptBuilder) Reset() *PromptBuilder

Reset 重置构建器

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) IsEmpty

func (p *ResponseParser) IsEmpty() bool

IsEmpty 检查内容是否为空

func (*ResponseParser) Length

func (p *ResponseParser) Length() int

Length 返回内容长度

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 格式 使用预编译的正则表达式提升性能,避免每次调用都重新编译

Directories

Path Synopsis
Package httpclient 提供统一的 HTTP 客户端管理
Package httpclient 提供统一的 HTTP 客户端管理

Jump to

Keyboard shortcuts

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