Add Transformers.js sample code
Browse files
README.md
CHANGED
|
@@ -9173,6 +9173,35 @@ for query, query_scores in zip(queries, scores):
|
|
| 9173 |
print(score, document)
|
| 9174 |
```
|
| 9175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9176 |
|
| 9177 |
## Contact
|
| 9178 |
|
|
|
|
| 9173 |
print(score, document)
|
| 9174 |
```
|
| 9175 |
|
| 9176 |
+
### Using Huggingface Transformers.js
|
| 9177 |
+
|
| 9178 |
+
If you haven't already, you can install the [Transformers.js](https://huggingface.co/docs/transformers.js) JavaScript library from [NPM](https://www.npmjs.com/package/@huggingface/transformers) using:
|
| 9179 |
+
```bash
|
| 9180 |
+
npm i @huggingface/transformers
|
| 9181 |
+
```
|
| 9182 |
+
|
| 9183 |
+
You can then use the model for retrieval, as follows:
|
| 9184 |
+
|
| 9185 |
+
```js
|
| 9186 |
+
import { pipeline, dot } from '@huggingface/transformers';
|
| 9187 |
+
|
| 9188 |
+
// Create feature extraction pipeline
|
| 9189 |
+
const extractor = await pipeline('feature-extraction', 'Snowflake/snowflake-arctic-embed-m-v2.0');
|
| 9190 |
+
|
| 9191 |
+
// Generate sentence embeddings
|
| 9192 |
+
const sentences = [
|
| 9193 |
+
'query: what is snowflake?',
|
| 9194 |
+
'The Data Cloud!',
|
| 9195 |
+
'Mexico City of Course!',
|
| 9196 |
+
]
|
| 9197 |
+
const output = await extractor(sentences, { normalize: true, pooling: 'cls' });
|
| 9198 |
+
|
| 9199 |
+
// Compute similarity scores
|
| 9200 |
+
const [source_embeddings, ...document_embeddings ] = output.tolist();
|
| 9201 |
+
const similarities = document_embeddings.map(x => dot(source_embeddings, x));
|
| 9202 |
+
console.log(similarities); // [0.32719788157046004, 0.06960141111667434]
|
| 9203 |
+
```
|
| 9204 |
+
|
| 9205 |
|
| 9206 |
## Contact
|
| 9207 |
|