Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
LlamaIndexTS
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
MachineLearning
run-llama
LlamaIndexTS
Commits
a7edc4d2
Commit
a7edc4d2
authored
1 year ago
by
Sourabh Desai
Browse files
Options
Downloads
Patches
Plain Diff
update method signature
parent
1bcf7604
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
packages/core/src/readers/PDFReader.ts
+3
-3
3 additions, 3 deletions
packages/core/src/readers/PDFReader.ts
packages/core/src/readers/SimpleDirectoryReader.ts
+44
-5
44 additions, 5 deletions
packages/core/src/readers/SimpleDirectoryReader.ts
with
47 additions
and
8 deletions
packages/core/src/readers/PDFReader.ts
+
3
−
3
View file @
a7edc4d2
...
@@ -5,13 +5,13 @@ import { DEFAULT_FS } from "../storage/constants";
...
@@ -5,13 +5,13 @@ import { DEFAULT_FS } from "../storage/constants";
import
{
default
as
pdfParse
}
from
"
pdf-parse
"
;
import
{
default
as
pdfParse
}
from
"
pdf-parse
"
;
import
_
from
"
lodash
"
;
import
_
from
"
lodash
"
;
export
class
PDFReader
implements
BaseReader
{
export
default
class
PDFReader
implements
BaseReader
{
async
loadData
(
async
loadData
(
file
:
string
,
file
:
string
,
fs
:
GenericFileSystem
=
DEFAULT_FS
fs
:
GenericFileSystem
=
DEFAULT_FS
):
Promise
<
Document
>
{
):
Promise
<
Document
[]
>
{
let
dataBuffer
=
(
await
fs
.
readFile
(
file
))
as
any
;
let
dataBuffer
=
(
await
fs
.
readFile
(
file
))
as
any
;
const
data
=
await
pdfParse
(
dataBuffer
);
const
data
=
await
pdfParse
(
dataBuffer
);
return
new
Document
(
data
.
text
,
file
);
return
[
new
Document
(
data
.
text
,
file
)
]
;
}
}
}
}
This diff is collapsed.
Click to expand it.
packages/core/src/readers/SimpleDirectoryReader.ts
+
44
−
5
View file @
a7edc4d2
import
_
from
"
lodash
"
;
import
{
Document
}
from
"
../Document
"
;
import
{
Document
}
from
"
../Document
"
;
import
{
BaseReader
}
from
"
./base
"
;
import
{
BaseReader
}
from
"
./base
"
;
import
{
CompleteFileSystem
,
walk
}
from
"
../storage/FileSystem
"
;
import
{
CompleteFileSystem
,
walk
}
from
"
../storage/FileSystem
"
;
import
{
DEFAULT_FS
}
from
"
../storage/constants
"
;
import
{
DEFAULT_FS
}
from
"
../storage/constants
"
;
import
PDFReader
from
"
./PDFReader
"
;
export
default
class
SimpleDirectory
Reader
implements
BaseReader
{
export
class
TextFile
Reader
implements
BaseReader
{
async
loadData
(
async
loadData
(
directoryPath
:
string
,
file
:
string
,
fs
:
CompleteFileSystem
=
DEFAULT_FS
as
CompleteFileSystem
fs
:
CompleteFileSystem
=
DEFAULT_FS
as
CompleteFileSystem
):
Promise
<
Document
[]
>
{
):
Promise
<
Document
[]
>
{
const
docs
:
Document
[]
=
[];
const
dataBuffer
=
await
fs
.
readFile
(
file
,
"
utf-8
"
);
return
[
new
Document
(
dataBuffer
,
file
)];
}
}
const
FILE_EXT_TO_READER
:
{
[
key
:
string
]:
BaseReader
}
=
{
txt
:
new
TextFileReader
(),
pdf
:
new
PDFReader
(),
};
export
type
SimpleDirectoryReaderLoadDataProps
=
{
directoryPath
:
string
;
fs
?:
CompleteFileSystem
;
defaultReader
?:
BaseReader
|
null
;
fileExtToReader
?:
{
[
key
:
string
]:
BaseReader
};
};
export
default
class
SimpleDirectoryReader
implements
BaseReader
{
async
loadData
({
directoryPath
,
fs
=
DEFAULT_FS
as
CompleteFileSystem
,
defaultReader
=
new
TextFileReader
(),
fileExtToReader
=
FILE_EXT_TO_READER
,
}:
SimpleDirectoryReaderLoadDataProps
):
Promise
<
Document
[]
>
{
let
docs
:
Document
[]
=
[];
for
await
(
const
filePath
of
walk
(
fs
,
directoryPath
))
{
for
await
(
const
filePath
of
walk
(
fs
,
directoryPath
))
{
try
{
try
{
const
fileData
=
await
fs
.
readFile
(
filePath
);
const
fileExt
=
_
.
last
(
filePath
.
split
(
"
.
"
))
||
""
;
docs
.
push
(
new
Document
(
fileData
,
directoryPath
));
let
reader
=
null
;
if
(
fileExt
in
fileExtToReader
)
{
reader
=
fileExtToReader
[
fileExt
];
}
else
if
(
!
_
.
isNil
(
defaultReader
))
{
reader
=
defaultReader
;
}
else
{
console
.
warn
(
`No reader for file extension of
${
filePath
}
`
);
continue
;
}
const
fileDocs
=
await
reader
.
loadData
(
filePath
,
fs
);
docs
.
push
(...
fileDocs
);
}
catch
(
e
)
{
}
catch
(
e
)
{
console
.
error
(
`Error reading file
${
filePath
}
:
${
e
}
`
);
console
.
error
(
`Error reading file
${
filePath
}
:
${
e
}
`
);
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment