-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathai-monitoring-tmpl.go
255 lines (218 loc) · 7.22 KB
/
ai-monitoring-tmpl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
_ "embed"
"encoding/json"
"fmt"
"io"
"slices"
"strings"
"text/template"
)
//go:embed tmpl/ai-monitoring-support.md
var aiMonitoringTmplString string
// RenderAiCompatDoc renders the specified AI Monitoring support JSON
// descriptor file into Markdown text that is readable and understandable (🤞)
// by humans.
func RenderAiCompatDoc(descriptorJsonFile string, writer io.Writer) error {
reader, err := appFS.Open(descriptorJsonFile)
if err != nil {
return fmt.Errorf("could not read descriptor json file: %w", err)
}
parsedJson, err := aiCompatReadJson(reader)
if err != nil {
return fmt.Errorf("could not parse descriptor json file: %w", err)
}
tmpl, err := aiCompatLoadTemplate()
if err != nil {
return fmt.Errorf("could not load template: %w", err)
}
tmplData := aiCompatBuildTmplData(parsedJson)
err = tmpl.Execute(writer, tmplData)
if err != nil {
return fmt.Errorf("failed to render template: %w", err)
}
return nil
}
func aiCompatReadJson(file io.Reader) (AiCompatJson, error) {
var result AiCompatJson
data, err := io.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("failed to read json from file reader: %w", err)
}
err = json.Unmarshal(data, &result)
return result, err
}
// aiCompatBuildTmplData translates the parsed JSON data into a structure
// that our template is able to use.
func aiCompatBuildTmplData(input AiCompatJson) AiCompatTemplateData {
result := AiCompatTemplateData{
Abstractions: make([]AiCompatAbstraction, 0),
Gateways: make([]AiCompatGateway, 0),
Sdks: make([]AiCompatSdk, 0),
}
for _, envelope := range input {
switch strings.ToLower(envelope.Kind) {
case AiCompatKindGateway:
gateway := aiCompatParseGateway(envelope)
result.Gateways = append(result.Gateways, gateway)
case AiCompatKindAbstraction:
abstraction := aiCompatParseAbstraction(envelope)
result.Abstractions = append(result.Abstractions, abstraction)
case AiCompatKindSdk:
sdk := aiCompatParseSdk(envelope)
result.Sdks = append(result.Sdks, sdk)
}
}
return result
}
// aiCompatParseGateway translates an envelope of `kind = "gateway"` into
// a narrow gateway type.
func aiCompatParseGateway(input AiCompatEnvelope) AiCompatGateway {
return AiCompatGateway{
Title: input.Title,
Preamble: input.Preamble,
Footnote: input.Footnote,
Models: input.Models,
}
}
// aiCompatParseAbstraction translates an envelope of `kind = "abstraction"`
// into a narrow abstraction type.
func aiCompatParseAbstraction(input AiCompatEnvelope) AiCompatAbstraction {
return AiCompatAbstraction{
Title: input.Title,
FeaturesPreamble: input.FeaturesPreamble,
ProvidersPreamble: input.ProvidersPreamble,
Features: input.Features,
Providers: input.Providers,
}
}
// aiCompatParseSdk translates an envelope of `kind = "sdk"` into a narrow
// sdk type.
func aiCompatParseSdk(input AiCompatEnvelope) AiCompatSdk {
return AiCompatSdk{
Title: input.Title,
FeaturesPreamble: input.FeaturesPreamble,
Features: input.Features,
}
}
// aiCompatLoadTemplate loads the templated Markdown into a [text/template]
// instance that has all of our custom utility methods attached to it.
func aiCompatLoadTemplate() (*template.Template, error) {
tmpl := template.New("aiMonitoring")
tmpl.Funcs(template.FuncMap{
"featuresToTable": aiFeaturesToTable,
"gatewayModelsToTable": aiModelsToTable,
"providersToTable": aiProvidersToTable,
})
tmpl, err := tmpl.Parse(aiMonitoringTmplString)
if err != nil {
return nil, err
}
return tmpl, nil
}
// aiCompatBoolEmoji converts a boolean into emoji text representing the
// respective value.
func aiCompatBoolEmoji(input bool) string {
if input == true {
return "✅"
}
return "❌"
}
// aiModelsToTable renders a set of gateway objects into a Markdown table.
// It is added to the AI Monitoring template as a convenience function.
func aiModelsToTable(input []AiCompatModel) string {
result := strings.Builder{}
featureTitles := make([]string, 0)
for _, val := range input[0].Features {
featureTitles = append(featureTitles, val.Title)
}
slices.Sort(featureTitles)
featureTitles = append([]string{"Model"}, featureTitles...)
result.WriteString(titlesToTableHeader(featureTitles))
// Gateways have multiple models behind them. Each model usually has an
// overlapping feature set, but each model may have a feature other models
// in the list do not. Which means we have a mismatch in the number of
// columns. So we need to build out our rows in a manner such that we can
// add "gap" columns if a model does not have a feature found in some other
// model.
modelNames := make([]string, 0)
rows := make(map[string]string)
for _, title := range featureTitles {
if title == "Model" {
for _, model := range input {
modelNames = append(modelNames, model.Name)
rows[model.Name] = fmt.Sprintf("| %s |", model.Name)
}
continue
}
for _, model := range input {
name := model.Name
idx := slices.IndexFunc(model.Features, func(f AiCompatFeature) bool { return f.Title == title })
if idx == -1 {
rows[name] += " - |"
} else {
feature := model.Features[idx]
rows[name] = fmt.Sprintf("%s %s |", rows[name], aiCompatBoolEmoji(feature.Supported))
}
}
}
slices.Sort(modelNames)
for _, name := range modelNames {
result.WriteString(rows[name] + "\n")
}
return strings.TrimSpace(result.String())
}
// aiFeaturesToTable renders a set of feature objects into a Markdown table.
// It is added to the AI Monitoring template as a convenience function.
func aiFeaturesToTable(input []AiCompatFeature) string {
result := strings.Builder{}
titles := make([]string, 0)
for _, val := range input {
titles = append(titles, val.Title)
}
slices.Sort(titles)
result.WriteString(titlesToTableHeader(titles))
slices.SortFunc(input, sortFeaturesFn)
for _, feature := range input {
col := fmt.Sprintf("| %s ", aiCompatBoolEmoji(feature.Supported))
result.WriteString(col)
}
result.WriteString("|\n")
return strings.TrimSpace(result.String())
}
// aiProvidersToTable renders a set of provider objects into a Markdown table.
// It is added to the AI Monitoring template as a convenience function.
func aiProvidersToTable(input []AiCompatProvider) string {
result := strings.Builder{}
result.WriteString("| Provider | Supported | Transitively |\n")
result.WriteString("| --- | --- | --- |\n")
for _, provider := range input {
row := fmt.Sprintf(
"| %s | %s | %s |\n",
provider.Name,
aiCompatBoolEmoji(provider.Supported),
aiCompatBoolEmoji(provider.Transitively),
)
result.WriteString(row)
}
return strings.TrimSpace(result.String())
}
// titlesToTableHeader converts a set of strings into a Markdown table heading.
// That is, a row of names followed by a row of heading markers.
func titlesToTableHeader(input []string) string {
header := "|"
separator := "|"
for _, val := range input {
header = fmt.Sprintf("%s %s |", header, val)
separator = fmt.Sprintf("%s --- |", separator)
}
return header + "\n" + separator + "\n"
}
// sortFeaturesFn is provided to [slices.SortFunc] as the function that
// determines the ordering of features.
func sortFeaturesFn(a AiCompatFeature, b AiCompatFeature) int {
if a.Title < b.Title {
return -1
}
return 1
}