| 1 | // Copyright 2025 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package celscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/ |
| 16 | |
| 17 | import ( |
| 18 | _ "embedscip-go gomod github.com/golang/go/src go1.23.0 embed/" |
| 19 | "sortscip-go gomod github.com/golang/go/src go1.23.0 sort/" |
| 20 | "stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/" |
| 21 | "text/templatescip-go gomod github.com/golang/go/src go1.23.0 `text/template`/" |
| 22 | |
| 23 | "github.com/google/cel-go/commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/" |
| 24 | "github.com/google/cel-go/common/operatorsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/operators`/" |
| 25 | "github.com/google/cel-go/common/overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/" |
| 26 | ) |
| 27 | |
| 28 | //go:embed templates/authoring.tmpl |
| 29 | var authoringPromptscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/authoringPrompt. string |
| 30 | |
| 31 | // AuthoringPrompt creates a prompt template from a CEL environment for the purpose of AI-assisted authoring. |
| 32 | func AuthoringPromptscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/AuthoringPrompt().(env *Envscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Env#) (*Promptscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#, error) { |
| 33 | funcMap := templatescip-go gomod github.com/golang/go/src go1.23.0 `text/template`/.FuncMapscip-go gomod github.com/golang/go/src go1.23.0 `text/template`/FuncMap#{ |
| 34 | "split": func(str string) []string { return stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.23.0 strings/Split().(str, "\n") }, |
| 35 | } |
| 36 | tmpl := templatescip-go gomod github.com/golang/go/src go1.23.0 `text/template`/.Newscip-go gomod github.com/golang/go/src go1.23.0 `text/template`/New().("cel").Funcsscip-go gomod github.com/golang/go/src go1.23.0 `text/template`/Template#Funcs().(funcMap) |
| 37 | tmpl, err := tmpl.Parsescip-go gomod github.com/golang/go/src go1.23.0 `text/template`/Template#Parse().(authoringPromptscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/authoringPrompt.) |
| 38 | if err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | return &Promptscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#{ |
| 42 | Personascip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#Persona.: defaultPersonascip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/defaultPersona., |
| 43 | FormatRulesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#FormatRules.: defaultFormatRulesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/defaultFormatRules., |
| 44 | GeneralUsagescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#GeneralUsage.: defaultGeneralUsagescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/defaultGeneralUsage., |
| 45 | tmplscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#tmpl.: tmpl, |
| 46 | envscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#env.: env, |
| 47 | }, nil |
| 48 | } |
| 49 | |
| 50 | // Prompt represents the core components of an LLM prompt based on a CEL environment. |
| 51 | // |
| 52 | // All fields of the prompt may be overwritten / modified with support for rendering the |
| 53 | // prompt to a human-readable string. |
| 54 | type Promptscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt# struct { |
| 55 | // Persona indicates something about the kind of user making the request |
| 56 | Personascip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#Persona. string |
| 57 | |
| 58 | // FormatRules indicate how the LLM should generate its output |
| 59 | FormatRulesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#FormatRules. string |
| 60 | |
| 61 | // GeneralUsage specifies additional context on how CEL should be used. |
| 62 | GeneralUsagescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#GeneralUsage. string |
| 63 | |
| 64 | // tmpl is the text template base-configuration for rendering text. |
| 65 | tmplscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#tmpl. *templatescip-go gomod github.com/golang/go/src go1.23.0 `text/template`/.Templatescip-go gomod github.com/golang/go/src go1.23.0 `text/template`/Template# |
| 66 | |
| 67 | // env reference used to collect variables, functions, and macros available to the prompt. |
| 68 | envscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#env. *Envscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Env# |
| 69 | } |
| 70 | |
| 71 | type promptInstscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/promptInst# struct { |
| 72 | *Promptscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/promptInst#Prompt. |
| 73 | |
| 74 | Variablesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/promptInst#Variables. []*commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc# |
| 75 | Macrosscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/promptInst#Macros. []*commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc# |
| 76 | Functionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/promptInst#Functions. []*commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc# |
| 77 | UserPromptscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/promptInst#UserPrompt. string |
| 78 | } |
| 79 | |
| 80 | // Render renders the user prompt with the associated context from the prompt template |
| 81 | // for use with LLM generators. |
| 82 | func (p *Promptscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#) Renderscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#Render().(userPrompt string) string { |
| 83 | var buffer stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/.Builderscip-go gomod github.com/golang/go/src go1.23.0 strings/Builder# |
| 84 | vars := make([]*commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#, len(p.envscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#env..Variablesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Env#Variables().())) |
| 85 | for i, v := range p.envscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#env..Variablesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Env#Variables().() { |
| 86 | vars[i] = v.Documentationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/decls`/VariableDecl#Documentation().() |
| 87 | } |
| 88 | sortscip-go gomod github.com/golang/go/src go1.23.0 sort/.SliceStablescip-go gomod github.com/golang/go/src go1.23.0 sort/SliceStable().(vars, func(i, j int) bool { |
| 89 | return vars[i].Namescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Name. < vars[j].Namescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Name. |
| 90 | }) |
| 91 | macs := make([]*commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#, len(p.envscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#env..Macrosscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Env#Macros().())) |
| 92 | for i, m := range p.envscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#env..Macrosscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Env#Macros().() { |
| 93 | macs[i] = m.(commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.Documentorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Documentor#).Documentationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Documentor#Documentation.() |
| 94 | } |
| 95 | funcs := make([]*commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#, 0, len(p.envscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#env..Functionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Env#Functions().())) |
| 96 | for _, f := range p.envscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#env..Functionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Env#Functions().() { |
| 97 | if _, hidden := hiddenFunctionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/hiddenFunctions.[f.Namescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/decls`/FunctionDecl#Name().()]; hidden { |
| 98 | continue |
| 99 | } |
| 100 | funcs = append(funcs, f.Documentationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/decls`/FunctionDecl#Documentation().()) |
| 101 | } |
| 102 | sortscip-go gomod github.com/golang/go/src go1.23.0 sort/.SliceStablescip-go gomod github.com/golang/go/src go1.23.0 sort/SliceStable().(funcs, func(i, j int) bool { |
| 103 | return funcs[i].Namescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Name. < funcs[j].Namescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Name. |
| 104 | }) |
| 105 | inst := &promptInstscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/promptInst#{ |
| 106 | Promptscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/promptInst#Prompt.: p, |
| 107 | Variablesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/promptInst#Variables.: vars, |
| 108 | Macrosscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/promptInst#Macros.: macs, |
| 109 | Functionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/promptInst#Functions.: funcs, |
| 110 | UserPromptscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/promptInst#UserPrompt.: userPrompt} |
| 111 | p.tmplscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/Prompt#tmpl..Executescip-go gomod github.com/golang/go/src go1.23.0 `text/template`/Template#Execute().(&buffer, inst) |
| 112 | return buffer.Stringscip-go gomod github.com/golang/go/src go1.23.0 strings/Builder#String().() |
| 113 | } |
| 114 | |
| 115 | const ( |
| 116 | defaultPersonascip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/defaultPersona. = `You are a software engineer with expertise in networking and application security |
| 117 | authoring boolean Common Expression Language (CEL) expressions to ensure firewall, |
| 118 | networking, authentication, and data access is only permitted when all conditions |
| 119 | are satisfied.` |
| 120 | |
| 121 | defaultFormatRulesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/defaultFormatRules. = `Output your response as a CEL expression. |
| 122 | |
| 123 | Write the expression with the comment on the first line and the expression on the |
| 124 | subsequent lines. Format the expression using 80-character line limits commonly |
| 125 | found in C++ or Java code.` |
| 126 | |
| 127 | defaultGeneralUsagescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/defaultGeneralUsage. = `CEL supports Protocol Buffer and JSON types, as well as simple types and aggregate types. |
| 128 | |
| 129 | Simple types include bool, bytes, double, int, string, and uint: |
| 130 | |
| 131 | * double literals must always include a decimal point: 1.0, 3.5, -2.2 |
| 132 | * uint literals must be positive values suffixed with a 'u': 42u |
| 133 | * byte literals are strings prefixed with a 'b': b'1235' |
| 134 | * string literals can use either single quotes or double quotes: 'hello', "world" |
| 135 | * string literals can also be treated as raw strings that do not require any |
| 136 | escaping within the string by using the 'R' prefix: R"""quote: "hi" """ |
| 137 | |
| 138 | Aggregate types include list and map: |
| 139 | |
| 140 | * list literals consist of zero or more values between brackets: "['a', 'b', 'c']" |
| 141 | * map literal consist of colon-separated key-value pairs within braces: "{'key1': 1, 'key2': 2}" |
| 142 | * Only int, uint, string, and bool types are valid map keys. |
| 143 | * Maps containing HTTP headers must always use lower-cased string keys. |
| 144 | |
| 145 | Comments start with two-forward slashes followed by text and a newline.` |
| 146 | ) |
| 147 | |
| 148 | var ( |
| 149 | hiddenFunctionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/cel`/hiddenFunctions. = map[string]bool{ |
| 150 | overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.DeprecatedInscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/DeprecatedIn.: true, |
| 151 | operatorsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/operators`/.OldInscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/operators`/OldIn.: true, |
| 152 | operatorsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/operators`/.OldNotStrictlyFalsescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/operators`/OldNotStrictlyFalse.: true, |
| 153 | operatorsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/operators`/.NotStrictlyFalsescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/operators`/NotStrictlyFalse.: true, |
| 154 | } |
| 155 | ) |