/rerank
Tipp
LiteLLM folgt der cohere API Anfrage / Antwort fĂĽr die rerank API
LiteLLM Python SDK Verwendung​
Schnellstart​
from litellm import rerank
import os
os.environ["COHERE_API_KEY"] = "sk-.."
query = "What is the capital of the United States?"
documents = [
"Carson City is the capital city of the American state of Nevada.",
"The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.",
"Washington, D.C. is the capital of the United States.",
"Capital punishment has existed in the United States since before it was a country.",
]
response = rerank(
model="cohere/rerank-english-v3.0",
query=query,
documents=documents,
top_n=3,
)
print(response)
Asynchrone Verwendung​
from litellm import arerank
import os, asyncio
os.environ["COHERE_API_KEY"] = "sk-.."
async def test_async_rerank():
query = "What is the capital of the United States?"
documents = [
"Carson City is the capital city of the American state of Nevada.",
"The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.",
"Washington, D.C. is the capital of the United States.",
"Capital punishment has existed in the United States since before it was a country.",
]
response = await arerank(
model="cohere/rerank-english-v3.0",
query=query,
documents=documents,
top_n=3,
)
print(response)
asyncio.run(test_async_rerank())
LiteLLM Proxy Verwendung​
LiteLLM bietet einen Cohere-API-kompatiblen /rerank-Endpunkt fĂĽr Rerank-Aufrufe.
Einrichtung
FĂĽgen Sie dies Ihrer LiteLLM Proxy config.yaml hinzu
model_list:
- model_name: Salesforce/Llama-Rank-V1
litellm_params:
model: together_ai/Salesforce/Llama-Rank-V1
api_key: os.environ/TOGETHERAI_API_KEY
- model_name: rerank-english-v3.0
litellm_params:
model: cohere/rerank-english-v3.0
api_key: os.environ/COHERE_API_KEY
LiteLLM starten
litellm --config /path/to/config.yaml
# RUNNING on http://0.0.0.0:4000
Testanfrage
curl http://0.0.0.0:4000/rerank \
-H "Authorization: Bearer sk-1234" \
-H "Content-Type: application/json" \
-d '{
"model": "rerank-english-v3.0",
"query": "What is the capital of the United States?",
"documents": [
"Carson City is the capital city of the American state of Nevada.",
"The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.",
"Washington, D.C. is the capital of the United States.",
"Capital punishment has existed in the United States since before it was a country."
],
"top_n": 3
}'
Unterstützte Anbieter​
| Anbieter | Link zur Verwendung |
|---|---|
| Cohere (v1 + v2 Clients) | Verwendung |
| Together AI | Verwendung |
| Azure AI | Verwendung |
| Jina AI | Verwendung |
| AWS Bedrock | Verwendung |
| Infinity | Verwendung |