Xenova HF staff commited on
Commit
104f505
1 Parent(s): 04f011c

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +61 -0
README.md CHANGED
@@ -127,6 +127,67 @@ pip install flash-attn --no-build-isolation
127
  ```
128
  Enjoy the 3x-6x speedup with flash attention! ⚡️⚡️⚡️
129
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  That's it! You can now use the `jina-reranker-v2-base-multilingual` model in your projects.
131
 
132
 
 
127
  ```
128
  Enjoy the 3x-6x speedup with flash attention! ⚡️⚡️⚡️
129
 
130
+
131
+ 3. You can also use the `transformers.js` library to run the model directly in JavaScript (in-browser, Node.js, Deno, etc.)!
132
+
133
+ If you haven't already, you can install the [Transformers.js](https://huggingface.co/docs/transformers.js) JavaScript library (v3) using:
134
+ ```bash
135
+ npm i xenova/transformers.js#v3
136
+ ```
137
+
138
+ Then, you can use the following code to interact with the model:
139
+ ```js
140
+ import { AutoTokenizer, AutoModelForSequenceClassification } from '@xenova/transformers';
141
+
142
+ const model_id = 'jinaai/jina-reranker-v1-turbo-en';
143
+ const model = await AutoModelForSequenceClassification.from_pretrained(model_id, { dtype: 'fp32' });
144
+ const tokenizer = await AutoTokenizer.from_pretrained(model_id);
145
+
146
+ /**
147
+ * Performs ranking with the CrossEncoder on the given query and documents. Returns a sorted list with the document indices and scores.
148
+ * @param {string} query A single query
149
+ * @param {string[]} documents A list of documents
150
+ * @param {Object} options Options for ranking
151
+ * @param {number} [options.top_k=undefined] Return the top-k documents. If undefined, all documents are returned.
152
+ * @param {number} [options.return_documents=false] If true, also returns the documents. If false, only returns the indices and scores.
153
+ */
154
+ async function rank(query, documents, {
155
+ top_k = undefined,
156
+ return_documents = false,
157
+ } = {}) {
158
+ const inputs = tokenizer(
159
+ new Array(documents.length).fill(query),
160
+ { text_pair: documents, padding: true, truncation: true }
161
+ )
162
+ const { logits } = await model(inputs);
163
+ return logits.sigmoid().tolist()
164
+ .map(([score], i) => ({
165
+ corpus_id: i,
166
+ score,
167
+ ...(return_documents ? { text: documents[i] } : {})
168
+ })).sort((a, b) => b.score - a.score).slice(0, top_k);
169
+ }
170
+
171
+ // Example usage:
172
+ const query = "Organic skincare products for sensitive skin"
173
+ const documents = [
174
+ "Organic skincare for sensitive skin with aloe vera and chamomile.",
175
+ "New makeup trends focus on bold colors and innovative techniques",
176
+ "Bio-Hautpflege für empfindliche Haut mit Aloe Vera und Kamille",
177
+ "Neue Make-up-Trends setzen auf kräftige Farben und innovative Techniken",
178
+ "Cuidado de la piel orgánico para piel sensible con aloe vera y manzanilla",
179
+ "Las nuevas tendencias de maquillaje se centran en colores vivos y técnicas innovadoras",
180
+ "针对敏感肌专门设计的天然有机护肤产品",
181
+ "新的化妆趋势注重鲜艳的颜色和创新的技巧",
182
+ "敏感肌のために特別に設計された天然有機スキンケア製品",
183
+ "新しいメイクのトレンドは鮮やかな色と革新的な技術に焦点を当てています",
184
+ ]
185
+
186
+ const results = await rank(query, documents, { return_documents: true, top_k: 3 });
187
+ console.log(results);
188
+ ```
189
+
190
+
191
  That's it! You can now use the `jina-reranker-v2-base-multilingual` model in your projects.
192
 
193