-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.go
160 lines (142 loc) · 3.75 KB
/
load.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
package main
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/tmc/langchaingo/documentloaders"
"github.com/tmc/langchaingo/schema"
"github.com/tmc/langchaingo/textsplitter"
)
func loadDocs(source string) (int, error) {
fmt.Println("loading data from", source)
var documentCount int
store, err := getVectorStore()
if err != nil {
return documentCount, err
}
docs, err := getDocs(source)
if err != nil {
return documentCount, err
}
fmt.Println("no. of documents to be loaded", len(docs))
if len(docs) > 0 && store != nil {
_, err = store.AddDocuments(context.Background(), docs)
}
if err != nil {
return documentCount, err
}
fmt.Println("data successfully loaded into vector store")
documentCount = len(docs)
return documentCount, nil
}
func getDocs(source string) ([]schema.Document, error) {
var urlParser = &url.URL{}
var err error
urlParser, err = url.Parse(source)
if err != nil {
return nil, err
}
if urlParser == nil {
urlParser = &url.URL{}
urlParser.Scheme = "file"
}
if urlParser.Scheme == "" {
urlParser.Scheme = "file"
}
switch urlParser.Scheme {
case "file":
var f *os.File
f, err = os.Open(source)
if err != nil {
return nil, err
}
defer f.Close()
b := bufio.NewReader(f)
switch filepath.Ext(source) {
case ".html":
fmt.Println("Processing html")
return processHTML(b)
case ".txt":
fmt.Println("Processing Text")
return processText(b)
case ".csv":
fmt.Println("Processing csv")
return processCSV(b)
case ".pdf":
fmt.Println("Processing pdf")
return processPDF(b)
default:
return nil, fmt.Errorf("unsupported file type: %s", filepath.Ext(source))
}
case "http", "https":
var resp *http.Response
resp, err = http.Get(source)
if err != nil {
return nil, err
}
defer resp.Body.Close()
contentType := resp.Header.Get("Content-Type")
switch {
case strings.Contains(contentType, "text/html"):
log.Println("Processing HTML")
return processHTML(resp.Body)
case strings.Contains(contentType, "text/plain"):
log.Println("Processing Text")
return processText(resp.Body)
case strings.Contains(contentType, "text/csv"):
log.Println("Processing CSV")
return processCSV(resp.Body)
case strings.Contains(contentType, "application/pdf"):
log.Println("Processing PDF")
return processPDF(resp.Body)
default:
return nil, fmt.Errorf("unsupported content type: %s", contentType)
}
default:
return nil, fmt.Errorf("unsupported scheme: %s for url %v", urlParser.Scheme, urlParser)
}
}
func processHTML(body io.Reader) ([]schema.Document, error) {
docs, err := documentloaders.NewHTML(body).LoadAndSplit(context.Background(), textsplitter.NewRecursiveCharacter())
if err != nil {
return nil, err
}
return docs, nil
}
func processText(body io.Reader) ([]schema.Document, error) {
docs, err := documentloaders.NewText(body).LoadAndSplit(context.Background(), textsplitter.NewRecursiveCharacter())
if err != nil {
return nil, err
}
return docs, nil
}
func processCSV(body io.Reader) ([]schema.Document, error) {
docs, err := documentloaders.NewCSV(body).LoadAndSplit(context.Background(), textsplitter.NewRecursiveCharacter())
if err != nil {
return nil, err
}
return docs, nil
}
func processPDF(body io.Reader) ([]schema.Document, error) {
buff := bytes.NewBuffer([]byte{})
size, err := io.Copy(buff, body)
if err != nil {
fmt.Println("error copying pdf body to buffer, err:", err)
return nil, err
}
reader := bytes.NewReader(buff.Bytes())
docs, err := documentloaders.NewPDF(reader, size).LoadAndSplit(context.Background(), textsplitter.NewRecursiveCharacter())
if err != nil {
fmt.Println("error loading pdf, err:", err)
return nil, err
}
return docs, nil
}