-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
363 lines (322 loc) · 10.5 KB
/
main.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
362
363
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/joho/godotenv"
"github.com/tmc/langchaingo/llms"
store "github.com/JackBekket/reflexia/pkg"
packagerunner "github.com/JackBekket/reflexia/pkg/package_runner"
"github.com/JackBekket/reflexia/pkg/project"
"github.com/JackBekket/reflexia/pkg/summarize"
)
type Config struct {
GithubLink *string
GithubBranch *string
GithubUsername *string
GithubToken *string
WithConfigFile *string
ExactPackages *string
LightCheck bool
WithFileSummary bool
UseEmbeddings bool
OverwriteReadme bool
OverwriteCache bool
EmbeddingsAIURL *string
EmbeddingsAIAPIKey *string
EmbeddingsDBURL *string
EmbeddingsSimSearchTestPrompt *string
CachePath *string
}
func main() {
config, err := initConfig()
if err != nil {
log.Fatalf("initConfig() error: %v", err)
}
workdir, err := processWorkingDirectory(
*config.GithubLink, *config.GithubBranch, *config.GithubUsername, *config.GithubToken)
if err != nil {
log.Fatalf("processWorkingDirectory(...) error: %v", err)
}
projectConfigVariants, err := project.GetProjectConfig(
workdir, *config.WithConfigFile, config.LightCheck,
)
if err != nil {
log.Fatalf("project.GetProjectConfig(...) error: %v", err)
}
projectConfig, err := chooseProjectConfig(projectConfigVariants)
if err != nil {
log.Fatalf("chooseProjectConfig(...) error: %v", err)
}
summarizeService := &summarize.SummarizeService{
HelperURL: loadEnv("HELPER_URL"),
Model: loadEnv("MODEL"),
ApiToken: loadEnv("API_TOKEN"),
Network: "local",
LlmOptions: []llms.CallOption{
llms.WithStopWords(
projectConfig.StopWords,
),
llms.WithRepetitionPenalty(0.7),
},
// Tests only
IgnoreCache: false,
OverwriteCache: config.OverwriteCache,
CachePath: *config.CachePath,
}
var embeddingsService *store.EmbeddingsService
if config.UseEmbeddings {
projectName := filepath.Base(projectConfig.RootPath)
vectorStore, err := store.NewVectorStoreWithPreDelete(
*config.EmbeddingsAIURL,
*config.EmbeddingsAIAPIKey,
*config.EmbeddingsDBURL,
projectName,
)
if err != nil {
log.Fatalf("store.NewVectorStoreWithPreDelete(...) error: %v", err)
}
embeddingsService = &store.EmbeddingsService{
Store: vectorStore,
}
fmt.Printf("Initialized vector store with %s as project name\n", projectName)
}
pkgFiles, err := projectConfig.BuildPackageFiles()
if err != nil {
log.Fatalf("projectConfig.BuildPackageFiles() error: %v", err)
}
packageRunnerService := packagerunner.PackageRunnerService{
PkgFiles: pkgFiles,
ProjectConfig: projectConfig,
SummarizeService: summarizeService,
EmbeddingsService: embeddingsService,
ExactPackages: config.ExactPackages,
OverwriteReadme: config.OverwriteReadme,
WithFileSummary: config.WithFileSummary,
}
fallbackFileResponses,
emptyFileResponses,
fallbackPackageResponses,
emptyPackageResponses,
err := packageRunnerService.RunPackages()
if err != nil {
log.Fatalf("packageRunnerService.RunPackages() error: %v", err)
}
printEmptyWarning("[WARN] %d fallback attempts for files\n", fallbackFileResponses)
printEmptyWarning("[WARN] %d empty LLM responses for files\n", emptyFileResponses)
printEmptyWarning("[WARN] %d fallback attempts for packages\n", fallbackPackageResponses)
printEmptyWarning("[WARN] %d empty LLM responses for packages\n", emptyPackageResponses)
if embeddingsService != nil && config.EmbeddingsSimSearchTestPrompt != nil {
results, err := embeddingsService.Store.SimilaritySearch(context.Background(), *config.EmbeddingsSimSearchTestPrompt, 2)
if err != nil {
log.Fatalf("embeddingsService.Store.SimilaritySearch() error: %v", err)
}
if len(results) == 0 {
log.Fatalf("No similarity search results found for %s test prompt\n", *config.EmbeddingsSimSearchTestPrompt)
}
fmt.Printf("\n\nSimilarity search results for a test prompt \"%s\":\n", *config.EmbeddingsSimSearchTestPrompt)
for i, result := range results {
fmt.Printf("%d: score %f\n", i, result.Score)
for k, v := range result.Metadata {
fmt.Printf(" %s: %s\n", k, v)
}
fmt.Printf("\n%s\n\n", result.PageContent)
}
}
}
func printEmptyWarning(message string, responses []string) {
if len(responses) == 0 {
return
}
fmt.Printf(message, len(responses))
for _, response := range responses {
fmt.Printf(" - %s\n", response)
}
}
func loadEnv(key string) string {
value := os.Getenv(key)
if value == "" {
log.Fatalf("empty environment key %s", key)
}
return value
}
func chooseProjectConfig(projectConfigVariants map[string]*project.ProjectConfig) (*project.ProjectConfig, error) {
switch len(projectConfigVariants) {
case 0:
return nil, errors.New(
"failed to detect project language, available languages: go, python, typescript",
)
case 1:
for _, pc := range projectConfigVariants {
return pc, nil
}
default:
var filenames []string
for filename := range projectConfigVariants {
filenames = append(filenames, filename)
}
fmt.Println("Multiple project config matches found!")
for i, filename := range filenames {
fmt.Printf("%d. %v\n", i+1, filename)
}
fmt.Print("Enter the number or filename: ")
for {
var input string
if _, err := fmt.Scanln(&input); err != nil {
log.Fatalf("fmt.Scanln(...) error: %v", err)
}
if index, err := strconv.Atoi(input); err == nil && index > 0 && index <= len(filenames) {
return projectConfigVariants[filenames[index-1]], nil
} else {
for filename, config := range projectConfigVariants {
if filename == input || strings.TrimSuffix(filename, ".toml") == input {
return config, nil
}
}
}
}
}
panic("unreachable")
}
func processWorkingDirectory(githubLink, githubBranch, githubUsername, githubToken string) (string, error) {
workdir, err := os.Getwd()
if err != nil {
return "", err
}
if githubLink != "" {
u, err := url.ParseRequestURI(githubLink)
if err != nil {
return "", err
}
sPath := strings.Split(strings.TrimPrefix(u.Path, "/"), "/")
if len(sPath) != 2 {
return "", errors.New("github repository url does not have two path elements")
}
tempDirEl := []string{workdir, "temp"}
if githubBranch != "" {
tempDirEl = append(tempDirEl, "with_branch")
tempDirEl = append(tempDirEl, sPath...)
tempDirEl = append(tempDirEl, githubBranch)
} else {
tempDirEl = append(tempDirEl, "root_branch")
tempDirEl = append(tempDirEl, sPath...)
}
tempDir := filepath.Join(tempDirEl...)
workdir = tempDir
if _, err := os.Stat(workdir); err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(workdir, os.FileMode(0755)); err != nil {
return "", err
}
cloneOptions := git.CloneOptions{
URL: githubLink,
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
Depth: 1,
}
if githubBranch != "" {
cloneOptions.ReferenceName = plumbing.ReferenceName(githubBranch)
cloneOptions.SingleBranch = true
}
if githubUsername != "" && githubToken != "" {
cloneOptions.Auth = &http.BasicAuth{
Username: githubUsername,
Password: githubToken,
}
}
if _, err := git.PlainClone(workdir, false, &cloneOptions); err != nil {
if err := os.RemoveAll(workdir); err != nil {
return "", err
}
return "", err
}
} else {
return "", err
}
}
} else if len(flag.Args()) > 0 {
workdir = flag.Arg(0)
if _, err := os.Stat(workdir); err != nil {
return "", err
}
}
return workdir, nil
}
func initConfig() (*Config, error) {
if err := godotenv.Load(); err != nil {
log.Println(err)
}
config := Config{}
config.GithubLink = flag.String("g", "", "valid link for github repository")
config.GithubBranch = flag.String("b", "", "optional branch for github repository")
config.GithubUsername = flag.String("u", "", "github username for ssh auth")
githubToken := os.Getenv("GH_TOKEN")
config.GithubToken = &githubToken
flag.StringVar(config.GithubToken, "t", *config.GithubToken, "github token for ssh auth")
config.WithConfigFile = flag.String("l", "", "config filename in project_config to use")
cachePath := os.Getenv("CACHE_PATH")
config.CachePath = &cachePath
flag.StringVar(config.CachePath, "a", *config.CachePath, "cache folder path (defaults to .reflexia_cache)")
if *config.CachePath == "" {
cachePath = ".reflexia_cache"
config.CachePath = &cachePath
}
embAIURL := os.Getenv("EMBEDDINGS_AI_URL")
config.EmbeddingsAIURL = &embAIURL
flag.StringVar(config.EmbeddingsAIURL, "eu", *config.EmbeddingsAIURL, "Embeddings AI URL")
embAIAPIKey := os.Getenv("EMBEDDINGS_AI_KEY")
config.EmbeddingsAIAPIKey = &embAIAPIKey
flag.StringVar(config.EmbeddingsAIAPIKey, "ea", *config.EmbeddingsAIAPIKey, "Embeddings AI API Key")
embDBURL := os.Getenv("EMBEDDINGS_DB_URL")
config.EmbeddingsDBURL = &embDBURL
flag.StringVar(config.EmbeddingsDBURL, "ed", *config.EmbeddingsDBURL, "Embeddings pgxpool DB connect URL")
embSimSearchTestPrompt := os.Getenv("EMBEDDINGS_SIM_SEARCH_TEST_PROMPT")
config.EmbeddingsSimSearchTestPrompt = &embSimSearchTestPrompt
flag.StringVar(config.EmbeddingsSimSearchTestPrompt, "et", *config.EmbeddingsSimSearchTestPrompt, "Embeddings similarity search validation test prompt")
config.ExactPackages = flag.String("p", "", "exact package names, ',' delimited")
config.LightCheck = false
config.WithFileSummary = false
config.OverwriteReadme = false
config.OverwriteCache = false
config.UseEmbeddings = false
flag.BoolFunc("c",
"do not check project root folder specific files such as go.mod or package.json",
func(_ string) error {
config.LightCheck = true
return nil
})
flag.BoolFunc("f",
"Save individual file summary intermediate result to the FILES.md",
func(_ string) error {
config.WithFileSummary = true
return nil
})
flag.BoolFunc("r",
"overwrite README.md instead of README_GENERATED.md creation/overwrite",
func(_ string) error {
config.OverwriteReadme = true
return nil
})
flag.BoolFunc("d",
"Overwrite generated summary caches",
func(_ string) error {
config.OverwriteCache = true
return nil
})
flag.BoolFunc("e", "Use Embeddings",
func(_ string) error {
config.UseEmbeddings = true
return nil
})
flag.Parse()
return &config, nil
}