-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_index.go
146 lines (112 loc) · 3.09 KB
/
cmd_index.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
package main
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/charmbracelet/log"
"github.com/hashicorp/go-retryablehttp"
"github.com/philippgille/chromem-go"
"github.com/shopwarelabs/copilot-extension/config"
"github.com/shopwarelabs/copilot-extension/copilot"
"github.com/spf13/cobra"
"github.com/tmc/langchaingo/documentloaders"
"github.com/tmc/langchaingo/textsplitter"
)
var indexCommand = &cobra.Command{
Use: "index",
Short: "Embed all files in the data directory to the vector database",
RunE: func(cmd *cobra.Command, args []string) error {
collection, err := config.GetCollection()
if err != nil {
return err
}
cfg, err := config.New()
if err != nil {
return err
}
fileNames := make([]string, 0)
filepath.WalkDir("data", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return nil
}
if d.IsDir() {
return nil
}
if strings.Contains(path, "draco") {
return nil
}
if strings.HasSuffix(d.Name(), ".md") || strings.HasSuffix(d.Name(), ".js") || strings.HasSuffix(d.Name(), ".php") || strings.HasSuffix(d.Name(), ".scss") || strings.HasSuffix(d.Name(), ".css") || strings.HasSuffix(d.Name(), ".twig") {
fileNames = append(fileNames, path)
}
return nil
})
split := textsplitter.NewRecursiveCharacter()
split.ChunkSize = 12000
split.ChunkOverlap = 30
filesCount := len(fileNames)
client := retryablehttp.NewClient()
client.Backoff = retryablehttp.DefaultBackoff
client.CheckRetry = retryablehttp.DefaultRetryPolicy
for i, fileName := range fileNames {
content, err := os.Open(fileName)
if err != nil {
continue
}
defer content.Close()
p := documentloaders.NewText(content)
docs, err := p.LoadAndSplit(cmd.Context(), split)
if err != nil {
continue
}
log.Infof("Indexing [%d/%d] %s", i+1, filesCount, fileName)
documents := make([]chromem.Document, 0)
for idx, doc := range docs {
var embeddings *copilot.EmbeddingsResponse
for {
embeddings, err = copilot.Embeddings(cmd.Context(), client, cfg.LocalGitHubIntegrationID, cfg.LocalGitHubToken, &copilot.EmbeddingsRequest{
Model: copilot.ModelEmbeddings,
Input: []string{doc.PageContent},
})
if err == nil {
break
}
if err != nil {
if strings.Contains(err.Error(), "429") {
log.Infof("Rate limited slowing down")
time.Sleep(time.Second * 3)
continue
}
log.Infof("File: %s, error: %s", fileName, err.Error())
err = nil
break
}
}
if embeddings != nil {
for _, embedding := range embeddings.Data {
documents = append(documents, chromem.Document{
ID: fmt.Sprintf("%s_%d", fileName, idx),
Content: doc.PageContent,
Embedding: embedding.Embedding,
})
}
} else {
break
}
}
if len(documents) == 0 {
continue
}
if err := collection.AddDocuments(cmd.Context(), documents, runtime.NumCPU()); err != nil {
return err
}
}
return nil
},
}
func init() {
rootCmd.AddCommand(indexCommand)
}