-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
161 lines (141 loc) · 3.85 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
)
func main() {
fmt.Println("Hello, World!")
// gehe durch alle Datein im Ordner 'dir' mit der Endung 'extension'
// Step 1: Receive the parameter values from the CLI.
args := os.Args[1:]
extension := args[0]
repoURL := args[1]
// Step 2: Print the parameter values to the console.
fmt.Println("Extension:", extension)
fmt.Println("Repo URL:", repoURL)
splitURL := strings.Split(repoURL, "/")
dir := strings.Replace(splitURL[4],".git","",-1)
fmt.Println("Directory: ", dir)
// wenn order nicht existiert
if _, err := os.Stat(dir); os.IsNotExist(err) {
gitClone(repoURL)
}
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
fmt.Println(path,info)
if err != nil {
return err
}
if filepath.Ext(path) == "."+extension {
fmt.Println(path)
// Lese den Inhalt der Datei und sende in an die Open.AI API um eine Dokumentation zum code zu erhalten. Der API Token steht in den ENV als OPEN_AI_API_TOKE
summary := getDokumentation(path)
fmt.Println(summary)
// summary string to Struct
task := getTask(summary)
// replace .go to .md in path
mdFile := strings.Replace(path, "."+extension, ".md", -1)
mdFile = strings.Replace(mdFile, dir, dir+"/doku", -1)
// make dir ist missing
os.MkdirAll(filepath.Dir(mdFile), os.ModePerm)
// write summary to file
for _,choise := range task.Choices {
err = ioutil.WriteFile(mdFile, []byte(choise.Text), 0644)
if err!= nil {
return err
}
}
}
return nil
})
if err != nil {
fmt.Println(err)
}
}
type Task struct {
Choices []Choise `json:"choices"`
ID int `json:"id"`
Usage Usage `json:"usage"`
Object string `json:"object"`
CreatedAt string `json:"created"`
Model string `json:"model"`
}
type Choise struct {
Text string `json:"text"`
Index int `json:"index"`
Logprops string `json:"logprobs"`
Finished bool `json:"finish_reason"`
}
type Usage struct {
PromtTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
func getTask(content string) Task {
task := Task{}
json.Unmarshal([]byte(content), &task)
return task
}
func gitClone(repoURL string) error {
cmd := exec.Command("git", "clone", repoURL)
err := cmd.Run()
if err != nil {
return err
}
return nil
}
func getDokumentation(filename string) string {
// Read the content of the file
content, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
// Setup the API request
return askGPT(string(content))
}
type RequestPayload struct {
Model string `json:"model"`
Prompt string `json:"prompt"`
Temperature float64 `json:"temperature"`
MaxTokens int `json:"max_tokens"`
}
func askGPT(content string) string {
apiKey := os.Getenv("OPEN_AI_API_TOKEN")
payload := RequestPayload{
Model: "text-davinci-003",
Prompt: fmt.Sprintf("Analysiere den folgenden Code und fasse es für die Dokumentation zusammen und formatiere den Text als Markdown: %s", content),
Temperature: 0.7,
MaxTokens: 2048,
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
log.Fatal(err)
}
url := "https://api.openai.com/v1/completions"
req, err := http.NewRequest("POST", url, bytes.NewReader(payloadBytes))
if err != nil {
log.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+apiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
// Read the API response
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
// Output the response
return string(body)
}