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
95602c79
Unverified
Commit
95602c79
authored
11 months ago
by
Thuc Pham
Committed by
GitHub
11 months ago
Browse files
Options
Downloads
Patches
Plain Diff
feat: overide generate hash function for image document (#751)
Co-authored-by:
Marcus Schiesser
<
mail@marcusschiesser.de
>
parent
20bc466c
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
examples/multimodal/load.ts
+0
-2
0 additions, 2 deletions
examples/multimodal/load.ts
packages/core/src/Node.ts
+31
-0
31 additions, 0 deletions
packages/core/src/Node.ts
packages/core/tests/Document.test.ts
+13
-1
13 additions, 1 deletion
packages/core/tests/Document.test.ts
with
44 additions
and
3 deletions
examples/multimodal/load.ts
+
0
−
2
View file @
95602c79
...
@@ -4,7 +4,6 @@ import {
...
@@ -4,7 +4,6 @@ import {
VectorStoreIndex
,
VectorStoreIndex
,
storageContextFromDefaults
,
storageContextFromDefaults
,
}
from
"
llamaindex
"
;
}
from
"
llamaindex
"
;
import
{
DocStoreStrategy
}
from
"
llamaindex/ingestion/strategies/index
"
;
import
*
as
path
from
"
path
"
;
import
*
as
path
from
"
path
"
;
...
@@ -32,7 +31,6 @@ async function generateDatasource() {
...
@@ -32,7 +31,6 @@ async function generateDatasource() {
});
});
await
VectorStoreIndex
.
fromDocuments
(
documents
,
{
await
VectorStoreIndex
.
fromDocuments
(
documents
,
{
storageContext
,
storageContext
,
docStoreStrategy
:
DocStoreStrategy
.
NONE
,
});
});
});
});
console
.
log
(
`Storage successfully generated in
${
ms
/
1000
}
s.`
);
console
.
log
(
`Storage successfully generated in
${
ms
/
1000
}
s.`
);
...
...
This diff is collapsed.
Click to expand it.
packages/core/src/Node.ts
+
31
−
0
View file @
95602c79
...
@@ -326,6 +326,37 @@ export class ImageNode<T extends Metadata = Metadata> extends TextNode<T> {
...
@@ -326,6 +326,37 @@ export class ImageNode<T extends Metadata = Metadata> extends TextNode<T> {
const
absPath
=
path
.
resolve
(
this
.
id_
);
const
absPath
=
path
.
resolve
(
this
.
id_
);
return
new
URL
(
`file://
${
absPath
}
`
);
return
new
URL
(
`file://
${
absPath
}
`
);
}
}
// Calculates the image part of the hash
private
generateImageHash
()
{
const
hashFunction
=
createSHA256
();
if
(
this
.
image
instanceof
Blob
)
{
// TODO: ideally we should use the blob's content to calculate the hash:
// hashFunction.update(new Uint8Array(await this.image.arrayBuffer()));
// as this is async, we're using the node's ID for the time being
hashFunction
.
update
(
this
.
id_
);
}
else
if
(
this
.
image
instanceof
URL
)
{
hashFunction
.
update
(
this
.
image
.
toString
());
}
else
if
(
typeof
this
.
image
===
"
string
"
)
{
hashFunction
.
update
(
this
.
image
);
}
else
{
throw
new
Error
(
`Unknown image type:
${
typeof
this
.
image
}
. Can't calculate hash`
,
);
}
return
hashFunction
.
digest
();
}
generateHash
()
{
const
hashFunction
=
createSHA256
();
// calculates hash based on hash of both components (image and text)
hashFunction
.
update
(
super
.
generateHash
());
hashFunction
.
update
(
this
.
generateImageHash
());
return
hashFunction
.
digest
();
}
}
}
export
class
ImageDocument
<
T
extends
Metadata
=
Metadata
>
extends
ImageNode
<
T
>
{
export
class
ImageDocument
<
T
extends
Metadata
=
Metadata
>
extends
ImageNode
<
T
>
{
...
...
This diff is collapsed.
Click to expand it.
packages/core/tests/Document.test.ts
+
13
−
1
View file @
95602c79
import
{
Document
}
from
"
llamaindex/Node
"
;
import
{
Document
,
ImageDocument
}
from
"
llamaindex/Node
"
;
import
{
describe
,
expect
,
test
}
from
"
vitest
"
;
import
{
describe
,
expect
,
test
}
from
"
vitest
"
;
describe
(
"
Document
"
,
()
=>
{
describe
(
"
Document
"
,
()
=>
{
...
@@ -6,4 +6,16 @@ describe("Document", () => {
...
@@ -6,4 +6,16 @@ describe("Document", () => {
const
doc
=
new
Document
({
text
:
"
text
"
,
id_
:
"
docId
"
});
const
doc
=
new
Document
({
text
:
"
text
"
,
id_
:
"
docId
"
});
expect
(
doc
).
toBeDefined
();
expect
(
doc
).
toBeDefined
();
});
});
test
(
"
should generate different hash for different image contents
"
,
()
=>
{
const
imageNode1
=
new
ImageDocument
({
id_
:
"
image
"
,
image
:
"
data:image/png;base64,sample_image_content1
"
,
});
const
imageNode2
=
new
ImageDocument
({
id_
:
"
image
"
,
image
:
"
data:image/png;base64,sample_image_content2
"
,
});
expect
(
imageNode1
.
hash
).
not
.
toBe
(
imageNode2
.
hash
);
});
});
});
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