-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathllmvalidate.go
361 lines (291 loc) · 7.26 KB
/
llmvalidate.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package llmvalidate
import (
"bufio"
"context"
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"unicode/utf8"
"github.com/danwakefield/fnmatch"
"github.com/google/generative-ai-go/genai"
"github.com/grafana/plugin-validator/pkg/logme"
"google.golang.org/api/option"
)
// these are not regular expressions
// these are unix filename patterns
var ignoreList = []string{
// hidden files
"**/.**",
".**",
//dependencies
"node_modules/**",
"*.lock",
// dist files
"dist/**",
//external files
"**/external/**",
"**/*.min.js",
// tests
"**/tests/**",
"**/test/**",
"**/test-**",
"**/__mocks__/**",
"**/*.test.*",
"**/*.spec.*",
"**/*_test.go",
"tests/**",
"mocks/**",
"server-*",
"cypress/**",
// config
"jest.config.*",
"babel.config.*",
"jest-setup.*",
"playwright.config.*",
"vite.config.*",
"**/tsconfig.*",
"Gruntfile.*",
"webpack.config.*",
"rollup.config.*",
}
var allowExtensions = map[string]struct{}{
".js": {},
".jsx": {},
".ts": {},
".tsx": {},
".cjs": {},
".mjs": {},
".go": {},
}
type LLMAnswer struct {
Question string `json:"question"`
Answer string `json:"answer"`
Files []string `json:"files"`
ShortAnswer string `json:"short_answer"`
CodeSnippet string `json:"code_snippet"`
}
type LLMValidateClient struct {
genaiClient *genai.Client
apiKey string
modelName string
ctx context.Context
}
func New(ctx context.Context, apiKey string, modelName string) (*LLMValidateClient, error) {
if apiKey == "" {
return nil, fmt.Errorf("API key is required")
}
if modelName == "" {
modelName = "gemini-1.5-flash-latest"
}
genaiClient, err := genai.NewClient(ctx, option.WithAPIKey(apiKey))
if err != nil {
return nil, err
}
return &LLMValidateClient{
genaiClient: genaiClient,
modelName: modelName,
apiKey: apiKey,
ctx: ctx,
}, nil
}
func (c *LLMValidateClient) AskLLMAboutCode(
codePath string,
questions []string,
subPathsOnly []string,
) ([]LLMAnswer, error) {
if len(questions) == 0 {
return nil, fmt.Errorf("No questions provided")
}
// check that codepath exists and it is a directory
stat, err := os.Stat(codePath)
if err != nil {
return nil, err
}
if !stat.IsDir() {
return nil, fmt.Errorf("%s is not a directory", codePath)
}
absCodePath, err := filepath.Abs(codePath)
if err != nil {
return nil, err
}
codePrompt, err := getPromptContentForCode(absCodePath, subPathsOnly)
if err != nil {
return nil, fmt.Errorf("Error walking files inside %s: %v", codePath, err)
}
model := c.genaiClient.GenerativeModel(c.modelName)
// ensure it outputs json
model.GenerationConfig.ResponseMIMEType = "application/json"
model.SystemInstruction = &genai.Content{
Parts: []genai.Part{
genai.Text(
`You are source code reviewer. You are provided with a source code repository information and files. You will answer questions only based on the context of the files provided
The output should be a valid plain JSON array. Each element with an answer containing fields:
* question: The original question
* answer: The answer
* files: An array of related files if applicable.
* short_answer: Yes/No/NA
* code_snippet: The code snippet relevant to the question. Empty if not applicable
`,
),
},
}
formattedQuestions := ""
for _, question := range questions {
formattedQuestions += fmt.Sprintf("- %s\n", question)
}
mainPrompt := fmt.Sprintf(`
The files in the repository are:
### START OF FILES ###
%s
### END OF FILES ###
Answer the following questions in the context of the code above. be brief in your answers.
%s
`, strings.Join(codePrompt, "\n"), formattedQuestions)
modelResponse, err := model.GenerateContent(c.ctx, genai.Text(mainPrompt))
if err != nil {
return nil, err
}
content := getTextContentFromModelContentResponse(modelResponse)
//unmarshall content into []LLMAnswer
var answers []LLMAnswer
err = json.Unmarshal([]byte(content), &answers)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal content: %v", err)
}
logme.Debugln("Got response from LLM with char length", len(answers))
return answers, nil
}
func getTextContentFromModelContentResponse(modelResponse *genai.GenerateContentResponse) string {
if len(modelResponse.Candidates) == 0 {
return ""
}
content := modelResponse.Candidates[0].Content
finalContent := ""
for _, part := range content.Parts {
finalContent += fmt.Sprint(part)
}
return finalContent
}
func getPromptContentForCode(codePath string, subPathsOnly []string) ([]string, error) {
var prompts []string
if len(subPathsOnly) == 0 {
subPathsOnly = []string{"."}
}
for _, path := range subPathsOnly {
subCodePath := filepath.Join(codePath, path)
// skip if it doesn't exist
_, err := os.Stat(subCodePath)
if err != nil {
continue
}
subPrompts, err := walkAndGetPrompts(subCodePath)
if err != nil {
return nil, err
}
prompts = append(prompts, subPrompts...)
}
return prompts, nil
}
func walkAndGetPrompts(codePath string) ([]string, error) {
var prompts []string
err := filepath.Walk(codePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
relFilePath, err := filepath.Rel(codePath, path)
if err != nil {
return err
}
extension := filepath.Ext(relFilePath)
if !isAllowedExtension(extension) {
return nil
}
if isIgnoredFile(relFilePath) {
return nil
}
prompt := getPromptContentForFile(codePath, relFilePath)
if prompt != "" {
prompts = append(prompts, prompt)
}
return nil
})
if err != nil {
return nil, err
}
return prompts, nil
}
func isAllowedExtension(extension string) bool {
_, ok := allowExtensions[extension]
return ok
}
func isIgnoredFile(file string) bool {
flags := fnmatch.FNM_PERIOD | fnmatch.FNM_NOESCAPE
for _, pattern := range ignoreList {
if fnmatch.Match(pattern, file, flags) {
return true
}
}
return false
}
func getPromptContentForFile(codePath, relFile string) string {
content, err := readFileContent(path.Join(codePath, relFile))
if err != nil {
logme.DebugFln("Error reading file %s: %v", relFile, err)
// we are ignoring this error because this might be a non-text file
return ""
}
if !utf8.ValidString(content) {
return ""
}
if isMinifiedJsFile(content) {
return ""
}
logme.DebugFln("llmvalidate: Including file %s", path.Join(codePath, relFile))
if len(content) == 0 {
return ""
}
promptContent := fmt.Sprintf(`
----##----
Source filename: %s
Source Content:
%s
----##----
`, relFile, content)
return promptContent
}
func isMinifiedJsFile(jsCode string) bool {
lines := strings.Split(jsCode, "\n")
totalLength := 0
for _, line := range lines {
totalLength += len(line)
}
averageLineLength := float64(totalLength) / float64(len(lines))
return averageLineLength > 100
}
func readFileContent(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()
scanner := bufio.NewScanner(file)
var content strings.Builder
for scanner.Scan() {
text := scanner.Text()
if !utf8.ValidString(text) {
return "", fmt.Errorf("invalid UTF-8 in file %s", filePath)
}
content.WriteString(text)
content.WriteRune('\n')
}
if err := scanner.Err(); err != nil {
return "", err
}
return content.String(), nil
}