Load reads from the io.Reader and returns a single document with the data.
(_ context.Context)
| 31 | |
| 32 | // Load reads from the io.Reader and returns a single document with the data. |
| 33 | func (c CSV) Load(_ context.Context) ([]schema.Document, error) { |
| 34 | var header []string |
| 35 | var docs []schema.Document |
| 36 | var rown int |
| 37 | |
| 38 | rd := csv.NewReader(c.r) |
| 39 | for { |
| 40 | row, err := rd.Read() |
| 41 | if errors.Is(err, io.EOF) { |
| 42 | break |
| 43 | } |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | if len(header) == 0 { |
| 48 | header = append(header, row...) |
| 49 | continue |
| 50 | } |
| 51 | |
| 52 | var content []string |
| 53 | for i, value := range row { |
| 54 | if len(c.columns) > 0 && |
| 55 | !slices.Contains(c.columns, header[i]) { |
| 56 | continue |
| 57 | } |
| 58 | |
| 59 | line := fmt.Sprintf("%s: %s", header[i], value) |
| 60 | content = append(content, line) |
| 61 | } |
| 62 | |
| 63 | rown++ |
| 64 | docs = append(docs, schema.Document{ |
| 65 | PageContent: strings.Join(content, "\n"), |
| 66 | Metadata: map[string]any{"row": rown}, |
| 67 | }) |
| 68 | } |
| 69 | |
| 70 | return docs, nil |
| 71 | } |
| 72 | |
| 73 | // LoadAndSplit reads text data from the io.Reader and splits it into multiple |
| 74 | // documents using a text splitter. |