DS DevShelfHub Projects · AI tools
Tutorials / LangChain / Reference / Classes / TogetherEmbeddings
Embeddings langchain-together Beginner

TogetherEmbeddings: Reference Guide

By DevShelfHub

Use Together AI's embeddings.

What is TogetherEmbeddings?

TogetherEmbeddings connects LangChain to Together AI's embedding service, which hosts a variety of open-source embedding models — including M2-BERT, BGE, and UAE-Large-V1 — through a single API. The class wraps Together AI's /embeddings endpoint and follows the standard LangChain Embeddings interface, making it a drop-in replacement anywhere OpenAIEmbeddings or HuggingFaceEmbeddings are used. The default model is togethercomputer/m2-bert-80M-32k, producing 768-dimensional vectors suited for long-context retrieval.

Together AI's pricing is significantly cheaper than OpenAI's text-embedding-3-small at scale, making TogetherEmbeddings attractive for batch indexing jobs. The class supports both embed_query() for single strings and embed_documents() for lists, mapping to single and batch API calls respectively. Together AI's batch API processes up to 256 texts per request; LangChain handles chunking automatically for larger lists via the chunk_size parameter.

The main tradeoff is model quality. Together AI's hosted open-source models perform well on general retrieval benchmarks but trail OpenAI's text-embedding-3-large on domain-specific tasks. For RAG pipelines where query-document relevance is critical — legal, biomedical, financial — benchmark on your own dataset before committing. Set TOGETHER_API_KEY as an environment variable; the class reads it via os.getenv and raises ValueError at construction time if it is absent.

When to Use

You want affordable embeddings with good quality. Use for budget-conscious projects.

Use Cases

  • Cost-effective RAG
  • Budget embeddings
  • Multiple models
  • Batch processing
  • Startup projects
  • Demo applications

Key Features

  • Affordable
  • Multiple models
  • Batch API
  • Fast
  • Quality
  • Simple

When NOT to Use

For highest quality—use OpenAI.

Notes

Model selection matters

Together hosts dozens of embedding models. togethercomputer/m2-bert-80M-32k-retrieval is optimized for retrieval; UAE-Large-V1 scores higher on MTEB benchmarks but is slower and more expensive. Check the Together AI docs for the current model list before picking one for production.

Rate limits on the free tier

The free tier caps at 60 requests per minute. Under burst load, add exponential backoff with LangChain's with_retry() wrapper: TogetherEmbeddings().with_retry(stop_after_attempt=3). Production tiers offer higher limits with SLA guarantees.

Dimension mismatch when switching models

Each Together AI model produces vectors of a different dimensionality — M2-BERT is 768-dim, UAE-Large-V1 is 1024-dim. Switching models mid-project makes all existing vectors incompatible with new ones. Re-embed the entire corpus when changing models, and pin the model name in your config.

Align chunk_size with the API batch limit

Together AI's API accepts up to 256 strings per call. LangChain's default chunk_size=512 in embed_documents() can produce calls that exceed this limit. Set chunk_size=256 explicitly: TogetherEmbeddings(chunk_size=256).

Import

python
from langchain_together import TogetherEmbeddings

Configuration

Parameter Type Default Purpose
model str togethercomputer/m2-bert-80M-32k Together model

Usage Examples

Basic query embedding

python
embeddings = TogetherEmbeddings()
vec = embeddings.embed_query('What is LangChain?')
print(f'Vector dim: {len(vec)}')

Retrieval model + batch embed_documents

python
from langchain_together import TogetherEmbeddings

embeddings = TogetherEmbeddings(
    model='togethercomputer/m2-bert-80M-32k-retrieval',
    chunk_size=256,
)
docs = [
    'LangChain is a framework for LLM apps.',
    'Together AI hosts open-source models.',
    'RAG pipelines retrieve relevant context.',
]
vectors = embeddings.embed_documents(docs)
print(f'Embedded {len(vectors)} docs, dim={len(vectors[0])}')

Build Chroma vector store with UAE-Large-V1

python
from langchain_together import TogetherEmbeddings
from langchain_chroma import Chroma

embeddings = TogetherEmbeddings(model='UAE-Large-V1')

store = Chroma.from_documents(
    documents=docs,
    embedding=embeddings,
    persist_directory='./chroma_together',
)
results = store.similarity_search('affordable LLM hosting', k=3)
for r in results:
    print(r.page_content)

Common Pitfalls

❌ Forget TOGETHER_API_KEY

✅ export TOGETHER_API_KEY='...'

Alternative Embedding Models

Model When to Use
OpenAIEmbeddings For best quality

Browse the full LangChain API reference index to explore more classes, methods, and decorators, or start with the LangChain introduction tutorial for end-to-end context on building with TogetherEmbeddings and the wider framework.

TogetherEmbeddings FAQ

What is TogetherEmbeddings in LangChain?

Use Together AI's embeddings. TogetherEmbeddings connects LangChain to Together AI's embedding service, which hosts a variety of open-source embedding models — including M2-BERT, BGE, and UAE-Large-V1 — through a single API. The class wraps Together AI's /embeddings endpoint and follows the standard LangChain Embeddings interface, making it a drop-in replacement anywhere OpenAIEmbeddings or HuggingFaceEmbeddings are used. The default model is togethercomputer/m2-bert-80M-32k, producing 768-dimensional vec…

Which package provides TogetherEmbeddings?

DevShelfHub documents TogetherEmbeddings from the langchain-together package. Pin your installed LangChain version and match imports to the snippet on this page.

When should I use TogetherEmbeddings?

You want affordable embeddings with good quality. Use for budget-conscious projects.

When should I avoid using TogetherEmbeddings?

For highest quality—use OpenAI.

How do I import TogetherEmbeddings in Python?

from langchain_together import TogetherEmbeddings

Where can I explore more LangChain API reference pages?

Open the LangChain API reference index on DevShelfHub to browse classes, methods, and decorators, each with runnable examples, parameters, common mistakes, and cross-links.