numb3r3 Xenova HF staff commited on
Commit
48ce9e5
1 Parent(s): 9c054fe

Add transformers.js example code (#6)

Browse files

- Add transformers.js example code (bbba93430bde5319fdfcfee8079796f9c44603dd)


Co-authored-by: Joshua <Xenova@users.noreply.huggingface.co>

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>
@@ -126,6 +127,66 @@ sentence_pairs = [[query, doc] for doc in documents]
126
  scores = model.compute_score(sentence_pairs)
127
  ```
128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  That's it! You can now use the `jina-reranker-v1-tiny-en` model in your projects.
130
 
131
  # Evaluation
@@ -152,4 +213,4 @@ For more details, please refer to our [benchmarking sheets](https://docs.google.
152
 
153
  # Contact
154
 
155
- 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>
 
127
  scores = model.compute_score(sentence_pairs)
128
  ```
129
 
130
+ 4. You can also use the `transformers.js` library to run the model directly in JavaScript (in-browser, Node.js, Deno, etc.)!
131
+
132
+ 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:
133
+ ```bash
134
+ npm i @xenova/transformers
135
+ ```
136
+
137
+ Then, you can use the following code to interact with the model:
138
+ ```js
139
+ import { AutoTokenizer, AutoModelForSequenceClassification } from '@xenova/transformers';
140
+
141
+ const model_id = 'jinaai/jina-reranker-v1-tiny-en';
142
+ const model = await AutoModelForSequenceClassification.from_pretrained(model_id, { quantized: false });
143
+ const tokenizer = await AutoTokenizer.from_pretrained(model_id);
144
+
145
+ /**
146
+ * Performs ranking with the CrossEncoder on the given query and documents. Returns a sorted list with the document indices and scores.
147
+ * @param {string} query A single query
148
+ * @param {string[]} documents A list of documents
149
+ * @param {Object} options Options for ranking
150
+ * @param {number} [options.top_k=undefined] Return the top-k documents. If undefined, all documents are returned.
151
+ * @param {number} [options.return_documents=false] If true, also returns the documents. If false, only returns the indices and scores.
152
+ */
153
+ async function rank(query, documents, {
154
+ top_k = undefined,
155
+ return_documents = false,
156
+ } = {}) {
157
+ const inputs = tokenizer(
158
+ new Array(documents.length).fill(query),
159
+ { text_pair: documents, padding: true, truncation: true }
160
+ )
161
+ const { logits } = await model(inputs);
162
+ return logits.sigmoid().tolist()
163
+ .map(([score], i) => ({
164
+ corpus_id: i,
165
+ score,
166
+ ...(return_documents ? { text: documents[i] } : {})
167
+ })).sort((a, b) => b.score - a.score).slice(0, top_k);
168
+ }
169
+
170
+ // Example usage:
171
+ const query = "Organic skincare products for sensitive skin"
172
+ const documents = [
173
+ "Eco-friendly kitchenware for modern homes",
174
+ "Biodegradable cleaning supplies for eco-conscious consumers",
175
+ "Organic cotton baby clothes for sensitive skin",
176
+ "Natural organic skincare range for sensitive skin",
177
+ "Tech gadgets for smart homes: 2024 edition",
178
+ "Sustainable gardening tools and compost solutions",
179
+ "Sensitive skin-friendly facial cleansers and toners",
180
+ "Organic food wraps and storage solutions",
181
+ "All-natural pet food for dogs with allergies",
182
+ "Yoga mats made from recycled materials",
183
+ ]
184
+
185
+ const results = await rank(query, documents, { return_documents: true, top_k: 3 });
186
+ console.log(results);
187
+ ```
188
+
189
+
190
  That's it! You can now use the `jina-reranker-v1-tiny-en` model in your projects.
191
 
192
  # Evaluation
 
213
 
214
  # Contact
215
 
216
+ Join our [Discord community](https://discord.jina.ai/) and chat with other community members about ideas.