Elffuss BraidHub

kikocis/lancedb public

main · 5.1 KiBRawPermalink
1import * as apiArrow from "apache-arrow";
2// Copyright 2024 Lance Developers.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15import * as arrow13 from "apache-arrow-13";
16import * as arrow14 from "apache-arrow-14";
17import * as arrow15 from "apache-arrow-15";
18import * as arrow16 from "apache-arrow-16";
19import * as arrow17 from "apache-arrow-17";
20
21import * as tmp from "tmp";
22
23import { connect } from "../lancedb";
24import { EmbeddingFunction, LanceSchema } from "../lancedb/embedding";
25import { getRegistry, register } from "../lancedb/embedding/registry";
26
27describe.each([arrow13, arrow14, arrow15, arrow16, arrow17])(
28 "LanceSchema",
29 (arrow) => {
30 test("should preserve input order", async () => {
31 const schema = LanceSchema({
32 id: new arrow.Int32(),
33 text: new arrow.Utf8(),
34 vector: new arrow.Float32(),
35 });
36 expect(schema.fields.map((x) => x.name)).toEqual([
37 "id",
38 "text",
39 "vector",
40 ]);
41 });
42 },
43);
44
45describe.each([arrow13, arrow14, arrow15, arrow16, arrow17])(
46 "Registry",
47 (arrow) => {
48 let tmpDir: tmp.DirResult;
49 beforeEach(() => {
50 tmpDir = tmp.dirSync({ unsafeCleanup: true });
51 });
52
53 afterEach(() => {
54 tmpDir.removeCallback();
55 getRegistry().reset();
56 });
57
58 it("should register a new item to the registry", async () => {
59 @register("mock-embedding")
60 class MockEmbeddingFunction extends EmbeddingFunction<string> {
61 toJSON(): object {
62 return {
63 someText: "hello",
64 };
65 }
66 constructor() {
67 super();
68 }
69 ndims() {
70 return 3;
71 }
72 embeddingDataType() {
73 return new arrow.Float32() as apiArrow.Float;
74 }
75 async computeSourceEmbeddings(data: string[]) {
76 return data.map(() => [1, 2, 3]);
77 }
78 }
79
80 const func = getRegistry()
81 .get<MockEmbeddingFunction>("mock-embedding")!
82 .create();
83
84 const schema = LanceSchema({
85 id: new arrow.Int32(),
86 text: func.sourceField(new arrow.Utf8() as apiArrow.DataType),
87 vector: func.vectorField(),
88 });
89
90 const db = await connect(tmpDir.name);
91 const table = await db.createTable(
92 "test",
93 [
94 { id: 1, text: "hello" },
95 { id: 2, text: "world" },
96 ],
97 { schema },
98 );
99 const expected = [
100 [1, 2, 3],
101 [1, 2, 3],
102 ];
103 const actual = await table.query().toArrow();
104 const vectors = actual.getChild("vector")!.toArray();
105 expect(JSON.parse(JSON.stringify(vectors))).toEqual(
106 JSON.parse(JSON.stringify(expected)),
107 );
108 });
109 test("should error if registering with the same name", async () => {
110 class MockEmbeddingFunction extends EmbeddingFunction<string> {
111 toJSON(): object {
112 return {
113 someText: "hello",
114 };
115 }
116 constructor() {
117 super();
118 }
119 ndims() {
120 return 3;
121 }
122 embeddingDataType() {
123 return new arrow.Float32() as apiArrow.Float;
124 }
125 async computeSourceEmbeddings(data: string[]) {
126 return data.map(() => [1, 2, 3]);
127 }
128 }
129 register("mock-embedding")(MockEmbeddingFunction);
130 expect(() => register("mock-embedding")(MockEmbeddingFunction)).toThrow(
131 'Embedding function with alias "mock-embedding" already exists',
132 );
133 });
134 test("schema should contain correct metadata", async () => {
135 class MockEmbeddingFunction extends EmbeddingFunction<string> {
136 toJSON(): object {
137 return {
138 someText: "hello",
139 };
140 }
141 constructor() {
142 super();
143 }
144 ndims() {
145 return 3;
146 }
147 embeddingDataType() {
148 return new arrow.Float32() as apiArrow.Float;
149 }
150 async computeSourceEmbeddings(data: string[]) {
151 return data.map(() => [1, 2, 3]);
152 }
153 }
154 const func = new MockEmbeddingFunction();
155
156 const schema = LanceSchema({
157 id: new arrow.Int32(),
158 text: func.sourceField(new arrow.Utf8() as apiArrow.DataType),
159 vector: func.vectorField(),
160 });
161 const expectedMetadata = new Map<string, string>([
162 [
163 "embedding_functions",
164 JSON.stringify([
165 {
166 sourceColumn: "text",
167 vectorColumn: "vector",
168 name: "MockEmbeddingFunction",
169 model: { someText: "hello" },
170 },
171 ]),
172 ],
173 ]);
174 expect(schema.metadata).toEqual(expectedMetadata);
175 });
176 },
177);