-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e66bcf
commit 65c9334
Showing
13 changed files
with
453 additions
and
176 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,27 @@ | ||
import 'dart:io'; | ||
import 'dart:math'; | ||
import 'package:html/parser.dart'; | ||
|
||
import 'package:langchain/langchain.dart'; | ||
|
||
class HTMLLoader extends BaseDocumentLoader { | ||
|
||
final String path; | ||
final int windowSize; | ||
const HTMLLoader(this.path, this.windowSize); | ||
|
||
@override | ||
Stream<Document> lazyLoad() async* { | ||
final data = await File(path).readAsString(); | ||
final text = parse(data).body?.text; | ||
if (text != null) { | ||
for (int i = 0; i < text.length; i += windowSize) { | ||
final content = text.substring(i, min(i + windowSize, text.length)); | ||
yield Document( | ||
pageContent: content, | ||
metadata: {"source": path}, | ||
); | ||
} | ||
} | ||
} | ||
} |
File renamed without changes.
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,23 @@ | ||
import 'dart:io'; | ||
import 'dart:math'; | ||
|
||
import 'package:langchain/langchain.dart'; | ||
|
||
class TextLoader extends BaseDocumentLoader { | ||
|
||
final String path; | ||
final int windowSize; | ||
const TextLoader(this.path, this.windowSize); | ||
|
||
@override | ||
Stream<Document> lazyLoad() async* { | ||
final text = await File(path).readAsString(); | ||
for (int i = 0; i < text.length; i += windowSize) { | ||
final content = text.substring(i, min(i + windowSize, text.length)); | ||
yield Document( | ||
pageContent: content, | ||
metadata: {"source": path}, | ||
); | ||
} | ||
} | ||
} |
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,38 @@ | ||
import 'package:inference/langchain/loaders/html_loader.dart'; | ||
import 'package:inference/langchain/loaders/pdf_loader.dart'; | ||
import 'package:inference/langchain/loaders/text_loader.dart'; | ||
import 'package:langchain/langchain.dart'; | ||
import 'package:path/path.dart'; | ||
|
||
const windowSize = 400; | ||
|
||
String defaultLoaderSelector(String path) { | ||
final ext = extension(path); | ||
if (ext == ".pdf") { | ||
return "PdfLoader"; | ||
} | ||
|
||
if (ext == ".html") { | ||
return "HTMLLoader"; | ||
} | ||
|
||
//if (ext == ".json") { | ||
// return JsonLoader(path,) | ||
//} | ||
|
||
return "TextLoader"; | ||
} | ||
|
||
BaseDocumentLoader loaderFromName(String name, String path) { | ||
switch (name) { | ||
case "PdfLoader": | ||
return PdfLoader(path, windowSize); | ||
case "HTMLLoader": | ||
return HTMLLoader(path, windowSize); | ||
case "TextLoader": | ||
return TextLoader(path, windowSize); | ||
default: | ||
throw Exception("Unknown loader name: $name"); | ||
} | ||
|
||
} |
Oops, something went wrong.