|
| 1 | +# Promptext Library Examples |
| 2 | + |
| 3 | +This directory contains example programs demonstrating how to use the promptext library in your Go applications. |
| 4 | + |
| 5 | +## Running the Examples |
| 6 | + |
| 7 | +Each example can be run independently: |
| 8 | + |
| 9 | +```bash |
| 10 | +# Basic usage examples |
| 11 | +cd basic |
| 12 | +go run main.go |
| 13 | + |
| 14 | +# Token budget and relevance filtering |
| 15 | +cd token-budget |
| 16 | +go run main.go |
| 17 | +``` |
| 18 | + |
| 19 | +## Examples |
| 20 | + |
| 21 | +### 1. Basic (`basic/`) |
| 22 | + |
| 23 | +Demonstrates fundamental library usage: |
| 24 | +- Simple extraction with defaults |
| 25 | +- Filtering by file extensions |
| 26 | +- Excluding patterns |
| 27 | +- Different output formats |
| 28 | +- Format conversion |
| 29 | +- Saving to files |
| 30 | +- Reusable extractors |
| 31 | +- Builder pattern |
| 32 | + |
| 33 | +**Run it:** |
| 34 | +```bash |
| 35 | +cd basic && go run main.go |
| 36 | +``` |
| 37 | + |
| 38 | +### 2. Token Budget (`token-budget/`) |
| 39 | + |
| 40 | +Shows how to work with token budgets and relevance filtering: |
| 41 | +- Setting token budgets for AI model limits |
| 42 | +- Relevance-based file filtering |
| 43 | +- Combining relevance and token budgets |
| 44 | +- Optimizing for different AI models |
| 45 | +- Understanding token efficiency |
| 46 | + |
| 47 | +**Run it:** |
| 48 | +```bash |
| 49 | +cd token-budget && go run main.go |
| 50 | +``` |
| 51 | + |
| 52 | +## Common Patterns |
| 53 | + |
| 54 | +### Simple Extraction |
| 55 | + |
| 56 | +```go |
| 57 | +result, err := promptext.Extract(".") |
| 58 | +if err != nil { |
| 59 | + log.Fatal(err) |
| 60 | +} |
| 61 | +fmt.Println(result.FormattedOutput) |
| 62 | +``` |
| 63 | + |
| 64 | +### With Options |
| 65 | + |
| 66 | +```go |
| 67 | +result, err := promptext.Extract(".", |
| 68 | + promptext.WithExtensions(".go", ".mod"), |
| 69 | + promptext.WithExcludes("vendor/", "*_test.go"), |
| 70 | + promptext.WithFormat(promptext.FormatPTX), |
| 71 | +) |
| 72 | +``` |
| 73 | + |
| 74 | +### Relevance Filtering |
| 75 | + |
| 76 | +```go |
| 77 | +result, err := promptext.Extract(".", |
| 78 | + promptext.WithRelevance("auth", "login"), |
| 79 | + promptext.WithTokenBudget(8000), |
| 80 | +) |
| 81 | +``` |
| 82 | + |
| 83 | +### Reusable Extractor |
| 84 | + |
| 85 | +```go |
| 86 | +extractor := promptext.NewExtractor( |
| 87 | + promptext.WithExtensions(".go"), |
| 88 | + promptext.WithTokenBudget(5000), |
| 89 | +) |
| 90 | + |
| 91 | +result1, _ := extractor.Extract("/project1") |
| 92 | +result2, _ := extractor.Extract("/project2") |
| 93 | +``` |
| 94 | + |
| 95 | +### Format Conversion |
| 96 | + |
| 97 | +```go |
| 98 | +result, _ := promptext.Extract(".", promptext.WithFormat(promptext.FormatPTX)) |
| 99 | + |
| 100 | +// Convert to different formats |
| 101 | +markdown, _ := result.As(promptext.FormatMarkdown) |
| 102 | +jsonl, _ := result.As(promptext.FormatJSONL) |
| 103 | +``` |
| 104 | + |
| 105 | +## Use Cases |
| 106 | + |
| 107 | +### 1. AI Context Generation |
| 108 | + |
| 109 | +Generate optimized code context for AI assistants: |
| 110 | + |
| 111 | +```go |
| 112 | +result, err := promptext.Extract(".", |
| 113 | + promptext.WithRelevance("authentication"), |
| 114 | + promptext.WithTokenBudget(8000), |
| 115 | + promptext.WithFormat(promptext.FormatPTX), |
| 116 | +) |
| 117 | +// Send result.FormattedOutput to AI |
| 118 | +``` |
| 119 | + |
| 120 | +### 2. Code Documentation |
| 121 | + |
| 122 | +Extract code for documentation purposes: |
| 123 | + |
| 124 | +```go |
| 125 | +result, err := promptext.Extract(".", |
| 126 | + promptext.WithExtensions(".go"), |
| 127 | + promptext.WithExcludes("*_test.go", "vendor/"), |
| 128 | + promptext.WithFormat(promptext.FormatMarkdown), |
| 129 | +) |
| 130 | +os.WriteFile("docs/codebase.md", []byte(result.FormattedOutput), 0644) |
| 131 | +``` |
| 132 | + |
| 133 | +### 3. CI/CD Integration |
| 134 | + |
| 135 | +Analyze code in CI pipelines: |
| 136 | + |
| 137 | +```go |
| 138 | +result, err := promptext.Extract(".", |
| 139 | + promptext.WithFormat(promptext.FormatJSONL), |
| 140 | +) |
| 141 | +// Parse result.FormattedOutput as JSONL for analysis |
| 142 | +``` |
| 143 | + |
| 144 | +### 4. Code Search and Analysis |
| 145 | + |
| 146 | +Find relevant code across large codebases: |
| 147 | + |
| 148 | +```go |
| 149 | +result, err := promptext.Extract(".", |
| 150 | + promptext.WithRelevance("database", "query", "migration"), |
| 151 | +) |
| 152 | + |
| 153 | +for _, file := range result.ProjectOutput.Files { |
| 154 | + fmt.Printf("%s: %d tokens\n", file.Path, file.Tokens) |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +## Available Options |
| 159 | + |
| 160 | +- `WithExtensions(extensions ...string)` - Filter by file extensions |
| 161 | +- `WithExcludes(patterns ...string)` - Exclude file patterns |
| 162 | +- `WithGitIgnore(enabled bool)` - Respect .gitignore (default: true) |
| 163 | +- `WithDefaultRules(enabled bool)` - Use built-in rules (default: true) |
| 164 | +- `WithRelevance(keywords ...string)` - Filter by keyword relevance |
| 165 | +- `WithTokenBudget(maxTokens int)` - Limit output tokens |
| 166 | +- `WithFormat(format Format)` - Set output format |
| 167 | +- `WithVerbose(enabled bool)` - Enable verbose logging |
| 168 | +- `WithDebug(enabled bool)` - Enable debug logging |
| 169 | + |
| 170 | +## Output Formats |
| 171 | + |
| 172 | +- `FormatPTX` - PTX v2.0 (recommended for AI, TOON-based) |
| 173 | +- `FormatTOON` - Alias for PTX (backward compatibility) |
| 174 | +- `FormatJSONL` - Machine-friendly JSONL |
| 175 | +- `FormatTOONStrict` - TOON v1.3 strict compliance |
| 176 | +- `FormatMarkdown` - Human-readable markdown |
| 177 | +- `FormatXML` - Machine-parseable XML |
| 178 | + |
| 179 | +## Error Handling |
| 180 | + |
| 181 | +```go |
| 182 | +result, err := promptext.Extract("/invalid/path") |
| 183 | +if err != nil { |
| 184 | + if errors.Is(err, promptext.ErrInvalidDirectory) { |
| 185 | + // Handle invalid directory |
| 186 | + } |
| 187 | + if errors.Is(err, promptext.ErrNoFilesMatched) { |
| 188 | + // Handle no matching files |
| 189 | + } |
| 190 | + // Handle other errors |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +## Further Reading |
| 195 | + |
| 196 | +- [Library Documentation](../pkg/promptext/doc.go) |
| 197 | +- [Main README](../README.md) |
| 198 | +- [API Reference](https://pkg.go.dev/github.com/1broseidon/promptext/pkg/promptext) |
0 commit comments