|
| 1 | +# Prompt Management with TextPrompts.jl |
| 2 | + |
| 3 | +[TextPrompts.jl](https://github.com/svilupp/textprompts/tree/main/packages/TextPrompts.jl) allows you to manage prompts as text files with optional TOML metadata. This enables version-controlled, collaborative prompt engineering that separates prompt content from code. |
| 4 | + |
| 5 | +## Why TextPrompts.jl? |
| 6 | + |
| 7 | +| Benefit | Description | |
| 8 | +|---------|-------------| |
| 9 | +| **Version Control** | Track prompt changes in git with full history and diffs | |
| 10 | +| **Collaboration** | Team members can edit prompts without touching code | |
| 11 | +| **Validation** | Catch placeholder typos before LLM calls execute | |
| 12 | +| **Metadata** | Track version, author, description for each prompt | |
| 13 | +| **Separation of Concerns** | Keep prompt engineering separate from application logic | |
| 14 | +| **Cross-Language** | Same prompt files work in Python, TypeScript, and Julia | |
| 15 | + |
| 16 | +## Cross-Language Ecosystem |
| 17 | + |
| 18 | +TextPrompts is available across multiple languages, enabling teams to share prompts across different codebases: |
| 19 | + |
| 20 | +| Language | Package | Installation | |
| 21 | +|----------|---------|--------------| |
| 22 | +| **Julia** | [TextPrompts.jl](https://github.com/svilupp/textprompts/tree/main/packages/TextPrompts.jl) | `Pkg.add("TextPrompts")` | |
| 23 | +| **Python** | [textprompts](https://github.com/svilupp/textprompts/tree/main/packages/textprompts) | `pip install textprompts` | |
| 24 | +| **TypeScript** | [@anthropic/textprompts](https://github.com/svilupp/textprompts/tree/main/packages/textprompts-ts) | `npm install @anthropic/textprompts` | |
| 25 | + |
| 26 | +The same prompt files with TOML frontmatter work identically across all three languages! |
| 27 | + |
| 28 | +## Installation |
| 29 | + |
| 30 | +TextPrompts.jl is a separate package. Install it to enable prompt file management: |
| 31 | + |
| 32 | +```julia |
| 33 | +using Pkg |
| 34 | +Pkg.add("TextPrompts") |
| 35 | +``` |
| 36 | + |
| 37 | +## Quick Start |
| 38 | + |
| 39 | +```julia |
| 40 | +using TextPrompts |
| 41 | +using PromptingTools |
| 42 | + |
| 43 | +# Load a prompt template from file |
| 44 | +prompt = load_prompt("prompts/system.txt") |
| 45 | + |
| 46 | +# Format with placeholders and convert to PromptingTools message |
| 47 | +system_msg = prompt(; role = "Julia expert", task = "explain macros") |> SystemMessage |
| 48 | + |
| 49 | +# Use in aigenerate |
| 50 | +response = aigenerate([system_msg, UserMessage("How do macros work?")]; model = "gpt4om") |
| 51 | +``` |
| 52 | + |
| 53 | +## Prompt File Format |
| 54 | + |
| 55 | +Prompts can include optional TOML frontmatter for metadata: |
| 56 | + |
| 57 | +``` |
| 58 | +--- |
| 59 | +title = "Expert System Prompt" |
| 60 | +version = "1.0" |
| 61 | +author = "Team Name" |
| 62 | +description = "A system prompt for expert assistance" |
| 63 | +--- |
| 64 | +You are a {role}. Be {style} and helpful. |
| 65 | +``` |
| 66 | + |
| 67 | +If no frontmatter is provided, the file is treated as plain text with the filename used as the title. |
| 68 | + |
| 69 | +### Metadata Modes |
| 70 | + |
| 71 | +TextPrompts supports three metadata handling modes: |
| 72 | + |
| 73 | +| Mode | Behavior | |
| 74 | +|------|----------| |
| 75 | +| `:allow` (default) | Parse metadata if present, otherwise use filename as title | |
| 76 | +| `:strict` | Require title, description, and version fields | |
| 77 | +| `:ignore` | Treat entire file as content; use filename as title | |
| 78 | + |
| 79 | +```julia |
| 80 | +# Strict mode - requires all metadata fields |
| 81 | +prompt = load_prompt("system.txt"; meta = :strict) |
| 82 | + |
| 83 | +# Ignore mode - treat as plain text |
| 84 | +prompt = load_prompt("system.txt"; meta = :ignore) |
| 85 | +``` |
| 86 | + |
| 87 | +## Core API |
| 88 | + |
| 89 | +### Loading Prompts |
| 90 | + |
| 91 | +```julia |
| 92 | +# Load a single prompt file |
| 93 | +prompt = load_prompt("prompts/system.txt") |
| 94 | + |
| 95 | +# Load all prompts from a directory |
| 96 | +all_prompts = load_prompts("prompts/"; recursive = true) |
| 97 | + |
| 98 | +# Create prompt from string (no file needed) |
| 99 | +inline_prompt = from_string(""" |
| 100 | +--- |
| 101 | +title = "Inline Example" |
| 102 | +--- |
| 103 | +Hello, {name}! |
| 104 | +""") |
| 105 | +``` |
| 106 | + |
| 107 | +### Accessing Prompt Data |
| 108 | + |
| 109 | +```julia |
| 110 | +prompt = load_prompt("greeting.txt") |
| 111 | + |
| 112 | +# Access raw content |
| 113 | +println(prompt.content) |
| 114 | + |
| 115 | +# Access metadata |
| 116 | +println(prompt.meta.title) |
| 117 | +println(prompt.meta.version) |
| 118 | +println(prompt.meta.description) |
| 119 | + |
| 120 | +# See available placeholders |
| 121 | +println(prompt.placeholders) # e.g., [:name, :day] |
| 122 | +``` |
| 123 | + |
| 124 | +### Formatting with Placeholders |
| 125 | + |
| 126 | +```julia |
| 127 | +prompt = load_prompt("greeting.txt") |
| 128 | + |
| 129 | +# Format by calling as a function |
| 130 | +formatted = prompt(; name = "World", day = "Monday") |
| 131 | + |
| 132 | +# Partial formatting (skip validation for missing placeholders) |
| 133 | +partial = prompt(; name = "World", skip_validation = true) |
| 134 | + |
| 135 | +# Alternative: use TextPrompts.format explicitly |
| 136 | +formatted2 = TextPrompts.format(prompt; name = "World", day = "Monday") |
| 137 | +``` |
| 138 | + |
| 139 | +### Integration with PromptingTools |
| 140 | + |
| 141 | +The pipe operator `|>` creates a seamless workflow: |
| 142 | + |
| 143 | +```julia |
| 144 | +using TextPrompts |
| 145 | +using PromptingTools |
| 146 | +using PromptingTools: SystemMessage, UserMessage |
| 147 | + |
| 148 | +# One-liner pattern for creating message arrays |
| 149 | +messages = [ |
| 150 | + load_prompt("system.txt")(; role = "Expert") |> SystemMessage, |
| 151 | + load_prompt("task.txt")(; task = "explain closures") |> UserMessage |
| 152 | +] |
| 153 | + |
| 154 | +response = aigenerate(messages; model = "gpt4om") |
| 155 | +``` |
| 156 | + |
| 157 | +### Dynamic Prompt Selection |
| 158 | + |
| 159 | +```julia |
| 160 | +# Load all templates and index by title |
| 161 | +templates = load_prompts("prompts/") |
| 162 | +by_title = Dict(p.meta.title => p for p in templates) |
| 163 | + |
| 164 | +# Select based on runtime conditions |
| 165 | +template_name = determine_template() # your logic |
| 166 | +prompt = by_title[template_name] |
| 167 | +msg = prompt(; task = "some task") |> UserMessage |
| 168 | +``` |
| 169 | + |
| 170 | +### Saving Prompts |
| 171 | + |
| 172 | +```julia |
| 173 | +# Save a prompt string to file |
| 174 | +save_prompt("output.txt", "Hello, {name}!") |
| 175 | + |
| 176 | +# Save a prompt object |
| 177 | +prompt = from_string("Hello, {name}!") |
| 178 | +save_prompt("output.txt", prompt) |
| 179 | +``` |
| 180 | + |
| 181 | +## Combining with Logfire.jl |
| 182 | + |
| 183 | +TextPrompts.jl works seamlessly with [Logfire.jl observability](observability_logfire.md): |
| 184 | + |
| 185 | +```julia |
| 186 | +using TextPrompts, PromptingTools, Logfire |
| 187 | + |
| 188 | +Logfire.configure(service_name = "my-app") |
| 189 | +Logfire.instrument_promptingtools!() |
| 190 | + |
| 191 | +# Prompts from files are automatically traced |
| 192 | +msg = load_prompt("task.txt")(; task = "analyze data") |> UserMessage |
| 193 | +response = aigenerate(msg; model = "gpt4om") |
| 194 | +# Traces include full conversation with formatted prompts |
| 195 | +``` |
| 196 | + |
| 197 | +## Recommended Workflow |
| 198 | + |
| 199 | +1. **Store prompts in a `prompts/` directory** in your project |
| 200 | +2. **Use TOML frontmatter** for metadata (version, description, author) |
| 201 | +3. **Version control with git** to track changes and collaborate |
| 202 | +4. **Load dynamically** based on use case or user input |
| 203 | +5. **Combine with Logfire.jl** for full observability of your LLM calls |
| 204 | + |
| 205 | +This workflow enables continuous prompt improvement: version prompts in git, trace calls in Logfire, and iterate based on real-world performance. |
| 206 | + |
| 207 | +## Example |
| 208 | + |
| 209 | +See the full example at [`examples/working_with_textprompts.jl`](https://github.com/svilupp/PromptingTools.jl/blob/main/examples/working_with_textprompts.jl). |
| 210 | + |
| 211 | +## Further Reading |
| 212 | + |
| 213 | +- [TextPrompts.jl GitHub](https://github.com/svilupp/textprompts/tree/main/packages/TextPrompts.jl) |
| 214 | +- [Discourse Announcement](https://discourse.julialang.org/t/announcing-logfire-jl-textprompts-jl-observability-and-prompt-management-for-julia-genai/134268) |
| 215 | +- [Logfire.jl Observability](observability_logfire.md) |
0 commit comments