Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Workflow:Togethercomputer Together python Embeddings And Reranking

From Leeroopedia
Knowledge Sources
Domains Embeddings, Information_Retrieval, RAG
Last Updated 2026-02-15 16:00 GMT

Overview

End-to-end process for generating text embeddings and reranking documents using Together AI's embedding and rerank APIs, forming the retrieval backbone for RAG pipelines.

Description

This workflow covers two complementary retrieval capabilities provided by the Together Python SDK. First, the embeddings API converts text into dense vector representations suitable for semantic search and similarity computation. Second, the rerank API takes a query and a set of candidate documents and re-orders them by relevance using a cross-encoder model. Together, these capabilities form the retrieval and refinement stages of Retrieval-Augmented Generation (RAG) systems.

Usage

Execute this workflow when building search or retrieval systems that require semantic understanding. The embeddings step applies when you need to index documents for similarity search or compute text similarity scores. The reranking step applies when you have a candidate set from an initial retrieval pass (BM25, vector search, etc.) and need to refine the ranking using a more powerful cross-encoder model.

Execution Steps

Step 1: Client Initialization

Create a Together client instance. The same client provides access to both the embeddings and rerank resources through their respective namespaces.

Key considerations:

  • A single client instance serves both embeddings and reranking operations
  • API key configuration follows the same pattern as other Together resources

Step 2: Text Preprocessing

Prepare input texts for embedding by cleaning and normalizing them. Replace newlines with spaces and ensure texts fit within the model's maximum context window.

Key considerations:

  • Replace newline characters with spaces for consistent embedding quality
  • Texts exceeding the model's context window will be truncated
  • Batch multiple texts in a single request for efficiency
  • Input can be a single string or a list of strings

Step 3: Embedding Generation

Send texts to the embeddings API to obtain dense vector representations. The API accepts a model identifier and input text(s), returning an array of embedding vectors with one vector per input text.

Key considerations:

  • Model must be a supported embedding model (e.g., m2-bert-80M-8k-retrieval)
  • Each text produces a fixed-dimensional float vector
  • The response data array is ordered to match the input array
  • Embeddings can be used for cosine similarity, dot product, or other distance metrics

Step 4: Document Reranking

Submit a query and candidate documents to the rerank API. The API scores each document against the query using a cross-encoder model and returns results ordered by relevance score.

Key considerations:

  • Documents can be plain strings or dictionaries with multiple fields
  • rank_fields specifies which dictionary fields to consider for ranking
  • top_n limits the number of returned results
  • return_documents=True includes the original document content in the response
  • Results include an index field mapping back to the original document array position
  • relevance_score indicates the model's confidence in each document's relevance

Step 5: Result Integration

Combine the reranked results into your application pipeline. Map the reranked indices back to the original documents, apply score thresholds if needed, and pass the top results to downstream components (e.g., LLM context for RAG).

Key considerations:

  • Use the index field to map results back to original documents
  • Apply relevance_score thresholds to filter low-confidence matches
  • The reranked order represents the model's assessment of query relevance
  • Feed top-ranked documents as context to a chat completion for RAG

Execution Diagram

GitHub URL

Workflow Repository