Render renders the user prompt with the associated context from the prompt template for use with LLM generators. User-supplied input is passed as template data via the UserPrompt field, which Go's text/template renders as a literal string value. Template action delimiters such as {{.Persona}} in th
(userPrompt string)
| 138 | // because text/template only executes directives present in the template definition |
| 139 | // itself, not in data values interpolated at render time. |
| 140 | func (p *Prompt) Render(userPrompt string) string { |
| 141 | var buffer strings.Builder |
| 142 | vars := make([]*promptVariable, len(p.env.Variables())) |
| 143 | for i, v := range p.env.Variables() { |
| 144 | vars[i] = &promptVariable{Doc: v.Documentation()} |
| 145 | if p.fieldPaths && v.Type().Kind() == types.StructKind { |
| 146 | var fieldPaths []*common.Doc |
| 147 | |
| 148 | paths := fieldPathsForType(p.env.CELTypeProvider(), v.Name(), v.Type()) |
| 149 | if len(paths) < 2 { |
| 150 | paths = nil |
| 151 | } else { |
| 152 | // First path is the variable which is already documented. |
| 153 | paths = paths[1:] |
| 154 | } |
| 155 | for _, path := range paths { |
| 156 | fieldPaths = append(fieldPaths, path.Documentation()) |
| 157 | } |
| 158 | |
| 159 | sort.SliceStable(fieldPaths, func(i, j int) bool { |
| 160 | return fieldPaths[i].Name < fieldPaths[j].Name |
| 161 | }) |
| 162 | vars[i].FieldPaths = fieldPaths |
| 163 | } |
| 164 | } |
| 165 | sort.SliceStable(vars, func(i, j int) bool { |
| 166 | return vars[i].Name < vars[j].Name |
| 167 | }) |
| 168 | macs := make([]*common.Doc, len(p.env.Macros())) |
| 169 | for i, m := range p.env.Macros() { |
| 170 | macs[i] = m.(common.Documentor).Documentation() |
| 171 | } |
| 172 | funcs := make([]*common.Doc, 0, len(p.env.Functions())) |
| 173 | for _, f := range p.env.Functions() { |
| 174 | if _, hidden := hiddenFunctions[f.Name()]; hidden { |
| 175 | continue |
| 176 | } |
| 177 | funcs = append(funcs, f.Documentation()) |
| 178 | } |
| 179 | sort.SliceStable(funcs, func(i, j int) bool { |
| 180 | return funcs[i].Name < funcs[j].Name |
| 181 | }) |
| 182 | inst := &promptInst{ |
| 183 | Prompt: p, |
| 184 | Variables: vars, |
| 185 | Macros: macs, |
| 186 | Functions: funcs, |
| 187 | UserPrompt: userPrompt, |
| 188 | } |
| 189 | p.tmpl.Execute(&buffer, inst) |
| 190 | return buffer.String() |
| 191 | } |
| 192 | |
| 193 | const ( |
| 194 | defaultPersona = `You are a software engineer with expertise in networking and application security |