-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (65 loc) · 1.97 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
package main
import (
"context"
_ "embed"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/blwsh/llmt/lib/file"
"github.com/blwsh/llmt/lib/logger"
"github.com/blwsh/llmt/pkg/analyzer"
"github.com/blwsh/llmt/pkg/analyzer/item_analyzer/openai"
"github.com/blwsh/llmt/pkg/analyzer/project_analyzer/chat"
)
const (
EnvOpenAIToken = "OPENAI_TOKEN"
)
var l = logger.New(false)
//go:embed prompt.txt
var prompt string
var (
cwd, _ = os.Getwd()
here = filepath.Join(cwd, "examples/overview")
)
func main() {
ctx := context.Background()
var openAIToken string
if openAIToken = os.Getenv(EnvOpenAIToken); os.Getenv(EnvOpenAIToken) == "" {
l.Fatal(EnvOpenAIToken + " environment variable not set")
}
err := chat.New(chat.WithLogger(l)).
AnalyzeProject(ctx, cwd+"/examples/examplePhpProject", here+"/docs", []analyzer.FileAnalyzerConfig{
{
Prompt: prompt, // you may want to just use empty string if your model has a system prompt already
Analyzer: openai.New(openAIToken, "gpt-4o-mini"), // you can also use ollama: ollama.New("http://localhost:11434", "overview"),
Condition: myFancyConditionFunc,
ResultHandler: myDocsWriterFunc,
},
})
if err != nil {
l.Fatal(err)
}
}
func myFancyConditionFunc(filePath string) bool {
return strings.HasSuffix(filePath, ".php") &&
// exclude tests
!strings.Contains(filePath, "test") &&
// exclude composer files and directories
!strings.Contains(filePath, "vendor")
}
func myDocsWriterFunc(destFilepath string, result string) error {
if _, err := os.Stat(filepath.Dir(destFilepath)); os.IsNotExist(err) {
err = os.MkdirAll(filepath.Dir(destFilepath), os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
}
outputPath := filepath.Join(destFilepath + ".md")
err := file.WriteTo(outputPath, result)
if err != nil {
l.Error(err)
}
l.Info("analyzed file: ", filepath.Base(destFilepath), " -> ", outputPath)
return nil
}