juliuslipp
commited on
Commit
•
654ef12
1
Parent(s):
d8e18fd
Update README.md
Browse files
README.md
CHANGED
@@ -56,6 +56,71 @@ documents = [
|
|
56 |
results = model.rank(query, documents, return_documents=True, top_k=3)
|
57 |
```
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
## Using API
|
60 |
|
61 |
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!
|
|
|
56 |
results = model.rank(query, documents, return_documents=True, top_k=3)
|
57 |
```
|
58 |
|
59 |
+
<details>
|
60 |
+
<summary>JavaScript Example</summary>
|
61 |
+
|
62 |
+
Currently, the best way to use our models is with the most recent version of sentence-transformers.
|
63 |
+
|
64 |
+
`npm i @xenova/transformers`
|
65 |
+
|
66 |
+
Let's say you have a query, and you want to rerank a set of documents. In JavaScript, you need to add a function:
|
67 |
+
|
68 |
+
```javascript
|
69 |
+
import { AutoTokenizer, AutoModelForSequenceClassification } from '@xenova/transformers';
|
70 |
+
|
71 |
+
const model_id = 'mixedbread-ai/mxbai-rerank-xsmall-v1';
|
72 |
+
const model = await AutoModelForSequenceClassification.from_pretrained(model_id);
|
73 |
+
const tokenizer = await AutoTokenizer.from_pretrained(model_id);
|
74 |
+
|
75 |
+
/**
|
76 |
+
* Performs ranking with the CrossEncoder on the given query and documents. Returns a sorted list with the document indices and scores.
|
77 |
+
* @param {string} query A single query
|
78 |
+
* @param {string[]} documents A list of documents
|
79 |
+
* @param {Object} options Options for ranking
|
80 |
+
* @param {number} [options.top_k=undefined] Return the top-k documents. If undefined, all documents are returned.
|
81 |
+
* @param {number} [options.return_documents=false] If true, also returns the documents. If false, only returns the indices and scores.
|
82 |
+
*/
|
83 |
+
async function rank(query, documents, {
|
84 |
+
top_k = undefined,
|
85 |
+
return_documents = false,
|
86 |
+
} = {}) {
|
87 |
+
const inputs = tokenizer(
|
88 |
+
new Array(documents.length).fill(query),
|
89 |
+
{
|
90 |
+
text_pair: documents,
|
91 |
+
padding: true,
|
92 |
+
truncation: true,
|
93 |
+
}
|
94 |
+
)
|
95 |
+
const { logits } = await model(inputs);
|
96 |
+
return logits
|
97 |
+
.sigmoid()
|
98 |
+
.tolist()
|
99 |
+
.map(([score], i) => ({
|
100 |
+
corpus_id: i,
|
101 |
+
score,
|
102 |
+
...(return_documents ? { text: documents[i] } : {})
|
103 |
+
}))
|
104 |
+
.sort((a, b) => b.score - a.score)
|
105 |
+
.slice(0, top_k);
|
106 |
+
}
|
107 |
+
|
108 |
+
// Example usage:
|
109 |
+
const query = "Who wrote 'To Kill a Mockingbird'?"
|
110 |
+
const documents = [
|
111 |
+
"'To Kill a Mockingbird' is a novel by Harper Lee published in 1960. It was immediately successful, winning the Pulitzer Prize, and has become a classic of modern American literature.",
|
112 |
+
"The novel 'Moby-Dick' was written by Herman Melville and first published in 1851. It is considered a masterpiece of American literature and deals with complex themes of obsession, revenge, and the conflict between good and evil.",
|
113 |
+
"Harper Lee, an American novelist widely known for her novel 'To Kill a Mockingbird', was born in 1926 in Monroeville, Alabama. She received the Pulitzer Prize for Fiction in 1961.",
|
114 |
+
"Jane Austen was an English novelist known primarily for her six major novels, which interpret, critique and comment upon the British landed gentry at the end of the 18th century.",
|
115 |
+
"The 'Harry Potter' series, which consists of seven fantasy novels written by British author J.K. Rowling, is among the most popular and critically acclaimed books of the modern era.",
|
116 |
+
"'The Great Gatsby', a novel written by American author F. Scott Fitzgerald, was published in 1925. The story is set in the Jazz Age and follows the life of millionaire Jay Gatsby and his pursuit of Daisy Buchanan."
|
117 |
+
]
|
118 |
+
|
119 |
+
const results = await rank(query, documents, { return_documents: true, top_k: 3 });
|
120 |
+
console.log(results);
|
121 |
+
```
|
122 |
+
</details>
|
123 |
+
|
124 |
## Using API
|
125 |
|
126 |
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!
|