Skip to content

Commit c185015

Browse files
authored
docs: Add reranking example (#243)
* docs: Add reranking example * chore: Added top_n to the example
1 parent 98cde7f commit c185015

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,29 @@ embeddings = get_embeddings(input_texts, model='togethercomputer/m2-bert-80M-8k-
209209
print(embeddings)
210210
```
211211

212+
### Reranking
213+
214+
```python
215+
from typing import List
216+
from together import Together
217+
218+
client = Together(api_key=os.environ.get("TOGETHER_API_KEY"))
219+
220+
def get_reranked_documents(query: str, documents: List[str], model: str, top_n: int = 3) -> List[str]:
221+
outputs = client.rerank.create(model=model, query=query, documents=documents, top_n=top_n)
222+
# sort by relevance score and returns the original docs
223+
return [documents[i] for i in [x.index for x in sorted(outputs.results, key=lambda x: x.relevance_score, reverse=True)]]
224+
225+
query = "What is the capital of the United States?"
226+
documents = ["New York","Washington, D.C.", "Los Angeles"]
227+
228+
reranked_documents = get_reranked_documents(query, documents, model='Salesforce/Llama-Rank-V1', top_n=1)
229+
230+
print(reranked_documents)
231+
```
232+
233+
Read more about Reranking [here](https://docs.together.ai/docs/rerank-overview).
234+
212235
### Files
213236

214237
The files API is used for fine-tuning and allows developers to upload data to fine-tune on. It also has several methods to list all files, retrive files, and delete files. Please refer to our fine-tuning docs [here](https://docs.together.ai/docs/fine-tuning-python).

0 commit comments

Comments
 (0)