Xenova HF staff commited on
Commit
affc1c9
1 Parent(s): 7a055cb

Add transformers.js sample code + tags

Browse files
Files changed (1) hide show
  1. README.md +27 -0
README.md CHANGED
@@ -1,6 +1,7 @@
1
  ---
2
  tags:
3
  - mteb
 
4
  model-index:
5
  - name: mxbai-angle-large-v1
6
  results:
@@ -2703,6 +2704,32 @@ similarities = cos_sim(embeddings[0], embeddings[1:])
2703
  print('similarities:', similarities)
2704
  ```
2705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2706
  ### Using API
2707
 
2708
  You’ll be able to use the models through our API as well. The API is coming soon and will have some exciting features. Stay tuned!
 
1
  ---
2
  tags:
3
  - mteb
4
+ - transformers.js
5
  model-index:
6
  - name: mxbai-angle-large-v1
7
  results:
 
2704
  print('similarities:', similarities)
2705
  ```
2706
 
2707
+ ### Transformers.js
2708
+
2709
+ ```js
2710
+ import { pipeline, cos_sim } from '@xenova/transformers';
2711
+
2712
+ // Create a feature extraction pipeline
2713
+ const extractor = await pipeline('feature-extraction', 'mixedbread-ai/mxbai-embed-large-v1', {
2714
+ quantized: false, // Comment out this line to use the quantized version
2715
+ });
2716
+
2717
+ // Generate sentence embeddings
2718
+ const docs = [
2719
+ 'Represent this sentence for searching relevant passages: A man is eating a piece of bread',
2720
+ 'A man is eating food.',
2721
+ 'A man is eating pasta.',
2722
+ 'The girl is carrying a baby.',
2723
+ 'A man is riding a horse.',
2724
+ ]
2725
+ const output = await extractor(docs, { pooling: 'cls' });
2726
+
2727
+ // Compute similarity scores
2728
+ const [source_embeddings, ...document_embeddings ] = output.tolist();
2729
+ const similarities = document_embeddings.map(x => cos_sim(source_embeddings, x));
2730
+ console.log(similarities); // [0.7919578577247139, 0.6369278664248345, 0.16512018371357193, 0.3620778366720027]
2731
+ ```
2732
+
2733
  ### Using API
2734
 
2735
  You’ll be able to use the models through our API as well. The API is coming soon and will have some exciting features. Stay tuned!