This repository has been archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: structured json dataloader (#151)
- Loading branch information
1 parent
fbc53d0
commit a75f75e
Showing
7 changed files
with
152 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"metadata": { | ||
"source": "https://example.com", | ||
"filename": "foo.pdf" | ||
}, | ||
"documents": [ | ||
{ | ||
"metadata": { | ||
"page": 1 | ||
}, | ||
"content": "This is the first page of the document." | ||
}, | ||
{ | ||
"metadata": { | ||
"page": 2 | ||
}, | ||
"content": "This is the second page of the document." | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package structured | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
vs "github.com/gptscript-ai/knowledge/pkg/vectorstore/types" | ||
"github.com/knadh/koanf/maps" | ||
) | ||
|
||
type StructuredInputDocument struct { | ||
Metadata map[string]any `json:"metadata"` | ||
Content string `json:"content"` | ||
} | ||
|
||
type StructuredInput struct { | ||
Metadata map[string]any `json:"metadata"` | ||
Documents []StructuredInputDocument `json:"documents"` | ||
} | ||
|
||
type Structured struct{} | ||
|
||
func (s *Structured) Load(ctx context.Context, reader io.Reader) ([]vs.Document, error) { | ||
var input StructuredInput | ||
if err := json.NewDecoder(reader).Decode(&input); err != nil { | ||
return nil, fmt.Errorf("failed to decode input: %w", err) | ||
} | ||
|
||
docs := make([]vs.Document, 0, len(input.Documents)) | ||
for _, doc := range input.Documents { | ||
maps.Merge(maps.Copy(input.Metadata), doc.Metadata) | ||
docs = append(docs, vs.Document{ | ||
Content: doc.Content, | ||
Metadata: doc.Metadata, | ||
}) | ||
} | ||
|
||
return docs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters