docs: Add FastEmbed usage example

#11
Files changed (1) hide show
  1. README.md +33 -0
README.md CHANGED
@@ -8,6 +8,7 @@ tags:
8
  - arctic
9
  - snowflake-arctic-embed
10
  - transformers.js
 
11
  model-index:
12
  - name: snowflake-snowflake-arctic-embed-xs
13
  results:
@@ -3049,6 +3050,38 @@ const similarities = document_embeddings.map(x => dot(source_embeddings, x));
3049
  console.log(similarities); // [0.5044895661144148, 0.5636021124426508]
3050
  ```
3051
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3052
  ## FAQ
3053
 
3054
 
 
8
  - arctic
9
  - snowflake-arctic-embed
10
  - transformers.js
11
+ - fastembed
12
  model-index:
13
  - name: snowflake-snowflake-arctic-embed-xs
14
  results:
 
3050
  console.log(similarities); // [0.5044895661144148, 0.5636021124426508]
3051
  ```
3052
 
3053
+ ### Using FastEmbed
3054
+
3055
+ You can also use [FastEmbed](https://github.com/qdrant/fastembed), a lightweight, fast, Python library built for embedding generation. It runs the ONNX export of the model. Like with transformers, add the query prefix to the queries.
3056
+
3057
+ ```
3058
+ pip install fastembed
3059
+ ```
3060
+
3061
+ ```python
3062
+ import numpy as np
3063
+ from fastembed import TextEmbedding
3064
+
3065
+ model = TextEmbedding("Snowflake/snowflake-arctic-embed-xs")
3066
+
3067
+ query_prefix = 'Represent this sentence for searching relevant passages: '
3068
+ queries = ['what is snowflake?', 'Where can I get the best tacos?']
3069
+ documents = ['The Data Cloud!', 'Mexico City of Course!']
3070
+
3071
+ query_embeddings = np.array(list(model.query_embed([query_prefix + q for q in queries])))
3072
+ document_embeddings = np.array(list(model.embed(documents)))
3073
+
3074
+ scores = query_embeddings @ document_embeddings.T
3075
+ for query, query_scores in zip(queries, scores):
3076
+ doc_score_pairs = list(zip(documents, query_scores))
3077
+ doc_score_pairs = sorted(doc_score_pairs, key=lambda x: x[1], reverse=True)
3078
+ # Output passages & scores
3079
+ print("Query:", query)
3080
+ for document, score in doc_score_pairs:
3081
+ print(score, document)
3082
+ ```
3083
+
3084
+
3085
  ## FAQ
3086
 
3087