-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
359 lines (325 loc) · 10.8 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
package main
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"github.com/google/go-github/v45/github"
"github.com/sashabaranov/go-openai"
"github.com/tmc/langchaingo/chains"
"github.com/tmc/langchaingo/documentloaders"
langchainOpenai "github.com/tmc/langchaingo/llms/openai"
"github.com/tmc/langchaingo/textsplitter"
"golang.org/x/oauth2"
)
const DeepSeekBaseURL = "https://api.deepseek.com"
const ModelDeepSeekChat = "deepseek-chat"
const ModelDeepSeekCoder = "deepseek-coder"
var MaxLLMInputLength = 4096
var LLMAuthorizationToken = os.Getenv("OPENAI_API_KEY")
// GitHubRepo represents the basic information about a GitHub repository.
type GitHubRepo struct {
Owner string
Name string
}
// FileContent represents the content of a file in a repository.
type FileContent struct {
Name string
Content string
}
// GetRepositoryContents retrieves the contents of a GitHub repository at a specific path.
func GetRepositoryContents(client *github.Client, repo GitHubRepo, path string) ([]*github.RepositoryContent, error) {
_, dirContents, _, err := client.Repositories.GetContents(context.Background(), repo.Owner, repo.Name, path, nil)
if err != nil {
return nil, err
}
return dirContents, nil
}
// GetFileContent retrieves the content of a file in a repository.
func GetFileContent(client *github.Client, repo GitHubRepo, filePath string) (string, error) {
fileContent, _, _, err := client.Repositories.GetContents(context.Background(), repo.Owner, repo.Name, filePath, nil)
if err != nil {
return "", err
}
content, err := fileContent.GetContent()
if err != nil {
return "", err
}
return content, nil
}
type SummarizeResult struct {
Readme string
StructureText string
StructureDetailText string
ALLInOne string
}
// SummarizeRepository generates a summary of the repository's structure and purpose.
func SummarizeRepository(readmeContent string, repo GitHubRepo, client *github.Client) (SummarizeResult, error) {
sr := SummarizeResult{}
sr.Readme = readmeContent
structure, structureDetail, err := getStructure(client, repo, "", 0, MaxLLMInputLength)
if err != nil {
return sr, err
}
summary := "### Repository Summary\n\n"
summary += "#### Purpose\n"
summary += readmeContent + "\n\n"
summary += "#### Structure\n"
beautyStructure, sErr := beautyFileStructureASCII(structure)
if sErr == nil {
structure = beautyStructure
}
summary += structure
summary += "\n\n#### Structure Detail\n"
summary += structureDetail
sr.StructureText = structure
sr.StructureDetailText = structureDetail
sr.ALLInOne = summary
return sr, nil
}
const CodeSummaryALLInOneFName = "./code_summary.txt"
const CodeSummaryReadmeFName = "./code_readme.txt"
const CodeSummaryStructureFName = "./code_structure.txt"
const CodeSummaryStructureDetailFName = "./code_structure_detail.txt"
const CodeRefineFName = "./code_refine.txt"
func main() {
// Replace with your GitHub token
token := os.Getenv("GITHUB_TOKEN")
maxLLMInputLength := os.Getenv("LLM_MAX_INPUT_LENGTH")
if maxLLMInputLength != "" {
// convert maxLLMInputLength to int
maxInputLength, err := strconv.Atoi(maxLLMInputLength)
if err != nil {
fmt.Printf("Error converting max input length: %v\n", err)
return
}
MaxLLMInputLength = maxInputLength
}
if LLMAuthorizationToken == "" {
fmt.Println("No LLM authorization token provided")
return
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
cmd := os.Args[1]
if cmd == "summary" {
Summary(client)
return
}
if cmd == "refine" {
Refine(client)
return
}
fmt.Println("Missing Correct cmd: summary or refine.")
}
func RecoverFromFile() SummarizeResult {
sr := SummarizeResult{}
if txt, err := os.ReadFile(CodeSummaryALLInOneFName); err == nil {
sr.ALLInOne = string(txt)
} else {
fmt.Println("Read all in one failed,err:", err)
}
if txt, err := os.ReadFile(CodeSummaryReadmeFName); err == nil {
sr.Readme = string(txt)
} else {
fmt.Println("Read readme failed,err:", err)
}
if txt, err := os.ReadFile(CodeSummaryStructureFName); err == nil {
sr.StructureText = string(txt)
} else {
fmt.Println("Read structure failed,err:", err)
}
if txt, err := os.ReadFile(CodeSummaryStructureDetailFName); err == nil {
sr.StructureDetailText = string(txt)
} else {
fmt.Println("Read structure detail failed,err:", err)
}
return sr
}
func Refine(client *github.Client) {
sr := RecoverFromFile()
if sr.StructureDetailText == "" {
fmt.Println("missing structure detail")
return
}
ctx := context.Background()
llm, err := langchainOpenai.New(
langchainOpenai.WithBaseURL(DeepSeekBaseURL),
langchainOpenai.WithModel(ModelDeepSeekChat),
)
if err != nil {
fmt.Println("create llm client failed,err:", err)
return
}
llmSummarizationChain := chains.LoadRefineSummarization(llm)
docs, err := documentloaders.NewText(strings.NewReader(sr.StructureDetailText)).LoadAndSplit(ctx,
textsplitter.NewRecursiveCharacter(textsplitter.WithChunkSize(MaxLLMInputLength)),
)
outputValues, err := chains.Call(ctx, llmSummarizationChain, map[string]any{"input_documents": docs})
if err != nil {
fmt.Println("call summary chain failed,err:", err)
return
}
out := outputValues["text"].(string)
fmt.Println("refine output:", out)
f, _ := os.Create(CodeRefineFName)
f.WriteString(out)
f.Close()
}
func Summary(client *github.Client) {
// read repo url from command argument
repoURL := os.Args[2]
// parse repo url to GitHubRepo
repo, err0 := ParseGitHubURL(repoURL)
if err0 != nil {
fmt.Printf("Error parsing repo url: %v\n", err0)
return
}
SummaryRepo(client, repo)
}
func SummaryRepo(client *github.Client, repo GitHubRepo) {
readmeContent, err := GetFileContent(client, repo, "README.md")
if err != nil {
fmt.Printf("Error getting README content: %v\n", err)
return
}
summary, err := SummarizeRepository(readmeContent, repo, client)
if err != nil {
fmt.Printf("Error summarizing repository: %v\n", err)
return
}
fmt.Println(summary)
f0, _ := os.Create(CodeSummaryALLInOneFName)
f0.WriteString(summary.ALLInOne)
f0.Close()
f1, _ := os.Create(CodeSummaryReadmeFName)
f1.WriteString(summary.Readme)
f1.Close()
f2, _ := os.Create(CodeSummaryStructureFName)
f2.WriteString(summary.StructureText)
f2.Close()
f3, _ := os.Create(CodeSummaryStructureDetailFName)
f3.WriteString(summary.StructureDetailText)
f3.Close()
}
// ParseGitHubURL parses a GitHub repository URL and extracts the owner and repo name.
func ParseGitHubURL(url string) (GitHubRepo, error) {
parts := strings.Split(strings.TrimPrefix(url, "https://github.com/"), "/")
if len(parts) != 2 {
return GitHubRepo{}, fmt.Errorf("invalid GitHub URL: %s", url)
}
return GitHubRepo{Owner: parts[0], Name: parts[1]}, nil
}
// isCodeFile checks if a file is a code file based on its extension.
func isCodeFile(filename string) bool {
extensions := []string{".go", ".py", ".c", ".js", ".ts", ".php", ".cpp"}
for _, ext := range extensions {
if strings.HasSuffix(filename, ext) {
return true
}
}
return false
}
// getStructure recursively retrieves the directory structure and returns it as a formatted string.
func getStructure(client *github.Client, repo GitHubRepo, path string, level int, maxLength int) (string, string, error) {
contents, err := GetRepositoryContents(client, repo, path)
if err != nil {
return "", "", err
}
var structureDetail strings.Builder
var structure strings.Builder
indent := strings.Repeat(" ", level)
for _, content := range contents {
structure.WriteString(fmt.Sprintf("%s- %s\n", indent, content.GetName()))
structureDetail.WriteString(fmt.Sprintf("%s- %s\n", indent, content.GetName()))
if content.GetType() == "dir" {
subStructure, subStructureDetail, err := getStructure(client, repo, content.GetPath(), level+1, maxLength)
if err != nil {
return "", "", err
}
structure.WriteString(subStructure)
structureDetail.WriteString(subStructureDetail)
} else {
if isCodeFile(content.GetName()) {
code, err := GetFileContent(client, repo, content.GetPath())
if err != nil {
return "", "", err
}
if len(code) > maxLength {
structureDetail.WriteString(fmt.Sprintf("%s [File too long to summarize]\n", indent))
} else {
summary := summarizeCode(content.GetName(), code)
structureDetail.WriteString(fmt.Sprintf("%s %s\n", indent, summary))
}
}
}
}
return structure.String(), structureDetail.String(), nil
}
// beautyFileStructureASCII 利用LLM转换输出好看的文件结构图,ascii风格
func beautyFileStructureASCII(code string) (string, error) {
config := openai.DefaultConfig(LLMAuthorizationToken)
config.BaseURL = "https://api.deepseek.com"
client := openai.NewClientWithConfig(config)
prompt := `请你将以下源代码文件列表整理成漂亮的ASCII风格的文件结构图: \n\n%s`
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: "deepseek-coder",
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "You are a helpful assistant.Use Chinese to answer questions.",
},
{
Role: openai.ChatMessageRoleUser,
Content: fmt.Sprintf(prompt, code),
},
},
},
)
if err != nil {
fmt.Println("ChatCompletion error: ", err)
return "", fmt.Errorf("ChatCompletion error: %v", err)
}
// Simulate LLM call - replace with actual LLM call logic
result := fmt.Sprintf("%s", resp.Choices[0].Message.Content)
fmt.Println("beautyFileStructureASCII result:", "---------------\n\n", result, "\n\n---------------\n\n")
return result, nil
}
// summarizeCode is a placeholder function to simulate calling an LLM to summarize code.
func summarizeCode(fileName, code string) string {
fmt.Println("Summarizing code for", fileName)
config := openai.DefaultConfig(LLMAuthorizationToken)
config.BaseURL = DeepSeekBaseURL
client := openai.NewClientWithConfig(config)
prompt := `总结以下代码文件内容,尽可能详细讲解功能和实现细节,便于读者学习阅读该代码: \n\n%s`
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: ModelDeepSeekCoder,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "You are a helpful assistant.Use Chinese to answer questions.",
},
{
Role: openai.ChatMessageRoleUser,
Content: fmt.Sprintf(prompt, code),
},
},
},
)
if err != nil {
return fmt.Sprintf("ChatCompletion error: %v\n", err)
}
// Simulate LLM call - replace with actual LLM call logic
result := fmt.Sprintf("Summarized code for %s\n\n%s", fileName, resp.Choices[0].Message.Content)
fmt.Println("process result:", "---------------\n\n", result, "\n\n---------------\n\n")
return result
}