TogetherEmbeddings#
- class langchain_together.embeddings.TogetherEmbeddings[source]#
Bases:
BaseModel
,Embeddings
Together embedding model integration.
- Setup:
Install
langchain_together
and set environment variableTOGETHER_API_KEY
.pip install -U langchain_together export TOGETHER_API_KEY="your-api-key"
- Key init args — completion params:
- model: str
Name of Together model to use.
- Key init args — client params:
api_key: Optional[SecretStr]
See full list of supported init args and their descriptions in the params section.
- Instantiate:
from __module_name__ import TogetherEmbeddings embed = TogetherEmbeddings( model="togethercomputer/m2-bert-80M-8k-retrieval", # api_key="...", # other params... )
- Embed single text:
input_text = "The meaning of life is 42" vector = embed.embed_query(input_text) print(vector[:3])
[-0.024603435769677162, -0.007543657906353474, 0.0039630369283258915]
- Embed multiple texts:
input_texts = ["Document 1...", "Document 2..."] vectors = embed.embed_documents(input_texts) print(len(vectors)) # The first 3 coordinates for the first vector print(vectors[0][:3])
2 [-0.024603435769677162, -0.007543657906353474, 0.0039630369283258915]
- Async:
vector = await embed.aembed_query(input_text) print(vector[:3]) # multiple: # await embed.aembed_documents(input_texts)
[-0.009100092574954033, 0.005071679595857859, -0.0029193938244134188]
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- param allowed_special: Literal['all'] | Set[str] = {}#
Not yet supported.
- param chunk_size: int = 1000#
Maximum number of texts to embed in each batch.
Not yet supported.
- param default_headers: Mapping[str, str] | None = None#
- param default_query: Mapping[str, object] | None = None#
- param dimensions: int | None = None#
The number of dimensions the resulting output embeddings should have.
Not yet supported.
- param disallowed_special: Literal['all'] | Set[str] | Sequence[str] = 'all'#
Not yet supported.
- param embedding_ctx_length: int = 4096#
The maximum number of tokens to embed at once.
Not yet supported.
- param http_async_client: Any | None = None#
Optional httpx.AsyncClient. Only used for async invocations. Must specify http_client as well if you’d like a custom client for sync invocations.
- param http_client: Any | None = None#
Optional httpx.Client. Only used for sync invocations. Must specify http_async_client as well if you’d like a custom client for async invocations.
- param max_retries: int = 2#
Maximum number of retries to make when generating.
- param model: str = 'togethercomputer/m2-bert-80M-8k-retrieval'#
Embeddings model name to use. Instead, use ‘togethercomputer/m2-bert-80M-8k-retrieval’ for example.
- param model_kwargs: Dict[str, Any] [Optional]#
Holds any model parameters valid for create call not explicitly specified.
- param request_timeout: float | Tuple[float, float] | Any | None = None (alias 'timeout')#
Timeout for requests to Together embedding API. Can be float, httpx.Timeout or None.
- param show_progress_bar: bool = False#
Whether to show a progress bar when embedding.
Not yet supported.
- param skip_empty: bool = False#
Whether to skip empty strings when embedding or raise an error. Defaults to not skipping.
Not yet supported.
- param together_api_base: str [Optional] (alias 'base_url')#
Endpoint URL to use.
- param together_api_key: SecretStr | None [Optional] (alias 'api_key')#
Together AI API key.
Automatically read from env variable TOGETHER_API_KEY if not provided.
- async aembed_documents(texts: List[str]) List[List[float]] [source]#
Embed a list of document texts using passage model asynchronously.
- Parameters:
texts (List[str]) – The list of texts to embed.
- Returns:
List of embeddings, one for each text.
- Return type:
List[List[float]]
- async aembed_query(text: str) List[float] [source]#
Asynchronous Embed query text using query model.
- Parameters:
text (str) – The text to embed.
- Returns:
Embedding for the text.
- Return type:
List[float]
Examples using TogetherEmbeddings