Elffuss BraidHub

kikocis/lancedb public

main · 1.4 KiBRawPermalink
1// Copyright 2023 Lance Developers.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15'use strict'
16
17async function example () {
18 const lancedb = require('vectordb')
19 // You need to provide an OpenAI API key, here we read it from the OPENAI_API_KEY environment variable
20 const apiKey = process.env.OPENAI_API_KEY
21 // The embedding function will create embeddings for the 'text' column(text in this case)
22 const embedding = new lancedb.OpenAIEmbeddingFunction('text', apiKey)
23
24 const db = await lancedb.connect('data/sample-lancedb')
25
26 const data = [
27 { id: 1, text: 'Black T-Shirt', price: 10 },
28 { id: 2, text: 'Leather Jacket', price: 50 }
29 ]
30
31 const table = await db.createTable('vectors', data, embedding)
32 console.log(await db.tableNames())
33
34 const results = await table
35 .search('keeps me warm')
36 .limit(1)
37 .execute()
38 console.log(results[0].text)
39}
40
41example().then(_ => { console.log('All done!') })