forked from ingyamilmolinar/doctorgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnose.go
More file actions
110 lines (103 loc) · 3.26 KB
/
diagnose.go
File metadata and controls
110 lines (103 loc) · 3.26 KB
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
package main
import (
"context"
"fmt"
"github.com/cenkalti/backoff/v4"
openai "github.com/sashabaranov/go-openai"
"go.uber.org/zap"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
type handler func(log *zap.SugaredLogger, fileName, outputDir, apiKey, model string, entryToDiagnose logEntry, logContext []logEntry) error
func handleTrigger(log *zap.SugaredLogger, fileName, outputDir, apiKey, model string, entryToDiagnose logEntry, logContext []logEntry) error {
err := backoff.Retry(func() error {
// create file and write to it
errorLocation := fileName + ":" + strconv.Itoa(entryToDiagnose.LineNo)
filename := outputDir + "/" + safeString(errorLocation) + ".diagnosing"
f, err := os.Create(filename)
if err != nil {
return fmt.Errorf("error creating diagnosis file: %w", err)
}
log.Infof("Log Line: %s", errorLocation)
_, err = f.WriteString(fmt.Sprintf("LOG LINE:\n%s\n\n", errorLocation))
if err != nil {
return fmt.Errorf("error writing to diagnosis file: %w", err)
}
log.Infof("Prompt: %s", basePrompt)
_, err = f.WriteString(fmt.Sprintf("BASE PROMPT:\n%s\n\n", basePrompt))
if err != nil {
return fmt.Errorf("error writing to diagnosis file: %w", err)
}
context := stringify(logContext)
log.Infof("Context: %s", context)
_, err = f.WriteString(fmt.Sprintf("CONTEXT:\n%s\n\n", context))
if err != nil {
return fmt.Errorf("error writing to diagnosis file: %w", err)
}
suggestion, err := suggestion(model, apiKey, basePrompt, context)
if err != nil {
return fmt.Errorf("error diagnosing using the openai API: %w", err)
}
log.Infof("Diagnosis: %s", suggestion)
_, err = f.WriteString(fmt.Sprintf("DIAGNOSIS:\n%s\n", suggestion))
if err != nil {
return fmt.Errorf("error writing to diagnosis file: %w", err)
}
err = f.Close()
if err != nil {
return fmt.Errorf("error closing the diagnosis file: %w", err)
}
fullNameNoExt := strings.TrimRight(filename, ".diagnosing")
err = os.Rename(filename, fullNameNoExt+".diagnosed")
if err != nil {
return fmt.Errorf("error renaming the diagnosis file: %w", err)
}
return nil
}, backoff.WithMaxRetries(backoff.NewConstantBackOff(2*time.Second), 3))
if err != nil {
log.Errorf("Failed to diagnose after retries: %v", err)
}
return err
}
func suggestion(model, key, basePrompt, errorMsg string) (string, error) {
prompt := strings.Replace(basePrompt, errorPlaceholder, errorMsg, 1)
client := openai.NewClient(key)
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: model,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: prompt,
},
},
},
)
if err != nil {
return "", fmt.Errorf("error generating text from API: %v", err)
}
if len(resp.Choices) == 0 {
return "", fmt.Errorf("chatGPT returned no choices")
}
return resp.Choices[0].Message.Content, nil
}
// TODO: Make file separator configurable
func safeString(s string) string {
result := strings.ReplaceAll(s, " ", "-")
result = strings.ReplaceAll(result, "/", "::")
if len(s) > 200 {
result = s[0:200]
}
return filepath.Clean(result)
}
func stringify(entries []logEntry) string {
var result string
for _, entry := range entries {
result += entry.Text + "\n"
}
return result
}