forked from run-llama/LlamaIndexTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: generate uuid when inserting to Qdrant (run-llama#1301)
- Loading branch information
Showing
5 changed files
with
68 additions
and
1 deletion.
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,6 @@ | ||
--- | ||
"@llamaindex/core": patch | ||
"llamaindex": patch | ||
--- | ||
|
||
fix: generate uuid when inserting to Qdrant |
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,22 @@ | ||
import { createSHA256 } from "@llamaindex/env"; | ||
|
||
export function UUIDFromString(input: string) { | ||
const hashFunction = createSHA256(); | ||
hashFunction.update(input); | ||
const base64Hash = hashFunction.digest(); | ||
|
||
// Convert base64 to hex | ||
const hexHash = Buffer.from(base64Hash, "base64").toString("hex"); | ||
|
||
// Format the hash to resemble a UUID (version 5 style) | ||
const uuid = [ | ||
hexHash.substring(0, 8), | ||
hexHash.substring(8, 12), | ||
"5" + hexHash.substring(12, 15), // Set the version to 5 (name-based) | ||
((parseInt(hexHash.substring(15, 17), 16) & 0x3f) | 0x80).toString(16) + | ||
hexHash.substring(17, 19), // Set the variant | ||
hexHash.substring(19, 31), | ||
].join("-"); | ||
|
||
return uuid; | ||
} |
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,37 @@ | ||
import { UUIDFromString } from "@llamaindex/core/utils"; | ||
import { describe, expect, it } from "vitest"; | ||
|
||
const UUID_REGEX = | ||
/^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; | ||
|
||
describe("UUIDFromString", () => { | ||
it("should convert string to UUID", () => { | ||
const string = "document_id_1"; | ||
const result = UUIDFromString(string); | ||
expect(result).toBeDefined(); | ||
expect(result).toMatch(UUID_REGEX); | ||
}); | ||
|
||
it("should return the same UUID for the same input string", () => { | ||
const string = "document_id_1"; | ||
const result1 = UUIDFromString(string); | ||
const result2 = UUIDFromString(string); | ||
expect(result1).toEqual(result2); | ||
}); | ||
|
||
it("should return the different UUID for different input strings", () => { | ||
const string1 = "document_id_1"; | ||
const string2 = "document_id_2"; | ||
const result1 = UUIDFromString(string1); | ||
const result2 = UUIDFromString(string2); | ||
expect(result1).not.toEqual(result2); | ||
}); | ||
|
||
it("should handle case-sensitive input strings", () => { | ||
const string1 = "document_id_1"; | ||
const string2 = "Document_Id_1"; | ||
const result1 = UUIDFromString(string1); | ||
const result2 = UUIDFromString(string2); | ||
expect(result1).not.toEqual(result2); | ||
}); | ||
}); |
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