@midsummer-ai/sdk 0.1.0-next.901 → 0.1.0-staging.3100
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/auth-1W-SzpD3.mjs +441 -0
- package/build-DQo-JrlN.d.mts +120 -0
- package/bytes-C2pcyl9u.mjs +280 -0
- package/{connection-CDk7H1FL.mjs → connection-Cu2GSOaR.mjs} +171 -31
- package/database/build.d.mts +2 -2
- package/database/build.mjs +1404 -117
- package/database/client.d.mts +85 -9
- package/database/client.mjs +550 -32
- package/database/contract.d.mts +92 -2
- package/database/contract.mjs +2179 -3
- package/database/errors.d.mts +1 -22
- package/database/react.d.mts +133 -0
- package/database/react.mjs +488 -0
- package/database/server.d.mts +2 -3
- package/database/server.mjs +3 -320
- package/database/stand-in.d.mts +95 -9
- package/database/stand-in.mjs +1436 -116
- package/errors-x2vXKi0u.d.mts +25 -0
- package/index-DNNXNny7.d.mts +410 -0
- package/key-BrRdFY-n.mjs +11 -0
- package/package.json +114 -1
- package/schema-CTHQMRsW.mjs +217 -0
- package/server-B5HK2Ou9.d.mts +574 -0
- package/server-EDRbF2u-.mjs +1893 -0
- package/{store-BzI9at5x.mjs → store-lIZ5GxVv.mjs} +1 -1
- package/{tabs-D-P8_ci_.mjs → tabs-zCh8kceo.mjs} +1 -1
- package/vector/contract.d.mts +1 -1
- package/vector/contract.mjs +773 -1
- package/vector/index.d.mts +2 -2
- package/vector/index.mjs +71 -3
- package/vector/stand-in.d.mts +1 -1
- package/vector/stand-in.mjs +326 -23
- package/vector/wire.d.mts +11 -2
- package/vector/wire.mjs +179 -14
- package/build-SQ44jwSa.d.mts +0 -41
- package/index-DdoK0y7D.d.mts +0 -192
- package/schema-BF-Y1EVu.mjs +0 -142
- package/schema-DS8x1Dn8.mjs +0 -115
- package/schema-f7E9uzUn.d.mts +0 -74
- package/server-C7O7CICU.d.mts +0 -182
package/vector/contract.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { b as VectorError } from "../schema-CTHQMRsW.mjs";
|
|
2
2
|
import { vectorDb } from "./index.mjs";
|
|
3
3
|
//#region src/vector/contract.ts
|
|
4
4
|
/**
|
|
@@ -592,6 +592,616 @@ const vectorContract = [
|
|
|
592
592
|
equal((await docs.describe()).count, 0, "the count after refused upserts");
|
|
593
593
|
}
|
|
594
594
|
},
|
|
595
|
+
{
|
|
596
|
+
name: "a filter's other operators, $and, $or and $not, null for a missing field, and arrays: each keeps exactly the Vectors it says",
|
|
597
|
+
async run(engine) {
|
|
598
|
+
await engine.create("shelf", {
|
|
599
|
+
dimension: 2,
|
|
600
|
+
metric: "dot"
|
|
601
|
+
});
|
|
602
|
+
const shelf = vectorDb(engine).namespace("shelf");
|
|
603
|
+
await shelf.upsert([
|
|
604
|
+
{
|
|
605
|
+
id: "a",
|
|
606
|
+
values: [1, 0],
|
|
607
|
+
metadata: {
|
|
608
|
+
tags: ["red", "blue"],
|
|
609
|
+
title: "Red Fox Runs",
|
|
610
|
+
n: [1, 5]
|
|
611
|
+
}
|
|
612
|
+
},
|
|
613
|
+
{
|
|
614
|
+
id: "b",
|
|
615
|
+
values: [0, 1],
|
|
616
|
+
metadata: {
|
|
617
|
+
tags: ["green"],
|
|
618
|
+
title: "blue whale",
|
|
619
|
+
n: [10]
|
|
620
|
+
}
|
|
621
|
+
},
|
|
622
|
+
{
|
|
623
|
+
id: "c",
|
|
624
|
+
values: [1, 1],
|
|
625
|
+
metadata: { title: "Fox in the box" }
|
|
626
|
+
},
|
|
627
|
+
{
|
|
628
|
+
id: "d",
|
|
629
|
+
values: [1, 2],
|
|
630
|
+
metadata: {
|
|
631
|
+
tags: ["red"],
|
|
632
|
+
n: [2]
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
]);
|
|
636
|
+
const ids = async (filter) => (await shelf.query([0, 0], {
|
|
637
|
+
topK: 10,
|
|
638
|
+
filter
|
|
639
|
+
})).map((m) => m.id).sort();
|
|
640
|
+
equal(await ids({ tags: { $contains: "red" } }), ["a", "d"], "$contains of an array");
|
|
641
|
+
equal(await ids({ tags: { $containsAny: ["green", "blue"] } }), ["a", "b"], "$containsAny");
|
|
642
|
+
equal(await ids({ tags: { $notContainsAny: ["red"] } }), ["b", "c"], "$notContainsAny, a missing field meeting it");
|
|
643
|
+
equal(await ids({ n: { $anyGt: 4 } }), ["a", "b"], "$anyGt");
|
|
644
|
+
equal(await ids({ title: { $glob: "*Fox*" } }), ["a", "c"], "$glob");
|
|
645
|
+
equal(await ids({ title: { $glob: "?ed F[a-p]x*" } }), ["a"], "$glob with ? and a set");
|
|
646
|
+
equal(await ids({ title: { $iglob: "*fox*" } }), ["a", "c"], "$iglob");
|
|
647
|
+
equal(await ids({ title: { $notGlob: "*Fox*" } }), ["b", "d"], "$notGlob, a missing field meeting it");
|
|
648
|
+
equal(await ids({ title: { $containsAllTokens: "fox box" } }), ["c"], "$containsAllTokens");
|
|
649
|
+
equal(await ids({ title: { $containsAnyToken: {
|
|
650
|
+
tokens: "whale ru",
|
|
651
|
+
lastAsPrefix: true
|
|
652
|
+
} } }), ["a", "b"], "$containsAnyToken, its last a prefix");
|
|
653
|
+
equal(await ids({ title: { $containsTokenSequence: "fox runs" } }), ["a"], "$containsTokenSequence");
|
|
654
|
+
equal(await ids({ title: { $eq: null } }), ["d"], "$eq null: the field missing");
|
|
655
|
+
equal(await ids({ title: { $in: [null, "blue whale"] } }), ["b", "d"], "$in with null");
|
|
656
|
+
equal(await ids({ $or: [{ tags: { $contains: "green" } }, { title: { $glob: "Fox*" } }] }), ["b", "c"], "$or");
|
|
657
|
+
equal(await ids({ $not: { tags: { $contains: "red" } } }), ["b", "c"], "$not");
|
|
658
|
+
equal(await ids({ $and: [{ tags: { $contains: "red" } }, { n: { $anyLt: 2 } }] }), ["a"], "$and");
|
|
659
|
+
equal(await ids({ id: { $in: ["a", "c"] } }), ["a", "c"], "id");
|
|
660
|
+
equal(await shelf.fetch(["a"]), [{
|
|
661
|
+
id: "a",
|
|
662
|
+
values: [1, 0],
|
|
663
|
+
metadata: {
|
|
664
|
+
tags: ["red", "blue"],
|
|
665
|
+
title: "Red Fox Runs",
|
|
666
|
+
n: [1, 5]
|
|
667
|
+
}
|
|
668
|
+
}], "arrays kept as given");
|
|
669
|
+
}
|
|
670
|
+
},
|
|
671
|
+
{
|
|
672
|
+
name: "a listing answers Vectors in the order of their metadata, a missing value last either way, ties by the next field then id; filter and topK apply",
|
|
673
|
+
async run(engine) {
|
|
674
|
+
await engine.create("compass", {
|
|
675
|
+
dimension: 2,
|
|
676
|
+
metric: "dot"
|
|
677
|
+
});
|
|
678
|
+
const compass = vectorDb(engine).namespace("compass");
|
|
679
|
+
await compass.upsert(points);
|
|
680
|
+
const ids = async (options) => (await compass.list(options)).map((v) => v.id);
|
|
681
|
+
equal(await ids({ orderBy: [["rank", "desc"]] }), [
|
|
682
|
+
"west",
|
|
683
|
+
"north-east",
|
|
684
|
+
"north",
|
|
685
|
+
"east"
|
|
686
|
+
], "rank descending");
|
|
687
|
+
equal(await ids({ orderBy: [["lonely", "desc"], ["rank", "asc"]] }), [
|
|
688
|
+
"west",
|
|
689
|
+
"east",
|
|
690
|
+
"north",
|
|
691
|
+
"north-east"
|
|
692
|
+
], "a field only one has, then rank");
|
|
693
|
+
equal(await ids({ orderBy: [["lonely", "asc"], ["id", "desc"]] }), [
|
|
694
|
+
"west",
|
|
695
|
+
"north-east",
|
|
696
|
+
"north",
|
|
697
|
+
"east"
|
|
698
|
+
], "a missing value last ascending too, then id descending");
|
|
699
|
+
equal(await ids({
|
|
700
|
+
orderBy: [["rank", "asc"]],
|
|
701
|
+
filter: { kind: "axis" },
|
|
702
|
+
topK: 2
|
|
703
|
+
}), ["east", "north"], "filtered and cut");
|
|
704
|
+
equal(await compass.list({
|
|
705
|
+
orderBy: [["rank", "asc"]],
|
|
706
|
+
topK: 1,
|
|
707
|
+
includeValues: true,
|
|
708
|
+
includeMetadata: true
|
|
709
|
+
}), [{
|
|
710
|
+
id: "east",
|
|
711
|
+
values: [1, 0],
|
|
712
|
+
metadata: {
|
|
713
|
+
kind: "axis",
|
|
714
|
+
rank: 1
|
|
715
|
+
}
|
|
716
|
+
}], "values and metadata, no score");
|
|
717
|
+
}
|
|
718
|
+
},
|
|
719
|
+
{
|
|
720
|
+
name: "a text search ranks the Vectors whose full-text field holds the words by BM25, best first; a field not kept full text is invalid",
|
|
721
|
+
async run(engine) {
|
|
722
|
+
await engine.create("notes", {
|
|
723
|
+
dimension: 2,
|
|
724
|
+
metric: "dot",
|
|
725
|
+
fullText: ["title"]
|
|
726
|
+
});
|
|
727
|
+
const notes = vectorDb(engine).namespace("notes");
|
|
728
|
+
await notes.upsert([
|
|
729
|
+
{
|
|
730
|
+
id: "a",
|
|
731
|
+
values: [1, 0],
|
|
732
|
+
metadata: { title: "the quick brown fox" }
|
|
733
|
+
},
|
|
734
|
+
{
|
|
735
|
+
id: "b",
|
|
736
|
+
values: [1, 0],
|
|
737
|
+
metadata: { title: "the lazy dog" }
|
|
738
|
+
},
|
|
739
|
+
{
|
|
740
|
+
id: "c",
|
|
741
|
+
values: [1, 0],
|
|
742
|
+
metadata: { title: ["quick quick", "fox jumps"] }
|
|
743
|
+
},
|
|
744
|
+
{
|
|
745
|
+
id: "d",
|
|
746
|
+
values: [1, 0],
|
|
747
|
+
metadata: {
|
|
748
|
+
title: "a dog and a fox",
|
|
749
|
+
lang: "en"
|
|
750
|
+
}
|
|
751
|
+
},
|
|
752
|
+
{
|
|
753
|
+
id: "e",
|
|
754
|
+
values: [1, 0],
|
|
755
|
+
metadata: { lang: "en" }
|
|
756
|
+
}
|
|
757
|
+
]);
|
|
758
|
+
const found = await waitBuilt(() => notes.textSearch("quick fox", {
|
|
759
|
+
field: "title",
|
|
760
|
+
topK: 10
|
|
761
|
+
}));
|
|
762
|
+
equal(found.map((m) => m.id), [
|
|
763
|
+
"c",
|
|
764
|
+
"a",
|
|
765
|
+
"d"
|
|
766
|
+
], "the order");
|
|
767
|
+
near(found.map((m) => m.score), [
|
|
768
|
+
1.760278582572937,
|
|
769
|
+
1.4319777488708496,
|
|
770
|
+
.5070822238922119
|
|
771
|
+
], "the BM25 scores");
|
|
772
|
+
equal((await notes.textSearch("FOX", {
|
|
773
|
+
field: "title",
|
|
774
|
+
filter: { lang: "en" },
|
|
775
|
+
includeMetadata: true
|
|
776
|
+
})).map((m) => [m.id, m.metadata]), [["d", {
|
|
777
|
+
title: "a dog and a fox",
|
|
778
|
+
lang: "en"
|
|
779
|
+
}]], "filtered, metadata included");
|
|
780
|
+
equal(await notes.textSearch("nothing", { field: "title" }), [], "no Vector holds the word");
|
|
781
|
+
await fails(notes.textSearch("fox", { field: "lang" }), "invalid", "a field not kept full text");
|
|
782
|
+
}
|
|
783
|
+
},
|
|
784
|
+
{
|
|
785
|
+
name: "full-text fields are only ever added: one added is searched once built, unavailable until then and never answered in part",
|
|
786
|
+
async run(engine) {
|
|
787
|
+
await engine.create("notes", {
|
|
788
|
+
dimension: 2,
|
|
789
|
+
metric: "dot"
|
|
790
|
+
});
|
|
791
|
+
const notes = vectorDb(engine).namespace("notes");
|
|
792
|
+
await notes.upsert(Array.from({ length: 40 }, (_, k) => ({
|
|
793
|
+
id: `n${String(k).padStart(2, "0")}`,
|
|
794
|
+
values: [1, 0],
|
|
795
|
+
metadata: { body: k % 4 === 0 ? "spring rain" : "dry summer" }
|
|
796
|
+
})));
|
|
797
|
+
await fails(notes.textSearch("rain", { field: "body" }), "invalid", "before the field is added");
|
|
798
|
+
await engine.update("notes", { addFullText: ["body"] });
|
|
799
|
+
await engine.update("notes", { addFullText: ["body"] });
|
|
800
|
+
equal((await waitBuilt(() => notes.textSearch("rain", {
|
|
801
|
+
field: "body",
|
|
802
|
+
topK: 20
|
|
803
|
+
}))).map((m) => m.id), [
|
|
804
|
+
"n00",
|
|
805
|
+
"n04",
|
|
806
|
+
"n08",
|
|
807
|
+
"n12",
|
|
808
|
+
"n16",
|
|
809
|
+
"n20",
|
|
810
|
+
"n24",
|
|
811
|
+
"n28",
|
|
812
|
+
"n32",
|
|
813
|
+
"n36"
|
|
814
|
+
], "every Vector with the word, once the field is built");
|
|
815
|
+
}
|
|
816
|
+
},
|
|
817
|
+
{
|
|
818
|
+
name: "a multiQuery answers each search's matches in the order asked, and with fuse their reciprocal-rank fusion",
|
|
819
|
+
async run(engine) {
|
|
820
|
+
await engine.create("compass", {
|
|
821
|
+
dimension: 2,
|
|
822
|
+
metric: "dot"
|
|
823
|
+
});
|
|
824
|
+
const compass = vectorDb(engine).namespace("compass");
|
|
825
|
+
await compass.upsert(points);
|
|
826
|
+
const { results, fused } = await compass.multiQuery([{
|
|
827
|
+
vector: [1, 0],
|
|
828
|
+
topK: 3
|
|
829
|
+
}, {
|
|
830
|
+
vector: [0, 1],
|
|
831
|
+
topK: 3
|
|
832
|
+
}], {
|
|
833
|
+
fuse: { rrf: {
|
|
834
|
+
k: 10,
|
|
835
|
+
weights: [1, 2]
|
|
836
|
+
} },
|
|
837
|
+
topK: 3
|
|
838
|
+
});
|
|
839
|
+
equal(results.map((r) => r.map((m) => m.id)), [[
|
|
840
|
+
"east",
|
|
841
|
+
"north-east",
|
|
842
|
+
"north"
|
|
843
|
+
], [
|
|
844
|
+
"north",
|
|
845
|
+
"north-east",
|
|
846
|
+
"east"
|
|
847
|
+
]], "each search's matches");
|
|
848
|
+
equal(fused?.map((m) => m.id), [
|
|
849
|
+
"north",
|
|
850
|
+
"north-east",
|
|
851
|
+
"east"
|
|
852
|
+
], "the fused order");
|
|
853
|
+
near(fused.map((m) => m.score), [
|
|
854
|
+
1 / 13 + 2 / 11,
|
|
855
|
+
3 / 12,
|
|
856
|
+
1 / 11 + 2 / 13
|
|
857
|
+
], "the fused scores");
|
|
858
|
+
const plain = await compass.multiQuery([{
|
|
859
|
+
vector: [1, 0],
|
|
860
|
+
topK: 1,
|
|
861
|
+
filter: { kind: "diagonal" }
|
|
862
|
+
}]);
|
|
863
|
+
equal([plain.results.map((r) => r.map((m) => m.id)), plain.fused], [[["north-east"]], void 0], "unfused, a filter on its search");
|
|
864
|
+
}
|
|
865
|
+
},
|
|
866
|
+
{
|
|
867
|
+
name: "an aggregate counts and sums the Vectors a filter keeps, grouped by fields' values with a missing one null and last; not grouped, one group",
|
|
868
|
+
async run(engine) {
|
|
869
|
+
await engine.create("compass", {
|
|
870
|
+
dimension: 2,
|
|
871
|
+
metric: "dot"
|
|
872
|
+
});
|
|
873
|
+
const compass = vectorDb(engine).namespace("compass");
|
|
874
|
+
await compass.upsert(points);
|
|
875
|
+
equal(await compass.aggregate({
|
|
876
|
+
aggregates: {
|
|
877
|
+
n: ["count"],
|
|
878
|
+
ranks: ["sum", "rank"]
|
|
879
|
+
},
|
|
880
|
+
groupBy: ["kind"]
|
|
881
|
+
}), [{
|
|
882
|
+
key: { kind: "axis" },
|
|
883
|
+
aggregates: {
|
|
884
|
+
n: 3,
|
|
885
|
+
ranks: 7
|
|
886
|
+
}
|
|
887
|
+
}, {
|
|
888
|
+
key: { kind: "diagonal" },
|
|
889
|
+
aggregates: {
|
|
890
|
+
n: 1,
|
|
891
|
+
ranks: 3
|
|
892
|
+
}
|
|
893
|
+
}], "by kind");
|
|
894
|
+
equal(await compass.aggregate({
|
|
895
|
+
aggregates: { n: ["count"] },
|
|
896
|
+
groupBy: ["lonely"]
|
|
897
|
+
}), [{
|
|
898
|
+
key: { lonely: true },
|
|
899
|
+
aggregates: { n: 1 }
|
|
900
|
+
}, {
|
|
901
|
+
key: { lonely: null },
|
|
902
|
+
aggregates: { n: 3 }
|
|
903
|
+
}], "a field some lack: null, last");
|
|
904
|
+
equal(await compass.aggregate({
|
|
905
|
+
aggregates: {
|
|
906
|
+
n: ["count"],
|
|
907
|
+
kinds: ["sum", "kind"]
|
|
908
|
+
},
|
|
909
|
+
filter: { rank: { $gte: 2 } }
|
|
910
|
+
}), [{
|
|
911
|
+
key: {},
|
|
912
|
+
aggregates: {
|
|
913
|
+
n: 3,
|
|
914
|
+
kinds: 0
|
|
915
|
+
}
|
|
916
|
+
}], "not grouped; a sum of strings adds nothing");
|
|
917
|
+
equal(await compass.aggregate({
|
|
918
|
+
aggregates: { n: ["count"] },
|
|
919
|
+
filter: { rank: 99 }
|
|
920
|
+
}), [{
|
|
921
|
+
key: {},
|
|
922
|
+
aggregates: { n: 0 }
|
|
923
|
+
}], "not grouped, no Vector kept");
|
|
924
|
+
equal((await compass.aggregate({
|
|
925
|
+
aggregates: { n: ["count"] },
|
|
926
|
+
groupBy: ["rank"],
|
|
927
|
+
limit: 2
|
|
928
|
+
})).map((g) => g.key), [{ rank: 1 }, { rank: 2 }], "limited, by key");
|
|
929
|
+
}
|
|
930
|
+
},
|
|
931
|
+
{
|
|
932
|
+
name: "listings, text searches, multiQueries and aggregates are checked before they reach the engine",
|
|
933
|
+
async run(engine) {
|
|
934
|
+
await engine.create("docs", {
|
|
935
|
+
dimension: 1,
|
|
936
|
+
metric: "dot"
|
|
937
|
+
});
|
|
938
|
+
const docs = vectorDb(engine).namespace("docs");
|
|
939
|
+
await fails(docs.list({ orderBy: [] }), "invalid", "an empty orderBy");
|
|
940
|
+
await fails(docs.list({ orderBy: Array.from({ length: 9 }, () => ["n", "asc"]) }), "invalid", "nine orderBy fields");
|
|
941
|
+
await fails(docs.list({ orderBy: [["n", "up"]] }), "invalid", "an unknown direction");
|
|
942
|
+
await fails(docs.textSearch(" ", { field: "body" }), "invalid", "a text search of no words");
|
|
943
|
+
await fails(docs.multiQuery([]), "invalid", "a multiQuery of no searches");
|
|
944
|
+
await fails(docs.multiQuery(Array.from({ length: 17 }, () => ({ vector: [1] }))), "invalid", "17 searches");
|
|
945
|
+
await fails(docs.multiQuery([{ vector: [1] }], { fuse: { rrf: { weights: [1, 2] } } }), "invalid", "a weight too many");
|
|
946
|
+
await fails(docs.aggregate({ aggregates: {} }), "invalid", "no aggregates");
|
|
947
|
+
await fails(docs.aggregate({ aggregates: { x: ["avg", "n"] } }), "invalid", "an unknown aggregate");
|
|
948
|
+
await fails(docs.query([1], { filter: { $and: [] } }), "invalid", "an empty $and");
|
|
949
|
+
await fails(docs.query([1], { filter: { tags: { $regex: "x" } } }), "invalid", "$regex, which is not offered");
|
|
950
|
+
await fails(docs.upsert([{
|
|
951
|
+
id: "a",
|
|
952
|
+
values: [1],
|
|
953
|
+
metadata: { tags: [true] }
|
|
954
|
+
}]), "invalid", "an array of booleans");
|
|
955
|
+
}
|
|
956
|
+
},
|
|
957
|
+
{
|
|
958
|
+
name: "seeded: every method answers as brute force does over 240 Vectors, for many filters and orders",
|
|
959
|
+
async run(engine) {
|
|
960
|
+
await engine.create("seeded", {
|
|
961
|
+
dimension: 4,
|
|
962
|
+
metric: "cosine",
|
|
963
|
+
fullText: ["title"]
|
|
964
|
+
});
|
|
965
|
+
const seeded = vectorDb(engine).namespace("seeded");
|
|
966
|
+
const rows = seededRows(240);
|
|
967
|
+
await seeded.upsert(rows);
|
|
968
|
+
for (const filter of [
|
|
969
|
+
{ n: { $gte: 30 } },
|
|
970
|
+
{ color: "red" },
|
|
971
|
+
{ color: { $nin: ["red", "blue"] } },
|
|
972
|
+
{ tags: { $containsAny: ["x", "z"] } },
|
|
973
|
+
{ title: { $containsAnyToken: "fox" } },
|
|
974
|
+
{ $or: [{ n: { $lt: 5 } }, { color: { $eq: null } }] },
|
|
975
|
+
{ $not: { tags: { $contains: "y" } } },
|
|
976
|
+
{ title: { $glob: "*o*" } }
|
|
977
|
+
]) {
|
|
978
|
+
const met = rows.filter((row) => meets(filter, row.id, row.metadata));
|
|
979
|
+
const q = [
|
|
980
|
+
.3,
|
|
981
|
+
-.2,
|
|
982
|
+
.9,
|
|
983
|
+
.1
|
|
984
|
+
];
|
|
985
|
+
nearOrder((await seeded.query(q, {
|
|
986
|
+
topK: 10,
|
|
987
|
+
filter
|
|
988
|
+
})).map((m) => m.id), bruteNearest(met, q, 10, true), 10, `query, filter ${JSON.stringify(filter)}`);
|
|
989
|
+
equal((await seeded.list({
|
|
990
|
+
orderBy: [["color", "asc"], ["n", "desc"]],
|
|
991
|
+
filter,
|
|
992
|
+
topK: 15
|
|
993
|
+
})).map((v) => v.id), bruteList(met, [["color", "asc"], ["n", "desc"]], 15), `list, filter ${JSON.stringify(filter)}`);
|
|
994
|
+
equal(await seeded.aggregate({
|
|
995
|
+
aggregates: {
|
|
996
|
+
n: ["count"],
|
|
997
|
+
total: ["sum", "n"]
|
|
998
|
+
},
|
|
999
|
+
groupBy: ["color"],
|
|
1000
|
+
filter
|
|
1001
|
+
}), bruteAggregate(met, "color", "n"), `aggregate, filter ${JSON.stringify(filter)}`);
|
|
1002
|
+
}
|
|
1003
|
+
for (const words of [
|
|
1004
|
+
"quick fox",
|
|
1005
|
+
"lazy dog dog",
|
|
1006
|
+
"brown",
|
|
1007
|
+
"jumps over"
|
|
1008
|
+
]) {
|
|
1009
|
+
const found = await waitBuilt(() => seeded.textSearch(words, {
|
|
1010
|
+
field: "title",
|
|
1011
|
+
topK: 10
|
|
1012
|
+
}));
|
|
1013
|
+
const want = bruteBm25(rows, "title", words, 10);
|
|
1014
|
+
equal(found.map((m) => m.id), want.map((w) => w[0]), `text search "${words}"`);
|
|
1015
|
+
near(found.map((m) => m.score), want.map((w) => w[1]), `text search scores "${words}"`);
|
|
1016
|
+
}
|
|
1017
|
+
const { results, fused } = await seeded.multiQuery([
|
|
1018
|
+
{
|
|
1019
|
+
vector: [
|
|
1020
|
+
1,
|
|
1021
|
+
0,
|
|
1022
|
+
0,
|
|
1023
|
+
0
|
|
1024
|
+
],
|
|
1025
|
+
topK: 8
|
|
1026
|
+
},
|
|
1027
|
+
{
|
|
1028
|
+
textSearch: "quick fox",
|
|
1029
|
+
field: "title",
|
|
1030
|
+
topK: 8
|
|
1031
|
+
},
|
|
1032
|
+
{
|
|
1033
|
+
vector: [
|
|
1034
|
+
0,
|
|
1035
|
+
1,
|
|
1036
|
+
0,
|
|
1037
|
+
1
|
|
1038
|
+
],
|
|
1039
|
+
topK: 8,
|
|
1040
|
+
filter: { color: "red" }
|
|
1041
|
+
}
|
|
1042
|
+
], {
|
|
1043
|
+
fuse: { rrf: {} },
|
|
1044
|
+
topK: 10
|
|
1045
|
+
});
|
|
1046
|
+
const near1 = bruteNearest(rows, [
|
|
1047
|
+
1,
|
|
1048
|
+
0,
|
|
1049
|
+
0,
|
|
1050
|
+
0
|
|
1051
|
+
], 8, true);
|
|
1052
|
+
const near2 = bruteNearest(rows.filter((r) => r.metadata.color === "red"), [
|
|
1053
|
+
0,
|
|
1054
|
+
1,
|
|
1055
|
+
0,
|
|
1056
|
+
1
|
|
1057
|
+
], 8, true);
|
|
1058
|
+
nearOrder(results[0].map((m) => m.id), near1, 8, "multiQuery's first search");
|
|
1059
|
+
equal(results[1].map((m) => m.id), bruteBm25(rows, "title", "quick fox", 8).map((w) => w[0]), "multiQuery's text search");
|
|
1060
|
+
nearOrder(results[2].map((m) => m.id), near2, 8, "multiQuery's third search");
|
|
1061
|
+
equal(fused?.map((m) => m.id), bruteRrf(results.map((r) => r.map((m) => m.id)), 60, [
|
|
1062
|
+
1,
|
|
1063
|
+
1,
|
|
1064
|
+
1
|
|
1065
|
+
], 10), "multiQuery fused");
|
|
1066
|
+
}
|
|
1067
|
+
},
|
|
1068
|
+
{
|
|
1069
|
+
name: "values come back bit for bit as written, a 32-bit float's every digit; with vectorType f16, as a 16-bit float holds them",
|
|
1070
|
+
async run(engine) {
|
|
1071
|
+
await engine.create("exact", {
|
|
1072
|
+
dimension: 3,
|
|
1073
|
+
metric: "cosine"
|
|
1074
|
+
});
|
|
1075
|
+
await engine.create("halves", {
|
|
1076
|
+
dimension: 3,
|
|
1077
|
+
metric: "cosine",
|
|
1078
|
+
vectorType: "f16"
|
|
1079
|
+
});
|
|
1080
|
+
const db = vectorDb(engine);
|
|
1081
|
+
const values = [
|
|
1082
|
+
Math.fround(.1),
|
|
1083
|
+
Math.fround(1 / 3),
|
|
1084
|
+
Math.fround(2 / 3)
|
|
1085
|
+
];
|
|
1086
|
+
equal(values, [
|
|
1087
|
+
.10000000149011612,
|
|
1088
|
+
.3333333432674408,
|
|
1089
|
+
.6666666865348816
|
|
1090
|
+
], "the values written");
|
|
1091
|
+
await db.namespace("exact").upsert([{
|
|
1092
|
+
id: "a",
|
|
1093
|
+
values
|
|
1094
|
+
}, {
|
|
1095
|
+
id: "z",
|
|
1096
|
+
values: [
|
|
1097
|
+
1,
|
|
1098
|
+
-0,
|
|
1099
|
+
0
|
|
1100
|
+
]
|
|
1101
|
+
}]);
|
|
1102
|
+
bits(await db.namespace("exact").fetch(["a", "z"]), [[
|
|
1103
|
+
.10000000149011612,
|
|
1104
|
+
.3333333432674408,
|
|
1105
|
+
.6666666865348816
|
|
1106
|
+
], [
|
|
1107
|
+
1,
|
|
1108
|
+
-0,
|
|
1109
|
+
0
|
|
1110
|
+
]], "kept exactly, -0 its own value");
|
|
1111
|
+
await db.namespace("halves").upsert([{
|
|
1112
|
+
id: "a",
|
|
1113
|
+
values
|
|
1114
|
+
}, {
|
|
1115
|
+
id: "s",
|
|
1116
|
+
values: [
|
|
1117
|
+
1,
|
|
1118
|
+
3 * 2 ** -26,
|
|
1119
|
+
0
|
|
1120
|
+
]
|
|
1121
|
+
}]);
|
|
1122
|
+
bits(await db.namespace("halves").fetch(["a", "s"]), [[
|
|
1123
|
+
.0999755859375,
|
|
1124
|
+
.333251953125,
|
|
1125
|
+
.66650390625
|
|
1126
|
+
], [
|
|
1127
|
+
1,
|
|
1128
|
+
2 ** -24,
|
|
1129
|
+
0
|
|
1130
|
+
]], "kept as 16-bit floats");
|
|
1131
|
+
await fails(engine.create("halves", {
|
|
1132
|
+
dimension: 3,
|
|
1133
|
+
metric: "cosine"
|
|
1134
|
+
}), "conflict", "making it again with exact values");
|
|
1135
|
+
}
|
|
1136
|
+
},
|
|
1137
|
+
{
|
|
1138
|
+
name: "what a caller does to an answer's metadata never changes what is stored",
|
|
1139
|
+
async run(engine) {
|
|
1140
|
+
await engine.create("tags", {
|
|
1141
|
+
dimension: 2,
|
|
1142
|
+
metric: "dot"
|
|
1143
|
+
});
|
|
1144
|
+
const tags = vectorDb(engine).namespace("tags");
|
|
1145
|
+
await tags.upsert([{
|
|
1146
|
+
id: "a",
|
|
1147
|
+
values: [1, 0],
|
|
1148
|
+
metadata: {
|
|
1149
|
+
tags: ["red"],
|
|
1150
|
+
n: 1
|
|
1151
|
+
}
|
|
1152
|
+
}]);
|
|
1153
|
+
const [found] = await tags.query([1, 0], { includeMetadata: true });
|
|
1154
|
+
found.metadata.tags[0] = "blue";
|
|
1155
|
+
const [listed] = await tags.list({
|
|
1156
|
+
orderBy: [["id", "asc"]],
|
|
1157
|
+
includeMetadata: true
|
|
1158
|
+
});
|
|
1159
|
+
listed.metadata.tags.push("green");
|
|
1160
|
+
const [kept] = await tags.fetch(["a"]);
|
|
1161
|
+
equal(kept.metadata, {
|
|
1162
|
+
tags: ["red"],
|
|
1163
|
+
n: 1
|
|
1164
|
+
}, "as written");
|
|
1165
|
+
equal((await tags.query([1, 0], { filter: { tags: { $contains: "red" } } })).map((m) => m.id), ["a"], "met as written");
|
|
1166
|
+
}
|
|
1167
|
+
},
|
|
1168
|
+
{
|
|
1169
|
+
name: "a namespace keeps 16 full-text fields at most, however they were added",
|
|
1170
|
+
async run(engine) {
|
|
1171
|
+
const sixteen = Array.from({ length: 16 }, (_, k) => `f${k}`);
|
|
1172
|
+
await engine.create("wide", {
|
|
1173
|
+
dimension: 2,
|
|
1174
|
+
metric: "dot",
|
|
1175
|
+
fullText: sixteen
|
|
1176
|
+
});
|
|
1177
|
+
await fails(engine.update("wide", { addFullText: ["f16"] }), "invalid", "a seventeenth");
|
|
1178
|
+
await engine.update("wide", { addFullText: ["f3"] });
|
|
1179
|
+
}
|
|
1180
|
+
},
|
|
1181
|
+
{
|
|
1182
|
+
name: "making a namespace again with other full-text fields is a conflict; with the same, nothing to do",
|
|
1183
|
+
async run(engine) {
|
|
1184
|
+
await engine.create("titled", {
|
|
1185
|
+
dimension: 2,
|
|
1186
|
+
metric: "dot",
|
|
1187
|
+
fullText: ["title"]
|
|
1188
|
+
});
|
|
1189
|
+
await engine.create("titled", {
|
|
1190
|
+
dimension: 2,
|
|
1191
|
+
metric: "dot",
|
|
1192
|
+
fullText: ["title"]
|
|
1193
|
+
});
|
|
1194
|
+
await fails(engine.create("titled", {
|
|
1195
|
+
dimension: 2,
|
|
1196
|
+
metric: "dot",
|
|
1197
|
+
fullText: ["body"]
|
|
1198
|
+
}), "conflict", "other fields");
|
|
1199
|
+
await fails(engine.create("titled", {
|
|
1200
|
+
dimension: 2,
|
|
1201
|
+
metric: "dot"
|
|
1202
|
+
}), "conflict", "none");
|
|
1203
|
+
}
|
|
1204
|
+
},
|
|
595
1205
|
{
|
|
596
1206
|
name: "a value beyond 1e17 in size is invalid",
|
|
597
1207
|
async run(engine) {
|
|
@@ -612,6 +1222,168 @@ const vectorContract = [
|
|
|
612
1222
|
}
|
|
613
1223
|
}
|
|
614
1224
|
];
|
|
1225
|
+
/** A text search's answer once its field is built: asked again while it is `unavailable`, for 30 seconds at most. */
|
|
1226
|
+
async function waitBuilt(ask) {
|
|
1227
|
+
for (let tries = 0;; tries++) try {
|
|
1228
|
+
return await ask();
|
|
1229
|
+
} catch (error) {
|
|
1230
|
+
if (!(error instanceof VectorError && error.code === "unavailable") || tries >= 300) throw error;
|
|
1231
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
function seededRows(count) {
|
|
1235
|
+
let seed = 625341585;
|
|
1236
|
+
const next = () => {
|
|
1237
|
+
seed = seed + 1831565813 | 0;
|
|
1238
|
+
let t = Math.imul(seed ^ seed >>> 15, 1 | seed);
|
|
1239
|
+
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
|
|
1240
|
+
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
1241
|
+
};
|
|
1242
|
+
const colors = [
|
|
1243
|
+
"red",
|
|
1244
|
+
"blue",
|
|
1245
|
+
"green",
|
|
1246
|
+
"gold"
|
|
1247
|
+
];
|
|
1248
|
+
const words = [
|
|
1249
|
+
"quick",
|
|
1250
|
+
"brown",
|
|
1251
|
+
"fox",
|
|
1252
|
+
"jumps",
|
|
1253
|
+
"over",
|
|
1254
|
+
"lazy",
|
|
1255
|
+
"dog",
|
|
1256
|
+
"red",
|
|
1257
|
+
"sun"
|
|
1258
|
+
];
|
|
1259
|
+
return Array.from({ length: count }, (_, k) => {
|
|
1260
|
+
const metadata = { n: Math.floor(next() * 50) };
|
|
1261
|
+
if (next() < .85) metadata.color = colors[Math.floor(next() * colors.length)];
|
|
1262
|
+
if (next() < .6) metadata.tags = Array.from({ length: 1 + Math.floor(next() * 3) }, () => [
|
|
1263
|
+
"x",
|
|
1264
|
+
"y",
|
|
1265
|
+
"z",
|
|
1266
|
+
"w"
|
|
1267
|
+
][Math.floor(next() * 4)]);
|
|
1268
|
+
if (next() < .8) metadata.title = Array.from({ length: 2 + Math.floor(next() * 6) }, () => words[Math.floor(next() * words.length)]).join(" ");
|
|
1269
|
+
return {
|
|
1270
|
+
id: `r${String(k).padStart(3, "0")}`,
|
|
1271
|
+
values: Array.from({ length: 4 }, () => Math.fround(next() - .5)),
|
|
1272
|
+
metadata
|
|
1273
|
+
};
|
|
1274
|
+
});
|
|
1275
|
+
}
|
|
1276
|
+
const byIdOrder = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
1277
|
+
const wordsOf = (text) => text.split(/[^\p{Alphabetic}\p{N}]+/u).filter(Boolean).map((w) => w.toLowerCase());
|
|
1278
|
+
function meets(filter, id, metadata) {
|
|
1279
|
+
if ("$and" in filter) return filter.$and.every((f) => meets(f, id, metadata));
|
|
1280
|
+
if ("$or" in filter) return filter.$or.some((f) => meets(f, id, metadata));
|
|
1281
|
+
if ("$not" in filter) return !meets(filter.$not, id, metadata);
|
|
1282
|
+
return Object.entries(filter).every(([field, condition]) => {
|
|
1283
|
+
const value = field === "id" ? id : metadata[field];
|
|
1284
|
+
return (typeof condition === "object" && condition !== null ? Object.entries(condition) : [["$eq", condition]]).every(([op, want]) => {
|
|
1285
|
+
const text = typeof value === "string" ? value : Array.isArray(value) ? value.filter((v) => typeof v === "string").join(" ") : "";
|
|
1286
|
+
switch (op) {
|
|
1287
|
+
case "$eq": return want === null ? value === void 0 : JSON.stringify(value) === JSON.stringify(want);
|
|
1288
|
+
case "$nin": return value === void 0 ? !want.includes(null) : !want.some((w) => JSON.stringify(w) === JSON.stringify(value));
|
|
1289
|
+
case "$gte": return typeof value === typeof want && typeof value !== "object" && value >= want;
|
|
1290
|
+
case "$lt": return typeof value === typeof want && typeof value !== "object" && value < want;
|
|
1291
|
+
case "$contains": return Array.isArray(value) && value.includes(want);
|
|
1292
|
+
case "$containsAny": return Array.isArray(value) && want.some((w) => value.includes(w));
|
|
1293
|
+
case "$containsAnyToken": return value !== void 0 && wordsOf(want).some((w) => wordsOf(text).includes(w));
|
|
1294
|
+
case "$glob": return typeof value === "string" && new RegExp(`^${want.split("*").map((p) => p.replace(/[.+?^${}()|[\]\\]/g, "\\$&")).join(".*")}$`, "su").test(value);
|
|
1295
|
+
default: throw new Error(`the seeded case's brute force has no ${op}`);
|
|
1296
|
+
}
|
|
1297
|
+
});
|
|
1298
|
+
});
|
|
1299
|
+
}
|
|
1300
|
+
function bruteNearest(rows, q, topK, scored) {
|
|
1301
|
+
const norm = (v) => Math.sqrt(v.reduce((s, x) => s + x * x, 0));
|
|
1302
|
+
const cos = (v) => v.reduce((s, x, i) => s + x * q[i], 0) / (norm(v) * norm(q));
|
|
1303
|
+
const all = rows.map((r) => [r.id, cos(r.values)]).sort((a, b) => b[1] - a[1] || byIdOrder(a[0], b[0]));
|
|
1304
|
+
return scored ? all.slice(0, topK + 5) : all.slice(0, topK).map((x) => x[0]);
|
|
1305
|
+
}
|
|
1306
|
+
/**
|
|
1307
|
+
* `got` as brute force's top `topK` (`want`, every row scored) in its order, as many as there are, but where two
|
|
1308
|
+
* scores are within a millionth of each other: an engine scoring in 32-bit floats may order such a near tie either
|
|
1309
|
+
* way, and take either at the cut. Fewer than that many is a failure, none included.
|
|
1310
|
+
*/
|
|
1311
|
+
function nearOrder(got, want, topK, what) {
|
|
1312
|
+
const score = new Map(want);
|
|
1313
|
+
const tie = (a, b) => Math.abs(a - b) <= 1e-6 * Math.max(1, Math.abs(a));
|
|
1314
|
+
const top = want.slice(0, Math.min(topK, want.length));
|
|
1315
|
+
if (got.length !== top.length || got.some((id, i) => {
|
|
1316
|
+
const s = score.get(id);
|
|
1317
|
+
return s === void 0 || !tie(s, top[i][1]) && s !== top[i][1];
|
|
1318
|
+
}) || got.some((id, i) => id !== top[i][0] && !tie(score.get(id), top[i][1]))) throw new Error(`${what}: expected ${JSON.stringify(top.map((w) => w[0]))}, got ${JSON.stringify(got)}`);
|
|
1319
|
+
}
|
|
1320
|
+
function bruteList(rows, orderBy, topK) {
|
|
1321
|
+
const cmp = (a, b) => {
|
|
1322
|
+
for (const [field, way] of orderBy) {
|
|
1323
|
+
const [x, y] = [a.metadata[field], b.metadata[field]];
|
|
1324
|
+
if (x === void 0 || y === void 0) {
|
|
1325
|
+
if (x !== y) return x === void 0 ? 1 : -1;
|
|
1326
|
+
continue;
|
|
1327
|
+
}
|
|
1328
|
+
const c = typeof x === typeof y && typeof x !== "object" ? x < y ? -1 : x > y ? 1 : 0 : 0;
|
|
1329
|
+
if (c) return way === "desc" ? -c : c;
|
|
1330
|
+
}
|
|
1331
|
+
return byIdOrder(a.id, b.id);
|
|
1332
|
+
};
|
|
1333
|
+
return [...rows].sort(cmp).slice(0, topK).map((r) => r.id);
|
|
1334
|
+
}
|
|
1335
|
+
function bruteAggregate(rows, groupBy, summed) {
|
|
1336
|
+
const groups = /* @__PURE__ */ new Map();
|
|
1337
|
+
for (const row of rows) {
|
|
1338
|
+
const key = row.metadata[groupBy] ?? null;
|
|
1339
|
+
const at = groups.get(JSON.stringify(key)) ?? {
|
|
1340
|
+
key,
|
|
1341
|
+
n: 0,
|
|
1342
|
+
total: 0
|
|
1343
|
+
};
|
|
1344
|
+
at.n += 1;
|
|
1345
|
+
at.total += typeof row.metadata[summed] === "number" ? row.metadata[summed] : 0;
|
|
1346
|
+
groups.set(JSON.stringify(key), at);
|
|
1347
|
+
}
|
|
1348
|
+
return [...groups.values()].sort((a, b) => a.key === null ? 1 : b.key === null ? -1 : a.key < b.key ? -1 : 1).map((g) => ({
|
|
1349
|
+
key: { [groupBy]: g.key },
|
|
1350
|
+
aggregates: {
|
|
1351
|
+
n: g.n,
|
|
1352
|
+
total: g.total
|
|
1353
|
+
}
|
|
1354
|
+
}));
|
|
1355
|
+
}
|
|
1356
|
+
function bruteBm25(rows, field, words, topK) {
|
|
1357
|
+
const counted = rows.map((r) => {
|
|
1358
|
+
const v = r.metadata[field];
|
|
1359
|
+
const t = typeof v === "string" ? v : Array.isArray(v) ? v.filter((x) => typeof x === "string").join(" ") : "";
|
|
1360
|
+
const m = /* @__PURE__ */ new Map();
|
|
1361
|
+
for (const w of wordsOf(t)) m.set(w, (m.get(w) ?? 0) + 1);
|
|
1362
|
+
return [r.id, m];
|
|
1363
|
+
});
|
|
1364
|
+
const length = (m) => [...m.values()].reduce((s, x) => s + x, 0);
|
|
1365
|
+
const score = /* @__PURE__ */ new Map();
|
|
1366
|
+
for (const term of [...new Set(wordsOf(words))].sort()) {
|
|
1367
|
+
const having = counted.filter(([, m]) => m.has(term));
|
|
1368
|
+
if (!having.length) continue;
|
|
1369
|
+
const mean = Math.max(having.reduce((s, [, m]) => s + length(m), 0) / having.length, 1);
|
|
1370
|
+
const idf = Math.log(Math.max(rows.length - having.length + .5, .5) / (having.length + .5) + 1);
|
|
1371
|
+
for (const [id, m] of having) {
|
|
1372
|
+
const tf = m.get(term);
|
|
1373
|
+
score.set(id, (score.get(id) ?? 0) + idf * tf * 2.2 / (tf + 1.2 * (.25 + .75 * length(m) / mean)));
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
return [...score].map(([id, s]) => [id, Math.fround(s)]).sort((a, b) => b[1] - a[1] || byIdOrder(a[0], b[0])).slice(0, topK);
|
|
1377
|
+
}
|
|
1378
|
+
function bruteRrf(lists, k, weights, topK) {
|
|
1379
|
+
const score = /* @__PURE__ */ new Map();
|
|
1380
|
+
lists.forEach((list, s) => list.forEach((id, rank) => score.set(id, (score.get(id) ?? 0) + weights[s] / (k + rank + 1))));
|
|
1381
|
+
return [...score].sort((a, b) => b[1] - a[1] || byIdOrder(a[0], b[0])).slice(0, topK).map((x) => x[0]);
|
|
1382
|
+
}
|
|
1383
|
+
/** Each Vector's values `expected`, every bit: -0 is not 0. */
|
|
1384
|
+
function bits(actual, expected, what) {
|
|
1385
|
+
if (!(actual.length === expected.length && actual.every((v, i) => v.values.length === expected[i].length && v.values.every((x, k) => Object.is(x, expected[i][k]))))) throw new Error(`${what}: expected ${JSON.stringify(expected.map((v) => v.map((x) => Object.is(x, -0) ? "-0" : x)))}, got ${JSON.stringify(actual.map((v) => v.values.map((x) => Object.is(x, -0) ? "-0" : x)))}`);
|
|
1386
|
+
}
|
|
615
1387
|
function equal(actual, expected, what) {
|
|
616
1388
|
const a = JSON.stringify(actual);
|
|
617
1389
|
const e = JSON.stringify(expected);
|