Python client for Together's Cloud Platform providing comprehensive AI model APIs
Document relevance scoring and reordering for improved search and retrieval results. Uses specialized reranking models to optimize document ordering based on query relevance, enhancing search quality for information retrieval systems.
Reorder documents based on relevance to a query using specialized reranking models.
def create(
model: str,
query: str,
documents: Union[List[str], List[Dict[str, Any]]],
top_n: Optional[int] = None,
return_documents: bool = False,
rank_fields: Optional[List[str]] = None,
**kwargs
) -> RerankResponse:
"""
Rerank documents based on query relevance.
Args:
model: Reranking model identifier
query: Search query for relevance comparison
documents: List of documents to rerank (strings or objects)
top_n: Number of top documents to return
return_documents: Include original documents in response
rank_fields: Fields to rank when documents are objects
Returns:
RerankResponse with relevance scores and rankings
"""Asynchronous document reranking for concurrent processing.
async def create(
model: str,
query: str,
documents: Union[List[str], List[Dict[str, Any]]],
**kwargs
) -> RerankResponse:
"""
Asynchronously rerank documents.
Returns:
RerankResponse with relevance scores and rankings
"""from together import Together
client = Together()
query = "What is the capital of the United States?"
documents = [
"New York is the most populous city in the United States.",
"Washington, D.C. is the capital of the United States.",
"Los Angeles is known for its entertainment industry.",
"The United States has 50 states and a federal district.",
"Chicago is the third-largest city in the United States."
]
response = client.rerank.create(
model="Salesforce/Llama-Rank-V1",
query=query,
documents=documents,
top_n=3
)
print("Reranked documents by relevance:")
for result in response.results:
print(f"Rank {result.index + 1}: {documents[result.index]}")
print(f"Relevance score: {result.relevance_score:.4f}")
print("---")# Documents with metadata
documents = [
{
"title": "Python Programming Basics",
"content": "Python is a high-level programming language known for its simplicity.",
"category": "programming"
},
{
"title": "Machine Learning Introduction",
"content": "Machine learning is a subset of artificial intelligence.",
"category": "ai"
},
{
"title": "Data Science with Python",
"content": "Python is widely used in data science for analysis and visualization.",
"category": "data-science"
}
]
query = "Python programming for beginners"
response = client.rerank.create(
model="Salesforce/Llama-Rank-V1",
query=query,
documents=documents,
top_n=2,
return_documents=True,
rank_fields=["title", "content"]
)
print("Top relevant documents:")
for result in response.results:
doc = documents[result.index]
print(f"Title: {doc['title']}")
print(f"Relevance: {result.relevance_score:.4f}")
print(f"Content: {doc['content'][:100]}...")
print("---")class RerankRequest:
model: str
query: str
documents: Union[List[str], List[Dict[str, Any]]]
top_n: Optional[int] = None
return_documents: bool = False
rank_fields: Optional[List[str]] = Noneclass RerankResponse:
id: str
results: List[RerankResult]
meta: RerankMeta
class RerankResult:
index: int
relevance_score: float
document: Optional[Dict[str, Any]] = None
class RerankMeta:
api_version: Dict[str, str]
billed_units: Dict[str, int]Salesforce/Llama-Rank-V1 - High-quality reranking modelBAAI/bge-reranker-large - BGE reranking modelBAAI/bge-reranker-base - Efficient reranking modelInstall with Tessl CLI
npx tessl i tessl/pypi-together