| 1 | # Vector embedding search using TransformersJS |
| 2 | |
| 3 | ## Embed and query data from LanceDB using TransformersJS |
| 4 | |
| 5 | <img id="splash" width="400" alt="transformersjs" src="https://github.com/lancedb/lancedb/assets/43097991/88a31e30-3d6f-4eef-9216-4b7c688f1b4f"> |
| 6 | |
| 7 | This example shows how to use the [transformers.js](https://github.com/xenova/transformers.js) library to perform vector embedding search using LanceDB's Javascript API. |
| 8 | |
| 9 | |
| 10 | ### Setting up |
| 11 | First, install the dependencies: |
| 12 | ```bash |
| 13 | npm install vectordb |
| 14 | npm i @xenova/transformers |
| 15 | ``` |
| 16 | |
| 17 | We will also be using the [all-MiniLM-L6-v2](https://huggingface.co/Xenova/all-MiniLM-L6-v2) model to make it compatible with Transformers.js |
| 18 | |
| 19 | Within our `index.js` file we will import the necessary libraries and define our model and database: |
| 20 | |
| 21 | ```javascript |
| 22 | const lancedb = require('vectordb') |
| 23 | const { pipeline } = await import('@xenova/transformers') |
| 24 | const pipe = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2'); |
| 25 | ``` |
| 26 | |
| 27 | ### Creating the embedding function |
| 28 | |
| 29 | Next, we will create a function that will take in a string and return the vector embedding of that string. We will use the `pipe` function we defined earlier to get the vector embedding of the string. |
| 30 | |
| 31 | ```javascript |
| 32 | // Define the function. `sourceColumn` is required for LanceDB to know |
| 33 | // which column to use as input. |
| 34 | const embed_fun = {} |
| 35 | embed_fun.sourceColumn = 'text' |
| 36 | embed_fun.embed = async function (batch) { |
| 37 | let result = [] |
| 38 | // Given a batch of strings, we will use the `pipe` function to get |
| 39 | // the vector embedding of each string. |
| 40 | for (let text of batch) { |
| 41 | // 'mean' pooling and normalizing allows the embeddings to share the |
| 42 | // same length. |
| 43 | const res = await pipe(text, { pooling: 'mean', normalize: true }) |
| 44 | result.push(Array.from(res['data'])) |
| 45 | } |
| 46 | return (result) |
| 47 | } |
| 48 | ``` |
| 49 | |
| 50 | ### Creating the database |
| 51 | |
| 52 | Now, we will create the LanceDB database and add the embedding function we defined earlier. |
| 53 | |
| 54 | ```javascript |
| 55 | // Link a folder and create a table with data |
| 56 | const db = await lancedb.connect('data/sample-lancedb') |
| 57 | |
| 58 | // You can also import any other data, but make sure that you have a column |
| 59 | // for the embedding function to use. |
| 60 | const data = [ |
| 61 | { id: 1, text: 'Cherry', type: 'fruit' }, |
| 62 | { id: 2, text: 'Carrot', type: 'vegetable' }, |
| 63 | { id: 3, text: 'Potato', type: 'vegetable' }, |
| 64 | { id: 4, text: 'Apple', type: 'fruit' }, |
| 65 | { id: 5, text: 'Banana', type: 'fruit' } |
| 66 | ] |
| 67 | |
| 68 | // Create the table with the embedding function |
| 69 | const table = await db.createTable('food_table', data, "create", embed_fun) |
| 70 | ``` |
| 71 | |
| 72 | ### Performing the search |
| 73 | |
| 74 | Now, we can perform the search using the `search` function. LanceDB automatically uses the embedding function we defined earlier to get the vector embedding of the query string. |
| 75 | |
| 76 | ```javascript |
| 77 | // Query the table |
| 78 | const results = await table |
| 79 | .search("a sweet fruit to eat") |
| 80 | .metricType("cosine") |
| 81 | .limit(2) |
| 82 | .execute() |
| 83 | console.log(results.map(r => r.text)) |
| 84 | ``` |
| 85 | ```bash |
| 86 | [ 'Banana', 'Cherry' ] |
| 87 | ``` |
| 88 | |
| 89 | Output of `results`: |
| 90 | ```bash |
| 91 | [ |
| 92 | { |
| 93 | vector: Float32Array(384) [ |
| 94 | -0.057455405592918396, |
| 95 | 0.03617725893855095, |
| 96 | -0.0367760956287384, |
| 97 | ... 381 more items |
| 98 | ], |
| 99 | id: 5, |
| 100 | text: 'Banana', |
| 101 | type: 'fruit', |
| 102 | _distance: 0.4919965863227844 |
| 103 | }, |
| 104 | { |
| 105 | vector: Float32Array(384) [ |
| 106 | 0.0009714411571621895, |
| 107 | 0.008223623037338257, |
| 108 | 0.009571489877998829, |
| 109 | ... 381 more items |
| 110 | ], |
| 111 | id: 1, |
| 112 | text: 'Cherry', |
| 113 | type: 'fruit', |
| 114 | _distance: 0.5540297031402588 |
| 115 | } |
| 116 | ] |
| 117 | ``` |
| 118 | |
| 119 | ### Wrapping it up |
| 120 | |
| 121 | In this example, we showed how to use the `transformers.js` library to perform vector embedding search using LanceDB's Javascript API. You can find the full code for this example on [Github](https://github.com/lancedb/lancedb/blob/main/node/examples/js-transformers/index.js)! |