Add transformers.js example code

#6
by Xenova HF staff - opened
Files changed (1) hide show
  1. README.md +65 -4
README.md CHANGED
@@ -2,10 +2,11 @@
2
  library_name: transformers
3
  license: apache-2.0
4
  language:
5
- - en
6
  tags:
7
- - reranker
8
- - cross-encoder
 
9
  ---
10
 
11
  <br><br>
@@ -127,6 +128,66 @@ sentence_pairs = [[query, doc] for doc in documents]
127
  scores = model.compute_score(sentence_pairs)
128
  ```
129
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  That's it! You can now use the `jina-reranker-v1-tiny-en` model in your projects.
131
 
132
  # Evaluation
@@ -153,4 +214,4 @@ For more details, please refer to our [benchmarking sheets](https://docs.google.
153
 
154
  # Contact
155
 
156
- Join our [Discord community](https://discord.jina.ai/) and chat with other community members about ideas.
 
2
  library_name: transformers
3
  license: apache-2.0
4
  language:
5
+ - en
6
  tags:
7
+ - reranker
8
+ - cross-encoder
9
+ - transformers.js
10
  ---
11
 
12
  <br><br>
 
128
  scores = model.compute_score(sentence_pairs)
129
  ```
130
 
131
+ 4. 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 from [NPM](https://www.npmjs.com/package/@xenova/transformers) using:
134
+ ```bash
135
+ npm i @xenova/transformers
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-tiny-en';
143
+ const model = await AutoModelForSequenceClassification.from_pretrained(model_id, { quantized: false });
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
+ "Eco-friendly kitchenware for modern homes",
175
+ "Biodegradable cleaning supplies for eco-conscious consumers",
176
+ "Organic cotton baby clothes for sensitive skin",
177
+ "Natural organic skincare range for sensitive skin",
178
+ "Tech gadgets for smart homes: 2024 edition",
179
+ "Sustainable gardening tools and compost solutions",
180
+ "Sensitive skin-friendly facial cleansers and toners",
181
+ "Organic food wraps and storage solutions",
182
+ "All-natural pet food for dogs with allergies",
183
+ "Yoga mats made from recycled materials",
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-v1-tiny-en` model in your projects.
192
 
193
  # Evaluation
 
214
 
215
  # Contact
216
 
217
+ Join our [Discord community](https://discord.jina.ai/) and chat with other community members about ideas.